codex
Loading...
Searching...
No Matches
reader.hpp
Go to the documentation of this file.
1#ifndef CODEX_LUA_READER_H
2#define CODEX_LUA_READER_H
3
4#include <cstddef>
5#include <iostream>
6
7#include <lua.hpp>
8
9#include "gen.hpp"
10
11template<std::size_t N>
12struct X {
13 using T = const char(&)[N];
14 constexpr X(T s_) : s{s_} {}
15 constexpr T operator--(void) { return this->s; }
17};
18
19void read_number(lua_State *L) {
20 std::cout << "number: " << lua_tonumber(L, -1) << '\n';
21 lua_pop(L, 1);
22}
23
24void read_string(lua_State *L) {
25 std::cout << "string: " << lua_tostring(L, -1) << '\n';
26 lua_pop(L, 1);
27}
28
29void read_table(lua_State *L) {
30 luaL_tolstring(L, -1, nullptr);
31 std::cout << lua_tostring(L, -1) << '\n';
32 lua_pop(L, 1);
33 if(!lua_type(L, -1) == LUA_TTABLE)
34 std::cerr << __func__ << ": invalid stack state\n";
35}
36
37void read_table_end(lua_State *L) {
38 lua_pop(L, 1);
39}
40
41void read_table_key(lua_State *L, std::string_view k) {
42 std::cout << "table_key: " << k << '\n';
43 lua_pushlstring(L, k.data(), k.size());
44 lua_gettable(L, -2);
45}
46
47void read(lua_State*, types<>) {}
48
49template<typename T, typename ...Ts>
50void read(lua_State *L, types<T, Ts...>) {
51// std::cerr << __PRETTY_FUNCTION__ << '\n';
52 constexpr types<Ts...> next = {};
53 if constexpr(std::derived_from<T, number_base>) {
54 read_number(L);
55 return read(L, next);
56 } else if constexpr(std::derived_from<T, string_base>) {
57 read_string(L);
58 return read(L, next);
59 } else if constexpr(std::same_as<T, table_begin>) {
60 read_table(L);
61 return read(L, next);
62 } else if constexpr(std::same_as<T, table_key>) {
63 read_table_key(L, std::string_view{types<Ts...>::front::value});
64 return read(L, next);
65 } else if constexpr(std::same_as<T, table_end>) {
67 return read(L, next);
68 } else
69 return read(L, next);
70}
71
72#endif
#define T(f, T, C)
Definition bench.cpp:136
#define N
Definition queue_atomic.c:4
void read_table_key(lua_State *L, std::string_view k)
Definition reader.hpp:41
void read_number(lua_State *L)
Definition reader.hpp:19
void read_table_end(lua_State *L)
Definition reader.hpp:37
void read_table(lua_State *L)
Definition reader.hpp:29
void read(lua_State *, types<>)
Definition reader.hpp:47
void read_string(lua_State *L)
Definition reader.hpp:24
Definition reader.hpp:12
T s
Definition reader.hpp:16
constexpr X(T s_)
Definition reader.hpp:14
const char(&)[N] T
Definition reader.hpp:13
constexpr T operator--(void)
Definition reader.hpp:15
Definition utils.hpp:38
constexpr fixed_string s
Definition test.cpp:6