Embedded Template Library 1.0
Loading...
Searching...
No Matches
singleton_base.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2019 John Wellbelove
11Copyright(c) 2024 BMW AG
12
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files(the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions :
19
20The above copyright notice and this permission notice shall be included in all
21copies or substantial portions of the Software.
22
23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
26AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29SOFTWARE.
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 "nullptr.h"
43#include "file_error_numbers.h"
44
45namespace etl
46{
47 //*************************************************************************
49 //*************************************************************************
59
60 //*************************************************************************
62 //*************************************************************************
64 {
65 public:
66
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 //*************************************************************************
77 {
78 public:
79
81 : singleton_base_exception(ETL_ERROR_TEXT("singleton_base:already created", ETL_SINGLETON_BASE_FILE_ID"A"), file_name_, line_number_)
82 {
83 }
84 };
85
86 //***********************************************************************
115 //***********************************************************************
116 template <typename T>
118 {
119 public:
120
121 //***********************************************************************
122 // Returns a reference to the instance.
123 //***********************************************************************
124 static T& instance()
125 {
126 ETL_ASSERT(m_self != ETL_NULLPTR, ETL_ERROR(etl::singleton_base_not_created));
127
128 return *m_self;
129 }
130
131 //***********************************************************************
133 //***********************************************************************
134 static bool is_valid()
135 {
136 return (m_self != ETL_NULLPTR);
137 }
138
139 protected:
140
141 //***********************************************************************
144 //***********************************************************************
146 {
147 ETL_ASSERT(m_self == ETL_NULLPTR, ETL_ERROR(etl::singleton_base_already_created));
148
149 m_self = &theInstance;
150 }
151
152 //***********************************************************************
154 //***********************************************************************
156 {
157 m_self = ETL_NULLPTR;
158 }
159
160 private:
161
162 static T* m_self;
163 };
164
165 //***********************************************************************
167 //***********************************************************************
168 template<class T>
169 T* singleton_base<T>::m_self = ETL_NULLPTR;
170}
171
172#endif
Singleton instance already exists.
Definition singleton_base.h:77
Base singleton error exception.
Definition singleton_base.h:51
Singleton not created error exception.
Definition singleton_base.h:64
Definition singleton_base.h:118
static bool is_valid()
Returns whether an instance has been attached to singleton<T> or not.
Definition singleton_base.h:134
~singleton_base()
Removes the internal reference to the instance passed in the constructor.
Definition singleton_base.h:155
singleton_base(T &theInstance)
Definition singleton_base.h:145
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
Definition exception.h:47
bitset_ext
Definition absolute.h:38
pair holds two objects of arbitrary type
Definition utility.h:164