PLCnext API Documentation 23.0.2.9
Singleton.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/TypeName.hxx"
9#include "Arp/System/Core/Exception.hpp"
10
11namespace Arp
12{
13
23template <class Derived>
25{
26protected: // typedefs
29
30private: // typedefs
31 typedef Derived Instance;
32 typedef Derived* Ptr;
33
34protected: // construction/destruction
36 Singleton(void) = default;
38 ~Singleton(void) = default;
39
40public: // static operations
46 template <class T = Instance, class ...Args>
47 static Instance & CreateInstance(Args && ... args);
48
51 static bool IsCreated(void);
52
56 static Instance& GetInstance(void);
57
60 static Instance* GetInstancePtr(void);
61
64 static void DisposeInstance(void);
65
66protected: // methods
70 static void SetInstance(Instance* pOther);
71
79 static void AssignInstanceFrom(Instance& other);
80
81private: // deleted methods to avoid copying
82 Singleton(const Singleton& arg) = delete;
83 Singleton& operator=(const Singleton& arg) = delete;
84
85private: // static fields
86 static Instance* pInstance;
87};
88
90// inline methods of class Singleton<T>
91template <class Derived>
92template <class T, class ...Args>
93inline typename Singleton<Derived>::Instance& Singleton<Derived>::CreateInstance(Args&& ... args)
94{
95 if(pInstance != nullptr)
96 {
97 // TODO: enable this exception when unloading of .so files on linux works correctly
98 // throw Exception("Singleton instance of type '{0}' was created yet!", TypeName<Derived>().Value);
99 printf("ERROR: Singleton instance of type '%s' was created yet!", TypeName<Derived>().Value.CStr()); // LogEngine not initialized here
100 }
101 pInstance = new T(std::forward<Args>(args)...);
102
103 return *pInstance;
104}
105
106template <class Derived>
108{
109 return pInstance != nullptr;
110}
111
112template <class Derived>
114{
115 if (pInstance == nullptr)
116 {
117 throw Exception("Singleton instance of type '{0}' was not created yet!", TypeName<Derived>().Value);
118 }
119
120 return *pInstance;
121}
122
123template <class Derived>
125{
126 return pInstance;
127}
128
129template <class Derived>
131{
132 if (pInstance != nullptr)
133 {
134 delete pInstance;
135 pInstance = nullptr;
136 }
137}
138
139template <class Derived>
140inline void Singleton<Derived>::SetInstance(Instance* pOther)
141{
142 pInstance = pOther;
143}
144
145template <class Derived>
146inline void Singleton<Derived>::AssignInstanceFrom(Instance& other)
147{
148 pInstance = &other;
149}
150
152// initializing of static member
153template <typename Derived>
154Derived* Singleton<Derived>::pInstance = nullptr;
155
156} // end of namesapce Arp
This class represents a single application domain for each process and is implemented as singleton.
Definition: AppDomain.hpp:122
This is the base class of all Arp exception classes.
Definition: Exception.hpp:16
This class implements the singleton pattern.
Definition: Singleton.hxx:25
static void DisposeInstance(void)
Disposes this singleton instance.
Definition: Singleton.hxx:130
static bool IsCreated(void)
Determines if this singleton instance is create yet.
Definition: Singleton.hxx:107
static Instance & CreateInstance(Args &&... args)
Creates this singleton instance.
Definition: Singleton.hxx:93
~Singleton(void)=default
The protected default destructor.
static void SetInstance(Instance *pOther)
Sets the singleton instance.
Definition: Singleton.hxx:140
static Instance & GetInstance(void)
Gets a reference of the singleton instance.
Definition: Singleton.hxx:113
Singleton(void)=default
The protected default constructor.
static Instance * GetInstancePtr(void)
Gets a pointer to the singleton instance.
Definition: Singleton.hxx:124
Singleton< Derived > SingletonBase
Defines this type to be used from derived classes.
Definition: Singleton.hxx:28
static void AssignInstanceFrom(Instance &other)
Assigns the singleton instance from another singleton instance of the same type.
Definition: Singleton.hxx:146
This (meta programming) class provides the C++ typename of the as template argument passed type.
Definition: TypeName.hxx:67
Root namespace for the PLCnext API