PLCnext API Documentation 24.0.0.71
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
61 ARP_DEPRECATED("Use &GetInstance() instead.")
62 static Instance* GetInstancePtr(void);
63
66 static void DisposeInstance(void);
67
68protected: // methods
73 ARP_DEPRECATED("Do not use this operation any more.")
74 static void SetInstance(Instance* pOther);
75
83 static void AssignInstanceFrom(Instance& other);
84
85private: // deleted methods to avoid copying
86 Singleton(const Singleton& arg) = delete;
87 Singleton& operator=(const Singleton& arg) = delete;
88
89private: // static fields
90 static Instance* pInstance;
91};
92
94// inline methods of class Singleton<T>
95template <class Derived>
96template <class T, class ...Args>
97inline typename Singleton<Derived>::Instance& Singleton<Derived>::CreateInstance(Args&& ... args)
98{
99 if(pInstance != nullptr)
100 {
101 // TODO: enable this exception when unloading of .so files on linux works correctly
102 // throw Exception("Singleton instance of type '{0}' was created yet!", TypeName<Derived>());
103 printf("ERROR: Singleton instance of type '%s' was created yet!", TypeName<Derived>().GetFullName().CStr()); // LogEngine not initialized here
104 }
105 pInstance = new T(std::forward<Args>(args)...);
106
107 return *pInstance;
108}
109
110template <class Derived>
112{
113 return pInstance != nullptr;
114}
115
116template <class Derived>
118{
119 if (pInstance == nullptr)
120 {
121 throw Exception("Singleton instance of type '{0}' was not created yet!", TypeName<Derived>());
122 }
123
124 return *pInstance;
125}
126
127template <class Derived>
129{
130 return pInstance;
131}
132
133template <class Derived>
135{
136 if (pInstance != nullptr)
137 {
138 delete pInstance;
139 pInstance = nullptr;
140 }
141}
142
143template <class Derived>
144inline void Singleton<Derived>::SetInstance(Instance* pOther)
145{
146 pInstance = pOther;
147}
148
149template <class Derived>
150inline void Singleton<Derived>::AssignInstanceFrom(Instance& other)
151{
152 pInstance = &other;
153}
154
156// initializing of static member
157template <typename Derived>
158Derived* Singleton<Derived>::pInstance = nullptr;
159
160} // end of namesapce Arp
This class represents a single application domain for each process and is implemented as singleton.
Definition: AppDomain.hpp:119
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:134
static bool IsCreated(void)
Determines if this singleton instance is create yet.
Definition: Singleton.hxx:111
static Instance & CreateInstance(Args &&... args)
Creates this singleton instance.
Definition: Singleton.hxx:97
~Singleton(void)=default
The protected default destructor.
static void SetInstance(Instance *pOther)
Depreacated! Sets the singleton instance.
Definition: Singleton.hxx:144
static Instance & GetInstance(void)
Gets a reference of the singleton instance.
Definition: Singleton.hxx:117
Singleton(void)=default
The protected default constructor.
static Instance * GetInstancePtr(void)
Deprecated! Gets a pointer to the singleton instance.
Definition: Singleton.hxx:128
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:150
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
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
Deprecated! The class implements an adapter for enums to define the string literals of the enum entri...
Definition: EnumStrings.hxx:38