PLCnext API Documentation  22.9.0.33
Singleton.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/TypeName.hxx"
9 #include "Arp/System/Core/Exception.hpp"
10 
11 namespace Arp
12 {
13 
23 template <class Derived>
24 class Singleton
25 {
26 protected: // typedefs
29 
30 private: // typedefs
31  typedef Derived Instance;
32  typedef Derived* Ptr;
33 
34 protected: // construction/destruction
36  Singleton(void) = default;
38  ~Singleton(void) = default;
39 
40 public: // 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 
66 protected: // methods
70  static void SetInstance(Instance* pOther);
71 
79  static void AssignInstanceFrom(Instance& other);
80 
81 private: // deleted methods to avoid copying
82  Singleton(const Singleton& arg) = delete;
83  Singleton& operator=(const Singleton& arg) = delete;
84 
85 private: // static fields
86  static Instance* pInstance;
87 };
88 
90 // inline methods of class Singleton<T>
91 template <class Derived>
92 template <class T, class ...Args>
93 inline 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 
106 template <class Derived>
108 {
109  return pInstance != nullptr;
110 }
111 
112 template <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 
123 template <class Derived>
125 {
126  return pInstance;
127 }
128 
129 template <class Derived>
131 {
132  if (pInstance != nullptr)
133  {
134  delete pInstance;
135  pInstance = nullptr;
136  }
137 }
138 
139 template <class Derived>
140 inline void Singleton<Derived>::SetInstance(Instance* pOther)
141 {
142  pInstance = pOther;
143 }
144 
145 template <class Derived>
146 inline void Singleton<Derived>::AssignInstanceFrom(Instance& other)
147 {
148  pInstance = &other;
149 }
150 
152 // initializing of static member
153 template <typename Derived>
154 Derived* 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:56
Root namespace for the PLCnext API