HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
declare.h
Go to the documentation of this file.
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 
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 
52 inline const std::string&
53 NdrGetIdentifierString(const NdrIdentifier& id) { return id.GetString(); }
54 typedef std::vector<NdrIdentifier> NdrIdentifierVec;
55 typedef std::unordered_set<NdrIdentifier,
57 
58 // Token
59 typedef std::vector<TfToken> NdrTokenVec;
60 typedef std::unordered_map<TfToken, std::string,
62 
63 // Property
66 typedef std::unique_ptr<NdrProperty> NdrPropertyUniquePtr;
67 typedef std::vector<NdrPropertyUniquePtr> NdrPropertyUniquePtrVec;
68 typedef std::unordered_map<TfToken, NdrPropertyConstPtr,
70 
71 // Node
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> SdfTypeIndicator;
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
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
111  int GetMajor() const { return _major; }
112  /// Return the minor version number or zero for an invalid version.
113  NDR_API
114  int GetMinor() const { return _minor; }
115  /// Return true iff this version is marked as default.
116  NDR_API
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
126 
127  /// Return a hash for the version.
128  NDR_API
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:
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.
207 };
208 
210 
211 #endif // PXR_USD_NDR_DECLARE_H
NDR_API int GetMajor() const
Return the major version number or zero for an invalid version.
Definition: declare.h:111
NDR_API std::size_t GetHash() const
Return a hash for the version.
Definition: declare.h:129
NdrVersionFilter
Enumeration used to select nodes by version.
Definition: declare.h:203
std::pair< SdfValueTypeName, TfToken > SdfTypeIndicator
Definition: declare.h:83
NDR_API int GetMinor() const
Return the minor version number or zero for an invalid version.
Definition: declare.h:114
std::vector< TfToken > NdrTokenVec
Definition: declare.h:59
NDR_API friend bool operator>=(const NdrVersion &lhs, const NdrVersion &rhs)
Return true iff the left side is greater than or equal to the right side.
Definition: declare.h:188
NDR_API std::string GetString() const
Return the version as a string.
NdrNode const * NdrNodeConstPtr
Definition: declare.h:73
std::vector< NdrNodeUniquePtr > NdrNodeUniquePtrVec
Definition: declare.h:76
TfToken NdrIdentifier
Definition: declare.h:50
std::vector< NdrOption > NdrOptionVec
Definition: declare.h:81
NDR_API friend bool operator>(const NdrVersion &lhs, const NdrVersion &rhs)
Return true iff the left side is greater than the right side.
Definition: declare.h:181
Functor to use for hash maps from tokens to other things.
Definition: token.h:166
NDR_API std::string GetStringSuffix() const
Return the version as a identifier suffix.
TfToken::HashFunctor NdrIdentifierHashFunctor
Definition: declare.h:51
std::vector< NdrNodeConstPtr > NdrNodeConstPtrVec
Definition: declare.h:75
NDR_API bool operator!() const
Return true iff the version is invalid.
Definition: declare.h:144
NDR_API friend bool operator<=(const NdrVersion &lhs, const NdrVersion &rhs)
Return true iff the left side is less than or equal to the right side.
Definition: declare.h:173
std::vector< NdrPropertyUniquePtr > NdrPropertyUniquePtrVec
Definition: declare.h:67
Definition: token.h:87
std::vector< NdrIdentifier > NdrIdentifierVec
Definition: declare.h:54
NDR_API NdrVersion GetAsDefault() const
Definition: declare.h:104
NDR_API friend bool operator!=(const NdrVersion &lhs, const NdrVersion &rhs)
Return true iff versions are not equal.
Definition: declare.h:158
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
Definition: node.h:48
std::vector< std::string > NdrStringVec
Definition: declare.h:79
std::unordered_set< NdrIdentifier, NdrIdentifierHashFunctor > NdrIdentifierSet
Definition: declare.h:56
#define NDR_API
Definition: api.h:40
std::unique_ptr< NdrProperty > NdrPropertyUniquePtr
Definition: declare.h:66
std::pair< TfToken, TfToken > NdrOption
Definition: declare.h:80
GLsizei const GLchar *const * string
Definition: glew.h:1844
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
NDR_API bool IsDefault() const
Return true iff this version is marked as default.
Definition: declare.h:117
NDR_API friend bool operator<(const NdrVersion &lhs, const NdrVersion &rhs)
Return true iff the left side is less than the right side.
Definition: declare.h:165
std::unique_ptr< NdrNode > NdrNodeUniquePtr
Definition: declare.h:74
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
NdrProperty * NdrPropertyPtr
Definition: declare.h:64
NDR_API friend bool operator==(const NdrVersion &lhs, const NdrVersion &rhs)
Return true iff versions are equal.
Definition: declare.h:151
std::unordered_set< std::string > NdrStringSet
Definition: declare.h:82
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > NdrTokenMap
Definition: declare.h:61
std::unordered_map< TfToken, NdrPropertyConstPtr, TfToken::HashFunctor > NdrPropertyPtrMap
Definition: declare.h:69
NdrNode * NdrNodePtr
Definition: declare.h:72
NDR_API NdrVersion()=default
Create an invalid version.
NdrProperty const * NdrPropertyConstPtr
Definition: declare.h:65
const std::string & NdrGetIdentifierString(const NdrIdentifier &id)
Definition: declare.h:53