PLCnext API Documentation 26.0.1.58
ServiceManager.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/Base/Core/CommonTypeName.hxx"
9#include "Arp/Base/Rsc/Commons/Rsc.hpp"
10#include "Arp/Base/Rsc/Commons/IRscService.hpp"
11#include "Arp/Base/Commons/Exceptions/InvalidCastException.hpp"
12#include "Arp/Base/Commons/Exceptions/InvalidOperationException.hpp"
13
14// forwards
15namespace Arp::Base::Rsc::Commons::Services
16{
19}
20
21namespace Arp::Base::Rsc
22{
23
24// imports
28
31class ARP_EXPORT ServiceManager
32{
33public: // forwards
34 class Impl;
35
36public: // construction
37 ServiceManager(void) = delete;
38
39public: // static service operations
40 template<class TService> static typename TService::Ptr GetService(void);
41 template<class TService> static typename TService::Ptr GetService(const char* serviceProviderName);
42 template<class TService> static bool TryGetService(typename TService::Ptr& result);
43 template<class TService> static bool TryGetService(const char* serviceProviderName, typename TService::Ptr& result);
44
45 template<class TService, class TServiceFactory> static void PublishService(void);
46 template<class TService, class TServiceFactory> static void PublishService(const char* serviceProviderName);
47
48 static void PublishComponentService(const char* componentName, const char* serviceName, IRscServiceFactory* pServiceFactory);
49
50public: // internal operations
51 static Impl& GetImpl(void);
52
53private: // methods
54 static IRscService::Ptr GetServiceInternal(IRscServiceProxyFactory& proxyFactory, const char* serviceProviderName = nullptr);
55 static void PublishServiceInternal(const char* serviceProviderName, const char* serviceName, IRscServiceFactory& serviceFactory);
56};
57
59// inline methods of class ServiceManager
60
65template<class TService>
66inline typename TService::Ptr ServiceManager::GetService()
67{
68 typename TService::Ptr result;
69 if (!ServiceManager::TryGetService<TService>(result))
70 {
71 throw InvalidOperationException("Could not retrieve service '{0}'.", TypeName<TService>());
72 }
73 return result;
74}
75
86template<class TService>
87inline typename TService::Ptr ServiceManager::GetService(const char* serviceProviderName)
88{
89 typename TService::Ptr result;
90 if (!ServiceManager::TryGetService<TService>(serviceProviderName, result))
91 {
92 throw InvalidOperationException("Could not retrieve service '{0}' from service provider '{1}'.", TypeName<TService>(), serviceProviderName);
93 }
94 return result;
95}
96
101template<class TService>
102inline bool ServiceManager::TryGetService(typename TService::Ptr& result)
103{
104 return ServiceManager::TryGetService<TService>("", result);
105}
106
116template<class TService>
117inline bool ServiceManager::TryGetService(const char* serviceProviderName, typename TService::Ptr& result)
118{
119 IRscService::Ptr servicePtr = ServiceManager::GetServiceInternal(TService::GetProxyFactory(), serviceProviderName);
120
121 if (servicePtr == nullptr)
122 {
123 return false; // not found
124 }
125 result = dynamic_pointer_cast<TService>(servicePtr);
126 if (result == nullptr)
127 {
128 // error: wrong type
129 throw InvalidCastException("The retrieved service does not implement service interface '{0}'.", TypeName<TService>());
130 }
131 // else
132 return true;
133}
134
139template<class TService, class TServiceFactory>
141{
142 String serviceProviderName = TServiceFactory::GetInstance().GetServiceProviderName();
143 ServiceManager::PublishServiceInternal(serviceProviderName, CommonTypeName<TService>().GetFullName(), TServiceFactory::GetInstance());
144}
145
154template<class TService, class TServiceFactory>
155inline void ServiceManager::PublishService(const char* serviceProviderName)
156{
157 ServiceManager::PublishServiceInternal(serviceProviderName, CommonTypeName<TService>().GetFullName(), TServiceFactory::GetInstance());
158}
159
160} // end of namespace Arp::Base::Rsc
This exception is thrown when an invalid cast occurs.
Definition: InvalidCastException.hpp:17
This exception is thrown when an operation cannot be executed because the related state is invalid.
Definition: InvalidOperationException.hpp:16
This (meta programming) class provides the type-name according the CLS (common language specification...
Definition: CommonTypeName.hxx:33
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
This (meta programming) class provides the C++ type-name of the as template argument passed type.
Definition: TypeName.hxx:20
This is the base interface of all Rsc services.
Definition: IRscService.hpp:22
std::shared_ptr< IRscService > Ptr
The shared_ptr type of IRscService.
Definition: IRscService.hpp:25
Interface for service factory classes to create instances of the service implementation,...
Definition: IRscServiceFactory.hpp:26
Interface for service proxy factories to create service proxies used by RSC clients.
Definition: IRscServiceProxyFactory.hpp:22
This class is used to publish and retrieve RSC services.
Definition: ServiceManager.hpp:32
static void PublishService(void)
Registers a RSC service.
Definition: ServiceManager.hpp:140
static bool TryGetService(typename TService::Ptr &result)
Tries to get a service of the service provider Arp.
Definition: ServiceManager.hpp:102
static TService::Ptr GetService(void)
Gets a RSC service. The service provider is determined by the proxy factory.
Definition: ServiceManager.hpp:66
ServiceManager(void)=delete
makes this class pure static