BasicProcedure.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_BASICPROCEDURE_H
31#define PT_REMOTING_BASICPROCEDURE_H
32
33#include <Pt/Remoting/Api.h>
34#include <Pt/Remoting/Arguments.h>
35#include <Pt/Remoting/ServiceProcedure.h>
36#include <Pt/System/EventLoop.h>
37#include <Pt/Decomposer.h>
38#include <Pt/Composer.h>
39#include <Pt/TypeTraits.h>
40#include <cstddef>
41#include <utility>
42
43namespace Pt {
44
45namespace Remoting {
46
47class Responder;
48
49
52template <typename R, typename... As>
53class BasicProcedure : public ServiceProcedure
54{
55 public:
56 BasicProcedure(const Callable<R, As...>& cb, Responder& resp)
57 : ServiceProcedure(resp)
58 , _cb(cb.clone())
59 , _r(&resp.context())
60 , _argv(&resp.context())
61 {
62 _argv.fill(0, _args);
63 }
64
65 ~BasicProcedure()
66 {
67 delete _cb;
68 }
69
70 protected:
71 Composer** onBeginArgs()
72 {
73 _argv.begin();
74 return _args;
75 }
76
77 virtual void onBeginCall(System::EventLoop&)
78 {
79 this->setReady();
80 }
81
82 Decomposer* onEndCall()
83 {
84 _rv = callWith(std::index_sequence_for<As...>());
85 _r.begin(_rv, "");
86 return &_r;
87 }
88
89 protected:
90 template <std::size_t... Is>
91 R callWith(std::index_sequence<Is...>)
92 {
93 return _cb->call(get<Is>(_argv)...);
94 }
95
96 private:
97 template <typename T>
98 using ValueType = typename TypeTraits<T>::Value;
99
100 Callable<R, As...>* _cb;
101
102 ValueType<R> _rv;
104
105 Arguments<ValueType<As>...> _argv;
106 Composer* _args[sizeof...(As) + 1];
107};
108
109
110template <typename R, typename... As>
111class BasicProcedureDef : public ServiceProcedureDef
112{
113 public:
114 BasicProcedureDef(const Callable<R, As...>& cb)
115 : _cb(0)
116 {
117 _cb = cb.clone();
118 }
119
120 ~BasicProcedureDef()
121 {
122 delete _cb;
123 }
124
125 protected:
126 virtual ServiceProcedure* onCreateProcedure(Responder& resp) const
127 {
128 return new BasicProcedure<R, As...>(*_cb, resp);
129 }
130
131 private:
132 Callable<R, As...>* _cb;
133};
134
135} // namespace Remoting
136
137} // namespace Pt
138
139#endif // PT_REMOTING_BASICPROCEDURE_H
Manages the decomposition of types during serialization.
Definition Decomposer.h:185
An interface for all callable entities.
Definition Callable.h:48
virtual Callable * clone() const =0
Returns a copy of this instance.
Composes types during serialization.
Definition Composer.h:101
Manages the decomposition of types during serialization.
Definition Decomposer.h:121
Dispatches requests to a service procedure.
Definition Responder.h:50
void setReady()
Indicates that the procedure has finished.
Definition ServiceProcedure.h:55
ServiceProcedure(Responder &r)
Constructor.
Definition ServiceProcedure.h:77
Event loop of a thread or process.
Definition EventLoop.h:83
Remote services and clients.
Definition HttpService.h:39
Core module.
Definition Allocator.h:33
IMPLEMENTATION_DEFINED Value
The derived value type.
Definition Api-TypeTraits.h:40