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_SDF_SPEC_TYPE_H
25 #define PXR_USD_SDF_SPEC_TYPE_H
26 
27 /// \file sdf/specType.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/sdf/types.h"
31 
32 #include <typeinfo>
33 #include <vector>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 class SdfSpec;
38 class TfType;
39 
40 /// \class SdfSpecTypeRegistration
41 ///
42 /// Provides functions to register spec types with the runtime typing system
43 /// used to cast between C++ spec types. Implementations of C++ spec types
44 /// should use as follows:
45 ///
46 /// For a concrete spec type that corresponds to a specific SdfSpecType:
47 /// TF_REGISTRY_FUNCTION(SdfSpecTypeRegistration) {
48 ///    SdfSpecTypeRegistration::RegisterSpecType<MyPrimSpec>();
49 /// }
50 ///
51 /// For an abstract spec type that has no corresponding SdfSpecType:
52 /// TF_REGISTRY_FUNCTION(SdfSpecTypeRegistration) {
53 ///    SdfSpecTypeRegistration::RegisterAbstractSpecType<MyPropertySpec>();
54 /// }
55 ///
56 class SdfSpecTypeRegistration
57 {
58 public:
59     /// Registers the C++ type T as a concrete spec class.
60     template <class SchemaType, class SpecType>
RegisterSpecType(SdfSpecType specTypeEnum)61     static void RegisterSpecType(SdfSpecType specTypeEnum)
62     {
63         _RegisterSpecType(typeid(SpecType), specTypeEnum, typeid(SchemaType));
64     }
65 
66     /// Registers the C++ type T as an abstract spec class.
67     template <class SchemaType, class SpecType>
RegisterAbstractSpecType()68     static void RegisterAbstractSpecType()
69     {
70         _RegisterAbstractSpecType(typeid(SpecType), typeid(SchemaType));
71     }
72 
73 private:
74     static void _RegisterSpecType(
75         const std::type_info& specCPPType, SdfSpecType specEnumType,
76         const std::type_info& schemaType);
77     static void _RegisterAbstractSpecType(
78         const std::type_info& specCPPType,
79         const std::type_info& schemaType);
80 };
81 
82 // This class holds type information for specs.  It associates a
83 // spec type with the corresponding TfType.
84 class Sdf_SpecType {
85 public:
86     // If \p spec can be represented by the C++ spec class \p to, returns
87     // the TfType for \p to. This includes verifying that \p spec's schema
88     // matches the schema associated with \p to.
89     static TfType Cast(const SdfSpec& spec, const std::type_info& to);
90 
91     // Returns whether the \p spec can be represented by the C++ spec
92     // class \p to. This includes verifying that \p spec's schema matches
93     // the schema associated with \p to.
94     static bool CanCast(const SdfSpec& spec, const std::type_info& to);
95 
96     // Returns whether a spec with spec type \p from can be represented by
97     // the C++ spec class \p to, regardless of schema.
98     static bool CanCast(SdfSpecType from, const std::type_info& to);
99 };
100 
101 PXR_NAMESPACE_CLOSE_SCOPE
102 
103 #endif // PXR_USD_SDF_SPEC_TYPE_H
104