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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_SDF_REFERENCE_H
8 #define PXR_USD_SDF_REFERENCE_H
9 
10 /// \file sdf/reference.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/sdf/api.h"
14 #include "pxr/usd/sdf/assetPath.h"
16 #include "pxr/usd/sdf/path.h"
17 #include "pxr/base/tf/hash.h"
18 #include "pxr/base/vt/dictionary.h"
19 #include "pxr/base/vt/value.h"
20 
21 #include <iosfwd>
22 #include <string>
23 #include <vector>
24 
26 
28 
29 typedef std::vector<SdfReference> SdfReferenceVector;
30 
31 /// \class SdfReference
32 ///
33 /// Represents a reference and all its meta data.
34 ///
35 /// A reference is expressed on a prim in a given layer and it identifies a
36 /// prim in a layer stack. All opinions in the namespace hierarchy
37 /// under the referenced prim will be composed with the opinions in the
38 /// namespace hierarchy under the referencing prim.
39 ///
40 /// The asset path specifies the layer stack being referenced. If this
41 /// asset path is non-empty, this reference is considered an 'external'
42 /// reference to the layer stack rooted at the specified layer. If this
43 /// is empty, this reference is considered an 'internal' reference to the
44 /// layer stack containing (but not necessarily rooted at) the layer where
45 /// the reference is authored.
46 ///
47 /// The prim path specifies the prim in the referenced layer stack from
48 /// which opinions will be composed. If this prim path is empty, it will
49 /// be considered a reference to the default prim specified in the root layer
50 /// of the referenced layer stack -- see SdfLayer::GetDefaultPrim.
51 ///
52 /// The meta data for a reference is its layer offset and custom data. The
53 /// layer offset is an affine transformation applied to all anim splines in
54 /// the referenced prim's namespace hierarchy, see SdfLayerOffset for details.
55 /// Custom data is for use by plugins or other non-tools supplied extensions
56 /// that need to be able to store data associated with references.
57 ///
58 class SdfReference {
59 public:
60  /// Creates a reference with all its meta data. The default reference is an
61  /// internal reference to the default prim. See SdfAssetPath for what
62  /// characters are valid in \p assetPath. If \p assetPath contains invalid
63  /// characters, issue an error and set this reference's asset path to the
64  /// empty asset path.
65  ///
67  const std::string &assetPath = std::string(),
68  const SdfPath &primPath = SdfPath(),
69  const SdfLayerOffset &layerOffset = SdfLayerOffset(),
70  const VtDictionary &customData = VtDictionary());
71 
72  /// Returns the asset path to the root layer of the referenced layer
73  /// stack. This will be empty in the case of an internal reference.
74  ///
75  const std::string &GetAssetPath() const {
76  return _assetPath;
77  }
78 
79  /// Sets the asset path for the root layer of the referenced layer stack.
80  /// This may be set to an empty string to specify an internal reference.
81  /// See SdfAssetPath for what characters are valid in \p assetPath. If \p
82  /// assetPath contains invalid characters, issue an error and set this
83  /// reference's asset path to the empty asset path.
84  void SetAssetPath(const std::string &assetPath) {
85  // Go through SdfAssetPath() to raise an error if \p assetPath contains
86  // illegal characters (i.e. control characters).
87  _assetPath = SdfAssetPath(assetPath).GetAssetPath();
88  }
89 
90  /// Returns the path of the referenced prim.
91  /// This will be empty if the referenced prim is the default prim specified
92  /// in the referenced layer stack.
93  ///
94  const SdfPath &GetPrimPath() const {
95  return _primPath;
96  }
97 
98  /// Sets the path of the referenced prim.
99  /// This may be set to an empty path to specify a reference to the default
100  /// prim in the referenced layer stack.
101  ///
102  void SetPrimPath(const SdfPath &primPath) {
103  _primPath = primPath;
104  }
105 
106  /// Returns the layer offset associated with the reference.
107  ///
109  return _layerOffset;
110  }
111 
112  /// Sets a new layer offset.
113  ///
114  void SetLayerOffset(const SdfLayerOffset &layerOffset) {
115  _layerOffset = layerOffset;
116  }
117 
118  /// Returns the custom data associated with the reference.
119  ///
120  const VtDictionary &GetCustomData() const {
121  return _customData;
122  }
123 
124  /// Sets the custom data associated with the reference.
125  ///
126  void SetCustomData(const VtDictionary &customData) {
127  _customData = customData;
128  }
129 
130  /// Sets a custom data entry for the reference.
131  ///
132  /// If \a value is empty, then this removes the given custom data entry.
133  ///
134  SDF_API void SetCustomData(const std::string &name, const VtValue &value);
135 
136  /// Swaps the custom data dictionary for this reference.
137  void SwapCustomData(VtDictionary &customData) {
138  _customData.swap(customData);
139  }
140 
141  /// Returns \c true in the case of an internal reference.
142  ///
143  /// An internal reference is a reference with an empty asset path.
144  ///
145  SDF_API bool IsInternal() const;
146 
147  friend inline size_t hash_value(const SdfReference &r) {
148  return TfHash::Combine(
149  r._assetPath,
150  r._primPath,
151  r._layerOffset,
152  r._customData
153  );
154  }
155 
156  /// Returns whether this reference equals \a rhs.
157  SDF_API bool operator==(const SdfReference &rhs) const;
158 
159  /// \sa SdfReference::operator==(const SdfReference&)
160  bool operator!=(const SdfReference &rhs) const {
161  return !(*this == rhs);
162  }
163 
164  /// Returns whether this reference is less than \a rhs. The meaning
165  /// of less than is somewhat arbitrary.
166  SDF_API bool operator<(const SdfReference &rhs) const;
167 
168  /// \sa SdfReference::operator<(const SdfReference&)
169  bool operator>(const SdfReference &rhs) const {
170  return rhs < *this;
171  }
172 
173  /// \sa SdfReference::operator<(const SdfReference&)
174  bool operator<=(const SdfReference &rhs) const {
175  return !(rhs < *this);
176  }
177 
178  /// \sa SdfReference::operator<(const SdfReference&)
179  bool operator>=(const SdfReference &rhs) const {
180  return !(*this < rhs);
181  }
182 
183  /// Struct that defines equality of SdfReferences based on their
184  /// identity (the asset path and prim path).
185  ///
186  struct IdentityEqual {
187  bool operator()(const SdfReference &lhs, const SdfReference &rhs) const {
188  return lhs._assetPath == rhs._assetPath &&
189  lhs._primPath == rhs._primPath;
190  }
191  };
192 
193  /// Struct that defines a strict weak ordering of SdfReferences based on
194  /// their identity (the asset path and prim path).
195  ///
197  bool operator()(const SdfReference &lhs, const SdfReference &rhs) const {
198  return lhs._assetPath < rhs._assetPath ||
199  (lhs._assetPath == rhs._assetPath &&
200  lhs._primPath < rhs._primPath);
201  }
202  };
203 
204 private:
205  // The asset path to the external layer.
206  std::string _assetPath;
207 
208  // The path to the referenced prim in the external layer.
209  SdfPath _primPath;
210 
211  // The layer offset to transform time.
212  SdfLayerOffset _layerOffset;
213 
214  // The custom data associated with the reference.
215  VtDictionary _customData;
216 };
217 
218 /// Convenience function to find the index of the reference in \a references
219 /// that has the same identity as the given reference \a referenceId.
220 ///
221 /// A reference's identity is given by its asset path and prim path alone
222 /// (i.e. the layer offset and custom data is ignored).
223 ///
224 /// If no reference with the same identity exists in \a reference, -1 is
225 /// returned. If more than one reference with the same identity exist in
226 /// \a references the index of the first one is returned.
227 ///
229  const SdfReferenceVector &references,
230  const SdfReference &referenceId);
231 
232 /// Writes the string representation of \a SdfReference to \a out.
233 SDF_API std::ostream & operator<<( std::ostream &out,
234  const SdfReference &reference );
235 
237 
238 #endif // PXR_USD_SDF_REFERENCE_H
bool operator>=(const SdfReference &rhs) const
Definition: reference.h:179
const VtDictionary & GetCustomData() const
Definition: reference.h:120
bool operator()(const SdfReference &lhs, const SdfReference &rhs) const
Definition: reference.h:197
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 GLfloat * value
Definition: glcorearb.h:824
void SetPrimPath(const SdfPath &primPath)
Definition: reference.h:102
const std::string & GetAssetPath() const
Definition: reference.h:75
bool operator()(const SdfReference &lhs, const SdfReference &rhs) const
Definition: reference.h:187
void SetAssetPath(const std::string &assetPath)
Definition: reference.h:84
void SetLayerOffset(const SdfLayerOffset &layerOffset)
Definition: reference.h:114
bool operator<=(const SdfReference &rhs) const
Definition: reference.h:174
const std::string & GetAssetPath() const &
Definition: assetPath.h:199
bool operator!=(const SdfReference &rhs) const
Definition: reference.h:160
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:273
friend size_t hash_value(const SdfReference &r)
Definition: reference.h:147
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:23
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
std::vector< SdfReference > SdfReferenceVector
Definition: reference.h:27
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
const SdfPath & GetPrimPath() const
Definition: reference.h:94
SDF_API bool operator<(const SdfReference &rhs) const
const SdfLayerOffset & GetLayerOffset() const
Definition: reference.h:108
void SetCustomData(const VtDictionary &customData)
Definition: reference.h:126
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDF_API bool operator==(const SdfReference &rhs) const
Returns whether this reference equals rhs.
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:146
bool operator>(const SdfReference &rhs) const
Definition: reference.h:169
void SwapCustomData(VtDictionary &customData)
Swaps the custom data dictionary for this reference.
Definition: reference.h:137