1 //
2 // Copyright 2018 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 
25 #ifndef PXR_USD_NDR_DECLARE_H
26 #define PXR_USD_NDR_DECLARE_H
27 
28 /// \file ndr/declare.h
29 
30 #include "pxr/pxr.h"
31 #include "pxr/usd/ndr/api.h"
32 #include "pxr/base/tf/token.h"
33 
34 #include <memory>
35 #include <string>
36 #include <unordered_map>
37 #include <unordered_set>
38 #include <vector>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
42 class NdrNode;
43 class NdrProperty;
44 class SdfValueTypeName;
45 
46 /// \file declare.h
47 ///
48 /// Common typedefs that are used throughout the NDR library.
49 
50 typedef TfToken NdrIdentifier;
51 typedef TfToken::HashFunctor NdrIdentifierHashFunctor;
52 inline const std::string&
NdrGetIdentifierString(const NdrIdentifier & id)53 NdrGetIdentifierString(const NdrIdentifier& id) { return id.GetString(); }
54 typedef std::vector<NdrIdentifier> NdrIdentifierVec;
55 typedef std::unordered_set<NdrIdentifier,
56                            NdrIdentifierHashFunctor> NdrIdentifierSet;
57 
58 // Token
59 typedef std::vector<TfToken> NdrTokenVec;
60 typedef std::unordered_map<TfToken, std::string,
61                            TfToken::HashFunctor> NdrTokenMap;
62 
63 // Property
64 typedef NdrProperty* NdrPropertyPtr;
65 typedef NdrProperty const* NdrPropertyConstPtr;
66 typedef std::unique_ptr<NdrProperty> NdrPropertyUniquePtr;
67 typedef std::vector<NdrPropertyUniquePtr> NdrPropertyUniquePtrVec;
68 typedef std::unordered_map<TfToken, NdrPropertyConstPtr,
69                            TfToken::HashFunctor> NdrPropertyPtrMap;
70 
71 // Node
72 typedef NdrNode* NdrNodePtr;
73 typedef NdrNode const* NdrNodeConstPtr;
74 typedef std::unique_ptr<NdrNode> NdrNodeUniquePtr;
75 typedef std::vector<NdrNodeConstPtr> NdrNodeConstPtrVec;
76 typedef std::vector<NdrNodeUniquePtr> NdrNodeUniquePtrVec;
77 
78 // Misc
79 typedef std::vector<std::string> NdrStringVec;
80 typedef std::pair<TfToken, TfToken> NdrOption;
81 typedef std::vector<NdrOption> NdrOptionVec;
82 typedef std::unordered_set<std::string> NdrStringSet;
83 typedef std::pair<SdfValueTypeName, TfToken> NdrSdfTypeIndicator;
84 
85 // Version
86 class NdrVersion {
87 public:
88     /// Create an invalid version.
89     NDR_API
90     NdrVersion() = default;
91     /// Create a version with the given major and minor numbers.
92     /// Numbers must be non-negative, and at least one must be non-zero.
93     /// On failure generates an error and yields an invalid version.
94     NDR_API
95     NdrVersion(int major, int minor = 0);
96     /// Create a version from a string.  On failure generates an error and
97     /// yields an invalid version.
98     NDR_API
99     NdrVersion(const std::string& x);
100 
101     /// Return an equal version marked as default.  It's permitted to mark
102     /// an invalid version as the default.
103     NDR_API
GetAsDefault()104     NdrVersion GetAsDefault() const
105     {
106         return NdrVersion(*this, true);
107     }
108 
109     /// Return the major version number or zero for an invalid version.
110     NDR_API
GetMajor()111     int GetMajor() const { return _major; }
112     /// Return the minor version number or zero for an invalid version.
113     NDR_API
GetMinor()114     int GetMinor() const { return _minor; }
115     /// Return true iff this version is marked as default.
116     NDR_API
IsDefault()117     bool IsDefault() const { return _isDefault; }
118 
119     /// Return the version as a string.
120     NDR_API
121     std::string GetString() const;
122 
123     /// Return the version as a identifier suffix.
124     NDR_API
125     std::string GetStringSuffix() const;
126 
127     /// Return a hash for the version.
128     NDR_API
GetHash()129     std::size_t GetHash() const
130     {
131         return (static_cast<std::size_t>(_major) << 32) +
132                 static_cast<std::size_t>(_minor);
133     }
134 
135     /// Return true iff the version is valid.
136     NDR_API
137     explicit operator bool() const
138     {
139         return !!*this;
140     }
141 
142     /// Return true iff the version is invalid.
143     NDR_API
144     bool operator!() const
145     {
146         return _major == 0 && _minor == 0;
147     }
148 
149     /// Return true iff versions are equal.
150     NDR_API
151     friend bool operator==(const NdrVersion& lhs, const NdrVersion& rhs)
152     {
153         return lhs._major == rhs._major && lhs._minor == rhs._minor;
154     }
155 
156     /// Return true iff versions are not equal.
157     NDR_API
158     friend bool operator!=(const NdrVersion& lhs, const NdrVersion& rhs)
159     {
160         return !(lhs == rhs);
161     }
162 
163     /// Return true iff the left side is less than the right side.
164     NDR_API
165     friend bool operator<(const NdrVersion& lhs, const NdrVersion& rhs)
166     {
167         return lhs._major < rhs._major ||
168                (lhs._major == rhs._major && lhs._minor < rhs._minor);
169     }
170 
171     /// Return true iff the left side is less than or equal to the right side.
172     NDR_API
173     friend bool operator<=(const NdrVersion& lhs, const NdrVersion& rhs)
174     {
175         return lhs._major < rhs._major ||
176                (lhs._major == rhs._major && lhs._minor <= rhs._minor);
177     }
178 
179     /// Return true iff the left side is greater than the right side.
180     NDR_API
181     friend bool operator>(const NdrVersion& lhs, const NdrVersion& rhs)
182     {
183         return !(lhs <= rhs);
184     }
185 
186     /// Return true iff the left side is greater than or equal to the right side.
187     NDR_API
188     friend bool operator>=(const NdrVersion& lhs, const NdrVersion& rhs)
189     {
190         return !(lhs < rhs);
191     }
192 
193 private:
NdrVersion(const NdrVersion & x,bool)194     NdrVersion(const NdrVersion& x, bool)
195         : _major(x._major), _minor(x._minor), _isDefault(true) { }
196 
197 private:
198     int _major = 0, _minor = 0;
199     bool _isDefault = false;
200 };
201 
202 /// Enumeration used to select nodes by version.
203 enum NdrVersionFilter {
204     NdrVersionFilterDefaultOnly,
205     NdrVersionFilterAllVersions,
206     NdrNumVersionFilters
207 };
208 
209 PXR_NAMESPACE_CLOSE_SCOPE
210 
211 #endif // PXR_USD_NDR_DECLARE_H
212