Native Calls

Native work Lua invokes in C++. More...

Classes

class  AsyncCall
 Asynchronous native call from Lua. More...
class  BasicAsyncCall< R >
 Asynchronous native call with result type R. More...
class  BasicAsyncCall< void >
 Asynchronous native call with no result. More...
class  Call
 Synchronous native call from Lua. More...
class  MethodCall
 Reflected method invocation from Lua. More...
class  PropertyGetCall
 Reflected property read from Lua. More...
class  PropertySetCall
 Reflected property write from Lua. More...
class  ConstructorCall
 Reflected constructor invocation from Lua. More...

Typedefs

template<typename... As>
using AsyncFunction = Pt::Reflex::BasicFunction<AsyncCall*, As...>
 Reflex function that returns an asynchronous Lua call.

Detailed Description

Lua reaches C++ through the Reflex catalog the context bound. A method, property, or constructor becomes a Call. The script yields, reports NativeCall, runs that call, and pushes the result back to Lua. The script owns the Call. Application code does not construct MethodCall, PropertyGetCall, PropertySetCall, or ConstructorCall.

A function or method whose return type is AsyncCall* is an asynchronous native call. Lua invokes it, the function returns a heap AsyncCall, and the script takes ownership. The script must be attached to an EventLoop. It binds the call, starts it with the loop, and resumes when finished() is emitted. Blocking advance() throws if such a call is pending.

Implement async work by subclassing BasicAsyncCall. For a result type R, override onBeginCall() to start work on the loop, onResult() to produce R, and onCancel() to abort. For void, there is no onResult(). Call setReady() when the work is done, or setError() if it failed. AsyncFunction is a Reflex function helper whose return type is already AsyncCall*.

The example is a timer wait registered as a Lua global. The script must use beginAdvance() on an event loop.

class WaitCall : public Pt::Lua::BasicAsyncCall<void>
, public Pt::Connectable
{
public:
explicit WaitCall(int ms)
: _ms(ms)
{}
private:
void onBeginCall(Pt::System::EventLoop& loop) override
{
_timer.setActive(loop);
_timer.timeout() += Pt::slot(*this, &WaitCall::onTimeout);
_timer.start(static_cast<std::size_t>(_ms));
}
void onCancel() override
{ _timer.stop(); }
void onTimeout()
{
_timer.stop();
setReady();
}
int _ms;
Pt::System::Timer _timer;
};
Pt::Lua::AsyncCall* waitAsync(int ms)
{
return new WaitCall(ms);
}
tm.registerFunction("wait", &waitAsync);
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Asynchronous native call from Lua.
Definition AsyncCall.h:42
Asynchronous native call with result type R.
Definition AsyncCall.h:121
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition ConstMethod.h:172
Event loop of a thread or process.
Definition EventLoop.h:83

Typedef Documentation

◆ AsyncFunction

template<typename... As>
using AsyncFunction = Pt::Reflex::BasicFunction<AsyncCall*, As...>

Alias for a Pt::Reflex::BasicFunction whose result type is AsyncCall*. Register an instance, or a free function with that return type, so Lua can invoke asynchronous native work.