HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
assetPath.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_ASSET_PATH_H
8 #define PXR_USD_SDF_ASSET_PATH_H
9 
10 /// \file sdf/assetPath.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/sdf/api.h"
14 #include "pxr/base/tf/hash.h"
15 #include "pxr/base/vt/traits.h"
17 
18 #include <iosfwd>
19 #include <string>
20 
22 
23 class SdfAssetPath;
24 class VtDictionary;
25 class VtValue;
26 template<typename T> class TfSpan;
28 
29 /// \struct SdfAssetPathParams
30 /// Helper class for explicitly setting values when creating a SdfAssetPath
31 ///
32 /// Example usage:
33 /// \code
34 /// SdfAssetPath myAssetPath(
35 /// SdfAssetPathParams()
36 /// .Authored("blah_{VAR}.usda")
37 /// .Evaluated("blah_foo.usda")
38 /// .Resolved("/foo/bar/blah_foo.usda")
39 /// );
40 /// \endcode
41 ///
43 public:
44  SdfAssetPathParams& Authored(const std::string &authoredPath_) {
45  authoredPath = authoredPath_;
46  return *this;
47  }
48 
49  SdfAssetPathParams& Evaluated(const std::string &evaluatedPath_) {
50  evaluatedPath = evaluatedPath_;
51  return *this;
52  }
53 
54  SdfAssetPathParams& Resolved(const std::string &resolvedPath_) {
55  resolvedPath = resolvedPath_;
56  return *this;
57  }
58 
59 private:
60  friend class SdfAssetPath;
61 
62  std::string authoredPath;
63  std::string evaluatedPath;
64  std::string resolvedPath;
65 };
66 
67 /// \class SdfAssetPath
68 ///
69 /// Contains an asset path and optional evaluated and resolved paths.
70 /// When this class is used to author scene description, the value returned
71 /// by GetAssetPath() is serialized out, all other fields are ignored.
72 /// Asset paths may contain non-control UTF-8 encoded characters.
73 /// Specifically, U+0000..U+001F (C0 controls), U+007F (delete),
74 /// and U+0080..U+009F (C1 controls) are disallowed. Attempts to construct
75 /// asset paths with such characters will issue a TfError and produce the
76 /// default-constructed empty asset path.
77 ///
79 {
80 public:
81  /// \name Constructors
82  /// @{
83  ///
84 
85  /// Construct an empty asset path.
87 
88  /// Construct an asset path with \p authoredPath and no associated
89  /// evaluated or resolved path.
90  ///
91  /// If the passed \p authoredPath is not valid UTF-8 or contains C0 or C1
92  /// control characters, raise a TfError and return the default-constructed
93  /// empty asset path.
94  SDF_API explicit SdfAssetPath(const std::string &authoredPath);
95 
96  /// Construct an asset path with \p authoredPath and an associated
97  /// \p resolvedPath.
98  ///
99  /// If either the passed \p authoredPath or \p resolvedPath are not valid
100  /// UTF-8 or either contain C0 or C1 control characters, raise a TfError and
101  /// return the default-constructed empty asset path.
102  SDF_API
103  SdfAssetPath(const std::string &authoredPath,
104  const std::string &resolvedPath);
105 
106  /// Construct an asset path using a SdfAssetPathParams object
107  ///
108  /// If any fields of the passed in structure are not valid UTF-8 or either
109  /// contain C0 or C1 control, characters, raise a TfError and return
110  /// the default-constructed empty asset path.
111  SDF_API
113 
114  /// @}
115 
116  ///\name Operators
117  /// @{
118 
119  /// Equality, including the evaluated and resolved paths.
120  bool operator==(const SdfAssetPath &rhs) const {
121  return _authoredPath == rhs._authoredPath &&
122  _evaluatedPath == rhs._evaluatedPath &&
123  _resolvedPath == rhs._resolvedPath;
124  }
125 
126  /// Inequality operator
127  /// \sa SdfAssetPath::operator==(const SdfAssetPath&)
128  bool operator!=(const SdfAssetPath& rhs) const {
129  return !(*this == rhs);
130  }
131 
132  /// Ordering first by asset path, resolved path, then by evaluated path.
133  SDF_API bool operator<(const SdfAssetPath &rhs) const;
134 
135  /// Less than or equal operator
136  /// \sa SdfAssetPath::operator<(const SdfAssetPath&)
137  bool operator<=(const SdfAssetPath& rhs) const {
138  return !(rhs < *this);
139  }
140 
141  /// Greater than operator
142  /// \sa SdfAssetPath::operator<(const SdfAssetPath&)
143  bool operator>(const SdfAssetPath& rhs) const {
144  return rhs < *this;
145  }
146 
147  /// Greater than or equal operator
148  /// \sa SdfAssetPath::operator<(const SdfAssetPath&)
149  bool operator>=(const SdfAssetPath& rhs) const {
150  return !(*this < rhs);
151  }
152 
153  /// Hash function
154  size_t GetHash() const {
155  return TfHash::Combine(_authoredPath, _evaluatedPath, _resolvedPath);
156  }
157 
158  /// \class Hash
159  struct Hash
160  {
161  size_t operator()(const SdfAssetPath &ap) const {
162  return ap.GetHash();
163  }
164  };
165 
166  friend size_t hash_value(const SdfAssetPath &ap) { return ap.GetHash(); }
167 
168  /// @}
169 
170  /// \name Accessors
171  /// @{
172 
173  /// Returns the asset path as it was authored in the original layer. When
174  /// authoring scene description, this value is used for serialization.
175  const std::string &GetAuthoredPath() const & {
176  return _authoredPath;
177  }
178 
179  /// Overload for rvalues, move out the asset path.
180  std::string GetAuthoredPath() const && {
181  return std::move(_authoredPath);
182  }
183 
184  /// Return the evaluated asset path, if any. The evaluated path's value
185  /// consists of the authored path with any expression variables
186  /// evaluated. If the authored path does not contain any expression
187  /// variables, this field will be empty.
188  ///
189  /// Note that SdfAssetPath carries an evaluated path only if its creator
190  /// passed one to the constructor. SdfAssetPath never performs variable
191  /// expression evaluation itself.
192  const std::string &GetEvaluatedPath() const & {
193  return _evaluatedPath;
194  }
195 
196  /// Overload for rvalues, move out the evaluated path.
197  std::string GetEvaluatedPath() const && {
198  return std::move(_evaluatedPath);
199  }
200 
201  /// Return the asset path. If the the evaluated path is not empty, it will
202  /// be returned, otherwise the raw, authored path is returned.
203  /// The value this function returns is the exact input that is passed to
204  /// asset resolution.
205  const std::string &GetAssetPath() const & {
206  return _evaluatedPath.empty() ? _authoredPath : _evaluatedPath;
207  }
208 
209  /// Overload for rvalues, move out the asset path.
210  std::string GetAssetPath() const && {
211  return std::move(
212  _evaluatedPath.empty() ? _authoredPath : _evaluatedPath);
213  }
214 
215  /// Return the resolved asset path, if any. This is the resolved value of
216  /// GetAssetPath()
217  ///
218  /// Note that SdfAssetPath carries a resolved path only if its creator
219  /// passed one to the constructor. SdfAssetPath never performs resolution
220  /// itself.
221  const std::string &GetResolvedPath() const & {
222  return _resolvedPath;
223  }
224 
225  /// Overload for rvalues, move out the asset path.
226  std::string GetResolvedPath() const && {
227  return std::move(_resolvedPath);
228  }
229 
230  /// @}
231 
232  /// \name Setters
233  /// @{
234 
235  /// Sets the authored path. This value is the path exactly as it is
236  /// authored in the layer.
237  void SetAuthoredPath(const std::string &authoredPath) {
238  _authoredPath = authoredPath;
239  }
240 
241  /// Sets the evaluated path. This value is the result of performing
242  /// variable expression resolution on the authored path.
243  void SetEvaluatedPath(const std::string &evaluatedPath) {
244  _evaluatedPath = evaluatedPath;
245  }
246 
247  /// Sets the resolved path. This value is the result of asset resolution.
248  void SetResolvedPath(const std::string &resolvedPath) {
249  _resolvedPath = resolvedPath;
250  }
251 
252  /// @}
253 
254 private:
255  friend inline void swap(SdfAssetPath &lhs, SdfAssetPath &rhs) {
256  lhs._authoredPath.swap(rhs._authoredPath);
257  lhs._evaluatedPath.swap(rhs._evaluatedPath);
258  lhs._resolvedPath.swap(rhs._resolvedPath);
259  }
260 
261  /// Raw path, as authored in the layer.
262  std::string _authoredPath;
263  /// Contains the evaluated authored path, if variable expressions
264  /// were present, otherwise empty.
265  std::string _evaluatedPath;
266  /// Fully evaluated and resolved path
267  std::string _resolvedPath;
268 };
269 
270 // SdfAssetPath supports value transforms.
272 
273 /// \name Related
274 /// @{
275 
276 /// Stream insertion operator for the string representation of this assetPath.
277 ///
278 /// \note This always encodes only the result of GetAssetPath(). The resolved
279 /// path is ignored for the purpose of this operator. This means that
280 /// two SdfAssetPath s that do not compare equal may produce
281 /// indistinguishable ostream output.
282 SDF_API std::ostream& operator<<(std::ostream& out, const SdfAssetPath& ap);
283 
284 /// @}
285 
286 /// Anchors all paths in \p assetPaths to the \p anchor layer. Evaluates any
287 /// expression variables listed in \p exprVars.
288 SDF_API
289 void SdfAnchorAssetPaths(const SdfLayerHandle &anchor,
290  const VtDictionary &exprVars,
291  TfSpan<SdfAssetPath> assetPaths,
292  std::vector<std::string> *errors);
293 
294 /// Anchors and resolves the given \p assetPaths with respect to the \p anchor
295 /// layer. Evaluates any expression variables listed in \p exprVars. Expects
296 /// resolver context to be bound beforehand.
297 SDF_API
298 void SdfResolveAssetPaths(const SdfLayerHandle &anchor,
299  const VtDictionary &exprVars,
300  TfSpan<SdfAssetPath> assetPaths,
301  std::vector<std::string> *errors);
302 
304 
305 #endif // PXR_USD_SDF_ASSET_PATH_H
Definition: layer.h:81
const std::string & GetEvaluatedPath() const &
Definition: assetPath.h:192
std::string GetAuthoredPath() const &&
Overload for rvalues, move out the asset path.
Definition: assetPath.h:180
SDF_API bool operator<(const SdfAssetPath &rhs) const
Ordering first by asset path, resolved path, then by evaluated path.
friend size_t hash_value(const SdfAssetPath &ap)
Equality, including the evaluated and resolved paths.
Definition: assetPath.h:166
const std::string & GetAuthoredPath() const &
Definition: assetPath.h:175
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
friend void swap(SdfAssetPath &lhs, SdfAssetPath &rhs)
Definition: assetPath.h:255
void SetResolvedPath(const std::string &resolvedPath)
Sets the resolved path. This value is the result of asset resolution.
Definition: assetPath.h:248
SDF_API SdfAssetPath()
Construct an empty asset path.
const std::string & GetResolvedPath() const &
Definition: assetPath.h:221
bool operator>(const SdfAssetPath &rhs) const
Definition: assetPath.h:143
std::string GetResolvedPath() const &&
Overload for rvalues, move out the asset path.
Definition: assetPath.h:226
size_t GetHash() const
Hash function.
Definition: assetPath.h:154
GLenum const GLfloat * params
Definition: glcorearb.h:105
SdfAssetPathParams & Authored(const std::string &authoredPath_)
Definition: assetPath.h:44
SdfAssetPathParams & Evaluated(const std::string &evaluatedPath_)
Definition: assetPath.h:49
Definition: span.h:70
const std::string & GetAssetPath() const &
Definition: assetPath.h:205
VT_VALUE_TYPE_CAN_TRANSFORM(SdfAssetPath)
std::string GetAssetPath() const &&
Overload for rvalues, move out the asset path.
Definition: assetPath.h:210
SDF_DECLARE_HANDLES(SdfLayer)
bool operator<=(const SdfAssetPath &rhs) const
Definition: assetPath.h:137
SDF_API void SdfResolveAssetPaths(const SdfLayerHandle &anchor, const VtDictionary &exprVars, TfSpan< SdfAssetPath > assetPaths, std::vector< std::string > *errors)
size_t operator()(const SdfAssetPath &ap) const
Definition: assetPath.h:161
bool operator==(const SdfAssetPath &rhs) const
Equality, including the evaluated and resolved paths.
Definition: assetPath.h:120
#define SDF_API
Definition: api.h:23
SdfAssetPathParams & Resolved(const std::string &resolvedPath_)
Definition: assetPath.h:54
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
SDF_API void SdfAnchorAssetPaths(const SdfLayerHandle &anchor, const VtDictionary &exprVars, TfSpan< SdfAssetPath > assetPaths, std::vector< std::string > *errors)
SDF_API std::ostream & operator<<(std::ostream &out, const SdfAssetPath &ap)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void SetAuthoredPath(const std::string &authoredPath)
Definition: assetPath.h:237
bool operator>=(const SdfAssetPath &rhs) const
Definition: assetPath.h:149
std::string GetEvaluatedPath() const &&
Overload for rvalues, move out the evaluated path.
Definition: assetPath.h:197
void SetEvaluatedPath(const std::string &evaluatedPath)
Definition: assetPath.h:243
Definition: value.h:89
bool operator!=(const SdfAssetPath &rhs) const
Definition: assetPath.h:128