8#include "Arp/System/Core/Singleton.hxx"
14#ifdef ARP_PLATFORM_LINUX
22namespace Arp {
namespace System {
namespace Acf
32#ifdef ARP_PLATFORM_LINUX
40 RdLockGuard(pthread_rwlock_t* rw_lock);
43 RdLockGuard(
const RdLockGuard& arg) =
delete;
46 RdLockGuard& operator=(
const RdLockGuard& arg) =
delete;
52 pthread_rwlock_t* rw_lock;
58inline RdLockGuard::RdLockGuard(pthread_rwlock_t* rw_lockArg)
61 pthread_rwlock_rdlock(rw_lock);
64inline RdLockGuard::~RdLockGuard()
66 pthread_rwlock_unlock(rw_lock);
77 WrLockGuard(pthread_rwlock_t* rw_lock);
80 WrLockGuard(
const WrLockGuard& arg) =
delete;
83 WrLockGuard& operator=(
const WrLockGuard& arg) =
delete;
89 pthread_rwlock_t* rw_lock;
95inline WrLockGuard::WrLockGuard(pthread_rwlock_t* rw_lockArg)
98 pthread_rwlock_wrlock(rw_lock);
101inline WrLockGuard::~WrLockGuard()
103 pthread_rwlock_unlock(rw_lock);
123 using Strings = std::vector<String>;
127 using SingletonDisposer = void(*)(void);
129 using TypeKey = std::type_index;
130 using Singletons = std::map<TypeKey, void*>;
143 static bool IsCreated(
void);
154 static TApp& Create(
void);
161 static TApp& Create(
const String& appName);
166 static void Dispose(
void);
180 size_t GetSingletonsCount(
void)
const;
190 template<
class TSingleton>
191 void AddSingleton(TSingleton* pSingleton);
196 template<
class TSingleton>
197 TSingleton& GetSingleton(
void);
202 template<
class TSingleton>
203 TSingleton* GetSingletonPtr(
void);
208 template<
class TSingleton>
209 bool RemoveSingleton(
void);
213 static TypeKey GetTypeKey(
void);
217 Singletons singletons;
219#ifdef ARP_PLATFORM_LINUX
220 pthread_rwlock_t rw_lock;
228inline AppDomain::AppDomain()
229 : pApplication(nullptr)
232#ifdef ARP_PLATFORM_LINUX
233 pthread_rwlock_init(&this->rw_lock, NULL);
248inline AppDomain::TypeKey AppDomain::GetTypeKey(
void)
250 return std::type_index(
typeid(T));
259 TApp::CreateInstance();
263 return dynamic_cast<TApp&
>(TApp::GetInstance());
272 TApp::CreateInstance(appName);
276 return dynamic_cast<TApp&
>(TApp::GetInstance());
290 TApp::DisposeInstance();
296 return *(this->pApplication);
301 return this->singletons.size();
304template<
class TSingleton>
307#ifdef ARP_PLATFORM_LINUX
308 WrLockGuard lock(&this->rw_lock);
310 std::lock_guard<std::mutex> lock(this->rw_lock);
314 auto result = this->singletons.insert(make_pair(AppDomain::GetTypeKey<TSingleton>(), pSingleton));
321template<
class TSingleton>
324 TSingleton* pResult = this->GetSingletonPtr<TSingleton>();
325 if (pResult ==
nullptr)
331template<
class TSingleton>
334#ifdef ARP_PLATFORM_LINUX
335 RdLockGuard lock(&this->rw_lock);
337 std::lock_guard<std::mutex> lock(this->rw_lock);
340 auto i = this->singletons.find(AppDomain::GetTypeKey<TSingleton>());
341 if (i == this->singletons.end())
346 return (TSingleton*)(i->second);
349template<
class TSingleton>
352#ifdef ARP_PLATFORM_LINUX
353 WrLockGuard lock(&this->rw_lock);
355 std::lock_guard<std::mutex> lock(this->rw_lock);
358 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:119
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
IApplication & GetApplication(void)
Gets the application instance of the current process.
Definition: AppDomain.hpp:294
static void Dispose(void)
Disposes the current AppDomain and destructs the application.
Definition: AppDomain.hpp:288
TSingleton * GetSingletonPtr(void)
Gets the singleton pointer of the specified type.
Definition: AppDomain.hpp:332
static TApp & Create(void)
Creates the current AppDomain and application instances.
Definition: AppDomain.hpp:254
static AppDomain & GetCurrent(void)
Gets the current AppDomain instance.
Definition: AppDomain.hpp:242
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:299
TSingleton & GetSingleton(void)
Gets the singleton instance of the specified type.
Definition: AppDomain.hpp:322
static void Assign(AppDomain &other)
Assgins the current AppdDomain singleton from the as argument passed other.
Definition: AppDomain.hpp:279
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 class implements the singleton pattern.
Definition: Singleton.hxx:25
static void DisposeInstance(void)
Disposes this singleton instance.
Definition: Singleton.hxx:134
static bool IsCreated(void)
Determines if this singleton instance is create yet.
Definition: Singleton.hxx:111
static Instance & CreateInstance(Args &&... args)
Creates this singleton instance.
Definition: Singleton.hxx:97
static Instance & GetInstance(void)
Gets a reference of the singleton instance.
Definition: Singleton.hxx:117
static void AssignInstanceFrom(Instance &other)
Assigns the singleton instance from another singleton instance of the same type.
Definition: Singleton.hxx:150
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
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API