8#ifndef NNGN_LUA_USER_DATA_H
9#define NNGN_LUA_USER_DATA_H
54 std::max(
alignof(
T),
alignof(
T*));
72 alloc_block<user_data_header<T>, detail::stored_type<T>>::storage
76 static constexpr auto meta =
78 std::remove_const_t<std::remove_pointer_t<T>>>;
103 static int gc(lua_State *L);
110 static int eq(lua_State *L);
137requires(std::same_as<void, std::decay_t<T>>)
139 static void *from_light(
const void *p);
144 if constexpr(std::is_pointer_v<T>)
145 return user_data<std::remove_pointer_t<T>>::get_pointer(lua,
i);
148 return p ? p->header.p :
nullptr;
160 return p ?
static_cast<const user_data*
>(p)->
get() :
nullptr;
165 if constexpr(std::is_pointer_v<T>)
176 if constexpr(is_pointer)
178 return stack_push<>::push(lua,
nil);
179 using UT = std::conditional_t<
181 new (lua.new_user_data<UT>()) UT{
FWD(
r)};
183 !std::same_as<
decltype(meta),
const empty>,
184 "meta table_name not defined for type");
185 auto mt = user_data_base::push_metatable(lua, meta);
186 lua_setmetatable(lua, -2);
193 const state_view lua = {L};
201 const auto [p0, p1] = state_view{L}.get<std::tuple<T*, T*>>(1);
202 return stack_push<>::push(L, p0 == p1);
207 : base_type{.header = {.p = &x}, .data = x} {}
210user_data<T>::user_data(
T &&x)
211 : base_type{.header = {.p = &this->
data}, .data =
FWD(x)} {}
215 auto *
const p = this->header.p;
216 auto *
const d = &this->
data;
217 if constexpr(std::same_as<empty, detail::stored_type<T>>)
218 assert(
static_cast<void*
>(p) !=
static_cast<void*
>(d));
static void * align_ptr(void *p, std::size_t a)
Definition math.h:570
Non-owning handle to an aggregate header and data block.
Definition block.h:31
Data block allocated for user data objects.
Definition user.h:73
static table push_metatable(state_view lua)
Pushes the type's meta table onto the stack.
user_data(const T &x)
Creates a non-owning (reference) object.
static get_type from_light(const void *p)
Retrieves a user data of type T from a light user data value.
void destroy(void)
Destroys the T value if this is an owning user data value.
get_type get(void) const
Pointer to the contained/referenced object.
Definition user.h:124
static get_type get_pointer(state_view lua, int i)
static int gc(lua_State *L)
Function to be used as a user data's __gc meta-method.
static constexpr auto meta
Definition user.h:76
user_data(T &&x)
Creates an owning (copy) object.
static int eq(lua_State *L)
Function to be used as a user data's __eq meta-method.
static int push(state_view lua, R &&r)
Pushes a value on the stack as a user data of type T.
static get_type get(state_view lua, int i)
Retrieves a user data of type T from the stack.
typename alloc_block< header_type, T >::storage base_type
Definition user.h:75
static bool check_type(state_view lua, int i)
Verifies that the value on the stack is a user data of type T.
std::remove_pointer_t< T > * get_type
Definition user.h:81
user_data_header< T > header_type
Definition user.h:74
Definition fundamental.h:41
assert
Definition debug.lua:3
for i
Definition font.lua:5
local r
Definition gamma.lua:7
local data
Definition house0.lua:10
#define NNGN_LOG_CONTEXT_CF(c)
Definition log.h:11
std::conditional_t< std::is_abstract_v< T >, empty, T > stored_type
Prevents forming a type with an abstract type as a member.
Definition user.h:48
Definition lua_audio.cpp:19
constexpr auto user_data_header_align
Definition user.h:53
constexpr struct nngn::lua::nil_type nil
constexpr empty metatable_name
Key in the global table where the meta-table for T is stored.
Definition lua.h:156
empty
Definition utils.h:89
get
Definition camera.lua:46
#define FWD(...)
Definition utils.h:18
local mt
Definition strict.lua:4
Underlying storage type.
Definition block.h:37
header_type header
Definition block.h:41
value_type data
Definition block.h:42
Base operations which do not depend on the template type.
Definition user.h:33
static bool check_pointer_type(state_view lua, int i, std::string_view meta)
Definition user.cpp:71
static bool check_type(state_view lua, int i, std::string_view meta)
Definition user.cpp:64
static table push_metatable(state_view lua, std::string_view meta)
Pushes the table stored as meta in the global table.
Definition user.cpp:59
Owning reference to a table on the stack, popped when destroyed.
Definition table.h:172
Operations on generic stack values.