8 #include "Arp/System/Core/Singleton.hxx"
14 #ifdef ARP_PLATFORM_LINUX
22 namespace Arp {
namespace System {
namespace Acf
35 #ifdef ARP_PLATFORM_LINUX
43 RdLockGuard(pthread_rwlock_t* rw_lock);
46 RdLockGuard(
const RdLockGuard& arg) =
delete;
49 RdLockGuard& operator=(
const RdLockGuard& arg) =
delete;
55 pthread_rwlock_t* rw_lock;
61 inline RdLockGuard::RdLockGuard(pthread_rwlock_t* rw_lockArg)
64 pthread_rwlock_rdlock(rw_lock);
67 inline RdLockGuard::~RdLockGuard()
69 pthread_rwlock_unlock(rw_lock);
80 WrLockGuard(pthread_rwlock_t* rw_lock);
83 WrLockGuard(
const WrLockGuard& arg) =
delete;
86 WrLockGuard& operator=(
const WrLockGuard& arg) =
delete;
92 pthread_rwlock_t* rw_lock;
98 inline WrLockGuard::WrLockGuard(pthread_rwlock_t* rw_lockArg)
101 pthread_rwlock_wrlock(rw_lock);
104 inline WrLockGuard::~WrLockGuard()
106 pthread_rwlock_unlock(rw_lock);
126 typedef void(*SingletonDisposer)(void);
128 typedef std::type_index TypeKey;
129 typedef std::map<TypeKey, void*> Singletons;
142 static bool IsCreated(
void);
153 static TApp& Create(
void);
160 static TApp& Create(
const String& appName);
165 static void Dispose(
void);
181 template<
class TSingleton>
182 void AddSingleton(TSingleton* pSingleton);
187 template<
class TSingleton>
188 TSingleton& GetSingleton(
void);
193 template<
class TSingleton>
194 TSingleton* GetSingletonPtr(
void);
199 template<
class TSingleton>
200 bool RemoveSingleton(
void);
204 static TypeKey GetTypeKey(
void);
208 Singletons singletons;
210 #ifdef ARP_PLATFORM_LINUX
211 pthread_rwlock_t rw_lock;
219 inline AppDomain::AppDomain()
220 : pApplication(nullptr)
223 #ifdef ARP_PLATFORM_LINUX
224 pthread_rwlock_init(&this->rw_lock, NULL);
239 inline AppDomain::TypeKey AppDomain::GetTypeKey(
void)
241 return std::type_index(
typeid(T));
250 TApp::CreateInstance();
254 return dynamic_cast<TApp&
>(TApp::GetInstance());
263 TApp::CreateInstance(appName);
267 return dynamic_cast<TApp&
>(TApp::GetInstance());
281 TApp::DisposeInstance();
287 return *(this->pApplication);
290 template<
class TSingleton>
293 #ifdef ARP_PLATFORM_LINUX
294 WrLockGuard lock(&this->rw_lock);
296 std::lock_guard<std::mutex> lock(this->rw_lock);
300 auto result = this->singletons.insert(make_pair(AppDomain::GetTypeKey<TSingleton>(), pSingleton));
307 template<
class TSingleton>
310 TSingleton* pResult = this->GetSingletonPtr<TSingleton>();
311 if (pResult ==
nullptr)
317 template<
class TSingleton>
320 #ifdef ARP_PLATFORM_LINUX
321 RdLockGuard lock(&this->rw_lock);
323 std::lock_guard<std::mutex> lock(this->rw_lock);
326 auto i = this->singletons.find(AppDomain::GetTypeKey<TSingleton>());
327 if (i == this->singletons.end())
332 return (TSingleton*)(i->second);
335 template<
class TSingleton>
338 #ifdef ARP_PLATFORM_LINUX
339 WrLockGuard lock(&this->rw_lock);
341 std::lock_guard<std::mutex> lock(this->rw_lock);
344 return this->singletons.erase(AppDomain::GetTypeKey<TSingleton>()) != 0;
This class represents a single application domain for each process and is implemented as singleton.
Definition: AppDomain.hpp:122
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
static void Dispose(void)
Disposes the current AppDomain and destructs the application.
Definition: AppDomain.hpp:279
TSingleton * GetSingletonPtr(void)
Gets the singleton pointer of the specified type.
Definition: AppDomain.hpp:318
static TApp & Create(void)
Creates the current AppDomain and application instances.
Definition: AppDomain.hpp:245
IApplication & GetApplication(void)
Gets the application instance of the current process.
Definition: AppDomain.hpp:285
static AppDomain & GetCurrent(void)
Gets the current AppDomain instance.
Definition: AppDomain.hpp:233
TSingleton & GetSingleton(void)
Gets the singleton instance of the specified type.
Definition: AppDomain.hpp:308
static void Assign(AppDomain &other)
Assgins the current AppdDomain singleton from the as argument passed other.
Definition: AppDomain.hpp:270
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 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
static Instance & GetInstance(void)
Gets a reference of the singleton instance.
Definition: Singleton.hxx:113
static void AssignInstanceFrom(Instance &other)
Assigns the singleton instance from another singleton instance of the same type.
Definition: Singleton.hxx:146
This interface shall be implemented by the application class.
Definition: IApplication.hpp:19
This (meta programming) class provides the C++ typename of the as template argument passed type.
Definition: TypeName.hxx:56
This is the namespace of the Application Component Framework.
Definition: ComponentBase.hpp:13
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API