HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
type_Impl.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_BASE_TF_TYPE_IMPL_H
25 #define PXR_BASE_TF_TYPE_IMPL_H
26 
27 #include "pxr/base/tf/mallocTag.h"
28 
30 
31 template <class DERIVED, class BASE>
32 inline void*
33 Tf_CastToParent(void* addr, bool derivedToBase);
34 
35 // Declare and register casts for all the C++ bases in the given TypeVector.
36 template <typename TypeVector>
37 struct Tf_AddBases;
38 
39 template <typename... Bases>
40 struct 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>
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
57  {
58  (type->_AddCppCastFunc(
59  typeid(Bases), &Tf_CastToParent<Derived, Bases>), ...);
60  }
61 };
62 
63 template <class T, class BaseTypes>
64 TfType 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 
75 template <typename T, typename BaseTypes>
76 TfType 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 
100 template <typename T>
101 TfType 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.
109 template <class DERIVED, class BASE>
110 inline void*
111 Tf_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 
127 
128 #endif // PXR_BASE_TF_TYPE_IMPL_H
static void RegisterCasts(TfType const *type)
Definition: type_Impl.h:56
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static TF_API std::string GetCanonicalTypeName(const std::type_info &)
static TfType const & Declare()
Definition: type_Impl.h:65
static TfType const & Define()
Definition: type_Impl.h:77
Metafunction returning sizeof(T) for a type T (or 0 if T is a void type).
Definition: type.h:737
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
PXR_NAMESPACE_OPEN_SCOPE void * Tf_CastToParent(void *addr, bool derivedToBase)
Definition: type_Impl.h:111
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: type.h:64
type
Definition: core.h:1059
static std::vector< TfType > Declare()
Definition: type_Impl.h:45