HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
schemaBase.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_USD_SCHEMA_BASE_H
8 #define PXR_USD_USD_SCHEMA_BASE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/usd/usd/api.h"
12 #include "pxr/usd/usd/prim.h"
13 #include "pxr/usd/usd/attribute.h"
15 #include "pxr/usd/usd/references.h"
16 
18 
19 
20 /// \class UsdSchemaBase
21 ///
22 /// The base class for all schema types in Usd.
23 ///
24 /// Schema objects hold a ::UsdPrim internally and provide a layer of specific
25 /// named API atop the underlying scene graph.
26 ///
27 /// Schema objects are polymorphic but they are intended to be created as
28 /// automatic local variables, so they may be passed and returned by-value.
29 /// This leaves them subject to <a
30 /// href="http://en.wikipedia.org/wiki/Object_slicing">slicing</a>. This means
31 /// that if one passes a <tt>SpecificSchema</tt> instance to a function that
32 /// takes a UsdSchemaBase \e by-value, all the polymorphic behavior specific to
33 /// <tt>SpecificSchema</tt> is lost.
34 ///
35 /// To avoid slicing, it is encouraged that functions taking schema object
36 /// arguments take them by <tt>const &</tt> if const access is sufficient,
37 /// otherwise by non-const pointer.
38 ///
40 public:
41  /// Compile time constant representing what kind of schema this class is.
42  ///
43  /// \sa UsdSchemaKind in usd/common.h
45 
46  /// Returns whether or not this class corresponds to a concrete instantiable
47  /// prim type in scene description. If this is true,
48  /// GetStaticPrimDefinition() will return a valid prim definition with
49  /// a non-empty typeName.
50  bool IsConcrete() const {
52  }
53 
54  /// Returns whether or not this class inherits from UsdTyped. Types which
55  /// inherit from UsdTyped can impart a typename on a UsdPrim.
56  bool IsTyped() const {
59  }
60 
61  /// Returns whether this is an API schema or not.
62  bool IsAPISchema() const {
66  }
67 
68  /// Returns whether this is an applied API schema or not. If this returns
69  /// true this class will have an Apply() method
70  bool IsAppliedAPISchema() const {
73  }
74 
75  /// Returns whether this is an applied API schema or not. If this returns
76  /// true the constructor, Get and Apply methods of this class will take
77  /// in the name of the API schema instance.
78  bool IsMultipleApplyAPISchema() const {
80  }
81 
82  /// Returns the kind of schema this class is.
84  return _GetSchemaKind();
85  }
86 
87  /// Construct and store \p prim as the held prim.
88  USD_API
89  explicit UsdSchemaBase(const UsdPrim& prim = UsdPrim());
90 
91  /// Construct and store for the same prim held by \p otherSchema
92  USD_API
93  explicit UsdSchemaBase(const UsdSchemaBase& otherSchema);
94 
95  /// Destructor.
96  USD_API
97  virtual ~UsdSchemaBase();
98 
99  /// \name Held prim access.
100  //@{
101 
102  /// Return this schema object's held prim.
103  UsdPrim GetPrim() const { return UsdPrim(_primData, _proxyPrimPath); }
104 
105  /// Shorthand for GetPrim()->GetPath().
106  SdfPath GetPath() const {
107  if (!_proxyPrimPath.IsEmpty()) {
108  return _proxyPrimPath;
109  }
110  else if (Usd_PrimDataConstPtr p = get_pointer(_primData)) {
111  return p->GetPath();
112  }
113  return SdfPath::EmptyPath();
114  }
115 
116  //@}
117 
118  /// \name PrimDefinition access.
119  //@{
120 
121  /// Return the prim definition associated with this schema instance if one
122  /// exists, otherwise return null. This does not use the held prim's type.
123  /// To get the held prim instance's definition, use
124  /// UsdPrim::GetPrimDefinition(). \sa UsdPrim::GetPrimDefinition()
125  USD_API
127 
128  //@}
129 
130  static const TfTokenVector &
131  GetSchemaAttributeNames(bool includeInherited=true)
132  {
133  /* This only exists for consistency */
134  static TfTokenVector names;
135  return names;
136  }
137 
138  /// \anchor UsdSchemaBase_bool
139  /// Return true if this schema object is compatible with its held prim,
140  /// false otherwise. For untyped schemas return true if the held prim is
141  /// not expired, otherwise return false. For typed schemas return true if
142  /// the held prim is not expired and its type is the schema's type or a
143  /// subtype of the schema's type. Otherwise return false. This method
144  /// invokes polymorphic behavior.
145  ///
146  /// \sa UsdSchemaBase::_IsCompatible()
147  USD_API
148  explicit operator bool() const {
149  return _primData && _IsCompatible();
150  }
151 
152 protected:
153  /// Returns the kind of schema this class is.
154  ///
155  /// \sa UsdSchemaBase::schemaKind
156  virtual UsdSchemaKind _GetSchemaKind() const {
157  return schemaKind;
158  }
159 
160  /// \deprecated
161  /// This has been replace with _GetSchemaKind but is around for now for
162  /// backwards compatibility while schemas are being updated.
163  ///
164  /// Leaving this around for one more release as schema classes up until now
165  /// have been generated with an override of this function. We don't want
166  /// those classes to immediately not compile before a chance is given to
167  /// regenerate the schemas.
168  virtual UsdSchemaKind _GetSchemaType() const {
169  return schemaKind;
170  }
171 
172  // Helper for subclasses to get the TfType for this schema object's dynamic
173  // C++ type.
174  const TfType &_GetType() const {
175  return _GetTfType();
176  }
177 
178  USD_API
179  UsdAttribute _CreateAttr(TfToken const &attrName,
180  SdfValueTypeName const & typeName,
181  bool custom, SdfVariability variability,
182  VtValue const &defaultValue,
183  bool writeSparsely) const;
184 
185  /// Subclasses may override _IsCompatible to do specific compatibility
186  /// checking with the given prim, such as type compatibility or value
187  /// compatibility. This check is performed when clients invoke the
188  /// explicit bool operator.
189  USD_API
190  virtual bool _IsCompatible() const;
191 
192 private:
193  // needs to invoke _GetStaticTfType.
194  friend class UsdSchemaRegistry;
195  USD_API
196  static const TfType &_GetStaticTfType();
197 
198  // Subclasses should not override _GetTfType. It is implemented by the
199  // schema class code generator.
200  USD_API
201  virtual const TfType &_GetTfType() const;
202 
203  // The held prim and proxy prim path.
204  Usd_PrimDataHandle _primData;
205  SdfPath _proxyPrimPath;
206 };
207 
208 
210 
211 #endif //PXR_USD_USD_SCHEMA_BASE_H
virtual UsdSchemaKind _GetSchemaKind() const
Definition: schemaBase.h:156
Non-applied API schema.
#define USD_API
Definition: api.h:23
virtual USD_API bool _IsCompatible() const
static const TfTokenVector & GetSchemaAttributeNames(bool includeInherited=true)
Definition: schemaBase.h:131
Single Apply API schema.
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
OutGridT const XformOp bool bool
Y * get_pointer(TfWeakPtrFacade< X, Y > const &p)
Definition: weakPtrFacade.h:63
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
Definition: token.h:70
bool IsMultipleApplyAPISchema() const
Definition: schemaBase.h:78
Represents a concrete typed schema.
Represents a non-concrete typed schema.
const TfType & _GetType() const
Definition: schemaBase.h:174
Definition: prim.h:116
bool IsAppliedAPISchema() const
Definition: schemaBase.h:70
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
virtual USD_API ~UsdSchemaBase()
Destructor.
Definition: path.h:273
virtual UsdSchemaKind _GetSchemaType() const
Definition: schemaBase.h:168
UsdSchemaKind
Definition: common.h:112
SdfVariability
Definition: types.h:156
static const UsdSchemaKind schemaKind
Definition: schemaBase.h:44
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USD_API UsdAttribute _CreateAttr(TfToken const &attrName, SdfValueTypeName const &typeName, bool custom, SdfVariability variability, VtValue const &defaultValue, bool writeSparsely) const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool IsConcrete() const
Definition: schemaBase.h:50
SdfPath GetPath() const
Shorthand for GetPrim()->GetPath().
Definition: schemaBase.h:106
Definition: type.h:47
USD_API const UsdPrimDefinition * GetSchemaClassPrimDefinition() const
USD_API UsdSchemaBase(const UsdPrim &prim=UsdPrim())
Construct and store prim as the held prim.
UsdSchemaKind GetSchemaKind() const
Returns the kind of schema this class is.
Definition: schemaBase.h:83
UsdPrim GetPrim() const
Return this schema object's held prim.
Definition: schemaBase.h:103
bool IsAPISchema() const
Returns whether this is an API schema or not.
Definition: schemaBase.h:62
Definition: value.h:146
Multiple Apply API Schema.
bool IsTyped() const
Definition: schemaBase.h:56