1
0
forked from Alepha/Alepha

StaticValue helper.

Incorporated into `Testing`, for now.  Semi-threadsafe statics,
initialize them before going multithreaded.
This commit is contained in:
2021-10-26 05:10:40 -04:00
parent 7b7e8a1b67
commit 439fbb1433
2 changed files with 53 additions and 6 deletions

47
Utility/StaticValue.h Normal file
View File

@ -0,0 +1,47 @@
static_assert( __cplusplus > 201700, "C++17 Required" );
#pragma once
namespace Alepha::Hydrogen::Utility
{
inline namespace exports { inline namespace static_value {} }
namespace detail::static_value
{
inline namespace exports {}
template< typename T >
struct default_init
{
T *operator()() const { return new T{}; }
};
namespace exports
{
template< typename T, typename Init= default_init< T > >
struct StaticValue;
}
template< typename T, typename Init >
struct exports::StaticValue
{
private:
T *storage= nullptr;
public:
decltype( auto )
get()
{
if( not storage ) storage= Init{}();
return *storage;
}
decltype( auto ) operator ()() { return get(); }
};
}
namespace exports::static_value
{
using namespace detail::static_value::exports;
}
}