Loading...
Searching...
No Matches
type_Impl.h
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
10PXR_NAMESPACE_OPEN_SCOPE
11
12template <class DERIVED, class BASE>
13inline void*
14Tf_CastToParent(void* addr, bool derivedToBase);
15
16template <typename TypeVector>
17struct Tf_BaseTypeInfos;
18
19template <>
20struct Tf_BaseTypeInfos<TfType::Bases<>>
21{
22 static const size_t NumBases = 0;
23 std::type_info const **baseTypeInfos = nullptr;
24};
25
26template <typename... Bases>
27struct 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
33template <class Derived, typename TypeVector>
34struct Tf_TypeCastFunctions;
35
36template <class Derived>
37struct Tf_TypeCastFunctions<Derived, TfType::Bases<>>
38{
39 using CastFunction = void *(*)(void *, bool);
40 CastFunction *castFunctions = nullptr;
41};
42
43template <class Derived, typename... Bases>
44struct 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
51template <class T, class BaseTypes>
52TfType const &
54{
55 Tf_BaseTypeInfos<BaseTypes> btis;
56 return _DeclareImpl(typeid(T), btis.baseTypeInfos, btis.NumBases);
57}
58
59template <typename T, typename BaseTypes>
60TfType const &
62{
63 Tf_BaseTypeInfos<BaseTypes> btis;
64 Tf_TypeCastFunctions<T, BaseTypes> tcfs;
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
74template <typename T>
75TfType 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.
83template <class DERIVED, class BASE>
84inline void*
85Tf_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
100PXR_NAMESPACE_CLOSE_SCOPE
101
102#endif // PXR_BASE_TF_TYPE_IMPL_H
TfType represents a dynamic runtime type.
Definition type.h:48
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.
Metafunction returning sizeof(T) for a type T (or 0 if T is a void type).
Definition type.h:737