spec.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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 #ifndef PXR_USD_SDF_SPEC_H
25 #define PXR_USD_SDF_SPEC_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/api.h"
31 #include "pxr/usd/sdf/abstractData.h"
32 #include "pxr/usd/sdf/identity.h"
33 #include "pxr/usd/sdf/declareSpec.h"
34 #include "pxr/usd/sdf/schema.h"
35 #include "pxr/usd/sdf/types.h"
36 #include "pxr/base/vt/value.h"
37 #include "pxr/base/tf/hash.h"
38 #include "pxr/base/tf/token.h"
39 #include "pxr/base/tf/type.h"
40 
41 #include <boost/type_traits/is_base_of.hpp>
42 #include <boost/utility/enable_if.hpp>
43 
44 #include <iosfwd>
45 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
52 class SdfSpec
53 {
54  SDF_DECLARE_BASE_SPEC(SdfSpec);
55 
56 public:
57  SDF_API
58  SdfSpec &operator=(const SdfSpec &other);
59 
60  SDF_API
61  ~SdfSpec();
62 
66 
68  SDF_API
69  const SdfSchemaBase& GetSchema() const;
70 
73  SDF_API
74  SdfSpecType GetSpecType() const;
75 
77  SDF_API
78  bool IsDormant() const;
79 
81  SDF_API
82  SdfLayerHandle GetLayer() const;
83 
85  SDF_API
86  SdfPath GetPath() const;
87 
89  SDF_API
90  bool PermissionToEdit() const;
91 
94  SDF_API
95  std::vector<TfToken> ListInfoKeys() const;
96 
105  SDF_API
106  std::vector<TfToken> GetMetaDataInfoKeys() const;
107 
110  SDF_API
111  TfToken GetMetaDataDisplayGroup(TfToken const &key) const;
112 
117  SDF_API
118  VtValue GetInfo(const TfToken &key) const;
119 
127  SDF_API
128  void SetInfo(const TfToken &key, const VtValue &value);
129 
132  SDF_API
133  void SetInfoDictionaryValue(const TfToken &dictionaryKey,
134  const TfToken &entryKey, const VtValue &value);
135 
152  SDF_API
153  bool HasInfo(const TfToken &key) const;
154 
163  SDF_API
164  void ClearInfo(const TfToken &key);
165 
167  SDF_API
168  TfType GetTypeForInfo(const TfToken &key) const;
169 
171  SDF_API
172  const VtValue& GetFallbackForInfo(const TfToken &key) const;
173 
175  SDF_API
176  bool WriteToStream(std::ostream&, size_t indent = 0) const;
177 
186  SDF_API
187  bool IsInert(bool ignoreChildren = false) const;
188 
190 
193 
195  SDF_API
196  std::vector<TfToken> ListFields() const;
197 
200  SDF_API
201  bool HasField(const TfToken &name) const;
202 
206  template <class T>
207  bool HasField(const TfToken &name, T* value) const
208  {
209  if (!value) {
210  return HasField(name);
211  }
212 
213  SdfAbstractDataTypedValue<T> outValue(value);
214  return _HasField(name, &outValue);
215  }
216 
218  SDF_API
219  VtValue GetField(const TfToken &name) const;
220 
224  template <typename T>
225  T GetFieldAs(const TfToken & name, const T& defaultValue = T()) const
226  {
227  VtValue v = GetField(name);
228  if (v.IsEmpty() || !v.IsHolding<T>())
229  return defaultValue;
230  return v.UncheckedGet<T>();
231  }
232 
234  SDF_API
235  bool SetField(const TfToken & name, const VtValue& value);
236 
238  template <typename T>
239  bool SetField(const TfToken & name, const T& value)
240  {
241  return SetField(name, VtValue(value));
242  }
243 
245  SDF_API
246  bool ClearField(const TfToken & name);
247 
249 
252 
253  SDF_API bool operator==(const SdfSpec& rhs) const;
254  SDF_API bool operator<(const SdfSpec& rhs) const;
255 
257 
259  template <class HashState>
260  friend void TfHashAppend(HashState &h, const SdfSpec& x);
261 
262 private:
263  SDF_API
264  bool _HasField(const TfToken& name, SdfAbstractDataValue* value) const;
265 
266 protected:
267  bool _MoveSpec(const SdfPath &oldPath, const SdfPath &newPath) const;
268  bool _DeleteSpec(const SdfPath &path);
269 
270 private:
271  Sdf_IdentityRefPtr _id;
272 };
273 
274 template <class HashState>
275 void TfHashAppend(HashState &h, const SdfSpec& x) {
276  h.Append(x._id.get());
277 }
278 
279 inline size_t hash_value(const SdfSpec &x) {
280  return TfHash()(x);
281 }
282 
283 PXR_NAMESPACE_CLOSE_SCOPE
284 
285 #endif // PXR_USD_SDF_SPEC_H
T GetFieldAs(const TfToken &name, const T &defaultValue=T()) const
Returns a field value by name.
Definition: spec.h:225
SDF_API SdfLayerHandle GetLayer() const
Returns the layer that this object belongs to.
SDF_API std::vector< TfToken > ListFields() const
Returns all fields with values.
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1093
A type-erased container for a field value in an SdfAbstractData.
Definition: abstractData.h:415
SDF_API bool HasInfo(const TfToken &key) const
Returns whether there is a setting for the scene spec info with the given key.
Generic class that provides information about scene description fields but doesn't actually provide a...
Definition: schema.h:62
Base class for all Sdf spec classes.
Definition: spec.h:52
SDF_API bool IsInert(bool ignoreChildren=false) const
Returns whether this object has no significant data.
SDF_API void SetInfo(const TfToken &key, const VtValue &value)
Sets the value for the given metadata key.
SDF_API const SdfSchemaBase & GetSchema() const
Returns the SdfSchemaBase for the layer that owns this spec.
SDF_API const VtValue & GetFallbackForInfo(const TfToken &key) const
Returns the fallback for the info with the given key.
SDF_API bool ClearField(const TfToken &name)
Clears a field.
SDF_API bool PermissionToEdit() const
Returns whether this object's layer can be edited.
SDF_API VtValue GetField(const TfToken &name) const
Returns a field value by name.
SDF_API TfType GetTypeForInfo(const TfToken &key) const
Returns the data type for the info with the given key.
Basic Sdf data types.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:504
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
SDF_API SdfSpecType GetSpecType() const
Returns the SdfSpecType specifying the spec type this object represents.
SDF_API std::vector< TfToken > ListInfoKeys() const
Returns the full list of info keys currently set on this object.
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1271
The fully-typed container for a field value in an SdfAbstractData.
Definition: abstractData.h:466
SDF_API TfToken GetMetaDataDisplayGroup(TfToken const &key) const
Returns this metadata key's displayGroup.
bool HasField(const TfToken &name, T *value) const
Returns true if the object has a non-empty value with name name and type T.
Definition: spec.h:207
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
bool SetField(const TfToken &name, const T &value)
Sets a field value of type T.
Definition: spec.h:239
SDF_API void SetInfoDictionaryValue(const TfToken &dictionaryKey, const TfToken &entryKey, const VtValue &value)
Sets the value for entryKey to value within the dictionary with the given metadata key dictionaryKey.
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:91
SDF_API std::vector< TfToken > GetMetaDataInfoKeys() const
Returns the list of metadata info keys for this object.
SDF_API bool IsDormant() const
Returns true if this object is invalid or expired.
SDF_API bool WriteToStream(std::ostream &, size_t indent=0) const
Writes this spec to the given stream.
friend void TfHashAppend(HashState &h, const SdfSpec &x)
Hash.
Definition: spec.h:275
SDF_API SdfPath GetPath() const
Returns the scene path of this object.
TfType represents a dynamic runtime type.
Definition: type.h:64
SDF_API void ClearInfo(const TfToken &key)
Clears the value for scene spec info with the given key.
SDF_API bool SetField(const TfToken &name, const VtValue &value)
Sets a field value as a boxed VtValue.
SDF_API VtValue GetInfo(const TfToken &key) const
Gets the value for the given metadata key.
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1053
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
SDF_API bool HasField(const TfToken &name) const
Returns true if the spec has a non-empty value with field name name.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...