ActiveProcedure.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_ACTIVEPROCEDURE_H
31 #define PT_REMOTING_ACTIVEPROCEDURE_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 
43 namespace Pt {
44 
45 namespace Remoting {
46 
47 class Responder;
48 
68 template <typename R, typename... As>
70 {
71  public:
75  : ServiceProcedure(resp)
76  , _r(&resp.context())
77  , _argv(&resp.context())
78  {
79  _argv.fill(0, _args);
80  }
81 
84  virtual ~ActiveProcedure()
85  {}
86 
89  template <std::size_t I>
90  auto& arg()
91  {
92  return get<I>(_argv);
93  }
94 
97  template <std::size_t I>
98  const auto& arg() const
99  {
100  return get<I>(_argv);
101  }
102 
103  protected:
104  // inherit docs
105  Composer** onBeginArgs()
106  {
107  _argv.begin();
108  return _args;
109  }
110 
111  // inherit docs
112  virtual void onBeginCall(System::EventLoop& loop)
113  {
114  invokeWith(loop, std::index_sequence_for<As...>());
115  }
116 
117  // inherit docs
118  Decomposer* onEndCall()
119  {
120  const R& r = onResult();
121  _r.begin(r, "");
122  return &_r;
123  }
124 
125  protected:
128  virtual void onInvoke(System::EventLoop& loop, const As&... args) = 0;
129 
132  virtual const R& onResult() = 0;
133 
134  protected:
135  template <std::size_t... Is>
136  void invokeWith(System::EventLoop& loop, std::index_sequence<Is...>)
137  {
138  onInvoke(loop, get<Is>(_argv)...);
139  }
140 
141  private:
142  template <typename T>
143  using ValueType = typename TypeTraits<T>::Value;
144 
146  Arguments<ValueType<As>...> _argv;
147  Composer* _args[sizeof...(As) + 1];
148 };
149 
150 
155 template <typename CallT>
156 class ActiveProcedureDef : public ServiceProcedureDef
157 {
158  public:
162  : _cb(0)
163  {
164  _cb = cb.clone();
165  }
166 
170  {
171  delete _cb;
172  }
173 
174  protected:
175  virtual ServiceProcedure* onCreateProcedure(Responder& resp) const
176  {
177  return _cb->call(resp);
178  }
179 
180  private:
181  const Callable<CallT*, Responder&>* _cb;
182 };
183 
184 } // namespace Remoting
185 
186 } // namespace Pt
187 
188 #endif // PT_REMOTING_ACTIVEPROCEDURE_H
Core module.
Definition: Allocator.h:33
auto & arg()
Access argument value by index.
Definition: ActiveProcedure.h:90
An interface for all callable entities.
Definition: Callable.h:48
Manages the decomposition of types during serialization.
Definition: Decomposer.h:108
void begin(const T &type, const char *name)
Begin decomposing a type.
Definition: Decomposer.h:123
IMPLEMENTATION_DEFINED Value
The derived value type.
Definition: Api-TypeTraits.h:22
ActiveProcedure(Responder &resp)
Constructs with Responder.
Definition: ActiveProcedure.h:74
Dispatches requests to a service procedure.
Definition: Responder.h:50
const auto & arg() const
Access argument value by index.
Definition: ActiveProcedure.h:98
Service procedure.
Definition: ServiceProcedure.h:46
virtual const R & onResult()=0
Return result of the asynchronous procedure.
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:83
Composes types during serialization.
Definition: Composer.h:43
Definition of an asynchronous service procedure.
Definition: ActiveProcedure.h:157
virtual ~ActiveProcedure()
Destructor.
Definition: ActiveProcedure.h:84
virtual void onInvoke(System::EventLoop &loop, const As &... args)=0
Start an asynchronous procedure.
~ActiveProcedureDef()
Destructor.
Definition: ActiveProcedure.h:169
virtual Callable * clone() const =0
Returns a copy of this instance.
ActiveProcedureDef(const Callable< CallT *, Responder & > &cb)
Constructs with a factory callback.
Definition: ActiveProcedure.h:161
Asynchronous service procedure.
Definition: ActiveProcedure.h:70