ServiceDefinition.h
1/*
2 Copyright (C) 2009-2026 by Marc Duerner
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 As a special exception, you may use this file as part of a free
10 software library without restriction. Specifically, if other files
11 instantiate templates or use macros or inline functions from this
12 file, or you compile this file and link it with other files to
13 produce an executable, this file does not by itself cause the
14 resulting executable to be covered by the GNU General Public
15 License. This exception does not however invalidate any other
16 reasons why the executable file might be covered by the GNU Library
17 General Public License.
18
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, write to the:
26 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301 USA
28*/
29
30#ifndef PT_REMOTING_SERVICEDEFINITION_H
31#define PT_REMOTING_SERVICEDEFINITION_H
32
33#include <Pt/Remoting/Api.h>
34#include <Pt/Remoting/ServiceProcedure.h>
35#include <Pt/System/Mutex.h>
36#include <Pt/NonCopyable.h>
37#include <Pt/Types.h>
38#include <string>
39#include <map>
40
41namespace Pt {
42
43namespace Remoting {
44
45class Responder;
46
58class PT_REMOTING_API ServiceDefinition : private NonCopyable
59{
60 public:
64
68
70 ServiceProcedure* getProcedure(const std::string& name, Responder& resp);
71
73 void releaseProcedure(ServiceProcedure* proc);
74
82 template <typename R, typename... As>
83 void registerProcedure(const std::string& name, R (*func)(As...))
84 {
85 ServiceProcedureDef* proc = new BasicProcedureDef<R, As...>(Pt::callable(func));
86 this->registerProcedure(name, proc);
87 }
88
96 template <typename R, class C, typename... As>
97 void registerProcedure(const std::string& name, C& obj, R (C::*mth)(As...) )
98 {
99 ServiceProcedureDef* proc = new BasicProcedureDef<R, As...>( callable(obj, mth) );
100 this->registerProcedure(name, proc);
101 }
102
110 template <typename R, typename... As>
111 void registerProcedure(const std::string& name, const Callable<R, As...>& cb)
112 {
113 ServiceProcedureDef* proc = new BasicProcedureDef<R, As...>(cb);
114 this->registerProcedure(name, proc);
115 }
116
124 template <typename A, class C>
125 void registerActiveProcedure(const std::string& name, A* (*fn)(Responder&) )
126 {
127 ServiceProcedureDef* proc = new ActiveProcedureDef<A>( callable(fn) );
128 this->registerProcedure(name, proc);
129 }
130
139 template <typename A, class C>
140 void registerActiveProcedure(const std::string& name, C& obj, A* (C::*method)(Responder&) )
141 {
142 ServiceProcedureDef* proc = new ActiveProcedureDef<A>( callable(obj, method) );
143 this->registerProcedure(name, proc);
144 }
145
154 template <typename A, class C>
155 void registerActiveProcedure(const std::string& name, C& obj, A* (C::*method)(Responder&) const )
156 {
157 ServiceProcedureDef* proc = new ActiveProcedureDef<A>( callable(obj, method) );
158 this->registerProcedure(name, proc);
159 }
160
161 protected:
162 void registerProcedure(const std::string& name, ServiceProcedureDef* proc);
163
164 System::Mutex& mutex();
165
166 System::Mutex& mutex() const;
167
168 private:
169 typedef std::map<std::string, ServiceProcedureDef*> ProcedureMap;
170 ProcedureMap _procedures;
171 mutable System::Mutex _mtx;
172 Pt::varint_t _r1;
173 Pt::varint_t _r2;
174};
175
176} // namespace Remoting
177
178} // namespace Pt
179
180#endif // PT_REMOTING_SERVICEDEFINITION_H
An interface for all callable entities.
Definition Callable.h:48
ConstMethod< R, ClassT, As... > callable(ClassT &object, R(BaseT::*method)(As...) const)
Returns a ConstMethod object for the given object/method pair.
Definition ConstMethod.h:161
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Definition of an asynchronous service procedure.
Definition ActiveProcedure.h:157
Dispatches requests to a service procedure.
Definition Responder.h:50
void registerActiveProcedure(const std::string &name, C &obj, A *(C::*method)(Responder &))
Registers a member function as an asynchronous procedure.
Definition ServiceDefinition.h:140
void registerProcedure(const std::string &name, const Callable< R, As... > &cb)
Registers a generic callable as a synchronous procedure.
Definition ServiceDefinition.h:111
void registerActiveProcedure(const std::string &name, C &obj, A *(C::*method)(Responder &) const)
Registers a const member function as an asynchronous procedure.
Definition ServiceDefinition.h:155
void registerProcedure(const std::string &name, R(*func)(As...))
Registers a function as a synchronous procedure.
Definition ServiceDefinition.h:83
void registerProcedure(const std::string &name, C &obj, R(C::*mth)(As...))
Registers a member function as a synchronous procedure.
Definition ServiceDefinition.h:97
void registerActiveProcedure(const std::string &name, A *(*fn)(Responder &))
Registers a function as an asynchronous procedure.
Definition ServiceDefinition.h:125
virtual ~ServiceDefinition()
Destructor.
Service procedure.
Definition ServiceProcedure.h:46
Mutual exclusion device.
Definition Mutex.h:73
Remote services and clients.
Definition HttpService.h:39
Core module.
Definition Allocator.h:33