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