HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
specType.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_SDF_SPEC_TYPE_H
8 #define PXR_USD_SDF_SPEC_TYPE_H
9 
10 /// \file sdf/specType.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/sdf/types.h"
14 
15 #include <typeinfo>
16 #include <vector>
17 
19 
20 class SdfSpec;
21 class TfType;
22 
23 /// \class SdfSpecTypeRegistration
24 ///
25 /// Provides functions to register spec types with the runtime typing system
26 /// used to cast between C++ spec types. Implementations of C++ spec types
27 /// should use as follows:
28 ///
29 /// For a concrete spec type that corresponds to a specific SdfSpecType:
30 /// TF_REGISTRY_FUNCTION(SdfSpecTypeRegistration) {
31 /// SdfSpecTypeRegistration::RegisterSpecType<MyPrimSpec>();
32 /// }
33 ///
34 /// For an abstract spec type that has no corresponding SdfSpecType:
35 /// TF_REGISTRY_FUNCTION(SdfSpecTypeRegistration) {
36 /// SdfSpecTypeRegistration::RegisterAbstractSpecType<MyPropertySpec>();
37 /// }
38 ///
40 {
41 public:
42  /// Registers the C++ type T as a concrete spec class.
43  template <class SchemaType, class SpecType>
44  static void RegisterSpecType(SdfSpecType specTypeEnum) {
45  _RegisterSpecType(
46  typeid(SpecType), specTypeEnum, typeid(SchemaType));
47  }
48 
49  /// Registers the C++ type T as an abstract spec class.
50  template <class SchemaType, class SpecType>
51  static void RegisterAbstractSpecType() {
52  _RegisterSpecType(
53  typeid(SpecType), SdfSpecTypeUnknown, typeid(SchemaType));
54  }
55 
56 private:
57  SDF_API
58  static void _RegisterSpecType(
59  const std::type_info& specCPPType,
60  SdfSpecType specEnumType,
61  const std::type_info& schemaType);
62 };
63 
64 // This class holds type information for specs. It associates a
65 // spec type with the corresponding TfType.
66 class Sdf_SpecType {
67 public:
68  // If \p spec can be represented by the C++ spec class \p to, returns
69  // the TfType for \p to. This includes verifying that \p spec's schema
70  // matches the schema associated with \p to.
71  static TfType Cast(const SdfSpec& spec, const std::type_info& to);
72 
73  // Returns whether the \p spec can be represented by the C++ spec
74  // class \p to. This includes verifying that \p spec's schema matches
75  // the schema associated with \p to.
76  static bool CanCast(const SdfSpec& spec, const std::type_info& to);
77 
78  // Returns whether a spec with spec type \p from can be represented by
79  // the C++ spec class \p to, regardless of schema.
80  static bool CanCast(SdfSpecType from, const std::type_info& to);
81 };
82 
84 
85 #endif // PXR_USD_SDF_SPEC_TYPE_H
Definition: spec.h:32
static void RegisterSpecType(SdfSpecType specTypeEnum)
Registers the C++ type T as a concrete spec class.
Definition: specType.h:44
#define SDF_API
Definition: api.h:23
static void RegisterAbstractSpecType()
Registers the C++ type T as an abstract spec class.
Definition: specType.h:51
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
SdfSpecType
Definition: types.h:68
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47
static bool CanCast(const SdfSpec &spec, const std::type_info &to)
static TfType Cast(const SdfSpec &spec, const std::type_info &to)