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 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 SDF_SPEC_H
25 #define 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/token.h"
38 #include "pxr/base/tf/type.h"
39 
40 #include <boost/type_traits/is_base_of.hpp>
41 #include <boost/utility/enable_if.hpp>
42 
43 #include <iosfwd>
44 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
51 class SdfSpec
52 {
53  SDF_DECLARE_BASE_SPEC(SdfSpec);
54 
55 public:
56  SDF_API
57  SdfSpec &operator=(const SdfSpec &other);
58 
59  SDF_API
60  ~SdfSpec();
61 
65 
67  SDF_API
68  const SdfSchemaBase& GetSchema() const;
69 
72  SDF_API
73  SdfSpecType GetSpecType() const;
74 
76  SDF_API
77  bool IsDormant() const;
78 
80  SDF_API
81  SdfLayerHandle GetLayer() const;
82 
84  SDF_API
85  SdfPath GetPath() const;
86 
88  SDF_API
89  bool PermissionToEdit() const;
90 
93  SDF_API
94  std::vector<TfToken> ListInfoKeys() const;
95 
104  SDF_API
105  std::vector<TfToken> GetMetaDataInfoKeys() const;
106 
109  SDF_API
110  TfToken GetMetaDataDisplayGroup(TfToken const &key) const;
111 
116  SDF_API
117  VtValue GetInfo(const TfToken &key) const;
118 
126  SDF_API
127  void SetInfo(const TfToken &key, const VtValue &value);
128 
131  SDF_API
132  void SetInfoDictionaryValue(const TfToken &dictionaryKey,
133  const TfToken &entryKey, const VtValue &value);
134 
151  SDF_API
152  bool HasInfo(const TfToken &key) const;
153 
162  SDF_API
163  void ClearInfo(const TfToken &key);
164 
166  SDF_API
167  TfType GetTypeForInfo(const TfToken &key) const;
168 
170  SDF_API
171  const VtValue& GetFallbackForInfo(const TfToken &key) const;
172 
174  SDF_API
175  bool WriteToStream(std::ostream&, size_t indent = 0) const;
176 
185  SDF_API
186  bool IsInert(bool ignoreChildren = false) const;
187 
189 
192 
194  SDF_API
195  std::vector<TfToken> ListFields() const;
196 
199  SDF_API
200  bool HasField(const TfToken &name) const;
201 
205  template <class T>
206  bool HasField(const TfToken &name, T* value) const
207  {
208  if (!value) {
209  return HasField(name);
210  }
211 
212  SdfAbstractDataTypedValue<T> outValue(value);
213  return _HasField(name, &outValue);
214  }
215 
217  SDF_API
218  VtValue GetField(const TfToken &name) const;
219 
223  template <typename T>
224  T GetFieldAs(const TfToken & name, const T& defaultValue = T()) const
225  {
226  VtValue v = GetField(name);
227  if (v.IsEmpty() || !v.IsHolding<T>())
228  return defaultValue;
229  return v.UncheckedGet<T>();
230  }
231 
233  SDF_API
234  bool SetField(const TfToken & name, const VtValue& value);
235 
237  template <typename T>
238  bool SetField(const TfToken & name, const T& value)
239  {
240  return SetField(name, VtValue(value));
241  }
242 
244  SDF_API
245  bool ClearField(const TfToken & name);
246 
248 
251 
252  SDF_API bool operator==(const SdfSpec& rhs) const;
253  SDF_API bool operator<(const SdfSpec& rhs) const;
254 
256 
258  friend size_t hash_value(const SdfSpec &x) {
259  return hash_value(x._id);
260  }
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 PXR_NAMESPACE_CLOSE_SCOPE
275 
276 #endif // SDF_SPEC_H
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:836
SDF_API SdfLayerHandle GetLayer() const
Returns the layer that this object belongs to.
SDF_API bool IsDormant() const
Returns true if this object is invalid or expired.
SDF_API SdfPath GetPath() const
Returns the scene path of this object.
SDF_API bool HasInfo(const TfToken &key) const
Returns whether there is a setting for the scene spec info with the given key.
A type-erased container for a field value in an SdfAbstractData.
Definition: abstractData.h:394
SDF_API std::vector< TfToken > ListInfoKeys() const
Returns the full list of info keys currently set on this object.
Generic class that provides information about scene description fields but doesn&#39;t actually provide a...
Definition: schema.h:62
Base class for all Sdf spec classes.
Definition: spec.h:51
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:808
friend size_t hash_value(const SdfSpec &x)
Hash.
Definition: spec.h:258
SDF_API void SetInfo(const TfToken &key, const VtValue &value)
Sets the value for the given metadata key.
SdfSpecType
An enum that specifies the type of an object.
Definition: types.h:91
SDF_API bool ClearField(const TfToken &name)
Clears a field.
SDF_API VtValue GetField(const TfToken &name) const
Returns a field value by name.
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.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
SDF_API std::vector< TfToken > GetMetaDataInfoKeys() const
Returns the list of metadata info keys for this object.
SDF_API const SdfSchemaBase & GetSchema() const
Returns the SdfSchemaBase for the layer that owns this spec.
SDF_API std::vector< TfToken > ListFields() const
Returns all fields with values.
SDF_API SdfSpecType GetSpecType() const
Returns the SdfSpecType specifying the spec type this object represents.
The fully-typed container for a field value in an SdfAbstractData.
Definition: abstractData.h:441
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
bool SetField(const TfToken &name, const T &value)
Sets a field value of type T.
Definition: spec.h:238
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:206
SDF_API const VtValue & GetFallbackForInfo(const TfToken &key) const
Returns the fallback for the info with the given key.
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...
SDF_API bool IsInert(bool ignoreChildren=false) const
Returns whether this object has no significant data.
T GetFieldAs(const TfToken &name, const T &defaultValue=T()) const
Returns a field value by name.
Definition: spec.h:224
SDF_API bool PermissionToEdit() const
Returns whether this object&#39;s layer can be edited.
TfType represents a dynamic runtime type.
Definition: type.h:70
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 bool HasField(const TfToken &name) const
Returns true if the spec has a non-empty value with field name name.
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:990
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
SDF_API VtValue GetInfo(const TfToken &key) const
Gets the value for the given metadata key.
SDF_API TfToken GetMetaDataDisplayGroup(TfToken const &key) const
Returns this metadata key&#39;s displayGroup.