Add Utility/staticpointer
This commit is contained in:
parent
8e96588829
commit
3dbd04a3eb
57
Utility/Inc/utility_staticpointer.hpp
Normal file
57
Utility/Inc/utility_staticpointer.hpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* utility_staticpointer.hpp
|
||||||
|
*
|
||||||
|
* Created on: Jun 8, 2021
|
||||||
|
* Author: erki
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SKULLC_UTILITY_STATICPOINTER_HPP_
|
||||||
|
#define SKULLC_UTILITY_STATICPOINTER_HPP_
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace Utility
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct StaticPointer
|
||||||
|
{
|
||||||
|
using value_type = T;
|
||||||
|
|
||||||
|
alignas(value_type) unsigned char storage[sizeof(value_type)];
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
value_type& setup(Args&&... args)
|
||||||
|
{
|
||||||
|
initialized_ = true;
|
||||||
|
return *(new (storage) value_type(std::forward<Args>(args)...));
|
||||||
|
}
|
||||||
|
|
||||||
|
value_type& operator*() const
|
||||||
|
{
|
||||||
|
return reinterpret_cast<T*>(storage);
|
||||||
|
}
|
||||||
|
|
||||||
|
value_type* operator->() const noexcept
|
||||||
|
{
|
||||||
|
return reinterpret_cast<T*>(storage);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr explicit operator bool() const
|
||||||
|
{
|
||||||
|
return isInitialized();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool isInitialized() const
|
||||||
|
{
|
||||||
|
return initialized_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool initialized_ = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
}// namespace Utility
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* SKULLC_UTILITY_STATICPOINTER_HPP_ */
|
||||||
Loading…
x
Reference in New Issue
Block a user