Loading...
Searching...
No Matches
singleton_base.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2019 John Wellbelove
Copyright(c) 2024 BMW AG
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_SINGLETON_BASE_INCLUDED
#define ETL_SINGLETON_BASE_INCLUDED
#include "platform.h"
#include "error_handler.h"
#include "file_error_numbers.h"
#include "nullptr.h"
namespace etl
{
//*************************************************************************
//*************************************************************************
class singleton_base_exception : public etl::exception
{
public:
singleton_base_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
: exception(reason_, file_name_, line_number_)
{
}
};
//*************************************************************************
//*************************************************************************
class singleton_base_not_created : public etl::singleton_base_exception
{
public:
singleton_base_not_created(string_type file_name_, numeric_type line_number_)
: singleton_base_exception(ETL_ERROR_TEXT("singleton_base:not created", ETL_SINGLETON_BASE_FILE_ID"A"), file_name_, line_number_)
{
}
};
//*************************************************************************
//*************************************************************************
class singleton_base_already_created : public etl::singleton_base_exception
{
public:
singleton_base_already_created(string_type file_name_, numeric_type line_number_)
: singleton_base_exception(ETL_ERROR_TEXT("singleton_base:already created", ETL_SINGLETON_BASE_FILE_ID"B"), file_name_, line_number_)
{
}
};
//***********************************************************************
//***********************************************************************
template <typename T>
class singleton_base
{
public:
//***********************************************************************
// Returns a reference to the instance.
//***********************************************************************
static T& instance()
{
ETL_ASSERT(m_self != ETL_NULLPTR, ETL_ERROR(etl::singleton_base_not_created));
return *m_self;
}
//***********************************************************************
//***********************************************************************
static bool is_valid()
{
return (m_self != ETL_NULLPTR);
}
protected:
//***********************************************************************
//***********************************************************************
explicit singleton_base(T& theInstance)
{
ETL_ASSERT(m_self == ETL_NULLPTR, ETL_ERROR(etl::singleton_base_already_created));
m_self = &theInstance;
}
//***********************************************************************
//***********************************************************************
~singleton_base()
{
m_self = ETL_NULLPTR;
}
private:
static T* m_self;
};
//***********************************************************************
//***********************************************************************
template <class T>
T* singleton_base<T>::m_self = ETL_NULLPTR;
} // namespace etl
#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