// Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors. // All rights reserved. // Code licensed under the BSD License. // http://www.anki3d.org/LICENSE #pragma once #include namespace anki { /// @addtogroup util_other /// @{ /// A wrapper template to compensate for the fact that some classes get initialized in the constructor but that's not /// always desirable. One solution is to use a pointer and dynamic allocation but that creates an indirection and it /// might cause bad cache locality. With this wrapper is scenario is avoided. template class ClassWrapper { public: ClassWrapper() { } /// Call the constructor of the TClass. template void init(TArgs&&... args) { ::new(&m_data[0]) TClass(std::forward(args)...); } /// Call the destructor of the TClass. void destroy() { reinterpret_cast(&m_data[0])->~TClass(); } /// Access the instance. TClass* operator->() { return reinterpret_cast(&m_data[0]); } /// Access the instance. const TClass* operator->() const { return reinterpret_cast(&m_data[0]); } /// Access the instance. TClass& operator*() { return *reinterpret_cast(&m_data[0]); } /// Access the instance. const TClass& operator*() const { return *reinterpret_cast(&m_data[0]); } /// Access the instance. TClass* get() { return reinterpret_cast(&m_data[0]); } /// Access the instance. const TClass* get() const { return reinterpret_cast(&m_data[0]); } private: /// The data as a POD with correct size and alignment. alignas(alignof(TClass)) Array m_data; }; /// @} } // end namespace anki