PLCnext API Documentation  22.9.0.33
AppDomainSingleton.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/Singleton.hxx"
9 #include "Arp/System/Core/AppDomain.hpp"
10 
11 namespace Arp
12 {
13 
23 template <class Derived>
25 {
26 protected: // typedefs
28  typedef AppDomainSingleton<Derived> SingletonBase; // for use in derived classes
29 
30 private: // typedefs
31  typedef Derived Instance;
32 
33 protected: // construction/destruction
35  AppDomainSingleton(void) = default;
37  ~AppDomainSingleton(void) = default;
38 
39 public: // static operations
45  template <class T = Instance, class ...Args>
46  static Derived & CreateInstance(Args && ... args);
47 
50  static bool IsCreated(void);
51 
55  static Derived& GetInstance(void);
56 
59  static Derived* GetInstancePtr(void);
60 
63  static void DisposeInstance(void);
64 
65 private: // deleted methods to avoid copying
66  AppDomainSingleton(const AppDomainSingleton& arg) = delete;
67  AppDomainSingleton& operator=(const AppDomainSingleton& arg) = delete;
68 };
69 
71 // inline methods of class AppDomainSingleton<T>
72 template <class Derived>
73 template <class T, class ...Args>
74 inline Derived& AppDomainSingleton<Derived>::CreateInstance(Args&& ... args)
75 {
76  Derived* pInstance = GetInstancePtr();
77  // create instance first
78  if(pInstance != nullptr)
79  {
80  throw Exception("AppDomain Singleton instance of type '{0}' was created yet!", TypeName<Derived>().Value);
81  }
82  // else
83  pInstance = new T(std::forward<Args>(args)...);
84  // and add it to appDomains singleton collection
86 
87  return *pInstance;
88 }
89 
90 template <class Derived>
92 {
93  if (!AppDomain::IsCreated())
94  {
95  return false;
96  }
97  // else
98  return AppDomainSingleton::GetInstancePtr() != nullptr;
99 }
100 
101 template <class Derived>
103 {
104  Derived* pResult = GetInstancePtr();
105  if (pResult == nullptr)
106  {
107  throw Exception("AppDomain Singleton instance of type '{0}' was not created yet!", TypeName<Derived>().Value);
108  }
109  return *pResult;
110 }
111 
112 template <class Derived>
114 {
115  return AppDomain::GetCurrent().GetSingletonPtr<Derived>(); // might return nullptr
116 }
117 
118 template <class Derived>
120 {
121  // destroy instance
122  delete GetInstancePtr();
123  // and remove singleton from appDomain
125 }
126 
127 } // end of namesapce Arp
This class implements the singleton pattern for singletons with process wide scope.
Definition: AppDomainSingleton.hxx:25
static Derived & GetInstance(void)
Gets a reference of the singleton instance.
Definition: AppDomainSingleton.hxx:102
static bool IsCreated(void)
Determines if this singleton instance is created yet.
Definition: AppDomainSingleton.hxx:91
static Derived & CreateInstance(Args &&... args)
Creates this singleton instance.
Definition: AppDomainSingleton.hxx:74
static Derived * GetInstancePtr(void)
Gets a pointer to the singleton instance.
Definition: AppDomainSingleton.hxx:113
AppDomainSingleton(void)=default
The protected default constructor.
static void DisposeInstance(void)
Disposes this singleton instance.
Definition: AppDomainSingleton.hxx:119
~AppDomainSingleton(void)=default
The protected default destructor.
AppDomainSingleton< Derived > SingletonBase
Defines this type to be used from derived classes.
Definition: AppDomainSingleton.hxx:28
void AddSingleton(TSingleton *pSingleton)
Adds a singleton instance to this AppDomain.
Definition: AppDomain.hpp:291
static bool IsCreated(void)
Determines if the appdomain was created yet.
Definition: AppDomain.hpp:228
TSingleton * GetSingletonPtr(void)
Gets the singleton pointer of the specified type.
Definition: AppDomain.hpp:318
static AppDomain & GetCurrent(void)
Gets the current AppDomain instance.
Definition: AppDomain.hpp:233
bool RemoveSingleton(void)
Removes the singleton pointer of the specified type from this AppDomain.
Definition: AppDomain.hpp:336
This is the base class of all Arp exception classes.
Definition: Exception.hpp:16
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