PLCnext API Documentation 25.0.2.69
AppDomainSingleton.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/AppDomainSingleton.hxx"
11
12#else
13
14#include "Arp/System/Core/Arp.h"
15#include "Arp/System/Core/Singleton.hxx"
16#include "Arp/System/Core/AppDomain.hpp"
17
18namespace Arp
19{
20
30template <class Derived>
31class AppDomainSingleton
32{
33protected: // typedefs
35 typedef AppDomainSingleton<Derived> SingletonBase; // for use in derived classes
36
37private: // typedefs
38 typedef Derived Instance;
39
40protected: // construction/destruction
42 AppDomainSingleton(void) = default;
44 ~AppDomainSingleton(void) = default;
45
46public: // static operations
52 template <class T = Instance, class ...Args>
53 static Derived & CreateInstance(Args && ... args);
54
57 static bool IsCreated(void);
58
62 static Derived& GetInstance(void);
63
67 ARP_DEPRECATED("Use &GetInstance() instead.")
68 static Derived* GetInstancePtr(void);
69
72 static void DisposeInstance(void);
73
74private:
75 static Derived* GetInstancePtrInternal(void);
76
77private: // deleted methods to avoid copying
78 AppDomainSingleton(const AppDomainSingleton& arg) = delete;
79 AppDomainSingleton& operator=(const AppDomainSingleton& arg) = delete;
80};
81
83// inline methods of class AppDomainSingleton<T>
84template <class Derived>
85template <class T, class ...Args>
86inline Derived& AppDomainSingleton<Derived>::CreateInstance(Args&& ... args)
87{
88 Derived* pInstance = GetInstancePtrInternal();
89 // create instance first
90 if(pInstance != nullptr)
91 {
92 throw Exception("AppDomain Singleton instance of type '{0}' was created yet!", TypeName<Derived>());
93 }
94 // else
95 pInstance = new T(std::forward<Args>(args)...);
96 // and add it to appDomains singleton collection
97 AppDomain::GetInstance().AddSingleton(pInstance);
98
99 return *pInstance;
100}
101
102template <class Derived>
103inline bool AppDomainSingleton<Derived>::IsCreated(void)
104{
106 {
107 return false;
108 }
109 // else
110 return AppDomainSingleton::GetInstancePtrInternal() != nullptr;
111}
112
113template <class Derived>
114inline Derived& AppDomainSingleton<Derived>::GetInstance()
115{
116 Derived* pResult = GetInstancePtrInternal();
117 if (!IsCreated())
118 {
119 throw Exception("AppDomain Singleton instance of type '{0}' was not created yet!", TypeName<Derived>());
120 }
121 return *pResult;
122}
123
124template <class Derived>
125inline Derived* AppDomainSingleton<Derived>::GetInstancePtr()
126{
127 return GetInstancePtrInternal();
128}
129
130template <class Derived>
131inline Derived* AppDomainSingleton<Derived>::GetInstancePtrInternal()
132{
133 return AppDomain::GetCurrent().GetSingletonPtr<Derived>(); // might return nullptr
134}
135
136template <class Derived>
137inline void AppDomainSingleton<Derived>::DisposeInstance()
138{
139 // destroy instance
140 delete GetInstancePtrInternal();
141 // and remove singleton from appDomain
142 AppDomain::GetInstance().RemoveSingleton<Derived>();
143}
144
145} // end of namesapce Arp
146
147#endif // ndef ARP_USE_ARP_SYSTEM_CORE
static bool IsCreated(void)
Determines if this singleton instance is created yet.
Definition: AppDomain.cpp:42
static Instance & GetCurrent(void)
Gets the singleton instance of this class.
Definition: AppDomain.cpp:83
static Instance & GetInstance(void)
Gets a reference of the singleton instance.
Definition: AppDomain.cpp:50
@ Instance
Instance member or field, respectively.
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