Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
singleton_base.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2019 John Wellbelove
11
Copyright(c) 2024 BMW AG
12
13
Permission is hereby granted, free of charge, to any person obtaining a copy
14
of this software and associated documentation files(the "Software"), to deal
15
in the Software without restriction, including without limitation the rights
16
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
17
copies of the Software, and to permit persons to whom the Software is
18
furnished to do so, subject to the following conditions :
19
20
The above copyright notice and this permission notice shall be included in all
21
copies or substantial portions of the Software.
22
23
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
26
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
SOFTWARE.
30
******************************************************************************/
31
32
#ifndef ETL_SINGLETON_BASE_INCLUDED
33
#define ETL_SINGLETON_BASE_INCLUDED
34
39
40
#include "
platform.h
"
41
#include "
error_handler.h
"
42
#include "
file_error_numbers.h
"
43
#include "
nullptr.h
"
44
45
namespace
etl
46
{
47
//*************************************************************************
49
//*************************************************************************
50
class
singleton_base_exception :
public
etl::exception
51
{
52
public
:
53
54
singleton_base_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
55
:
exception
(reason_, file_name_, line_number_)
56
{
57
}
58
};
59
60
//*************************************************************************
62
//*************************************************************************
63
class
singleton_base_not_created :
public
etl::singleton_base_exception
64
{
65
public
:
66
67
singleton_base_not_created(string_type file_name_, numeric_type line_number_)
68
: singleton_base_exception(ETL_ERROR_TEXT(
"singleton_base:not created"
, ETL_SINGLETON_BASE_FILE_ID
"A"
), file_name_, line_number_)
69
{
70
}
71
};
72
73
//*************************************************************************
75
//*************************************************************************
76
class
singleton_base_already_created :
public
etl::singleton_base_exception
77
{
78
public
:
79
80
singleton_base_already_created(string_type file_name_, numeric_type line_number_)
81
: singleton_base_exception(ETL_ERROR_TEXT(
"singleton_base:already created"
, ETL_SINGLETON_BASE_FILE_ID
"B"
), file_name_, line_number_)
82
{
83
}
84
};
85
86
//***********************************************************************
116
//***********************************************************************
117
template
<
typename
T>
118
class
singleton_base
119
{
120
public
:
121
122
//***********************************************************************
123
// Returns a reference to the instance.
124
//***********************************************************************
125
static
T& instance()
126
{
127
ETL_ASSERT
(m_self != ETL_NULLPTR, ETL_ERROR(
etl::singleton_base_not_created
));
128
129
return
*m_self;
130
}
131
132
//***********************************************************************
134
//***********************************************************************
135
static
bool
is_valid
()
136
{
137
return
(m_self != ETL_NULLPTR);
138
}
139
140
protected
:
141
142
//***********************************************************************
146
//***********************************************************************
147
explicit
singleton_base
(T& theInstance)
148
{
149
ETL_ASSERT
(m_self == ETL_NULLPTR, ETL_ERROR(
etl::singleton_base_already_created
));
150
151
m_self = &theInstance;
152
}
153
154
//***********************************************************************
157
//***********************************************************************
158
~singleton_base
()
159
{
160
m_self = ETL_NULLPTR;
161
}
162
163
private
:
164
165
static
T*
m_self
;
166
};
167
168
//***********************************************************************
170
//***********************************************************************
171
template
<
class
T>
172
T* singleton_base<T>::m_self = ETL_NULLPTR;
173
}
// namespace etl
174
175
#endif
etl::singleton_base_already_created
Singleton instance already exists.
Definition
singleton_base.h:77
etl::singleton_base_exception
Base singleton error exception.
Definition
singleton_base.h:51
etl::singleton_base_not_created
Singleton not created error exception.
Definition
singleton_base.h:64
etl::singleton_base::is_valid
static bool is_valid()
Returns whether an instance has been attached to singleton<T> or not.
Definition
singleton_base.h:135
etl::singleton_base::~singleton_base
~singleton_base()
Definition
singleton_base.h:158
etl::singleton_base::singleton_base
singleton_base(T &theInstance)
Definition
singleton_base.h:147
error_handler.h
file_error_numbers.h
ETL_ASSERT
#define ETL_ASSERT(b, e)
Definition
error_handler.h:511
etl::exception::exception
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition
exception.h:81
etl::exception
Definition
exception.h:59
etl
Definition
absolute.h:40
etl::m_self
T * singleton_base< T >::m_self
No violation of one definition rule as this is a class template.
Definition
singleton_base.h:172
nullptr.h
platform.h
include
etl
singleton_base.h
Generated on
for Embedded Template Library by
1.17.0