Call.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_CALL_H
6#define PT_LUA_CALL_H
7
8#include <Pt/Lua/Api.h>
9#include <Pt/Reflex/MethodInfo.h>
10#include <Pt/Reflex/PropertyInfo.h>
11#include <Pt/Reflex/ConstructorInfo.h>
12#include <Pt/Reflex/Argument.h>
13#include <Pt/Any.h>
14
15#include <string>
16#include <vector>
17
18namespace Pt {
19
20namespace Lua {
21
37class Call
38{
39 public:
42 virtual ~Call()
43 {}
44
47 virtual Pt::Any call() = 0;
48
51 virtual Pt::Reflex::Type* rtype() const = 0;
52
55 bool hasError() const
56 { return ! _errorMsg.empty(); }
57
60 const std::string& errorMessage() const
61 { return _errorMsg; }
62
63 protected:
64 Call() = default;
65
66 Call(const Call&) = delete;
67
68 Call& operator=(const Call&) = delete;
69
72 void setError(const std::string& msg)
73 { _errorMsg = msg; }
74
75 private:
76 std::string _errorMsg;
77};
78
79
84class MethodCall : public Call
85{
86 public:
89 MethodCall(Pt::Reflex::MethodInfo* mi, void* self,
90 std::vector<Pt::Reflex::Argument> args)
91 : _mi(mi)
92 , _self(self)
93 , _args(args)
94 {}
95
96 Pt::Any call() override
97 {
98 try
99 {
100 Pt::Reflex::ArgumentList arglist(
101 _args.empty() ? static_cast<Pt::Reflex::Argument*>(0) : &_args[0],
102 _args.size());
103 return _mi->call(_self, arglist);
104 }
105 catch(const std::exception& e) { setError(e.what()); }
106
107 return Pt::Any();
108 }
109
110 Pt::Reflex::Type* rtype() const override
111 { return &_mi->rtype(); }
112
113 private:
114 Pt::Reflex::MethodInfo* _mi;
115 void* _self;
116 std::vector<Pt::Reflex::Argument> _args;
117};
118
119
124class PropertyGetCall : public Call
125{
126 public:
129 PropertyGetCall(Pt::Reflex::PropertyInfo* pi, void* self)
130 : _pi(pi)
131 , _self(self)
132 {}
133
134 Pt::Any call() override
135 {
136 try { return _pi->get(_self); }
137 catch(const std::exception& e) { setError(e.what()); }
138
139 return Pt::Any();
140 }
141
142 Pt::Reflex::Type* rtype() const override
143 { return &_pi->type(); }
144
145 private:
146 Pt::Reflex::PropertyInfo* _pi;
147 void* _self;
148};
149
150
155class PropertySetCall : public Call
156{
157 public:
160 PropertySetCall(Pt::Reflex::PropertyInfo* pi, void* self,
161 Pt::Reflex::Argument value)
162 : _pi(pi)
163 , _self(self)
164 , _value(value)
165 {}
166
167 Pt::Any call() override
168 {
169 try { _pi->set(_self, _value.toAny(), _value.type()); }
170 catch(const std::exception& e) { setError(e.what()); }
171
172 return Pt::Any();
173 }
174
175 Pt::Reflex::Type* rtype() const override
176 { return 0; }
177
178 private:
179 Pt::Reflex::PropertyInfo* _pi;
180 void* _self;
181 Pt::Reflex::Argument _value;
182};
183
184
189class ConstructorCall : public Call
190{
191 public:
194 ConstructorCall(Pt::Reflex::ConstructorInfo* ci, void* instance,
195 std::vector<Pt::Reflex::Argument> args)
196 : _ci(ci)
197 , _instance(instance)
198 , _args(args)
199 {}
200
201 Pt::Any call() override
202 {
203 try
204 {
205 Pt::Reflex::ArgumentList arglist(
206 _args.empty() ? static_cast<Pt::Reflex::Argument*>(0) : &_args[0],
207 _args.size());
208 _ci->call(_instance, arglist);
209 }
210 catch(const std::exception& e) { setError(e.what()); }
211
212 return Pt::Any();
213 }
214
215 Pt::Reflex::Type* rtype() const override
216 { return 0; }
217
218 private:
219 Pt::Reflex::ConstructorInfo* _ci;
220 void* _instance;
221 std::vector<Pt::Reflex::Argument> _args;
222};
223
224} // namespace
225
226} // namespace
227
228#endif // PT_LUA_CALL_H
Value of any copyable type.
Definition Any.h:79
Synchronous native call from Lua.
Definition Call.h:38
virtual Pt::Reflex::Type * rtype() const =0
Returns the Reflex result type, or a null pointer.
virtual ~Call()
Destroys the call.
Definition Call.h:42
const std::string & errorMessage() const
Returns the stored error text.
Definition Call.h:60
void setError(const std::string &msg)
Stores msg as the error of this call.
Definition Call.h:72
virtual Pt::Any call()=0
Performs the invocation and returns the result.
bool hasError() const
Returns true when the invocation stored an error.
Definition Call.h:55
ConstructorCall(Pt::Reflex::ConstructorInfo *ci, void *instance, std::vector< Pt::Reflex::Argument > args)
Creates a construction of instance with ci and args.
Definition Call.h:194
Pt::Reflex::Type * rtype() const override
Returns the Reflex result type, or a null pointer.
Definition Call.h:215
Pt::Any call() override
Performs the invocation and returns the result.
Definition Call.h:201
Pt::Reflex::Type * rtype() const override
Returns the Reflex result type, or a null pointer.
Definition Call.h:110
MethodCall(Pt::Reflex::MethodInfo *mi, void *self, std::vector< Pt::Reflex::Argument > args)
Creates a call of mi on self with args.
Definition Call.h:89
Pt::Any call() override
Performs the invocation and returns the result.
Definition Call.h:96
Pt::Reflex::Type * rtype() const override
Returns the Reflex result type, or a null pointer.
Definition Call.h:142
PropertyGetCall(Pt::Reflex::PropertyInfo *pi, void *self)
Creates a read of pi on self.
Definition Call.h:129
Pt::Any call() override
Performs the invocation and returns the result.
Definition Call.h:134
Pt::Reflex::Type * rtype() const override
Returns the Reflex result type, or a null pointer.
Definition Call.h:175
PropertySetCall(Pt::Reflex::PropertyInfo *pi, void *self, Pt::Reflex::Argument value)
Creates a write of value to pi on self.
Definition Call.h:160
Pt::Any call() override
Performs the invocation and returns the result.
Definition Call.h:167
Lua runtime and reflected bindings.
Core module.
Definition Allocator.h:33
T pi()
The constant pi.