|
| 1 | +///\file |
| 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 | + |
| 35 | +///\defgroup singleton_base singleton_base |
| 36 | +/// Templated version of the singleton pattern, implemented as base class |
| 37 | +/// for singletons. |
| 38 | +///\ingroup etl |
| 39 | + |
| 40 | +#include "platform.h" |
| 41 | +#include "error_handler.h" |
| 42 | +#include "nullptr.h" |
| 43 | +#include "file_error_numbers.h" |
| 44 | + |
| 45 | +namespace etl |
| 46 | +{ |
| 47 | + //************************************************************************* |
| 48 | + /// Base singleton error exception. |
| 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 | + //************************************************************************* |
| 61 | + /// Singleton not created error exception. |
| 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 | + //************************************************************************* |
| 74 | + /// Singleton instance already exists. |
| 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"A"), file_name_, line_number_) |
| 82 | + { |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + //*********************************************************************** |
| 87 | + /// Base class for singletons. |
| 88 | + /// \tparam T Any type that wants to expose the instance() interface. |
| 89 | + /// |
| 90 | + /// This class is designed to work as a generic base class for any class that wants to |
| 91 | + /// provide a singleton interface. It'll also work for classes that do not have a |
| 92 | + /// default constructor. |
| 93 | + /// |
| 94 | + /// Usage example: |
| 95 | + /// |
| 96 | + /// class Origin |
| 97 | + /// : singleton<Origin> |
| 98 | + /// { |
| 99 | + /// public: |
| 100 | + /// Origin(int x, int y) |
| 101 | + /// : singleton<Origin>(*this) |
| 102 | + /// {} |
| 103 | + /// |
| 104 | + /// int getX() const; |
| 105 | + /// } theOrigin(0, 0); |
| 106 | + /// |
| 107 | + /// int x = Origin::instance().getX(); |
| 108 | + /// |
| 109 | + /// |
| 110 | + /// Note: |
| 111 | + /// |
| 112 | + /// It is important that a call to instance() will not create the instance of the class. It needs |
| 113 | + /// to be created by the user before calling instance(). This way, the user has better control |
| 114 | + /// over the instance lifetime instead of e.g. lazy initialization. |
| 115 | + //*********************************************************************** |
| 116 | + template <typename T> |
| 117 | + class singleton_base |
| 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 | + //*********************************************************************** |
| 132 | + /// Returns whether an instance has been attached to singleton<T> or not. |
| 133 | + //*********************************************************************** |
| 134 | + static bool is_valid() |
| 135 | + { |
| 136 | + return (m_self != ETL_NULLPTR); |
| 137 | + } |
| 138 | + |
| 139 | + protected: |
| 140 | + |
| 141 | + //*********************************************************************** |
| 142 | + /// Constructs the instance of singleton. |
| 143 | + /// theInstance Reference to T, which will be returned when instance() is called. |
| 144 | + //*********************************************************************** |
| 145 | + explicit singleton_base(T& theInstance) |
| 146 | + { |
| 147 | + ETL_ASSERT(m_self == ETL_NULLPTR, ETL_ERROR(etl::singleton_base_already_created)); |
| 148 | + |
| 149 | + m_self = &theInstance; |
| 150 | + } |
| 151 | + |
| 152 | + //*********************************************************************** |
| 153 | + /// Removes the internal reference to the instance passed in the constructor. |
| 154 | + //*********************************************************************** |
| 155 | + ~singleton_base() |
| 156 | + { |
| 157 | + m_self = ETL_NULLPTR; |
| 158 | + } |
| 159 | + |
| 160 | + private: |
| 161 | + |
| 162 | + static T* m_self; |
| 163 | + }; |
| 164 | + |
| 165 | + //*********************************************************************** |
| 166 | + /// No violation of one definition rule as this is a class template |
| 167 | + //*********************************************************************** |
| 168 | + template<class T> |
| 169 | + T* singleton_base<T>::m_self = ETL_NULLPTR; |
| 170 | +} |
| 171 | + |
| 172 | +#endif |
0 commit comments