1 #ifndef PROFILER_SRC_MOLPRO_PROFILER_TREE_SINGLE_H
2 #define PROFILER_SRC_MOLPRO_PROFILER_TREE_SINGLE_H
17 template <
class Object>
20 using key_t = std::tuple<std::string, std::weak_ptr<Object>, Object*>;
26 template <
typename... T>
27 static std::shared_ptr<Object>
single(
const std::string& key, T&&... constructor_args) {
28 std::shared_ptr<Object> result =
nullptr;
32 result = std::get<1>(*it).lock();
34 result = std::make_shared<Object>(std::forward<T>(constructor_args)...);
41 static std::shared_ptr<Object>
single() {
44 result->set_max_depth(0);
47 assert(!
m_register.empty() &&
"First must make a call to single(key, ...) to create an object");
48 std::shared_ptr<Object> result = std::get<1>(
m_register.back()).lock();
49 assert(result &&
"The last registered object was deallocated");
54 static void erase(Object* obj) {
62 static void erase(
const std::string& key) {
static std::shared_ptr< Profiler > single()
Access the last registered Profiler.
Definition: Profiler.cpp:31
std::shared_ptr< Profiler > s_saver
Definition: WeakSingleton.h:11
Implements the mechanism for the weak singleton pattern.
Definition: WeakSingleton.h:18
static void erase(Object *obj)
Remove object from the register. This should be called in the destructor of class that exposes this p...
Definition: WeakSingleton.h:54
static std::list< key_t > m_register
stores all objects created by a call to single
Definition: WeakSingleton.h:72
static void erase(const std::string &key)
Remove object registered under the name key.
Definition: WeakSingleton.h:62
std::tuple< std::string, std::weak_ptr< Object >, Object * > key_t
Definition: WeakSingleton.h:20
static std::shared_ptr< Object > single(const std::string &key, T &&... constructor_args)
Creates an instance of Object or returns an already registered instance.
Definition: WeakSingleton.h:27
static void clear()
Remove all registered objects.
Definition: WeakSingleton.h:70
static std::shared_ptr< Object > single()
Access the last registered object.
Definition: WeakSingleton.h:41