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