10 #include <glm/glm.hpp>
12 #include "luawrapperfunction.h"
23 void loadScript(
const std::string & script);
24 void loadString(
const std::string & script);
25 void removeScript(
const std::string & script);
28 bool has(
const std::string & fun);
30 static void reloadAll();
35 void pushFunc(
const std::string & func)
const;
36 void callFunc(
const int numArgs,
const int numRet);
38 void popStack(
const int index);
40 template <
typename T,
typename... Ts>
41 void push(
const T value,
const Ts... values)
const
48 void push(
const std::string & value)
const;
49 void push(
const char * value)
const;
50 void push(
const int8_t value)
const;
51 void push(
const uint8_t value)
const;
52 void push(
const int16_t value)
const;
53 void push(
const uint16_t value)
const;
54 void push(
const int32_t value)
const;
55 void push(
const uint32_t value)
const;
56 void push(
const int64_t value)
const;
57 void push(
const uint64_t value)
const;
58 void push(
const float value)
const;
59 void push(
const double value)
const;
60 void push(
const bool value)
const;
61 void push(
const glm::vec3 & value)
const;
63 template <
typename T> T fetch(
const int index)
const;
65 template <size_t,
typename... Ts>
68 typedef std::tuple<Ts...> type;
71 static std::tuple<T> worker(
const LuaWrapper &instance,
const int index)
73 return std::make_tuple(instance.fetch<T>(index));
76 template <
typename T1,
typename T2,
typename... Rest>
77 static std::tuple<T1, T2, Rest...> worker(
const LuaWrapper &instance,
const int index)
79 std::tuple<T1> head = std::make_tuple(instance.fetch<T1>(index));
80 return std::tuple_cat(head, worker<T2, Rest...>(instance, index + 1));
85 type ret = worker<Ts...>(instance, 1);
86 instance.popStack(
sizeof...(Ts));
91 template <
typename... Ts>
105 T ret = instance.fetch<T>(-1);
107 instance.popStack(1);
112 template <
typename... T>
113 typename _pop<
sizeof...(T), T...>::type pop()
115 return _pop<
sizeof...(T), T...>::apply(*
this);
120 template <
typename... Ret,
typename... Args>
121 typename _pop<
sizeof...(Ret), Ret...>::type call(
const std::string &fun,
const Args&... args)
123 pushFunc(fun.c_str());
126 const int numArgs =
sizeof...(Args);
127 const int numRet =
sizeof...(Ret);
129 callFunc(numArgs, numRet);
131 return pop<Ret...>();
135 template<
typename Return,
typename Class,
typename... Args>
136 void Register(
const std::string & name, Class * obj, Return(Class::*
const method) (Args...))
138 std::function<Return(typename std::remove_reference<Args>::type...)>
function = [obj, method](Args... args) {
139 return (obj->*method)(args...);
142 Register(name,
function);
145 template <
typename Return,
typename... Args>
146 void Register(
const std::string & name, std::function<Return(Args...)>
function)
148 auto tmp = std::unique_ptr<BaseLuaFunction>(
new LuaFunction<1, Return, Args...>{m_state, name,
function});
149 m_functions.insert(std::make_pair(name, std::move(tmp)));
152 template <
typename Return,
typename... Args>
153 void Register(
const std::string & name, Return (*
function)(Args...))
155 auto tmp = std::unique_ptr<BaseLuaFunction>(
new LuaFunction<1, Return, Args...>{m_state, name,
function});
156 m_functions.insert(std::make_pair(name, std::move(tmp)));
159 template <
typename... Return,
typename... Args>
160 void Register(
const std::string & name, std::function<std::tuple<Return...>(Args...)>
function)
162 const int num_return =
sizeof...(Return);
163 auto tmp = std::unique_ptr<BaseLuaFunction>(
new LuaFunction<num_return, std::tuple<Return...>, Args...>{m_state, name,
function});
164 m_functions.insert(std::make_pair(name, std::move(tmp)));
167 template <
typename... Return,
typename... Args>
168 void Register(
const std::string & name, std::tuple<Return...> (*
function)(Args...))
170 const int num_return =
sizeof...(Return);
171 auto tmp = std::unique_ptr<BaseLuaFunction>(
new LuaFunction<num_return, std::tuple<Return...>, Args...>{m_state, name,
function});
172 m_functions.insert(std::make_pair(name, std::move(tmp)));
175 void Unregister(
const std::string &name) {
176 auto it = m_functions.find(name);
177 if (it != m_functions.end()) m_functions.erase(it);
184 std::vector<std::string> m_scripts;
187 std::map<std::string, std::unique_ptr<BaseLuaFunction>> m_functions;
189 static std::list<LuaWrapper *> s_instances;
Definition: luawrapper.h:66
Definition: luawrapperfunction.h:115
Definition: luawrapper.h:17