ALib C++ Library
by
Library Version:
2412 R0
Documentation generated by
Loading...
Searching...
No Matches
home
dev
A-Worx
ALib
src
alib
singletons
singleton.hpp
Go to the documentation of this file.
1
//==================================================================================================
2
/// \file
3
/// This header file is part of module \alib_singletons of the \aliblong.
4
///
5
/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6
/// Published under \ref mainpage_license "Boost Software License".
7
//==================================================================================================
8
#ifndef HPP_ALIB_SINGLETONS_SINGLETON
9
#define HPP_ALIB_SINGLETONS_SINGLETON 1
10
#pragma once
11
#if !defined(DOXYGEN)
12
# include "
alib/alib.hpp
"
13
#endif
14
#if ALIB_STRINGS
15
# include "
alib/strings/fwds.hpp
"
16
#endif
17
18
namespace
alib
{
namespace
singletons {
19
20
// #################################################################################################
21
// Unordered map utility for storing type_info objects
22
// #################################################################################################
23
24
//! @cond NO_DOX
25
#if ALIB_FEAT_SINGLETON_MAPPED
26
extern
ALIB_API
void
* getSingleton (
const
std::type_info& type );
27
extern
ALIB_API
void
storeSingleton (
const
std::type_info& type,
void
* theSingleton );
28
extern
ALIB_API
void
removeSingleton(
const
std::type_info& type );
29
# if ALIB_THREADS
30
extern
ALIB_API
void
unlock();
31
# else
32
inline
void
unlock() {};
33
# endif
34
#endif
35
//! @endcond
36
37
//==================================================================================================
38
/// This class implements the "singleton pattern" for C++ using a common templated approach.
39
/// In case of Windows OS and DLL usage, the class overcomes the problem of having
40
/// a global data segment per DLL in addition to the one associated with the process that is using
41
/// the DLL.
42
///
43
/// All details about implementation and usage of this class is provided in the module's
44
/// \ref alib_mod_singletons "Programmer's Manual".
45
///
46
/// @tparam TDerivedClass Template parameter that denotes the name of the class that implements
47
/// the singleton.
48
//==================================================================================================
49
template
<
typename
TDerivedClass>
50
class
Singleton
51
{
52
protected
:
53
/// A pointer to the one and only singleton.
54
static
TDerivedClass*
singleton
;
55
56
public
:
57
/// Creates (if not done, yet) and returns the singleton of type \p{TDerivedClass}.
58
/// @return The singleton instance.
59
static
TDerivedClass&
GetSingleton
()
60
{
61
#if !ALIB_FEAT_SINGLETON_MAPPED
62
63
if
(
singleton
==
nullptr
)
64
singleton
=
new
TDerivedClass();
65
66
return
*
singleton
;
67
68
#else
69
// already created and known
70
if
(
singleton
!=
nullptr
)
71
return
*
singleton
;
72
73
74
// try loading from static map
75
void
* storedSingleton= getSingleton(
typeid
(TDerivedClass) );
76
if
( storedSingleton !=
nullptr
) {
77
singleton
=
dynamic_cast<
TDerivedClass*
>
(
78
reinterpret_cast<
Singleton<TDerivedClass>
*
>
(storedSingleton) );
79
unlock();
80
return
*
singleton
;
81
}
82
83
// create and store in map
84
auto
* firstInstance=
new
TDerivedClass();
85
storeSingleton(
typeid
(TDerivedClass),
dynamic_cast<
Singleton<TDerivedClass>
*
>
( firstInstance ) );
86
87
// In debug mode, do not set this singleton right away. This "simulates"
88
// a windows DLL/Exec scope change
89
#if ALIB_DEBUG
90
return
*firstInstance;
91
#else
92
return
*(
singleton
= firstInstance);
93
#endif
94
#endif
95
}
96
97
/// Virtual destructor.
98
virtual
~Singleton
()
99
{
100
#if ALIB_FEAT_SINGLETON_MAPPED
101
removeSingleton(
typeid
(TDerivedClass) );
102
#endif
103
}
104
105
};
// class Singleton
106
107
// The static singleton instance initialization
108
template
<
typename
TDerivedClass>
109
TDerivedClass*
Singleton<TDerivedClass>::singleton
=
nullptr
;
110
111
//==================================================================================================
112
/// Deletes the singletons.
113
/// Upon exit of the process, programmers might want to explicitly free the hash table to avoid
114
/// the detection of memory leaks by metrics tools like \http{Valgrind,valgrind.org/}.
115
/// (Otherwise this can be omitted, as the memory is cleaned by the OS probably much faster when a
116
/// process exits).
117
///
118
/// The \ref alib_manual_bootstrapping "standard bootstrap" code of \alib, hence the (overloaded)
119
/// functions \ref alib::Shutdown will call this function.
120
///
121
/// \note This method is not thread-safe and hence must be called only on termination of the process
122
/// when all threads which are using singletons are terminated.
123
//==================================================================================================
124
ALIB_API
void
Shutdown
();
125
126
}
// namespace alib[::singletons]
127
128
/// Type alias in namespace \b alib.
129
template
<
typename
T>
130
using
Singleton
=
singletons::Singleton<T>
;
131
132
}
// namespace alib
133
134
135
136
#endif
// HPP_ALIB_SINGLETONS_SINGLETON
137
alib.hpp
alib::singletons::Singleton
Definition
singleton.hpp:51
alib::singletons::Singleton::~Singleton
virtual ~Singleton()
Virtual destructor.
Definition
singleton.hpp:98
alib::singletons::Singleton::singleton
static TDerivedClass * singleton
A pointer to the one and only singleton.
Definition
singleton.hpp:54
alib::singletons::Singleton::GetSingleton
static TDerivedClass & GetSingleton()
Definition
singleton.hpp:59
ALIB_API
#define ALIB_API
Definition
alib.hpp:639
alib::singletons::Shutdown
void Shutdown()
Definition
singleton.cpp:149
alib
Definition
alib.cpp:69
fwds.hpp