8#include "Arp/System/Core/Singleton.hxx"
14#ifdef ARP_PLATFORM_LINUX
22namespace 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;
61inline RdLockGuard::RdLockGuard(pthread_rwlock_t* rw_lockArg)
64 pthread_rwlock_rdlock(rw_lock);
67inline 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;
98inline WrLockGuard::WrLockGuard(pthread_rwlock_t* rw_lockArg)
101 pthread_rwlock_wrlock(rw_lock);
104inline WrLockGuard::~WrLockGuard()
106 pthread_rwlock_unlock(rw_lock);
126 using Strings = std::vector<String>;
129 using SingletonDisposer = void(*)(void);
131 using TypeKey = std::type_index;
132 using Singletons = std::map<TypeKey, void*>;
145 static bool IsCreated(
void);
156 static TApp& Create(
void);
163 static TApp& Create(
const String& appName);
168 static void Dispose(
void);
182 size_t GetSingletonsCount(
void)
const;
192 template<
class TSingleton>
193 void AddSingleton(TSingleton* pSingleton);
198 template<
class TSingleton>
199 TSingleton& GetSingleton(
void);
204 template<
class TSingleton>
205 TSingleton* GetSingletonPtr(
void);
210 template<
class TSingleton>
211 bool RemoveSingleton(
void);
215 static TypeKey GetTypeKey(
void);
219 Singletons singletons;
221#ifdef ARP_PLATFORM_LINUX
222 pthread_rwlock_t rw_lock;
230inline AppDomain::AppDomain()
231 : pApplication(nullptr)
234#ifdef ARP_PLATFORM_LINUX
235 pthread_rwlock_init(&this->rw_lock, NULL);
250inline AppDomain::TypeKey AppDomain::GetTypeKey(
void)
252 return std::type_index(
typeid(T));
261 TApp::CreateInstance();
265 return dynamic_cast<TApp&
>(TApp::GetInstance());
274 TApp::CreateInstance(appName);
278 return dynamic_cast<TApp&
>(TApp::GetInstance());
292 TApp::DisposeInstance();
298 return *(this->pApplication);
303 return this->singletons.size();
306template<
class TSingleton>
309#ifdef ARP_PLATFORM_LINUX
310 WrLockGuard lock(&this->rw_lock);
312 std::lock_guard<std::mutex> lock(this->rw_lock);
316 auto result = this->singletons.insert(make_pair(AppDomain::GetTypeKey<TSingleton>(), pSingleton));
323template<
class TSingleton>
326 TSingleton* pResult = this->GetSingletonPtr<TSingleton>();
327 if (pResult ==
nullptr)
333template<
class TSingleton>
336#ifdef ARP_PLATFORM_LINUX
337 RdLockGuard lock(&this->rw_lock);
339 std::lock_guard<std::mutex> lock(this->rw_lock);
342 auto i = this->singletons.find(AppDomain::GetTypeKey<TSingleton>());
343 if (i == this->singletons.end())
348 return (TSingleton*)(i->second);
351template<
class TSingleton>
354#ifdef ARP_PLATFORM_LINUX
355 WrLockGuard lock(&this->rw_lock);
357 std::lock_guard<std::mutex> lock(this->rw_lock);
360 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:307
static bool IsCreated(void)
Determines if the appdomain was created yet.
Definition: AppDomain.hpp:239
static void Dispose(void)
Disposes the current AppDomain and destructs the application.
Definition: AppDomain.hpp:290
TSingleton * GetSingletonPtr(void)
Gets the singleton pointer of the specified type.
Definition: AppDomain.hpp:334
static TApp & Create(void)
Creates the current AppDomain and application instances.
Definition: AppDomain.hpp:256
IApplication & GetApplication(void)
Gets the application instance of the current process.
Definition: AppDomain.hpp:296
static AppDomain & GetCurrent(void)
Gets the current AppDomain instance.
Definition: AppDomain.hpp:244
Strings GetSingletonsTypeName(void) const
Gets the type names of currently available singletons
size_t GetSingletonsCount(void) const
Gets the count of available singletons.
Definition: AppDomain.hpp:301
TSingleton & GetSingleton(void)
Gets the singleton instance of the specified type.
Definition: AppDomain.hpp:324
static void Assign(AppDomain &other)
Assgins the current AppdDomain singleton from the as argument passed other.
Definition: AppDomain.hpp:281
bool RemoveSingleton(void)
Removes the singleton pointer of the specified type from this AppDomain.
Definition: AppDomain.hpp:352
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:67
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