PLCnext API Documentation 25.0.2.69
Singleton.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7
8#ifndef ARP_USE_ARP_SYSTEM_CORE
9
10#include "Arp/Base/Core/Singleton.hxx"
11
12#else
13
14#include "Arp/System/Core/Arp.h"
15#include "Arp/System/Core/TypeName.hxx"
16#include "Arp/System/Core/Exception.hpp"
17
18namespace Arp
19{
20
30template <class Derived>
31class Singleton
32{
33protected: // typedefs
35 typedef Singleton<Derived> SingletonBase;
36
37private: // typedefs
38 typedef Derived Instance;
39 typedef Derived* Ptr;
40
41protected: // construction/destruction
43 Singleton(void) = default;
45 ~Singleton(void) = default;
46
47public: // static operations
53 template <class T = Instance, class ...Args>
54 static Instance & CreateInstance(Args && ... args);
55
58 static bool IsCreated(void);
59
63 static Instance& GetInstance(void);
64
68 ARP_DEPRECATED("Use &GetInstance() instead.")
69 static Instance* GetInstancePtr(void);
70
73 static void DisposeInstance(void);
74
75protected: // methods
80 ARP_DEPRECATED("Do not use this operation any more.")
81 static void SetInstance(Instance* pOther);
82
90 static void AssignInstanceFrom(Instance& other);
91
92private: // deleted methods to avoid copying
93 Singleton(const Singleton& arg) = delete;
94 Singleton& operator=(const Singleton& arg) = delete;
95
96private: // static fields
97 static Instance* pInstance;
98};
99
101// inline methods of class Singleton<T>
102template <class Derived>
103template <class T, class ...Args>
104inline typename Singleton<Derived>::Instance& Singleton<Derived>::CreateInstance(Args&& ... args)
105{
106 if(pInstance != nullptr)
107 {
108 // throw Exception("Singleton instance of type '{0}' was created yet!", TypeName<Derived>());
109 printf("ERROR: Singleton instance of type '%s' was created yet!", TypeName<Derived>().GetFullName().CStr()); // LogEngine not initialized here
110 }
111 pInstance = new T(std::forward<Args>(args)...);
112
113 return *pInstance;
114}
115
116template <class Derived>
117inline bool Singleton<Derived>::IsCreated(void)
118{
119 return pInstance != nullptr;
120}
121
122template <class Derived>
123inline Derived& Singleton<Derived>::GetInstance()
124{
125 if (pInstance == nullptr)
126 {
127 throw Exception("Singleton instance of type '{0}' was not created yet!", TypeName<Derived>());
128 }
129
130 return *pInstance;
131}
132
133template <class Derived>
134inline Derived* Singleton<Derived>::GetInstancePtr()
135{
136 return pInstance;
137}
138
139template <class Derived>
141{
142 if (pInstance != nullptr)
143 {
144 delete pInstance;
145 pInstance = nullptr;
146 }
147}
148
149template <class Derived>
150inline void Singleton<Derived>::SetInstance(Instance* pOther)
151{
152 pInstance = pOther;
153}
154
155template <class Derived>
156inline void Singleton<Derived>::AssignInstanceFrom(Instance& other)
157{
158 pInstance = &other;
159}
160
162// initializing of static member
163template <typename Derived>
164Derived* Singleton<Derived>::pInstance = nullptr;
165
166} // end of namesapce Arp
167
168#endif // ndef ARP_USE_ARP_SYSTEM_CORE
static void DisposeInstance(void)
Disposes this singleton instance.
Definition: Singleton.ipp:66
static void SetInstance(T *pInstance)
DEPRECATED.
Definition: Singleton.ipp:100
~Singleton(void)
The protected destructor.
Singleton< T > SingletonBase
Defines this type to be used from derived classes.
Definition: Singleton.hxx:26
static Instance & CreateInstance(Args &&... args)
Creates this singleton instance.
Definition: Singleton.ipp:31
static Instance & GetInstance(void)
Gets a reference of the singleton instance.
Definition: Singleton.ipp:53
static bool IsCreated(void)
Determines if this singleton instance is create yet.
Definition: Singleton.ipp:44
Singleton(void)
The protected default constructor.
@ Instance
Instance member or field, respectively.
Root namespace for the PLCnext API