nngn
Loading...
Searching...
No Matches
value.h
Go to the documentation of this file.
1
5#ifndef NNGN_LUA_VALUE_H
6#define NNGN_LUA_VALUE_H
7
8#include <lua.hpp>
9
10#include "utils/utils.h"
11
12#include "lua.h"
13#include "state.h"
14
15namespace nngn::lua {
16
19public:
21 value_view(lua_State *L, int i) : value_view{state_view{L}, i} {}
23 ~value_view(void) = default;
24 state_view state(void) const { return this->m_state; }
25 /* Disowns the current reference and returns a non-owning view to it. */
26 state_view release(void) { return this->m_state.release(); }
28 int index(void) const { return this->m_index; }
30 type get_type(void) const;
32 bool is_nil(void) const { return lua_isnil(this->state(), this->index()); }
34 auto to_string(void) const;
35 // Stack API
36 void get(lua_State *L, int i) { *this = value_view{L, i}; }
37 template<typename T> requires(detail::can_get<T>) T get(void) const;
38 int push(void) const;
39 template<typename R = value_view> R push(void) const;
40 template<typename T>
41 explicit operator T(void) const { return this->get<T>(); }
42private:
44 int m_index = 0;
45};
46
48struct value : value_view {
52 value(value &&rhs) noexcept : value_view{rhs.release()} {}
53 value &operator=(value &&rhs) noexcept;
54 ~value(void) { if(this->state()) this->state().remove(this->index()); }
55 /* Disowns the current reference and returns a non-owning view to it. */
56 value_view release(void) { return {value_view::release(), this->index()}; }
57 using value_view::push;
58 template<typename R = value>
59 R push(void) const { return value_view::push<R>(); }
60 value remove(void) { return std::move(*this); }
61};
62
63inline type value_view::get_type(void) const {
64 return static_cast<type>(lua_type(this->state(), this->index()));
65}
66
67inline auto value_view::to_string(void) const {
68 return this->state().to_string(this->index());
69}
70
71inline int value_view::push(void) const {
72 lua_pushvalue(this->state(), this->index());
73 return 1;
74}
75
76template<typename T>
77requires(detail::can_get<T>)
78T value_view::get(void) const {
79 return this->state().get<T>(this->index());
80}
81
82template<typename R>
83R value_view::push(void) const {
84 lua_pushvalue(this->state(), this->index());
85 return {this->state(), lua_gettop(this->state())};
86}
87
88inline value &value::operator=(value &&rhs) noexcept {
89 this->remove();
90 static_cast<value_view&>(*this) = rhs.release();
91 return *this;
92}
93
94inline std::pair<value, std::string_view> state_view::to_string(int i) const {
95 std::size_t n = 0;
96 const char *s = luaL_tolstring(*this, i, &n);
97 return {
98 value{*this, lua_gettop(*this)},
99 std::string_view{s, n},
100 };
101}
102
103template<typename T>
104bool operator==(T &&lhs, const value_view &rhs) {
105 return FWD(lhs) == rhs.state().get<std::decay_t<T>>(rhs.index());
106}
107
108template<typename T>
109bool operator==(const value_view &lhs, T &&rhs) {
110 return lhs.state().get<std::decay_t<T>>(lhs.index()) == FWD(rhs);
111}
112
113template<std::derived_from<value> T>
114inline constexpr bool is_stack_ref<T> = true;
115
116static_assert(detail::stack_type<value_view>);
117static_assert(detail::stack_type<value>);
118
119}
120
121#endif
Definition: state.h:88
T get(int i) const
Reads a value from the stack.
Definition: state.h:230
state_view release(void)
Definition: state.h:105
std::pair< value, std::string_view > to_string(int i) const
Pushes a string representation of a value onto the stack.
Definition: value.h:94
void remove(int i) const
Definition: state.h:152
Base, non-owning generic stack value reference.
Definition: value.h:18
type get_type(void) const
Type of value at index i.
Definition: value.h:63
T get(void) const
Definition: value.h:78
void get(lua_State *L, int i)
Definition: value.h:36
state_view m_state
Definition: value.h:43
value_view(lua_State *L, int i)
Definition: value.h:21
state_view release(void)
Definition: value.h:26
int push(void) const
Definition: value.h:71
~value_view(void)=default
int index(void) const
Lua stack index.
Definition: value.h:28
value_view(state_view l, int i)
Definition: value.h:22
bool is_nil(void) const
Definition: value.h:32
int m_index
Definition: value.h:44
auto to_string(void) const
Definition: value.h:67
state_view state(void) const
Definition: value.h:24
function g l
Definition: plot.lua:8
Definition: alloc.cpp:100
constexpr bool is_stack_ref< T >
Definition: table.h:452
type
LUA_T* constants as a scoped enumeration.
Definition: lua.h:77
operator==(T &&lhs, const table_proxy< PT, Ks... > &rhs)
Definition: table.h:346
v[1]
Definition: math.lua:19
lua_State wrappers.
Owning lua_State wrapper.
Definition: state.h:166
Owning stack value reference.
Definition: value.h:48
value remove(void)
Definition: value.h:60
R push(void) const
Definition: value.h:59
~value(void)
Definition: value.h:54
value_view release(void)
Definition: value.h:56
value(value &&rhs) noexcept
Definition: value.h:52
value(value_view v)
Definition: value.h:51
value & operator=(value &&rhs) noexcept
Definition: value.h:88
std::chrono::seconds s
Definition: timing.cpp:6
#define NNGN_DEFAULT_CONSTRUCT(x)
Definition: utils.h:20
#define NNGN_NO_COPY(x)
Definition: utils.h:35
#define FWD(...)
Definition: utils.h:18