nngn
Loading...
Searching...
No Matches
function.h
Go to the documentation of this file.
1
64#ifndef NNGN_LUA_FUNCTION_H
65#define NNGN_LUA_FUNCTION_H
66
67#include <optional>
68#include <tuple>
69#include <utility>
70
71#include <lua.hpp>
72
73#include "utils/log.h"
74
75#include "lua.h"
76#include "push.h"
77#include "state.h"
78#include "utils.h"
79#include "value.h"
80
81namespace nngn::lua {
82
83namespace detail {
84
86template<typename CRTP>
89 void operator()(auto &&...args) const;
90};
91
92}
93
100};
101
106struct function_value : value, detail::function_base<function_value> {
108 using value::value;
109 ~function_value(void) = default;
110};
111
112namespace detail {
113
114template<typename CRTP>
115void function_base<CRTP>::operator()(auto &&...args) const {
117 const auto &crtp = static_cast<const CRTP&>(*this);
118 crtp.state().call(crtp, FWD(args)...);
119}
120
121}
122
124template<typename R, typename ...Args>
125R call(lua_State *L, R(*f)(Args...), int i) {
126 return std::apply(f, stack_get<std::tuple<Args...>>::get(L, i));
127}
128
130template<typename R, typename T, typename ...Args>
131R call(lua_State *L, R(T::*f)(Args...), int i) {
132 return std::apply(f, stack_get<std::tuple<T&, Args...>>::get(L, i));
133}
134
136template<typename R, typename T, typename ...Args>
137R call(lua_State *L, R(T::*f)(Args...) const, int i) {
138 return std::apply(f, stack_get<std::tuple<const T&, Args...>>::get(L, i));
139}
140
142template<stateless_lambda T>
143decltype(auto) call(lua_State *L, T, int i) {
144 return call(L, +T{}, i);
145}
146
147static_assert(detail::stack_type<function_view>);
148static_assert(detail::stack_type<function_value>);
149
150}
151
152#endif
Base, non-owning generic stack value reference.
Definition: value.h:18
value_view(lua_State *L, int i)
Definition: value.h:21
#define NNGN_LOG_CONTEXT_CF(c)
Definition: log.h:11
Definition: alloc.cpp:100
R call(lua_State *L, R(*f)(Args...), int i)
Calls the regular function f with arguments taken from the stack.
Definition: function.h:125
Functions for pushing values onto the stack.
get
Definition: camera.lua:36
function f()) end
lua_State wrappers.
Reference to a function on the stack.
Definition: function.h:87
void operator()(auto &&...args) const
Pushes each argument onto the stack and calls the function.
Definition: function.h:115
Owning reference to a function on the stack.
Definition: function.h:106
~function_value(void)=default
Non-owning reference to a function on the stack.
Definition: function.h:98
Reads a value from the Lua stack.
Definition: lua.h:117
Owning stack value reference.
Definition: value.h:48
value(value_view v)
Definition: value.h:51
#define FWD(...)
Definition: utils.h:18
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39
Operations on generic stack values.