SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
LuaArray.h
Go to the documentation of this file.
1#ifndef LUAARRAY_H
2#define LUAARRAY_H
3
4#include <lua.h>
5#include <array>
6
7
13template <size_t DIM>
15{
21 {
22 elements.fill(0.0);
23 }
24
31 void read(lua_State * lua, int index)
32 {
33 std::vector data = getNumericLuaTable(lua, index);
34 if (data.size() != DIM)
35 {
36 std::string err = "Wrong number of elements to LuaArray<"+std::to_string(DIM)+">, " + std::to_string(data.size());
37 lua_pushliteral(lua, "Wrong number of elements to LuaArray");
38 lua_error(lua);
39 throw std::runtime_error(err);
40 }
41 else
42 {
43 // gauranteed the correct size
44 std::copy_n(data.begin(), DIM, elements.begin());
45 }
46 }
47
56 bool readField(lua_State * lua, const char * name)
57 {
58 int returnType = lua_getfield(lua, 1, name);
59
61 {
62 read(lua, 2);
63 lua_pop(lua,1);
64 return true;
65 }
66 else
67 {
68 lua_pop(lua,1);
69 return false;
70 }
71 }
72
81 bool readGlobal(lua_State * lua, const char * name)
82 {
83 int returnType = lua_getglobal(lua, name);
84
86 {
87 read(lua, 2);
88 lua_pop(lua,1);
89 return true;
90 }
91 else
92 {
93 lua_pop(lua,1);
94 return false;
95 }
96 }
97
98 double & operator [] (size_t i){ return elements[i]; }
99
100 operator std::array<double, DIM>() { return elements; }
101
102 std::array<double, DIM> elements;
103};
104
105#endif /* LUAARRAY_H */
glm::vec< L, float, glm::qualifier::highp > vec
Definition commandLine.h:214
std::vector< double > getNumericLuaTable(lua_State *lua, int index)
Get the a Lua table of doubles.
Definition lua.h:24
Interop for a fixed size numeric array in Lua.
Definition LuaArray.h:15
std::array< double, DIM > elements
Definition LuaArray.h:102
double & operator[](size_t i)
Definition LuaArray.h:98
bool readGlobal(lua_State *lua, const char *name)
Read the array from a global table field.
Definition LuaArray.h:81
void read(lua_State *lua, int index)
Read the array from stack index index.
Definition LuaArray.h:31
bool readField(lua_State *lua, const char *name)
Read the array from a table field.
Definition LuaArray.h:56
LuaArray()
Construct an array of 0s.
Definition LuaArray.h:20