Skip to content

Commit

Permalink
Move around some pi_lua_generic* functions to minimize interdependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
laarmen committed Apr 9, 2014
1 parent 26f4157 commit 8a9410b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
20 changes: 20 additions & 0 deletions src/LuaObject.h
Expand Up @@ -321,4 +321,24 @@ template <typename Key, typename ...Args>
class SystemPath;
template <> void LuaObject<SystemPath>::PushToLua(const SystemPath &o);

// LuaPushPull stuff.
template <class T> void pi_lua_generic_pull(lua_State * l, int index, T* & out) {
assert(l == Lua::manager->GetLuaState());
out = LuaObject<T>::CheckFromLua(index);
}

template <class T> bool pi_lua_strict_pull(lua_State * l, int index, T* & out) {
assert(l == Lua::manager->GetLuaState());
out = LuaObject<T>::GetFromLua(index);
return out != 0;
}

template <class T> void pi_lua_generic_push(lua_State * l, T* value) {
assert(l == Lua::manager->GetLuaState());
if (value)
LuaObject<T>::PushToLua(value);
else
lua_pushnil(l);
}

#endif
33 changes: 0 additions & 33 deletions src/LuaPushPull.h
Expand Up @@ -5,8 +5,6 @@
#define _LUAPUSHPULL_H

#include "lua/lua.hpp"
#include "LuaObject.h"
#include "LuaVector.h"
#include "Lua.h"
#include <string>

Expand All @@ -18,15 +16,6 @@ inline void pi_lua_generic_push(lua_State * l, const char * value) { lua_pushstr
inline void pi_lua_generic_push(lua_State * l, const std::string & value) {
lua_pushlstring(l, value.c_str(), value.size());
}
template <class T> void pi_lua_generic_push(lua_State * l, T* value) {
assert(l == Lua::manager->GetLuaState());
if (value)
LuaObject<T>::PushToLua(value);
else
lua_pushnil(l);
}

inline void pi_lua_generic_push(lua_State * l, const vector3d & value) { LuaVector::PushToLua(l, value); }

inline void pi_lua_generic_pull(lua_State * l, int index, bool & out) { out = lua_toboolean(l, index); }
inline void pi_lua_generic_pull(lua_State * l, int index, int & out) { out = luaL_checkinteger(l, index); }
Expand All @@ -39,15 +28,6 @@ inline void pi_lua_generic_pull(lua_State * l, int index, std::string & out) {
const char *buf = luaL_checklstring(l, index, &len);
std::string(buf, len).swap(out);
}
template <class T> void pi_lua_generic_pull(lua_State * l, int index, T* & out) {
assert(l == Lua::manager->GetLuaState());
out = LuaObject<T>::CheckFromLua(index);
}

inline void pi_lua_generic_pull(lua_State * l, int index, vector3d& out) {
out = *LuaVector::CheckFromLua(l, index);
}

inline bool pi_lua_strict_pull(lua_State * l, int index, bool & out) {
if (lua_type(l, index) == LUA_TBOOLEAN) {
out = lua_toboolean(l, index);
Expand Down Expand Up @@ -92,19 +72,6 @@ inline bool pi_lua_strict_pull(lua_State * l, int index, std::string & out) {
}
return false;
}
template <class T> bool pi_lua_strict_pull(lua_State * l, int index, T* & out) {
assert(l == Lua::manager->GetLuaState());
out = LuaObject<T>::GetFromLua(index);
return out != 0;
}
inline bool pi_lua_strict_pull(lua_State * l, int index, vector3d & out) {
const vector3d* tmp = LuaVector::GetFromLua(l, index);
if (tmp) {
out = *tmp;
return true;
}
return false;
}

template <typename Head, typename ...Tail> inline void pi_lua_multiple_push(lua_State *l, Head arg1, Tail ...rest) {
pi_lua_generic_push(l, arg1);
Expand Down
9 changes: 9 additions & 0 deletions src/LuaRef.h
Expand Up @@ -45,4 +45,13 @@ inline void pi_lua_generic_push(lua_State *l, const LuaRef &r) {
r.PushCopyToStack();
}

inline void pi_lua_generic_pull(lua_State *l, int index, LuaRef &r) {
r = LuaRef(l, index);
}

inline bool pi_lua_strict_pull(lua_State *l, int index, LuaRef &r) {
r = LuaRef(l, index);
return true;
}

#endif
15 changes: 15 additions & 0 deletions src/LuaVector.h
Expand Up @@ -22,4 +22,19 @@ namespace LuaVector {
inline vector3f CheckFromLuaF(lua_State *L, int idx) { return vector3f(*CheckFromLua(L, idx)); }
}

inline void pi_lua_generic_push(lua_State * l, const vector3d & value) { LuaVector::PushToLua(l, value); }

inline void pi_lua_generic_pull(lua_State * l, int index, vector3d& out) {
out = *LuaVector::CheckFromLua(l, index);
}

inline bool pi_lua_strict_pull(lua_State * l, int index, vector3d & out) {
const vector3d* tmp = LuaVector::GetFromLua(l, index);
if (tmp) {
out = *tmp;
return true;
}
return false;
}

#endif

0 comments on commit 8a9410b

Please sign in to comment.