HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
reference.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_SDF_REFERENCE_H
25 #define PXR_USD_SDF_REFERENCE_H
26 
27 /// \file sdf/reference.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/api.h"
31 #include "pxr/usd/sdf/assetPath.h"
33 #include "pxr/usd/sdf/path.h"
34 #include "pxr/base/tf/hash.h"
35 #include "pxr/base/vt/dictionary.h"
36 #include "pxr/base/vt/value.h"
37 
38 #include <iosfwd>
39 #include <string>
40 #include <vector>
41 
43 
45 
46 typedef std::vector<SdfReference> SdfReferenceVector;
47 
48 /// \class SdfReference
49 ///
50 /// Represents a reference and all its meta data.
51 ///
52 /// A reference is expressed on a prim in a given layer and it identifies a
53 /// prim in a layer stack. All opinions in the namespace hierarchy
54 /// under the referenced prim will be composed with the opinions in the
55 /// namespace hierarchy under the referencing prim.
56 ///
57 /// The asset path specifies the layer stack being referenced. If this
58 /// asset path is non-empty, this reference is considered an 'external'
59 /// reference to the layer stack rooted at the specified layer. If this
60 /// is empty, this reference is considered an 'internal' reference to the
61 /// layer stack containing (but not necessarily rooted at) the layer where
62 /// the reference is authored.
63 ///
64 /// The prim path specifies the prim in the referenced layer stack from
65 /// which opinions will be composed. If this prim path is empty, it will
66 /// be considered a reference to the default prim specified in the root layer
67 /// of the referenced layer stack -- see SdfLayer::GetDefaultPrim.
68 ///
69 /// The meta data for a reference is its layer offset and custom data. The
70 /// layer offset is an affine transformation applied to all anim splines in
71 /// the referenced prim's namespace hierarchy, see SdfLayerOffset for details.
72 /// Custom data is for use by plugins or other non-tools supplied extensions
73 /// that need to be able to store data associated with references.
74 ///
75 class SdfReference {
76 public:
77  /// Creates a reference with all its meta data. The default reference is an
78  /// internal reference to the default prim. See SdfAssetPath for what
79  /// characters are valid in \p assetPath. If \p assetPath contains invalid
80  /// characters, issue an error and set this reference's asset path to the
81  /// empty asset path.
82  ///
84  const std::string &assetPath = std::string(),
85  const SdfPath &primPath = SdfPath(),
86  const SdfLayerOffset &layerOffset = SdfLayerOffset(),
87  const VtDictionary &customData = VtDictionary());
88 
89  /// Returns the asset path to the root layer of the referenced layer
90  /// stack. This will be empty in the case of an internal reference.
91  ///
92  const std::string &GetAssetPath() const {
93  return _assetPath;
94  }
95 
96  /// Sets the asset path for the root layer of the referenced layer stack.
97  /// This may be set to an empty string to specify an internal reference.
98  /// See SdfAssetPath for what characters are valid in \p assetPath. If \p
99  /// assetPath contains invalid characters, issue an error and set this
100  /// reference's asset path to the empty asset path.
101  void SetAssetPath(const std::string &assetPath) {
102  // Go through SdfAssetPath() to raise an error if \p assetPath contains
103  // illegal characters (i.e. control characters).
104  _assetPath = SdfAssetPath(assetPath).GetAssetPath();
105  }
106 
107  /// Returns the path of the referenced prim.
108  /// This will be empty if the referenced prim is the default prim specified
109  /// in the referenced layer stack.
110  ///
111  const SdfPath &GetPrimPath() const {
112  return _primPath;
113  }
114 
115  /// Sets the path of the referenced prim.
116  /// This may be set to an empty path to specify a reference to the default
117  /// prim in the referenced layer stack.
118  ///
119  void SetPrimPath(const SdfPath &primPath) {
120  _primPath = primPath;
121  }
122 
123  /// Returns the layer offset associated with the reference.
124  ///
126  return _layerOffset;
127  }
128 
129  /// Sets a new layer offset.
130  ///
131  void SetLayerOffset(const SdfLayerOffset &layerOffset) {
132  _layerOffset = layerOffset;
133  }
134 
135  /// Returns the custom data associated with the reference.
136  ///
137  const VtDictionary &GetCustomData() const {
138  return _customData;
139  }
140 
141  /// Sets the custom data associated with the reference.
142  ///
143  void SetCustomData(const VtDictionary &customData) {
144  _customData = customData;
145  }
146 
147  /// Sets a custom data entry for the reference.
148  ///
149  /// If \a value is empty, then this removes the given custom data entry.
150  ///
151  SDF_API void SetCustomData(const std::string &name, const VtValue &value);
152 
153  /// Swaps the custom data dictionary for this reference.
154  void SwapCustomData(VtDictionary &customData) {
155  _customData.swap(customData);
156  }
157 
158  /// Returns \c true in the case of an internal reference.
159  ///
160  /// An internal reference is a reference with an empty asset path.
161  ///
162  SDF_API bool IsInternal() const;
163 
164  friend inline size_t hash_value(const SdfReference &r) {
165  return TfHash::Combine(
166  r._assetPath,
167  r._primPath,
168  r._layerOffset,
169  r._customData
170  );
171  }
172 
173  /// Returns whether this reference equals \a rhs.
174  SDF_API bool operator==(const SdfReference &rhs) const;
175 
176  /// \sa SdfReference::operator==(const SdfReference&)
177  bool operator!=(const SdfReference &rhs) const {
178  return !(*this == rhs);
179  }
180 
181  /// Returns whether this reference is less than \a rhs. The meaning
182  /// of less than is somewhat arbitrary.
183  SDF_API bool operator<(const SdfReference &rhs) const;
184 
185  /// \sa SdfReference::operator<(const SdfReference&)
186  bool operator>(const SdfReference &rhs) const {
187  return rhs < *this;
188  }
189 
190  /// \sa SdfReference::operator<(const SdfReference&)
191  bool operator<=(const SdfReference &rhs) const {
192  return !(rhs < *this);
193  }
194 
195  /// \sa SdfReference::operator<(const SdfReference&)
196  bool operator>=(const SdfReference &rhs) const {
197  return !(*this < rhs);
198  }
199 
200  /// Struct that defines equality of SdfReferences based on their
201  /// identity (the asset path and prim path).
202  ///
203  struct IdentityEqual {
204  bool operator()(const SdfReference &lhs, const SdfReference &rhs) const {
205  return lhs._assetPath == rhs._assetPath &&
206  lhs._primPath == rhs._primPath;
207  }
208  };
209 
210  /// Struct that defines a strict weak ordering of SdfReferences based on
211  /// their identity (the asset path and prim path).
212  ///
214  bool operator()(const SdfReference &lhs, const SdfReference &rhs) const {
215  return lhs._assetPath < rhs._assetPath ||
216  (lhs._assetPath == rhs._assetPath &&
217  lhs._primPath < rhs._primPath);
218  }
219  };
220 
221 private:
222  // The asset path to the external layer.
223  std::string _assetPath;
224 
225  // The path to the referenced prim in the external layer.
226  SdfPath _primPath;
227 
228  // The layer offset to transform time.
229  SdfLayerOffset _layerOffset;
230 
231  // The custom data associated with the reference.
232  VtDictionary _customData;
233 };
234 
235 /// Convenience function to find the index of the reference in \a references
236 /// that has the same identity as the given reference \a referenceId.
237 ///
238 /// A reference's identity is given by its asset path and prim path alone
239 /// (i.e. the layer offset and custom data is ignored).
240 ///
241 /// If no reference with the same identity exists in \a reference, -1 is
242 /// returned. If more than one reference with the same identity exist in
243 /// \a references the index of the first one is returned.
244 ///
246  const SdfReferenceVector &references,
247  const SdfReference &referenceId);
248 
249 /// Writes the string representation of \a SdfReference to \a out.
250 SDF_API std::ostream & operator<<( std::ostream &out,
251  const SdfReference &reference );
252 
254 
255 #endif // PXR_USD_SDF_REFERENCE_H
bool operator>=(const SdfReference &rhs) const
Definition: reference.h:196
const VtDictionary & GetCustomData() const
Definition: reference.h:137
bool operator()(const SdfReference &lhs, const SdfReference &rhs) const
Definition: reference.h:214
SDF_API std::ostream & operator<<(std::ostream &out, const SdfReference &reference)
Writes the string representation of SdfReference to out.
SDF_API bool IsInternal() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
void SetPrimPath(const SdfPath &primPath)
Definition: reference.h:119
const std::string & GetAssetPath() const
Definition: reference.h:92
bool operator()(const SdfReference &lhs, const SdfReference &rhs) const
Definition: reference.h:204
void SetAssetPath(const std::string &assetPath)
Definition: reference.h:101
void SetLayerOffset(const SdfLayerOffset &layerOffset)
Definition: reference.h:131
bool operator<=(const SdfReference &rhs) const
Definition: reference.h:191
const std::string & GetAssetPath() const &
Return the asset path.
Definition: assetPath.h:130
bool operator!=(const SdfReference &rhs) const
Definition: reference.h:177
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:290
friend size_t hash_value(const SdfReference &r)
Definition: reference.h:164
SDF_API SdfReference(const std::string &assetPath=std::string(), const SdfPath &primPath=SdfPath(), const SdfLayerOffset &layerOffset=SdfLayerOffset(), const VtDictionary &customData=VtDictionary())
#define SDF_API
Definition: api.h:40
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:492
std::vector< SdfReference > SdfReferenceVector
Definition: reference.h:44
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
const SdfPath & GetPrimPath() const
Definition: reference.h:111
SDF_API bool operator<(const SdfReference &rhs) const
const SdfLayerOffset & GetLayerOffset() const
Definition: reference.h:125
void SetCustomData(const VtDictionary &customData)
Definition: reference.h:143
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SDF_API bool operator==(const SdfReference &rhs) const
Returns whether this reference equals rhs.
Definition: core.h:1131
SDF_API int SdfFindReferenceByIdentity(const SdfReferenceVector &references, const SdfReference &referenceId)
GLboolean r
Definition: glcorearb.h:1222
VT_API void swap(VtDictionary &dict)
Swaps the contents of two VtDictionaries.
that also have some descendant prim *whose name begins with which in turn has a child named baz where *the predicate and *a name There is also one special expression reference
Definition: value.h:164
bool operator>(const SdfReference &rhs) const
Definition: reference.h:186
void SwapCustomData(VtDictionary &customData)
Swaps the custom data dictionary for this reference.
Definition: reference.h:154