PLCnext API Documentation 25.0.2.69
ServiceManager.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7#ifndef ARP_USE_ARP_SYSTEM_RSC
8#include "Arp/Base/Rsc/ServiceManager.hpp"
9namespace Arp::System::Rsc
10{
12}
13#else
14#include "Arp/System/Core/Arp.h"
15#include "Arp/Base/Rsc/Commons/Rsc.hpp"
16#include "Arp/Base/Rsc/Commons/IRscService.hpp"
17#include "Arp/Base/Rsc/Commons/Services/IRscServiceProxyFactory.hpp"
18
19// forwards
20namespace Arp::Base::Rsc::Commons::Services
21{
24}
25
26// forwards
27namespace CommonRemoting
28{
29class IService;
30class IServiceFactory;
31class IServiceProxyFactory;
32class CRemotingServiceProviderBase;
33}
34
35namespace Arp { namespace System { namespace Rsc
36{
37
38using namespace CommonRemoting;
39
42class ARP_EXPORT ARP_DEPRECATED("Use Arp::Base::Rsc::ServiceManager instead.") ServiceManager
43{
44public: // usings
45 using IRscService = Arp::Base::Rsc::Commons::IRscService;
48
49private: // deleted construction/destruction
51 ServiceManager(void) = delete;
53 ServiceManager(const ServiceManager& arg) = delete;
55 ServiceManager(ServiceManager&& arg)noexcept = delete;
57 ServiceManager& operator=(const ServiceManager& arg) = delete;
59 ServiceManager& operator=(ServiceManager&& arg)noexcept = delete;
61 ~ServiceManager(void) = delete;
62
63public: // static Rsc service operations
68 template<class TService, class TServiceFactory>
69 static void PublishService(void);
70
75 template<class TService, class TServiceFactory>
76 static void PublishService(const char* serviceProviderName);
77
82 template<class TService>
83 static typename TService::Ptr GetService(void);
84
91 template<class TService>
92 static typename TService::Ptr GetService(const char* serviceProviderName);
93
98 template<class TService>
99 static bool TryGetService(typename TService::Ptr& result);
100
106 template<class TService>
107 static bool TryGetService(const char* serviceProviderName, typename TService::Ptr& result);
108
109public: // static Remoting service operations
114 static void PublishComponentService(const char* componentName, const char* serviceName, IRscServiceFactory* pServiceFactory);
115
119 static void PublishRemotingServiceProvider(CRemotingServiceProviderBase* pServiceProvider);
120
126 template<class TService, class TServiceFactory>
127 static void PublishRemotingService(const char* serviceProviderName);
128
134 template<class TService>
135 static typename TService::Ptr GetRemotingService(const char* serviceProviderName);
136
137private: // Rsc service methods
138 static void PublishService(const char* serviceProviderName, const char* serviceName, IRscServiceFactory& serviceFactory);
139 static void PublishService(const char* serviceProviderName, const char* serviceName, IServiceFactory& serviceFactory);
140 static IRscService::Ptr GetService(const char* serviceProviderName, IRscServiceProxyFactory& proxyFactory);
141 static IService* GetService(const char* serviceProviderName, IServiceProxyFactory& proxyFactory);
142};
143
145// inline methods of class ServiceManager
146
147ARP_DEPRECATED_IGNORE_BEGIN
148
149template<class TService, class TServiceFactory>
150inline void ServiceManager::PublishService(void)
151{
152 String serviceProviderName = TServiceFactory::GetInstance().GetServiceProviderName();
153 ServiceManager::PublishService(serviceProviderName, TService::GetProxyFactory().GetServiceName(), TServiceFactory::GetInstance());
154}
155
156template<class TService, class TServiceFactory>
157inline void ServiceManager::PublishService(const char* serviceProviderName)
158{
159 ServiceManager::PublishService(serviceProviderName, TService::GetProxyFactory().GetServiceName(), TServiceFactory::GetInstance());
160}
161
162template<class TService, class TServiceFactory>
163inline void ServiceManager::PublishRemotingService(const char* serviceProviderName)
164{
165 ServiceManager::PublishService(serviceProviderName, TService::GetProxyFactory().GetServiceName(), TServiceFactory::GetInstance());
166}
167
168template<class TService>
169inline typename TService::Ptr ServiceManager::GetService()
170{
171 String serviceProviderName = TService::GetProxyFactory().GetServiceProviderName();
172 if (serviceProviderName.IsEmpty())
173 {
174 throw ArgumentException::Create("TService", TService::GetProxyFactory().GetServiceName(), "Service does not specify a service provider");
175 }
176 typename TService::Ptr result;
177 if (!ServiceManager::TryGetService<TService>(serviceProviderName, result))
178 {
179 throw InvalidOperationException("Could not retrieve service '{0}' from service provider '{1}'.", TypeName<TService>(), serviceProviderName);
180 }
181 return result;
182}
183
184template<class TService>
185inline typename TService::Ptr ServiceManager::GetService(const char* serviceProviderName)
186{
187 typename TService::Ptr result;
188 if (!ServiceManager::TryGetService<TService>(serviceProviderName, result))
189 {
190 throw InvalidOperationException("Could not retrieve service '{0}' from service provider '{1}'.", TypeName<TService>(), serviceProviderName);
191 }
192 return result;
193}
194
195template<class TService>
196inline bool ServiceManager::TryGetService(typename TService::Ptr& result)
197{
198 const String& serviceProviderName = TService::GetProxyFactory().GetServiceProviderName();
199 if (serviceProviderName.IsEmpty())
200 {
201 throw ArgumentException::Create("TService", TService::GetProxyFactory().GetServiceName(), "Service does not specify a service provider");
202 }
203 // else
204 return ServiceManager::TryGetService<TService>(serviceProviderName, result);
205}
206
207template<class TService>
208inline bool ServiceManager::TryGetService(const char* serviceProviderName, typename TService::Ptr& result)
209{
210 IRscService::Ptr servicePtr = ServiceManager::GetService(serviceProviderName, TService::GetProxyFactory());
211
212 if (servicePtr == nullptr)
213 {
214 return false; // not found
215 }
216 result = dynamic_pointer_cast<TService>(servicePtr);
217 if (result == nullptr)
218 {
219 // error: wrong type
220 throw InvalidCastException("The service does not implement service interface '{0}'.", TService::GetProxyFactory().GetServiceName());
221 }
222 // else
223 return true;
224}
225
226template<class TService>
227inline typename TService::Ptr ServiceManager::GetRemotingService(const char* serviceProviderName)
228{
229 IService* pService = ServiceManager::GetService(serviceProviderName, TService::GetProxyFactory());
230 if (pService == nullptr)
231 {
232 return typename TService::Ptr(); // not found
233 }
234 TService* pResult = dynamic_cast<TService*>(pService);
235 if (pResult == nullptr)
236 {
237 // error: wrong type
238 throw InvalidCastException("The service does not implement service interface '{0}'.", TService::GetProxyFactory().GetServiceName());
239 }
240 // else
241 return typename TService::Ptr(pResult);
242}
243
244ARP_DEPRECATED_IGNORE_END
245
246}}} // end of namespace Arp::System::Rsc
247
248#endif // ndef ARP_USE_ARP_SYSTEM_RSC
static ArgumentException Create(const char *paramName, const T &paramValue)
Creates an ArgumentException using a default message.
Definition: ArgumentException.hpp:92
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:31
static void PublishService(void)
Registers a RSC service.
Definition: ServiceManager.hpp:139
static bool TryGetService(typename TService::Ptr &result)
Tries to get a service of the service provider Arp.
Definition: ServiceManager.hpp:101
static TService::Ptr GetService(void)
Gets a RSC service. The service provider is determined by the proxy factory.
Definition: ServiceManager.hpp:65
Namespace for the Remote Service Call implementation
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