LibCarna Version 3.4.0
Loading...
Searching...
No Matches
Singleton.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#ifndef SINGLETON_H_6014714286
16#define SINGLETON_H_6014714286
17
25
26namespace LibCarna
27{
28
29namespace base
30{
31
32
33
34// ----------------------------------------------------------------------------------
35// Singleton
36// ----------------------------------------------------------------------------------
37
61template< typename InstanceType >
63{
64
66
70 static InstanceType* instancePtr;
71
72protected:
73
78 {
79 LIBCARNA_ASSERT_EX( instancePtr == nullptr, "Multiple singleton instances created." );
80 instancePtr = static_cast< InstanceType* >( this );
81 }
82
86 static void reset()
87 {
88 if( instancePtr != nullptr )
89 {
90 delete instancePtr;
91 }
92 }
93
94public:
95
100
104 virtual ~Singleton()
105 {
106 instancePtr = nullptr;
107 }
108
113 {
114 return instancePtr == nullptr ? *new InstanceType() : *instancePtr;
115 }
116
120 static bool exists()
121 {
122 return instancePtr != nullptr;
123 }
124
125}; // Singleton
126
127
128template< typename InstanceType >
129InstanceType* Singleton< InstanceType >::instancePtr = nullptr;
130
131
132
133} // namespace LibCarna :: base
134
135} // namespace LibCarna
136
137#endif // SINGLETON_H_6014714286
Defines LibCarna::base::LibCarnaException and LibCarna::base::AssertionFailure.
#define LIBCARNA_ASSERT_EX(expression, description)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Represents an association.
Singleton base class
Definition Singleton.hpp:63
static InstanceType & instance()
Returns the only instance from class InstanceType.
static void reset()
Deletes the only instance from class InstanceType.
Definition Singleton.hpp:86
Singleton()
Denotes that the instance was created. Default constructor is hidden.
Definition Singleton.hpp:77
InstanceType Instance
Denotes the class, that is derived from this class template.
Definition Singleton.hpp:99
static bool exists()
Tells whether the instance from class InstanceType currently exists.
virtual ~Singleton()
Denotes that the instance was deleted.
Defines LibCarna::base::noncopyable and NON_COPYABLE.
#define NON_COPYABLE
Marks the class that it is placed in as non-copyable.