PLCnext API Documentation 23.6.0.37
AppDomainSingleton.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/Singleton.hxx"
9#include "Arp/System/Core/AppDomain.hpp"
10
11namespace Arp
12{
13
23template <class Derived>
25{
26protected: // typedefs
28 typedef AppDomainSingleton<Derived> SingletonBase; // for use in derived classes
29
30private: // typedefs
31 typedef Derived Instance;
32
33protected: // construction/destruction
35 AppDomainSingleton(void) = default;
37 ~AppDomainSingleton(void) = default;
38
39public: // 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
60 ARP_DEPRECATED("Use &GetInstance() instead.")
61 static Derived* GetInstancePtr(void);
62
65 static void DisposeInstance(void);
66
67private:
68 static Derived* GetInstancePtrInternal(void);
69
70private: // deleted methods to avoid copying
71 AppDomainSingleton(const AppDomainSingleton& arg) = delete;
72 AppDomainSingleton& operator=(const AppDomainSingleton& arg) = delete;
73};
74
76// inline methods of class AppDomainSingleton<T>
77template <class Derived>
78template <class T, class ...Args>
79inline Derived& AppDomainSingleton<Derived>::CreateInstance(Args&& ... args)
80{
81 Derived* pInstance = GetInstancePtrInternal();
82 // create instance first
83 if(pInstance != nullptr)
84 {
85 throw Exception("AppDomain Singleton instance of type '{0}' was created yet!", TypeName<Derived>().Value);
86 }
87 // else
88 pInstance = new T(std::forward<Args>(args)...);
89 // and add it to appDomains singleton collection
91
92 return *pInstance;
93}
94
95template <class Derived>
97{
99 {
100 return false;
101 }
102 // else
103 return AppDomainSingleton::GetInstancePtrInternal() != nullptr;
104}
105
106template <class Derived>
108{
109 Derived* pResult = GetInstancePtrInternal();
110 if (!IsCreated())
111 {
112 throw Exception("AppDomain Singleton instance of type '{0}' was not created yet!", TypeName<Derived>().Value);
113 }
114 return *pResult;
115}
116
117template <class Derived>
119{
120 return GetInstancePtrInternal();
121}
122
123template <class Derived>
125{
126 return AppDomain::GetCurrent().GetSingletonPtr<Derived>(); // might return nullptr
127}
128
129template <class Derived>
131{
132 // destroy instance
133 delete GetInstancePtrInternal();
134 // and remove singleton from appDomain
136}
137
138} // 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:107
static bool IsCreated(void)
Determines if this singleton instance is created yet.
Definition: AppDomainSingleton.hxx:96
static Derived & CreateInstance(Args &&... args)
Creates this singleton instance.
Definition: AppDomainSingleton.hxx:79
static Derived * GetInstancePtr(void)
Depreacated! Gets a pointer to the singleton instance.
Definition: AppDomainSingleton.hxx:118
AppDomainSingleton(void)=default
The protected default constructor.
static void DisposeInstance(void)
Disposes this singleton instance.
Definition: AppDomainSingleton.hxx:130
~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:305
static bool IsCreated(void)
Determines if the appdomain was created yet.
Definition: AppDomain.hpp:237
TSingleton * GetSingletonPtr(void)
Gets the singleton pointer of the specified type.
Definition: AppDomain.hpp:332
static AppDomain & GetCurrent(void)
Gets the current AppDomain instance.
Definition: AppDomain.hpp:242
bool RemoveSingleton(void)
Removes the singleton pointer of the specified type from this AppDomain.
Definition: AppDomain.hpp:350
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: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