Loading...
Searching...
No Matches
type_Impl.h
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_BASE_TF_TYPE_IMPL_H
25#define PXR_BASE_TF_TYPE_IMPL_H
26
28
29PXR_NAMESPACE_OPEN_SCOPE
30
31template <class DERIVED, class BASE>
32inline void*
33Tf_CastToParent(void* addr, bool derivedToBase);
34
35// Declare and register casts for all the C++ bases in the given TypeVector.
36template <typename TypeVector>
37struct Tf_AddBases;
38
39template <typename... Bases>
40struct Tf_AddBases<TfType::Bases<Bases...>>
41{
42 // Declare types in Bases as TfTypes and accumulate them into a runtime
43 // vector.
44 static std::vector<TfType>
45 Declare()
46 {
47 return std::vector<TfType> {
49 TfType::GetCanonicalTypeName( typeid(Bases) ))...
50 };
51 }
52
53 // Register casts to and from Derived and each base type in Bases.
54 template <typename Derived>
55 static void
56 RegisterCasts(TfType const* type)
57 {
58 (type->_AddCppCastFunc(
59 typeid(Bases), &Tf_CastToParent<Derived, Bases>), ...);
60 }
61};
62
63template <class T, class BaseTypes>
64TfType const &
66{
67 // Declare each of the base types.
68 std::vector<TfType> baseTfTypes = Tf_AddBases<BaseTypes>::Declare();
69 // Declare our type T.
70 const std::type_info &typeInfo = typeid(T);
71 const std::string typeName = TfType::GetCanonicalTypeName(typeInfo);
72 return TfType::Declare(typeName, baseTfTypes);
73}
74
75template <typename T, typename BaseTypes>
76TfType const&
78{
79 TfAutoMallocTag2 tag2("Tf", "TfType::Define");
80
81 // Declare each of the base types.
82 std::vector<TfType> baseTfTypes = Tf_AddBases<BaseTypes>::Declare();
83
84 // Declare our type T.
85 const std::type_info &typeInfo = typeid(T);
86 const std::string typeName = TfType::GetCanonicalTypeName(typeInfo);
87 TfType const& newType = TfType::Declare(typeName, baseTfTypes);
88
89 // Record traits information about T.
90 const bool isPodType = std::is_pod<T>::value;
91 const bool isEnumType = std::is_enum<T>::value;
92 const size_t sizeofType = TfSizeofType<T>::value;
93
94 newType._DefineCppType(typeInfo, sizeofType, isPodType, isEnumType);
95 Tf_AddBases<BaseTypes>::template RegisterCasts<T>(&newType);
96
97 return newType;
98}
99
100template <typename T>
101TfType const&
103{
104 return Define<T, Bases<> >();
105}
106
107// Helper function to implement up/down casts between TfType types.
108// This was taken from the previous TfType implementation.
109template <class DERIVED, class BASE>
110inline void*
111Tf_CastToParent(void* addr, bool derivedToBase)
112{
113 if (derivedToBase) {
114 // Upcast -- can be done implicitly.
115 DERIVED* derived = reinterpret_cast<DERIVED*>(addr);
116 BASE* base = derived;
117 return base;
118 } else {
119 // Downcast -- use static_cast.
120 BASE* base = reinterpret_cast<BASE*>(addr);
121 DERIVED* derived = static_cast<DERIVED*>(base);
122 return derived;
123 }
124}
125
126PXR_NAMESPACE_CLOSE_SCOPE
127
128#endif // PXR_BASE_TF_TYPE_IMPL_H
Scoped (i.e.
Definition: mallocTag.h:255
TfType represents a dynamic runtime type.
Definition: type.h:65
static TfType const & Define()
Define a TfType with the given C++ type T and C++ base types B.
static TfType const & Declare()
Declares a TfType with the given C++ type T and C++ base types Bases.
static TF_API std::string GetCanonicalTypeName(const std::type_info &)
Return the canonical typeName used for a given std::type_info.
Metafunction returning sizeof(T) for a type T (or 0 if T is a void type).
Definition: type.h:737