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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_SDR_DECLARE_H
8 #define PXR_USD_SDR_DECLARE_H
9 
10 /// \file sdr/declare.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/sdr/api.h"
14 #include "pxr/base/tf/token.h"
15 
16 #include <memory>
17 #include <string>
18 #include <unordered_map>
19 #include <unordered_set>
20 #include <vector>
21 
23 
24 class SdrShaderNode;
25 class SdrShaderProperty;
27 
28 /// Common typedefs that are used throughout the SDR library.
29 
30 typedef TfToken SdrIdentifier;
32 inline const std::string&
33 SdrGetIdentifierString(const SdrIdentifier& id) { return id.GetString(); }
34 typedef std::vector<SdrIdentifier> SdrIdentifierVec;
35 typedef std::unordered_set<SdrIdentifier,
37 
38 // Token
39 typedef std::vector<TfToken> SdrTokenVec;
40 typedef std::unordered_map<TfToken, std::string,
42 
43 // ShaderNode
46 typedef std::unique_ptr<SdrShaderNode> SdrShaderNodeUniquePtr;
47 typedef std::vector<SdrShaderNodeConstPtr> SdrShaderNodeConstPtrVec;
49 typedef std::vector<SdrShaderNodeUniquePtr> SdrShaderNodeUniquePtrVec;
50 
51 // ShaderProperty
54 typedef std::unique_ptr<SdrShaderProperty> SdrShaderPropertyUniquePtr;
55 typedef std::vector<SdrShaderPropertyUniquePtr> SdrShaderPropertyUniquePtrVec;
56 typedef std::unordered_map<TfToken, SdrShaderPropertyConstPtr,
58 typedef SdrShaderPropertyMap SdrPropertyMap;
59 
60 // Misc
61 typedef std::vector<std::string> SdrStringVec;
62 typedef std::pair<TfToken, TfToken> SdrOption;
63 typedef std::vector<SdrOption> SdrOptionVec;
64 typedef std::unordered_set<std::string> SdrStringSet;
65 
66 /// SdrVersion
67 class SdrVersion {
68 public:
69  /// Create an invalid version.
70  SDR_API
71  SdrVersion() = default;
72  /// Create a version with the given major and minor numbers.
73  /// Numbers must be non-negative, and at least one must be non-zero.
74  /// On failure generates an error and yields an invalid version.
75  SDR_API
76  SdrVersion(int major, int minor = 0);
77  /// Create a version from a string. On failure generates an error and
78  /// yields an invalid version.
79  SDR_API
80  SdrVersion(const std::string& x);
81 
82  /// Return an equal version marked as default. It's permitted to mark
83  /// an invalid version as the default.
84  SDR_API
85  SdrVersion GetAsDefault() const
86  {
87  return SdrVersion(*this, true);
88  }
89 
90  /// Return the major version number or zero for an invalid version.
91  SDR_API
92  int GetMajor() const { return _major; }
93  /// Return the minor version number or zero for an invalid version.
94  SDR_API
95  int GetMinor() const { return _minor; }
96  /// Return true iff this version is marked as default.
97  SDR_API
98  bool IsDefault() const { return _isDefault; }
99 
100  /// Return the version as a string.
101  SDR_API
102  std::string GetString() const;
103 
104  /// Return the version as a identifier suffix.
105  SDR_API
106  std::string GetStringSuffix() const;
107 
108  /// Return a hash for the version.
109  SDR_API
110  std::size_t GetHash() const
111  {
112  return TfHash::Combine(_major, _minor);
113  }
114 
115  /// Return true iff the version is valid.
116  SDR_API
117  explicit operator bool() const
118  {
119  return !!*this;
120  }
121 
122  /// Return true iff the version is invalid.
123  SDR_API
124  bool operator!() const
125  {
126  return _major == 0 && _minor == 0;
127  }
128 
129  /// Return true iff versions are equal.
130  SDR_API
131  friend bool operator==(const SdrVersion& lhs, const SdrVersion& rhs)
132  {
133  return lhs._major == rhs._major && lhs._minor == rhs._minor;
134  }
135 
136  /// Return true iff versions are not equal.
137  SDR_API
138  friend bool operator!=(const SdrVersion& lhs, const SdrVersion& rhs)
139  {
140  return !(lhs == rhs);
141  }
142 
143  /// Return true iff the left side is less than the right side.
144  SDR_API
145  friend bool operator<(const SdrVersion& lhs, const SdrVersion& rhs)
146  {
147  return lhs._major < rhs._major ||
148  (lhs._major == rhs._major && lhs._minor < rhs._minor);
149  }
150 
151  /// Return true iff the left side is less than or equal to the right side.
152  SDR_API
153  friend bool operator<=(const SdrVersion& lhs, const SdrVersion& rhs)
154  {
155  return lhs._major < rhs._major ||
156  (lhs._major == rhs._major && lhs._minor <= rhs._minor);
157  }
158 
159  /// Return true iff the left side is greater than the right side.
160  SDR_API
161  friend bool operator>(const SdrVersion& lhs, const SdrVersion& rhs)
162  {
163  return !(lhs <= rhs);
164  }
165 
166  /// Return true iff the left side is greater than or equal to the right side.
167  SDR_API
168  friend bool operator>=(const SdrVersion& lhs, const SdrVersion& rhs)
169  {
170  return !(lhs < rhs);
171  }
172 
173 private:
174  SdrVersion(const SdrVersion& x, bool)
175  : _major(x._major), _minor(x._minor), _isDefault(true) { }
176 
177 private:
178  int _major = 0, _minor = 0;
179  bool _isDefault = false;
180 };
181 
182 /// Enumeration used to select nodes by version.
187 };
188 
190 
191 #endif // PXR_USD_SDR_DECLARE_H
TfToken::HashFunctor SdrIdentifierHashFunctor
Definition: declare.h:31
std::vector< std::string > SdrStringVec
Definition: declare.h:61
SDR_API friend bool operator<(const SdrVersion &lhs, const SdrVersion &rhs)
Return true iff the left side is less than the right side.
Definition: declare.h:145
SdrVersionFilter
Enumeration used to select nodes by version.
Definition: declare.h:183
std::pair< TfToken, TfToken > SdrOption
Definition: declare.h:62
SDR_API std::string GetString() const
Return the version as a string.
std::unordered_set< SdrIdentifier, SdrIdentifierHashFunctor > SdrIdentifierSet
Definition: declare.h:36
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
SDR_API std::size_t GetHash() const
Return a hash for the version.
Definition: declare.h:110
Functor to use for hash maps from tokens to other things.
Definition: token.h:149
std::vector< SdrIdentifier > SdrIdentifierVec
Definition: declare.h:34
SDR_API friend bool operator<=(const SdrVersion &lhs, const SdrVersion &rhs)
Return true iff the left side is less than or equal to the right side.
Definition: declare.h:153
SdrShaderNode const * SdrShaderNodeConstPtr
Definition: declare.h:45
SDR_API friend bool operator>(const SdrVersion &lhs, const SdrVersion &rhs)
Return true iff the left side is greater than the right side.
Definition: declare.h:161
OutGridT const XformOp bool bool
SdrVersion.
Definition: declare.h:67
SDR_API int GetMinor() const
Return the minor version number or zero for an invalid version.
Definition: declare.h:95
SdrShaderNodeConstPtrVec SdrShaderNodePtrVec
Definition: declare.h:48
SdrShaderNode * SdrShaderNodePtr
Definition: declare.h:44
Definition: token.h:70
SDR_API SdrVersion GetAsDefault() const
Definition: declare.h:85
TfToken SdrIdentifier
Common typedefs that are used throughout the SDR library.
Definition: declare.h:26
SDR_API SdrVersion()=default
Create an invalid version.
SDR_API friend bool operator!=(const SdrVersion &lhs, const SdrVersion &rhs)
Return true iff versions are not equal.
Definition: declare.h:138
SDR_API friend bool operator==(const SdrVersion &lhs, const SdrVersion &rhs)
Return true iff versions are equal.
Definition: declare.h:131
SdrShaderPropertyMap SdrPropertyMap
Definition: declare.h:58
SDR_API std::string GetStringSuffix() const
Return the version as a identifier suffix.
SDR_API bool operator!() const
Return true iff the version is invalid.
Definition: declare.h:124
std::unordered_set< std::string > SdrStringSet
Definition: declare.h:64
SdrShaderProperty const * SdrShaderPropertyConstPtr
Definition: declare.h:53
GLint GLenum GLint x
Definition: glcorearb.h:409
std::vector< SdrShaderNodeConstPtr > SdrShaderNodeConstPtrVec
Definition: declare.h:47
const std::string & SdrGetIdentifierString(const SdrIdentifier &id)
Definition: declare.h:33
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
std::unique_ptr< SdrShaderProperty > SdrShaderPropertyUniquePtr
Definition: declare.h:54
std::unique_ptr< SdrShaderNode > SdrShaderNodeUniquePtr
Definition: declare.h:46
std::unordered_map< TfToken, SdrShaderPropertyConstPtr, TfToken::HashFunctor > SdrShaderPropertyMap
Definition: declare.h:57
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::vector< SdrShaderNodeUniquePtr > SdrShaderNodeUniquePtrVec
Definition: declare.h:49
SdrShaderProperty * SdrShaderPropertyPtr
Definition: declare.h:52
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
SDR_API int GetMajor() const
Return the major version number or zero for an invalid version.
Definition: declare.h:92
SDR_API friend bool operator>=(const SdrVersion &lhs, const SdrVersion &rhs)
Return true iff the left side is greater than or equal to the right side.
Definition: declare.h:168
#define SDR_API
Definition: api.h:23
std::vector< SdrOption > SdrOptionVec
Definition: declare.h:63
SDR_API bool IsDefault() const
Return true iff this version is marked as default.
Definition: declare.h:98
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:41
std::vector< SdrShaderPropertyUniquePtr > SdrShaderPropertyUniquePtrVec
Definition: declare.h:55