30 #ifndef PT_REMOTING_REMOTEPROCEDURE_H
31 #define PT_REMOTING_REMOTEPROCEDURE_H
33 #include <Pt/Remoting/Api.h>
34 #include <Pt/Remoting/Client.h>
35 #include <Pt/SerializationContext.h>
36 #include <Pt/Composer.h>
37 #include <Pt/Decomposer.h>
38 #include <Pt/NonCopyable.h>
39 #include <Pt/Signal.h>
40 #include <Pt/String.h>
53 RemoteCall(Client& client,
const String& name);
55 RemoteCall(Client& client,
const std::string& name);
57 RemoteCall(Client& client,
const char* name);
59 virtual ~RemoteCall();
68 const String& name()
const
74 {
return _client->isFailed(); }
91 virtual void onReady() = 0;
95 virtual void onReset() = 0;
99 virtual void onClear() = 0;
109 template <
typename R>
119 _result =
new (_mem) R;
127 void init(RemoteCall* call)
136 return _call->isFailed();
160 _result =
new (_mem) R;
165 char _mem[
sizeof(R) ];
172 template <
typename R>
173 class RemoteProcedureBase :
public RemoteCall
176 RemoteProcedureBase(Client& client,
const std::string& name)
177 : RemoteCall(client, name)
184 ~RemoteProcedureBase()
187 _r->~BasicComposer<R>();
199 const Result<R>& result()
const
206 Signal< const Result<R>& >& finished()
207 {
return _finished; }
211 { _finished.send(_result); }
213 BasicComposer<R>& beginResult()
217 _r->~BasicComposer<R>();
221 _r =
new (_mem) BasicComposer<R>( &client().context() );
222 _r->begin( result().value() );
226 virtual void onReset()
230 _r->~BasicComposer<R>();
235 virtual void onClear()
242 Signal< const Result<R>& > _finished;
244 char _mem[
sizeof(BasicComposer<R>) ];
245 BasicComposer<R>* _r;
249 template <
typename A>
253 RemoteArgument(SerializationContext* )
260 _decomposer->~BasicDecomposer<A>();
263 void begin(
const A& a,
const char* name, SerializationContext& ctx)
267 _decomposer->~BasicDecomposer<A>();
271 _decomposer =
new (_mem) BasicDecomposer<A>(&ctx);
272 _decomposer->begin(a, name);
275 void clear(SerializationContext* )
279 _decomposer->~BasicDecomposer<A>();
285 BasicDecomposer<A>* decomposer()
286 {
return reinterpret_cast<BasicDecomposer<A>*
>(_mem); }
289 char _mem[
sizeof(BasicDecomposer<A>) ];
290 BasicDecomposer<A>* _decomposer;
294 template <
typename... Ts>
295 class RemoteArguments;
299 class RemoteArguments<>
302 explicit RemoteArguments(SerializationContext* )
305 void begin(SerializationContext& )
308 void fill(std::size_t i, Decomposer** args)
313 void clear(SerializationContext* )
318 template <
typename T,
typename... Ts>
319 class RemoteArguments<T, Ts...>
322 explicit RemoteArguments(SerializationContext* ctx)
327 void begin(SerializationContext& ctx,
const T& a,
const Ts&... as)
329 _head.begin(a,
"", ctx);
330 _tail.begin(ctx, as...);
333 void fill(std::size_t i, Decomposer** args)
335 args[i] = _head.decomposer();
336 _tail.fill(i + 1, args);
339 void clear(SerializationContext* ctx)
346 RemoteArgument<T> _head;
347 RemoteArguments<Ts...> _tail;
372 template <
typename R,
typename... As>
379 : RemoteProcedureBase<R>(client, name)
380 , _argv( &client.context() )
382 _argv.fill(0, _args);
396 _argv.begin(this->client().context(), args...);
399 this->client().beginCall(r, *
this, _args,
sizeof...(As));
410 const R&
call(
const As&... args)
414 _argv.begin(this->client().context(), args...);
417 this->client().call(r, *
this, _args,
sizeof...(As));
426 return this->result().value();
433 return this->
call(args...);
439 RemoteProcedureBase<R>::onReset();
440 _argv.clear( &this->client().context() );
444 RemoteArguments<As...> _argv;
452 #endif // PT_REMOTING_REMOTEPROCEDURE_H