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_ASSET_PATH_H
25 #define PXR_USD_SDF_ASSET_PATH_H
26 
27 /// \file sdf/assetPath.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/api.h"
31 
32 #include <boost/functional/hash.hpp>
33 #include <boost/operators.hpp>
34 #include <iosfwd>
35 #include <string>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 /// \class SdfAssetPath
40 ///
41 /// Contains an asset path and an optional resolved path.  Asset paths may
42 /// contain non-control UTF-8 encoded characters.  Specifically, U+0000..U+001F
43 /// (C0 controls), U+007F (delete), and U+0080..U+009F (C1 controls) are
44 /// disallowed.  Attempts to construct asset paths with such characters will
45 /// issue a TfError and produce the default-constructed empty asset path.
46 ///
47 class SdfAssetPath:
48     public boost::totally_ordered<SdfAssetPath>
49 {
50 public:
51     /// \name Constructors
52     /// @{
53     ///
54 
55     /// Construct an empty asset path.
56     SDF_API SdfAssetPath();
57 
58     /// Construct an asset path with \p path and no associated resolved path.
59     ///
60     /// If the passed \p path is not valid UTF-8 or contains C0 or C1 control
61     /// characters, raise a TfError and return the default-constructed empty
62     /// asset path.
63     SDF_API explicit SdfAssetPath(const std::string &path);
64 
65     /// Construct an asset path with \p path and an associated \p resolvedPath.
66     ///
67     /// If either the passed \path or \p resolvedPath are not valid UTF-8 or
68     /// either contain C0 or C1 control characters, raise a TfError and return
69     /// the default-constructed empty asset path.
70     SDF_API
71     SdfAssetPath(const std::string &path, const std::string &resolvedPath);
72 
73     /// @}
74 
75     ///\name Operators
76     /// @{
77 
78     /// Equality, including the resolved path.
79     bool operator==(const SdfAssetPath &rhs) const {
80         return _assetPath == rhs._assetPath &&
81                _resolvedPath == rhs._resolvedPath;
82     }
83 
84     /// Ordering first by asset path, then by resolved path.
85     SDF_API bool operator<(const SdfAssetPath &rhs) const;
86 
87     /// Hash function
GetHash()88     size_t GetHash() const {
89         size_t hash = 0;
90         boost::hash_combine(hash, _assetPath);
91         boost::hash_combine(hash, _resolvedPath);
92         return hash;
93     }
94 
95     /// \class Hash
96     struct Hash
97     {
operatorHash98         size_t operator()(const SdfAssetPath &ap) const {
99             return ap.GetHash();
100         }
101     };
102 
hash_value(const SdfAssetPath & ap)103     friend size_t hash_value(const SdfAssetPath &ap) { return ap.GetHash(); }
104 
105     /// @}
106 
107     /// \name Accessors
108     /// @{
109 
110     /// Return the asset path.
GetAssetPath()111     const std::string &GetAssetPath() const {
112         return _assetPath;
113     }
114 
115     /// Return the resolved asset path, if any.
116     ///
117     /// Note that SdfAssetPath carries a resolved path only if its creator
118     /// passed one to the constructor.  SdfAssetPath never performs resolution
119     /// itself.
GetResolvedPath()120     const std::string &GetResolvedPath() const {
121         return _resolvedPath;
122     }
123 
124     /// @}
125 
126 private:
swap(SdfAssetPath & lhs,SdfAssetPath & rhs)127     friend inline void swap(SdfAssetPath &lhs, SdfAssetPath &rhs) {
128         lhs._assetPath.swap(rhs._assetPath);
129         lhs._resolvedPath.swap(rhs._resolvedPath);
130     }
131 
132     std::string _assetPath;
133     std::string _resolvedPath;
134 };
135 
136 /// \name Related
137 /// @{
138 
139 /// Stream insertion operator for the string representation of this assetPath.
140 ///
141 /// \note This always encodes only the result of GetAssetPath().  The resolved
142 ///       path is ignored for the purpose of this operator.  This means that
143 ///       two SdfAssetPath s that do not compare equal may produce
144 ///       indistinguishable ostream output.
145 SDF_API std::ostream& operator<<(std::ostream& out, const SdfAssetPath& ap);
146 
147 /// @}
148 
149 PXR_NAMESPACE_CLOSE_SCOPE
150 
151 #endif // PXR_USD_SDF_ASSET_PATH_H
152