All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 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
70template <typename T>
71TfType 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.
79template <class DERIVED, class BASE>
80inline void*
81Tf_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
96PXR_NAMESPACE_CLOSE_SCOPE
97
98#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