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 
66  constexpr bool isPodType =
67  std::is_trivial_v<T> && std::is_standard_layout_v<T>;
68 
69  return _DefineImpl(
70  typeid(T), btis.baseTypeInfos, tcfs.castFunctions, btis.NumBases,
71  TfSizeofType<T>::value, isPodType, std::is_enum_v<T>);
72 }
73 
74 template <typename T>
75 TfType const&
77 {
78  return Define<T, Bases<>>();
79 }
80 
81 // Helper function to implement up/down casts between TfType types.
82 // This was taken from the previous TfType implementation.
83 template <class DERIVED, class BASE>
84 inline void*
85 Tf_CastToParent(void* addr, bool derivedToBase)
86 {
87  if (derivedToBase) {
88  // Upcast -- can be done implicitly.
89  DERIVED* derived = reinterpret_cast<DERIVED*>(addr);
90  BASE* base = derived;
91  return base;
92  } else {
93  // Downcast -- use static_cast.
94  BASE* base = reinterpret_cast<BASE*>(addr);
95  DERIVED* derived = static_cast<DERIVED*>(base);
96  return derived;
97  }
98 }
99 
101 
102 #endif // PXR_BASE_TF_TYPE_IMPL_H
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
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_OPEN_SCOPE void * Tf_CastToParent(void *addr, bool derivedToBase)
Definition: type_Impl.h:85
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47