HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
spec.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_SPEC_H
8 #define PXR_USD_SDF_SPEC_H
9 
10 /// \file sdf/spec.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/sdf/api.h"
15 #include "pxr/usd/sdf/identity.h"
17 #include "pxr/usd/sdf/schema.h"
18 #include "pxr/usd/sdf/types.h"
19 #include "pxr/base/vt/value.h"
20 #include "pxr/base/tf/hash.h"
21 #include "pxr/base/tf/token.h"
22 #include "pxr/base/tf/type.h"
23 
24 #include <iosfwd>
25 
27 
28 /// \class SdfSpec
29 ///
30 /// Base class for all Sdf spec classes.
31 ///
32 class SdfSpec
33 {
34  SDF_DECLARE_BASE_SPEC(SdfSpec);
35 
36 public:
37  SDF_API
38  SdfSpec &operator=(const SdfSpec &other);
39 
40  SDF_API
41  ~SdfSpec();
42 
43  /// \name SdSpec generic API
44  ///
45  /// @{
46 
47  /// Returns the SdfSchemaBase for the layer that owns this spec.
48  SDF_API
49  const SdfSchemaBase& GetSchema() const;
50 
51  /// Returns the SdfSpecType specifying the spec type this object
52  /// represents.
53  SDF_API
54  SdfSpecType GetSpecType() const;
55 
56  /// Returns true if this object is invalid or expired.
57  SDF_API
58  bool IsDormant() const;
59 
60  /// Returns the layer that this object belongs to.
61  SDF_API
62  SdfLayerHandle GetLayer() const;
63 
64  /// Returns the scene path of this object.
65  SDF_API
66  SdfPath GetPath() const;
67 
68  /// Returns whether this object's layer can be edited.
69  SDF_API
70  bool PermissionToEdit() const;
71 
72  /// Returns the full list of info keys currently set on this object.
73  /// \note This does not include fields that represent names of children.
74  SDF_API
75  std::vector<TfToken> ListInfoKeys() const;
76 
77  /// Returns the list of metadata info keys for this object.
78  ///
79  /// This is not the complete list of keys, it is only those that
80  /// should be considered to be metadata by inspectors or other
81  /// presentation UI.
82  ///
83  /// This is interim API which is likely to change. Only editors with
84  /// an immediate specific need (like the Inspector) should use this API.
85  SDF_API
86  std::vector<TfToken> GetMetaDataInfoKeys() const;
87 
88  /// Returns this metadata key's displayGroup.
89  ///
90  SDF_API
91  TfToken GetMetaDataDisplayGroup(TfToken const &key) const;
92 
93  /// Gets the value for the given metadata key.
94  ///
95  /// This is interim API which is likely to change. Only editors with
96  /// an immediate specific need (like the Inspector) should use this API.
97  SDF_API
98  VtValue GetInfo(const TfToken &key) const;
99 
100  /// Sets the value for the given metadata key.
101  ///
102  /// It is an error to pass a value that is not the correct type for
103  /// that given key.
104  ///
105  /// This is interim API which is likely to change. Only editors with
106  /// an immediate specific need (like the Inspector) should use this API.
107  SDF_API
108  void SetInfo(const TfToken &key, const VtValue &value);
109 
110  /// Sets the value for \p entryKey to \p value within the dictionary
111  /// with the given metadata key \p dictionaryKey
112  SDF_API
113  void SetInfoDictionaryValue(const TfToken &dictionaryKey,
114  const TfToken &entryKey, const VtValue &value);
115 
116  /// Returns whether there is a setting for the scene spec info
117  /// with the given key.
118  ///
119  /// When asked for a value for one of its scene spec info, a valid value
120  /// will always be returned. But if this API returns \b false for a scene
121  /// spec info, the value of that info will be the defined default value.
122  ///
123  /// When dealing with a composedLayer, it is not necessary to worry about
124  /// whether a scene spec info "has a value" because the composed layer will
125  /// always have a valid value, even if it is the default.
126  ///
127  /// A spec may or may not have an expressed value for some of its
128  /// scene spec info.
129  ///
130  /// This is interim API which is likely to change. Only editors with
131  /// an immediate specific need (like the Inspector) should use this API.
132  SDF_API
133  bool HasInfo(const TfToken &key) const;
134 
135  /// Clears the value for scene spec info with the given \a key.
136  ///
137  /// After calling this, HasInfo() will return \b false.
138  /// To make HasInfo() return \b true just set a value for that
139  /// scene spec info.
140  ///
141  /// This is interim API which is likely to change. Only editors with
142  /// an immediate specific need (like the Inspector) should use this API.
143  SDF_API
144  void ClearInfo(const TfToken &key);
145 
146  /// Returns the data type for the info with the given \a key.
147  SDF_API
148  TfType GetTypeForInfo(const TfToken &key) const;
149 
150  /// Returns the fallback for the info with the given \a key.
151  SDF_API
152  const VtValue& GetFallbackForInfo(const TfToken &key) const;
153 
154  /// Writes this spec to the given stream.
155  SDF_API
156  bool WriteToStream(std::ostream&, size_t indent = 0) const;
157 
158  /// Returns whether this object has no significant data.
159  ///
160  /// "Significant" here means that the object contributes opinions to
161  /// a scene. If this spec has any child scenegraph objects (e.g.,
162  /// prim or property spec), it will be considered significant even if
163  /// those child objects are not.
164  /// However, if \p ignoreChildren is \c true, these child objects
165  /// will be ignored.
166  SDF_API
167  bool IsInert(bool ignoreChildren = false) const;
168 
169  /// @}
170 
171  /// \name Field-based Generic API
172  /// @{
173 
174  /// Returns all fields with values.
175  SDF_API
176  std::vector<TfToken> ListFields() const;
177 
178  /// Returns \c true if the spec has a non-empty value with field
179  /// name \p name.
180  SDF_API
181  bool HasField(const TfToken &name) const;
182 
183  /// Returns \c true if the object has a non-empty value with name
184  /// \p name and type \p T. If value ptr is provided, returns the
185  /// value found.
186  template <class T>
187  bool HasField(const TfToken &name, T* value) const
188  {
189  if (!value) {
190  return HasField(name);
191  }
192 
193  SdfAbstractDataTypedValue<T> outValue(value);
194  return _HasField(name, &outValue);
195  }
196 
197  /// Returns a field value by name.
198  SDF_API
199  VtValue GetField(const TfToken &name) const;
200 
201  /// Returns a field value by name. If the object is invalid, or the
202  /// value doesn't exist, isn't set, or isn't of the given type then
203  /// returns defaultValue.
204  template <typename T>
205  T GetFieldAs(const TfToken & name, const T& defaultValue = T()) const
206  {
207  VtValue v = GetField(name);
208  if (v.IsEmpty() || !v.IsHolding<T>())
209  return defaultValue;
210  return v.UncheckedGet<T>();
211  }
212 
213  /// Sets a field value as a boxed VtValue.
214  SDF_API
215  bool SetField(const TfToken & name, const VtValue& value);
216 
217  /// Sets a field value of type T.
218  template <typename T>
219  bool SetField(const TfToken & name, const T& value)
220  {
221  return SetField(name, VtValue(value));
222  }
223 
224  /// Clears a field.
225  SDF_API
226  bool ClearField(const TfToken & name);
227 
228  /// @}
229 
230  /// \name Comparison operators
231  /// @{
232 
233  SDF_API bool operator==(const SdfSpec& rhs) const;
234  SDF_API bool operator<(const SdfSpec& rhs) const;
235 
236  /// @}
237 
238  /// Hash.
239  template <class HashState>
240  friend void TfHashAppend(HashState &h, const SdfSpec& x);
241 
242 private:
243  SDF_API
244  bool _HasField(const TfToken& name, SdfAbstractDataValue* value) const;
245 
246 protected:
247  bool _MoveSpec(const SdfPath &oldPath, const SdfPath &newPath) const;
248  bool _DeleteSpec(const SdfPath &path);
249 
250 private:
251  Sdf_IdentityRefPtr _id;
252 };
253 
254 template <class HashState>
255 void TfHashAppend(HashState &h, const SdfSpec& x) {
256  h.Append(x._id.get());
257 }
258 
259 inline size_t hash_value(const SdfSpec &x) {
260  return TfHash()(x);
261 }
262 
264 
265 #endif // PXR_USD_SDF_SPEC_H
SDF_API SdfSpec & operator=(const SdfSpec &other)
bool HasField(const TfToken &name, T *value) const
Definition: spec.h:187
SDF_API VtValue GetInfo(const TfToken &key) const
SDF_API bool operator<(const SdfSpec &rhs) const
SDF_API bool HasField(const TfToken &name) const
SDF_API VtValue GetField(const TfToken &name) const
Returns a field value by name.
T const & UncheckedGet() const &
Definition: value.h:1104
SDF_API void SetInfoDictionaryValue(const TfToken &dictionaryKey, const TfToken &entryKey, const VtValue &value)
const GLdouble * v
Definition: glcorearb.h:837
SDF_API bool SetField(const TfToken &name, const VtValue &value)
Sets a field value as a boxed VtValue.
GLsizei const GLfloat * value
Definition: glcorearb.h:824
SDF_API bool operator==(const SdfSpec &rhs) const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
SDF_API const VtValue & GetFallbackForInfo(const TfToken &key) const
Returns the fallback for the info with the given key.
Definition: spec.h:32
SDF_API std::vector< TfToken > ListInfoKeys() const
SDF_API ~SdfSpec()
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1285
SDF_API bool PermissionToEdit() const
Returns whether this object's layer can be edited.
SDF_API const SdfSchemaBase & GetSchema() const
Returns the SdfSchemaBase for the layer that owns this spec.
SDF_API SdfLayerHandle GetLayer() const
Returns the layer that this object belongs to.
Definition: hash.h:472
T GetFieldAs(const TfToken &name, const T &defaultValue=T()) const
Definition: spec.h:205
RawPtrType get() const noexcept
Return the underlying pointer.
SDF_API std::vector< TfToken > ListFields() const
Returns all fields with values.
SDF_API bool ClearField(const TfToken &name)
Clears a field.
Definition: token.h:70
SDF_API bool HasInfo(const TfToken &key) const
SDF_API void SetInfo(const TfToken &key, const VtValue &value)
GLuint const GLchar * name
Definition: glcorearb.h:786
SDF_API SdfSpecType GetSpecType() const
Definition: path.h:273
SDF_API bool IsInert(bool ignoreChildren=false) const
GLint GLenum GLint x
Definition: glcorearb.h:409
#define SDF_API
Definition: api.h:23
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
SdfSpecType
Definition: types.h:68
SDF_API TfToken GetMetaDataDisplayGroup(TfToken const &key) const
bool IsHolding() const
Definition: value.h:1064
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
friend void TfHashAppend(HashState &h, const SdfSpec &x)
Hash.
Definition: spec.h:255
size_t hash_value(const SdfSpec &x)
Definition: spec.h:259
Definition: type.h:47
SDF_API SdfPath GetPath() const
Returns the scene path of this object.
void TfHashAppend(HashState &h, const SdfSpec &x)
Definition: spec.h:255
bool _DeleteSpec(const SdfPath &path)
SDF_API bool WriteToStream(std::ostream &, size_t indent=0) const
Writes this spec to the given stream.
SDF_API TfType GetTypeForInfo(const TfToken &key) const
Returns the data type for the info with the given key.
SDF_API void ClearInfo(const TfToken &key)
bool SetField(const TfToken &name, const T &value)
Sets a field value of type T.
Definition: spec.h:219
Definition: value.h:146
bool _MoveSpec(const SdfPath &oldPath, const SdfPath &newPath) const
SDF_API std::vector< TfToken > GetMetaDataInfoKeys() const
SDF_API bool IsDormant() const
Returns true if this object is invalid or expired.