7#ifndef PXR_EXEC_VDF_FIXED_SIZE_POLYMORPHIC_HOLDER_H
8#define PXR_EXEC_VDF_FIXED_SIZE_POLYMORPHIC_HOLDER_H
12#include "pxr/base/arch/defines.h"
19PXR_NAMESPACE_OPEN_SCOPE
35template <
class Base,
size_t BufferSize>
38 static_assert(std::is_polymorphic<Base>::value,
"");
41 using _Storage =
typename std::aligned_storage_t<BufferSize,
sizeof(
void *)>;
58 template <
class Derived,
class ... Args>
59 void New(Args && ... args) {
61 std::is_base_of<Base, Derived>::value,
62 "Derived is not a derived class of Base.");
64 sizeof(Derived) <=
sizeof(_Storage),
65 "The size of the derived type is larger than the availble storage.");
67 alignof(_Storage) %
alignof(Derived) == 0,
68 "The derived type has incompatible alignment.");
69 new (_GetStorage()) Derived(std::forward<Args>(args)...);
73 static_cast<Base *
>(
static_cast<Derived *
>(_GetStorage()));
82#if defined(ARCH_COMPILER_GCC) && ARCH_COMPILER_GCC_MAJOR < 11
84 ARCH_PRAGMA_MAYBE_UNINITIALIZED
87#if defined(ARCH_COMPILER_GCC) && ARCH_COMPILER_GCC_MAJOR < 11
93 inline Base
const *
Get()
const {
94 return static_cast<Base
const *
>(_GetStorage());
99 return static_cast<Base *
>(_GetStorage());
105 inline void *_GetStorage() {
110 inline void const *_GetStorage()
const {
122PXR_NAMESPACE_CLOSE_SCOPE
Used to implement small object optimizations for the type-erasure (Any) pattern.
Base * Get()
Returns a Base pointer to the held instance.
void New(Args &&... args)
Creates an instance.
void Destroy()
Destroys a held instance.
Base const * Get() const
Returns a Base pointer to the held instance.
Stripped down version of diagnostic.h that doesn't define std::string.
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
Pragmas for controlling compiler-specific behaviors.