AsyncCall.h
1/* Copyright (C) 2020-2026 by Marc Boris Duerner
2 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
3*/
4
5#ifndef PT_LUA_ASYNCCALL_H
6#define PT_LUA_ASYNCCALL_H
7
8#include <Pt/Lua/Api.h>
9#include <Pt/Signal.h>
10#include <Pt/Reflex/TypeManager.h>
11#include <Pt/Reflex/Argument.h>
12#include <Pt/Reflex/ArgumentTraits.h>
13#include <Pt/System/EventLoop.h>
14#include <Pt/Any.h>
15
16#include <exception>
17#include <string>
18
19namespace Pt {
20
21namespace Lua {
22
41class PT_LUA_API AsyncCall
42{
43 public:
47
50 virtual ~AsyncCall();
51
54 void bind(Pt::Reflex::TypeManager& tm);
55
58 Pt::Reflex::Type* rtype() const;
59
63
67
71
74 void cancel();
75
78 bool hasError() const;
79
82 const std::string& errorMessage() const;
83
84 protected:
87 void setReady();
88
91 void setError(const std::string& msg);
92
93 private:
94 virtual Pt::Reflex::Type* onBind(Pt::Reflex::TypeManager& tm) = 0;
95
96 virtual void onBeginCall(Pt::System::EventLoop& loop) = 0;
97
98 virtual Pt::Any onGetResult() = 0;
99
100 virtual void onCancel() = 0;
101
102 private:
103 Pt::Signal<> _finished;
104 bool _hasError;
105 std::string _errorMsg;
106 Pt::Reflex::Type* _rtype;
107};
108
109
119template <typename R>
121{
122 protected:
123 virtual Pt::Reflex::Type* onBind(Pt::Reflex::TypeManager& tm) override
124 {
125 return tm.getType( typeid(R) );
126 }
127
130 virtual R onResult() = 0;
131
132 private:
133 Pt::Any onGetResult() override
134 {
135 R r = onResult();
136 return Pt::Any(r);
137 }
138};
139
140
149template <>
150class BasicAsyncCall<void> : public AsyncCall
151{
152 protected:
153 virtual Pt::Reflex::Type* onBind(Pt::Reflex::TypeManager& tm) override
154 {
155 return tm.getType( typeid(void) );
156 }
157
158 private:
159 Pt::Any onGetResult() override
160 { return Pt::Any(); }
161};
162
163} // namespace
164
165} // namespace
166
167#endif // PT_LUA_ASYNCCALL_H
Value of any copyable type.
Definition Any.h:79
void cancel()
Cancels the call.
virtual ~AsyncCall()
Destroys the call.
Pt::Signal & finished()
Signal emitted when the call is ready or has failed.
void bind(Pt::Reflex::TypeManager &tm)
Binds the result type from tm.
const std::string & errorMessage() const
Returns the stored error text.
Pt::Reflex::Type * rtype() const
Returns the Reflex result type, or a null pointer before bind.
void setError(const std::string &msg)
Stores msg as the error of this call.
bool hasError() const
Returns true when the call stored an error.
void setReady()
Emits finished() to mark the call ready.
void beginCall(Pt::System::EventLoop &loop)
Starts the call on loop.
Pt::Any getResult()
Returns the result after the call has finished.
AsyncCall()
Creates an idle asynchronous call.
Asynchronous native call with result type R.
Definition AsyncCall.h:121
virtual R onResult()=0
Returns the result of the finished call.
Multicast Signal to call multiple slots.
Definition Signal.h:190
Event loop of a thread or process.
Definition EventLoop.h:83
Lua runtime and reflected bindings.
Core module.
Definition Allocator.h:33