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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_TYPE_IMPL_H
8 #define PXR_BASE_TF_TYPE_IMPL_H
9 
11 
12 template <class DERIVED, class BASE>
13 inline void*
14 Tf_CastToParent(void* addr, bool derivedToBase);
15 
16 template <typename TypeVector>
18 
19 template <>
20 struct Tf_BaseTypeInfos<TfType::Bases<>>
21 {
22  static const size_t NumBases = 0;
23  std::type_info const **baseTypeInfos = nullptr;
24 };
25 
26 template <typename... Bases>
27 struct Tf_BaseTypeInfos<TfType::Bases<Bases...>>
28 {
29  static const size_t NumBases = sizeof...(Bases);
30  std::type_info const *baseTypeInfos[NumBases] = { &typeid(Bases)... };
31 };
32 
33 template <class Derived, typename TypeVector>
35 
36 template <class Derived>
37 struct Tf_TypeCastFunctions<Derived, TfType::Bases<>>
38 {
39  using CastFunction = void *(*)(void *, bool);
40  CastFunction *castFunctions = nullptr;
41 };
42 
43 template <class Derived, typename... Bases>
44 struct Tf_TypeCastFunctions<Derived, TfType::Bases<Bases...>>
45 {
46  using CastFunction = void *(*)(void *, bool);
47  CastFunction castFunctions[sizeof...(Bases)] = {
48  &Tf_CastToParent<Derived, Bases>... };
49 };
50 
51 template <class T, class BaseTypes>
52 TfType const &
54 {
56  return _DeclareImpl(typeid(T), btis.baseTypeInfos, btis.NumBases);
57 }
58 
59 template <typename T, typename BaseTypes>
60 TfType const &
62 {
65  return _DefineImpl(
66  typeid(T), btis.baseTypeInfos, tcfs.castFunctions, btis.NumBases,
67  TfSizeofType<T>::value, std::is_pod_v<T>, std::is_enum_v<T>);
68 }
69 
70 template <typename T>
71 TfType const&
73 {
74  return Define<T, Bases<>>();
75 }
76 
77 // Helper function to implement up/down casts between TfType types.
78 // This was taken from the previous TfType implementation.
79 template <class DERIVED, class BASE>
80 inline void*
81 Tf_CastToParent(void* addr, bool derivedToBase)
82 {
83  if (derivedToBase) {
84  // Upcast -- can be done implicitly.
85  DERIVED* derived = reinterpret_cast<DERIVED*>(addr);
86  BASE* base = derived;
87  return base;
88  } else {
89  // Downcast -- use static_cast.
90  BASE* base = reinterpret_cast<BASE*>(addr);
91  DERIVED* derived = static_cast<DERIVED*>(base);
92  return derived;
93  }
94 }
95 
97 
98 #endif // PXR_BASE_TF_TYPE_IMPL_H
OutGridT const XformOp bool bool
static TfType const & Declare()
Definition: type_Impl.h:53
static TfType const & Define()
Definition: type_Impl.h:61
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:1425
PXR_NAMESPACE_OPEN_SCOPE void * Tf_CastToParent(void *addr, bool derivedToBase)
Definition: type_Impl.h:81
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47