Simple Lua
A Simple C++ Lua Wrapper
Loading...
Searching...
No Matches
Table.hpp
1#pragma once
2
3#include "../Def.hpp"
4#include "Lua.hpp"
5
6#include <optional>
7#include <unordered_map>
8#include <memory>
9#include <functional>
10
11namespace SL
12{
13 struct Table
14 {
18 struct Data
19 {
20 std::shared_ptr<void> data;
21 int type;
22
29 template<typename T>
30 SL_SYMBOL static Data fromValue(const T& value);
31
38 template<typename T>
39 SL_SYMBOL static std::shared_ptr<void> emplace(const T& value);
40 };
41
42 using Map = std::unordered_map<std::string, Data>;
43
44 Table() = default;
45 SL_SYMBOL Table(const Map& map);
46
55 SL_SYMBOL Table(State L);
56 ~Table() = default;
57
58 SL_SYMBOL const Data& getRaw(const std::string& name) const;
59
60 template<typename T>
61 SL_SYMBOL void each(std::function<void(uint32_t, T&)> lambda);
62
63 template<typename T>
64 SL_SYMBOL void each(std::function<void(uint32_t, const T&)> lambda) const;
65
66 template<typename T>
67 SL_SYMBOL void try_get(const std::string& name, std::function<void(T&)> lambda, std::optional<std::function<void()>> if_not = std::nullopt);
68
69 template<typename T>
70 SL_SYMBOL void try_get(const std::string& name, std::function<void(const T&)> lambda, std::optional<std::function<void()>> if_not = std::nullopt) const;
71
76 SL_SYMBOL void fromTable(const Table& table);
77
82 SL_SYMBOL void superimpose(const Table& table);
83
88 SL_SYMBOL void superimpose(const Map& map);
89
90 SL_SYMBOL bool hasValue(const std::string& name) const;
91
98 template<typename T>
99 SL_SYMBOL T& get(const std::string& name);
100
101 template<typename T>
102 SL_SYMBOL std::vector<T> get() const;
103
110 template<typename T>
111 SL_SYMBOL const T& get(const std::string& name) const;
112 SL_SYMBOL void* get(const std::string& name) const;
113
120 template<typename T>
121 SL_SYMBOL void set(const std::string& name, const T& value);
122 SL_SYMBOL void set(const std::string& name, void* value);
123
128 SL_SYMBOL const Map& getMap() const;
129
134 SL_SYMBOL void toStack(State L) const;
135
136 SL_SYMBOL void fromStack(State L);
137
138 SL_SYMBOL std::string toString(uint32_t indent = 0) const;
139
140 private:
141 Map dictionary;
142 };
143}
Represents a value in the table stored in memory with a type.
Definition Table.hpp:19
static SL_SYMBOL std::shared_ptr< void > emplace(const T &value)
Allocate room and copy the memory of a given value.
static SL_SYMBOL Data fromValue(const T &value)
Construct a data entry from a value.
Definition Table.hpp:14
SL_SYMBOL void superimpose(const Map &map)
Copy the entries from a map into this table.
SL_SYMBOL Table(State L)
Constructs a table from the current Lua stack.
SL_SYMBOL const Map & getMap() const
Get the raw mapping of this table.
SL_SYMBOL const T & get(const std::string &name) const
Get a constant reference to a value in the table.
SL_SYMBOL T & get(const std::string &name)
Get a reference to a value in the table.
SL_SYMBOL void toStack(State L) const
Dump all the map information onto the given Lua stack.
SL_SYMBOL void set(const std::string &name, const T &value)
Set the value associated with a key.
SL_SYMBOL void fromTable(const Table &table)
Make the entries equivalent to another table.
SL_SYMBOL void superimpose(const Table &table)
Copy the entries from a table into this table.