HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
valueTypeName.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_VALUE_TYPE_NAME_H
8 #define PXR_USD_SDF_VALUE_TYPE_NAME_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/sdf/api.h"
12 #include "pxr/base/tf/token.h"
13 
14 #include <iosfwd>
15 #include <string>
16 #include <vector>
17 
19 
20 class TfEnum;
21 class TfType;
22 class VtValue;
24 class Sdf_ValueTypeImpl;
25 
26 /// \struct SdfTupleDimensions
27 ///
28 /// Represents the shape of a value type (or that of an element in an array).
29 ///
31 {
32 public:
34  SdfTupleDimensions(size_t m) : size(1) { d[0] = m; }
35  SdfTupleDimensions(size_t m, size_t n) : size(2) { d[0] = m; d[1] = n; }
36  SdfTupleDimensions(const size_t (&s)[2])
37  : size(2) { d[0] = s[0]; d[1] = s[1]; }
38 
39  SDF_API
40  bool operator==(const SdfTupleDimensions& rhs) const;
41  bool operator!=(const SdfTupleDimensions& rhs) const {
42  return !(*this == rhs);
43  }
44 
45 public:
46  size_t d[2];
47  size_t size;
48 };
49 
50 /// \class SdfValueTypeName
51 ///
52 /// Represents a value type name, i.e. an attribute's type name. Usually,
53 /// a value type name associates a string with a \c TfType and an optional
54 /// role, along with additional metadata. A schema registers all known
55 /// value type names and may register multiple names for the same TfType
56 /// and role pair. All name strings for a given pair are collectively
57 /// called its aliases.
58 ///
59 /// A value type name may also represent just a name string, without a
60 /// \c TfType, role or other metadata. This is currently used exclusively
61 /// to unserialize and re-serialize an attribute's type name where that
62 /// name is not known to the schema.
63 ///
64 /// Because value type names can have aliases and those aliases may change
65 /// in the future, clients should avoid using the value type name's string
66 /// representation except to report human readable messages and when
67 /// serializing. Clients can look up a value type name by string using
68 /// \c SdfSchemaBase::FindType() and shouldn't otherwise need the string.
69 /// Aliases compare equal, even if registered by different schemas.
70 ///
72 {
73 public:
74  /// Constructs an invalid type name.
75  SDF_API
77 
78  /// Returns the type name as a token. This should not be used for
79  /// comparison purposes.
80  SDF_API
81  TfToken GetAsToken() const;
82 
83  /// Returns the \c TfType of the type.
84  SDF_API
85  const TfType& GetType() const;
86 
87  /// Return true if `value` holds an object whose type is compatible with
88  /// this scene description type in a strict sense. Specifically, return
89  /// true if `value` holds an object of this type, or if this IsArray() and
90  /// `value` holds a VtArrayEdit whose element type matches this type's
91  /// element type.
92  SDF_API
93  bool CanRepresent(const VtValue &value) const;
94 
95  /// \overload
96  SDF_API
97  bool CanRepresent(const SdfAbstractDataConstValue &value) const;
98 
99  /// Returns the C++ type name for this type. This may not be the same
100  /// as the type name returned by GetType().GetTypeName(), since that
101  /// method may have had additional transformations applied for
102  /// readability.
103  SDF_API
104  const std::string& GetCPPTypeName() const;
105 
106  /// Returns the type's role.
107  SDF_API
108  const TfToken& GetRole() const;
109 
110  /// Returns the default value for the type.
111  SDF_API
112  const VtValue& GetDefaultValue() const;
113 
114  /// Returns the default unit enum for the type.
115  SDF_API
116  const TfEnum& GetDefaultUnit() const;
117 
118  /// Returns the scalar version of this type name if it's an array type
119  /// name, otherwise returns this type name. If there is no scalar type
120  /// name then this returns the invalid type name.
121  SDF_API
122  SdfValueTypeName GetScalarType() const;
123 
124  /// Returns the array version of this type name if it's an scalar type
125  /// name, otherwise returns this type name. If there is no array type
126  /// name then this returns the invalid type name.
127  SDF_API
128  SdfValueTypeName GetArrayType() const;
129 
130  /// Returns \c true iff this type is a scalar. The invalid type is
131  /// considered neither scalar nor array.
132  SDF_API
133  bool IsScalar() const;
134 
135  /// Returns \c true iff this type is an array. The invalid type is
136  /// considered neither scalar nor array.
137  SDF_API
138  bool IsArray() const;
139 
140  /// Returns the dimensions of the scalar value, e.g. 3 for a 3D point.
141  SDF_API
143 
144  /// Returns \c true if this type name is equal to \p rhs. Aliases
145  /// compare equal.
146  SDF_API
147  bool operator==(const SdfValueTypeName& rhs) const;
148  bool operator!=(const SdfValueTypeName& rhs) const {
149  return !(*this == rhs);
150  }
151 
152  /// Returns \c true if this type name is equal to \p rhs. Aliases
153  /// compare equal. Avoid relying on this overload.
154  SDF_API
155  bool operator==(const std::string& rhs) const;
156  bool operator!=(const std::string& rhs) const {
157  return !(*this == rhs);
158  }
159 
160  /// Returns \c true if this type name is equal to \p rhs. Aliases
161  /// compare equal. Avoid relying on this overload.
162  SDF_API
163  bool operator==(const TfToken& rhs) const;
164  bool operator!=(const TfToken& rhs) const {
165  return !(*this == rhs);
166  }
167 
168  friend inline
169  bool operator==(const std::string& lhs, const SdfValueTypeName& rhs) {
170  return rhs == lhs;
171  }
172  friend inline
173  bool operator!=(const std::string& lhs, const SdfValueTypeName& rhs) {
174  return !(rhs == lhs);
175  }
176 
177  friend inline
178  bool operator==(const TfToken& lhs, const SdfValueTypeName &rhs) {
179  return rhs == lhs;
180  }
181  friend inline
182  bool operator!=(const TfToken& lhs, const SdfValueTypeName &rhs) {
183  return !(rhs == lhs);
184  }
185 
186  /// Returns a hash value for this type name.
187  SDF_API
188  size_t GetHash() const;
189 
190  /// Explicit bool conversion operator. Converts to \c true if this is a
191  /// valid, non-empty type, \c false otherwise.
192  explicit operator bool() const
193  {
194  return !_IsEmpty();
195  }
196 
197  /// Returns all aliases of the type name as tokens. These should not
198  /// be used for comparison purposes.
199  SDF_API
200  std::vector<TfToken> GetAliasesAsTokens() const;
201 
202 private:
203  friend class Sdf_ValueTypeRegistry;
204  friend struct Sdf_ValueTypePrivate;
205 
206  SDF_API
207  explicit SdfValueTypeName(const Sdf_ValueTypeImpl*);
208 
209  SDF_API
210  bool _IsEmpty() const;
211 
212 private:
213  const Sdf_ValueTypeImpl* _impl;
214 };
215 
216 /// Functor for hashing a \c SdfValueTypeName.
218  size_t operator()(const SdfValueTypeName& x) const
219  {
220  return x.GetHash();
221  }
222 };
223 
224 inline size_t
225 hash_value(const SdfValueTypeName& typeName)
226 {
227  return typeName.GetHash();
228 }
229 
230 SDF_API std::ostream& operator<<(std::ostream&, const SdfValueTypeName& typeName);
231 
233 
234 #endif // PXR_USD_SDF_VALUE_TYPE_NAME_H
friend bool operator==(const std::string &lhs, const SdfValueTypeName &rhs)
SDF_API const VtValue & GetDefaultValue() const
Returns the default value for the type.
bool operator!=(const SdfValueTypeName &rhs) const
bool operator!=(const SdfTupleDimensions &rhs) const
Definition: valueTypeName.h:41
friend bool operator==(const TfToken &lhs, const SdfValueTypeName &rhs)
SdfTupleDimensions(size_t m, size_t n)
Definition: valueTypeName.h:35
SDF_API const std::string & GetCPPTypeName() const
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
bool operator!=(const TfToken &rhs) const
size_t hash_value(const SdfValueTypeName &typeName)
GLdouble s
Definition: glad.h:3009
Definition: enum.h:119
SDF_API SdfValueTypeName GetScalarType() const
SDF_API SdfTupleDimensions GetDimensions() const
Returns the dimensions of the scalar value, e.g. 3 for a 3D point.
SDF_API std::ostream & operator<<(std::ostream &, const SdfValueTypeName &typeName)
SdfTupleDimensions(const size_t(&s)[2])
Definition: valueTypeName.h:36
OutGridT const XformOp bool bool
SDF_API bool IsScalar() const
SDF_API bool CanRepresent(const VtValue &value) const
SDF_API TfToken GetAsToken() const
SDF_API bool IsArray() const
GLdouble n
Definition: glcorearb.h:2008
Definition: token.h:70
friend bool operator!=(const TfToken &lhs, const SdfValueTypeName &rhs)
SDF_API SdfValueTypeName()
Constructs an invalid type name.
SDF_API const TfEnum & GetDefaultUnit() const
Returns the default unit enum for the type.
GLint GLenum GLint x
Definition: glcorearb.h:409
SDF_API const TfToken & GetRole() const
Returns the type's role.
friend bool operator!=(const std::string &lhs, const SdfValueTypeName &rhs)
SDF_API bool operator==(const SdfValueTypeName &rhs) const
#define SDF_API
Definition: api.h:23
GLsizeiptr size
Definition: glcorearb.h:664
Functor for hashing a SdfValueTypeName.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDF_API std::vector< TfToken > GetAliasesAsTokens() const
Definition: type.h:47
friend class Sdf_ValueTypeRegistry
size_t operator()(const SdfValueTypeName &x) const
friend struct Sdf_ValueTypePrivate
bool operator!=(const std::string &rhs) const
SDF_API const TfType & GetType() const
Returns the TfType of the type.
SDF_API SdfValueTypeName GetArrayType() const
SdfTupleDimensions(size_t m)
Definition: valueTypeName.h:34
SDF_API size_t GetHash() const
Returns a hash value for this type name.
Definition: value.h:89
SDF_API bool operator==(const SdfTupleDimensions &rhs) const