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

View File

@ -15,6 +15,8 @@ static_assert( __cplusplus > 201700, "C++17 Required" );
#include <Alepha/console.h>
#include <Alepha/types.h>
#include <Alepha/Utility/evaluation.h>
#include <Alepha/Utility/StaticValue.h>
namespace Alepha::Hydrogen::Testing
{
@ -34,6 +36,8 @@ namespace Alepha::Hydrogen::Testing
}
using namespace std::literals::string_literals;
using namespace Utility::exports::evaluation;
using namespace Utility::exports::static_value;
struct TestName
{
@ -65,12 +69,8 @@ namespace Alepha::Hydrogen::Testing
}
}
inline auto &
registry()
{
static std::vector< std::tuple< std::string, bool, std::function< void() > > > registry;
return registry;
}
StaticValue< std::vector< std::tuple< std::string, bool, std::function< void() > > > > registry;
auto initRegistry= enroll <=registry;
// It is okay to discard this, if making tests in an enroll block.
inline auto

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;
}
}