Simple Lua
A Simple C++ Lua Wrapper
Loading...
Searching...
No Matches
Lib.hpp
1#pragma once
2
3#include "TypeMap.hpp"
4
5#include "../Util.hpp"
6#include "../Def.hpp"
7
8namespace SL
9{
10 struct Runtime;
11}
12
13namespace SL::Lib
14{
18 struct Base
19 {
20 Base(const Base&) = delete;
21 Base(Base&&) = delete;
22
23 using Map = std::unordered_map<std::string, SL::Function>;
24
31 template<typename... Args>
32 static std::tuple<Args...>
33 extractArgs(State L);
34
39 SL_SYMBOL void registerFunctions(SL::Runtime& runtime) const;
40
41 SL_SYMBOL SL::Table asTable() const;
42
43 protected:
44 SL_SYMBOL Base(
45 const std::string& name,
46 const Map& funcs);
47
48 virtual ~Base() = default;
49
50 std::string _name;
51 Map _funcs;
52 };
53
54 namespace detail
55 {
56
57 SL_SYMBOL std::size_t __getTop(State L);
58 SL_SYMBOL bool __isNil(State L);
59 SL_SYMBOL void __pop(State L, uint32_t n);
60
61 }
62
63 /* struct Base */
64 template<typename... Args>
65 std::tuple<Args...>
67 {
68 SL_ASSERT(detail::__getTop(L) == sizeof...(Args), "Lua arguments do not match expected args.");
69 std::tuple<Args...> values;
70 Util::CompileTime::static_for<sizeof...(Args)>([&](auto n) {
71 SL_ASSERT(!detail::__isNil(L), "Argument nil");
72
73 const std::size_t I = n;
74 const auto i = sizeof...(Args) - I - 1;
75 using Type = Util::CompileTime::NthType<i, Args...>;
76 SL_ASSERT(SL::CompileTime::TypeMap<Type>::check(L), "Type mismatch");
77
78 const auto count = detail::__getTop(L);
79 std::get<i>(values) = SL::CompileTime::TypeMap<Type>::construct(L);
80 if (detail::__getTop(L) == count) detail::__pop(L, 1);
81 });
82 return values;
83 }
84
85} // SL::Lib
Definition TypeMap.hpp:19
Represents a base library package in a Lua script.
Definition Lib.hpp:19
static std::tuple< Args... > extractArgs(State L)
Helper function extracting a list of types arguments from a Lua stack.
Definition Lib.hpp:66
SL_SYMBOL void registerFunctions(SL::Runtime &runtime) const
Registers the functions in this library with the given runtime.
Represents a single Lua runtime.
Definition Runtime.hpp:17
Definition Table.hpp:14