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 UsdSchemaType 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 
100  return _GetSchemaType();
101  }
102 
103  /// Construct and store \p prim as the held prim.
104  USD_API
105  explicit UsdSchemaBase(const UsdPrim& prim = UsdPrim());
106 
107  /// Construct and store for the same prim held by \p otherSchema
108  USD_API
109  explicit UsdSchemaBase(const UsdSchemaBase& otherSchema);
110 
111  /// Destructor.
112  USD_API
113  virtual ~UsdSchemaBase();
114 
115  /// \name Held prim access.
116  //@{
117 
118  /// Return this schema object's held prim.
119  UsdPrim GetPrim() const { return UsdPrim(_primData, _proxyPrimPath); }
120 
121  /// Shorthand for GetPrim()->GetPath().
122  SdfPath GetPath() const {
123  if (!_proxyPrimPath.IsEmpty()) {
124  return _proxyPrimPath;
125  }
126  else if (Usd_PrimDataConstPtr p = get_pointer(_primData)) {
127  return p->GetPath();
128  }
129  return SdfPath::EmptyPath();
130  }
131 
132  //@}
133 
134  /// \name PrimDefinition access.
135  //@{
136 
137  /// Return the prim definition associated with this schema instance if one
138  /// exists, otherwise return null. This does not use the held prim's type.
139  /// To get the held prim instance's definition, use
140  /// UsdPrim::GetPrimDefinition(). \sa UsdPrim::GetPrimDefinition()
141  USD_API
143 
144  //@}
145 
146  static const TfTokenVector &
147  GetSchemaAttributeNames(bool includeInherited=true)
148  {
149  /* This only exists for consistency */
150  static TfTokenVector names;
151  return names;
152  }
153 
154  /// Return true if this schema object is compatible with its held prim,
155  /// false otherwise. For untyped schemas return true if the held prim is
156  /// not expired, otherwise return false. For typed schemas return true if
157  /// the held prim is not expired and its type is the schema's type or a
158  /// subtype of the schema's type. Otherwise return false. This method
159  /// invokes polymorphic behavior.
160  ///
161  /// \sa UsdSchemaBase::_IsCompatible()
162  USD_API
163  explicit operator bool() const {
164  return _primData && _IsCompatible();
165  }
166 
167 protected:
168  /// Returns the type of schema this class is.
169  ///
170  /// \sa UsdSchemaBase::schemaType
171  virtual UsdSchemaType _GetSchemaType() const {
172  return schemaType;
173  }
174  // Helper for subclasses to get the TfType for this schema object's dynamic
175  // C++ type.
176  const TfType &_GetType() const {
177  return _GetTfType();
178  }
179 
180  USD_API
181  UsdAttribute _CreateAttr(TfToken const &attrName,
182  SdfValueTypeName const & typeName,
183  bool custom, SdfVariability variability,
184  VtValue const &defaultValue,
185  bool writeSparsely) const;
186 
187  /// Subclasses may override _IsCompatible to do specific compatibility
188  /// checking with the given prim, such as type compatibility or value
189  /// compatibility. This check is performed when clients invoke the
190  /// explicit bool operator.
191  USD_API
192  virtual bool _IsCompatible() const;
193 
194 private:
195  // Subclasses should not override _GetTfType. It is implemented by the
196  // schema class code generator.
197  USD_API
198  virtual const TfType &_GetTfType() const;
199 
200  // The held prim and proxy prim path.
201  Usd_PrimDataHandle _primData;
202  SdfPath _proxyPrimPath;
203 };
204 
205 
207 
208 #endif //PXR_USD_USD_SCHEMA_BASE_H
#define USD_API
Definition: api.h:40
static const UsdSchemaType schemaType
Definition: schemaBase.h:61
virtual USD_API bool _IsCompatible() const
static const TfTokenVector & GetSchemaAttributeNames(bool includeInherited=true)
Definition: schemaBase.h:147
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:413
Represents a concrete typed schema.
Y * get_pointer(TfWeakPtrFacade< X, Y > const &p)
Definition: weakPtrFacade.h:86
virtual UsdSchemaType _GetSchemaType() const
Definition: schemaBase.h:171
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
Definition: token.h:87
bool IsMultipleApplyAPISchema() const
Definition: schemaBase.h:95
Multiple Apply API Schema.
Non-applied API schema.
const TfType & _GetType() const
Definition: schemaBase.h:176
GLuint const GLuint * names
Definition: glew.h:2690
UsdSchemaType
Definition: common.h:127
Definition: prim.h:132
bool IsAppliedAPISchema() const
Definition: schemaBase.h:87
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:446
Single Apply API schema.
Represents a non-concrete typed schema.
virtual USD_API ~UsdSchemaBase()
Destructor.
Definition: path.h:288
SdfVariability
Definition: types.h:179
GLfloat GLfloat p
Definition: glew.h:16321
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
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:122
Definition: type.h:64
UsdSchemaType GetSchemaType() const
Definition: schemaBase.h:99
USD_API const UsdPrimDefinition * GetSchemaClassPrimDefinition() const
USD_API UsdSchemaBase(const UsdPrim &prim=UsdPrim())
Construct and store prim as the held prim.
UsdPrim GetPrim() const
Return this schema object's held prim.
Definition: schemaBase.h:119
bool IsAPISchema() const
Returns whether this is an API schema or not.
Definition: schemaBase.h:79
Definition: value.h:174
bool IsTyped() const
Definition: schemaBase.h:73