dl
dl_lua_register_container.h
浏览该文件的文档.
1
12#pragma once
13
14#include <LuaBridge/Array.h>
15#include <LuaBridge/Vector.h>
16//
18//namespace luabridge {
19//
20// //=================================================================================================
21// /**
22// * @brief Stack specialization for `dl::Array`.
23// */
24// template <class T, std::size_t Size>
25// struct Stack<dl::Array<T, Size>>
26// {
27// using Type = dl::Array<T, Size>;
28//
29// [[nodiscard]] static Result push(lua_State* L, const Type& array)
30// {
31//#if LUABRIDGE_SAFE_STACK_CHECKS
32// if (!lua_checkstack(L, 3))
33// return makeErrorCode(ErrorCode::LuaStackOverflow);
34//#endif
35//
36// StackRestore stackRestore(L);
37//
38// lua_createtable(L, static_cast<int>(Size), 0);
39//
40// for (std::size_t i = 0; i < Size; ++i)
41// {
42// lua_pushinteger(L, static_cast<lua_Integer>(i + 1));
43//
44// auto result = Stack<T>::push(L, array[i]);
45// if (!result)
46// return result;
47//
48// lua_settable(L, -3);
49// }
50//
51// stackRestore.reset();
52// return {};
53// }
54//
55// [[nodiscard]] static TypeResult<Type> get(lua_State* L, int index)
56// {
57// if (!lua_istable(L, index))
58// return makeErrorCode(ErrorCode::InvalidTypeCast);
59//
60// if (get_length(L, index) != Size)
61// return makeErrorCode(ErrorCode::InvalidTableSizeInCast);
62//
63// const StackRestore stackRestore(L);
64//
65// Type array;
66//
67// int absIndex = lua_absindex(L, index);
68// lua_pushnil(L);
69//
70// int arrayIndex = 0;
71// while (lua_next(L, absIndex) != 0)
72// {
73// auto item = Stack<T>::get(L, -1);
74// if (!item)
75// return makeErrorCode(ErrorCode::InvalidTypeCast);
76//
77// array[arrayIndex++] = *item;
78// lua_pop(L, 1);
79// }
80//
81// return array;
82// }
83//
84// [[nodiscard]] static bool isInstance(lua_State* L, int index)
85// {
86// return lua_istable(L, index) && get_length(L, index) == Size;
87// }
88// };
89//
90//} // namespace luabridge
91
92namespace luabridge{
93//=================================================================================================
97template <class T, class Index>
98struct Stack<dl::Vector2<T, Index>, T>
99{
101
102 [[nodiscard]] static Result push(lua_State* L, const Type& vec)
103 {
104#if LUABRIDGE_SAFE_STACK_CHECKS
105 if (!lua_checkstack(L, 3))
106 return makeErrorCode(ErrorCode::LuaStackOverflow);
107#endif
108
109 StackRestore stackRestore(L);
110
111 lua_createtable(L, 0, static_cast<int>(vec.size()));
112
113 for (auto it = vec.begin(); it != vec.end(); ++it)
114 {
115 auto result = Stack<T>::push(L, *it);
116 if (!result)
117 return result;
118
119 lua_settable(L, -3);
120 }
121
122 stackRestore.reset();
123 return {};
124 }
125
126 [[nodiscard]] static TypeResult<Type> get(lua_State* L, int index)
127 {
128 if (!lua_istable(L, index))
129 return makeErrorCode(ErrorCode::InvalidTypeCast);
130
131 const StackRestore stackRestore(L);
132
133 Type vec;
134
135 int absIndex = lua_absindex(L, index);
136 lua_pushnil(L);
137
138 while (lua_next(L, absIndex) != 0)
139 {
140 auto value = Stack<T>::get(L, -1);
141 if (!value)
142 return makeErrorCode(ErrorCode::InvalidTypeCast);
143
144 vec.emplace_back(*value);
145 lua_pop(L, 1);
146 }
147
148 return vec;
149 }
150
151 [[nodiscard]] static bool isInstance(lua_State* L, int index)
152 {
153 return lua_istable(L, index);
154 }
155};
156
157} // namespace luabridge
size_t size() const
void emplace_back(T &&t)
Stack specialization for dl::Array.
static Result push(lua_State *L, const Type &vec)
static TypeResult< Type > get(lua_State *L, int index)