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