init: add runtime initialisation cookie
This commit is contained in:
parent
aa4a79aae2
commit
0927816e42
@ -407,6 +407,8 @@ list (
|
|||||||
hash/wang.hpp
|
hash/wang.hpp
|
||||||
hash/xxhash.cpp
|
hash/xxhash.cpp
|
||||||
hash/xxhash.hpp
|
hash/xxhash.hpp
|
||||||
|
init.cpp
|
||||||
|
init.hpp
|
||||||
introspection.cpp
|
introspection.cpp
|
||||||
introspection.hpp
|
introspection.hpp
|
||||||
io.cpp
|
io.cpp
|
||||||
|
65
init.cpp
Normal file
65
init.cpp
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
_ _
|
||||||
|
| | | |
|
||||||
|
| | ___ | |__ ___
|
||||||
|
| |/ _ \| '_ \ / _ \
|
||||||
|
| | (_) | |_) | (_) |
|
||||||
|
|_|\___/|_.__/ \___/
|
||||||
|
Copyright:
|
||||||
|
Danny Robson, 2020
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#include "init.hpp"
|
||||||
|
|
||||||
|
#include "log.hpp"
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
using cruft::init::cookie;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
namespace {
|
||||||
|
// Stores the various RAII handlers for any registered functionality that
|
||||||
|
// requires it.
|
||||||
|
struct state {
|
||||||
|
state ()
|
||||||
|
: log (cruft::log::setup_level_reflection ())
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
cruft::parse::enumeration::cookie log;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<state> s_state;
|
||||||
|
std::atomic<int> s_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Increase the reference count and initialising the requisite functionality if
|
||||||
|
// this is the first instance of the class.
|
||||||
|
static void up (void)
|
||||||
|
{
|
||||||
|
if (int old = s_count++; !old)
|
||||||
|
s_state = std::make_unique<state> ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Decrease the reference count and release any resources if this was the last
|
||||||
|
// instance of the class.
|
||||||
|
static void down (void)
|
||||||
|
{
|
||||||
|
if (int old = --s_count; !old)
|
||||||
|
s_state.reset ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
cookie::cookie () { up (); }
|
||||||
|
cookie::cookie (cookie &&) noexcept { up (); }
|
||||||
|
cookie::cookie (cookie const&) { up (); }
|
||||||
|
cookie& cookie::operator= (cookie &&) noexcept { up (); return *this; }
|
||||||
|
cookie& cookie::operator= (cookie const&) { up (); return *this; }
|
||||||
|
cookie::~cookie () { down (); }
|
33
init.hpp
Normal file
33
init.hpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
_ _
|
||||||
|
| | | |
|
||||||
|
| | ___ | |__ ___
|
||||||
|
| |/ _ \| '_ \ / _ \
|
||||||
|
| | (_) | |_) | (_) |
|
||||||
|
|_|\___/|_.__/ \___/
|
||||||
|
Copyright:
|
||||||
|
Danny Robson, 2020
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
namespace cruft::init {
|
||||||
|
/// An object that ensures functionality that requires runtime
|
||||||
|
/// registration has been setup, and clears these resources at destruction
|
||||||
|
/// time.
|
||||||
|
///
|
||||||
|
/// Do not operate on instances of this class at the same time across
|
||||||
|
/// multiple threads. (It is safe to do so if access is has been locked)
|
||||||
|
///
|
||||||
|
/// In particular, this class ensures that:
|
||||||
|
/// * log::level_t has been registered for reflection
|
||||||
|
struct cookie {
|
||||||
|
cookie ();
|
||||||
|
cookie (cookie const&);
|
||||||
|
cookie& operator= (cookie const&);
|
||||||
|
cookie (cookie &&) noexcept;
|
||||||
|
cookie& operator= (cookie &&) noexcept;
|
||||||
|
~cookie ();
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user