Skip to content

Commit

Permalink
Clean up and removed debug printfs
Browse files Browse the repository at this point in the history
  • Loading branch information
AdUki committed Mar 23, 2014
1 parent 00b6616 commit 87b4414
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 59 deletions.
11 changes: 5 additions & 6 deletions include/LuaFunctor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ namespace lua {
struct BaseFunctor
{
BaseFunctor(lua_State* luaState, size_t size) {
printf("Functor %p created!\n", this);
LUASTATE_DEBUG_LOG("Functor %p created!\n", this);
}

virtual ~BaseFunctor() {
printf("Functor %p destructed!\n", this);
LUASTATE_DEBUG_LOG("Functor %p destructed!\n", this);
}

virtual int call(lua_State* luaState) = 0;
Expand All @@ -36,7 +35,7 @@ namespace lua {
}

int call(lua_State* luaState) {
Ret value = apply(function, stack::get_and_pop<Args...>(luaState));
Ret value = traits::apply(function, stack::get_and_pop<Args...>(luaState));
return stack::push(luaState, value);
}
};
Expand All @@ -50,7 +49,7 @@ namespace lua {
, function(function) {}

int call(lua_State* luaState) {
apply(function, stack::get_and_pop<Args...>(luaState));
traits::apply(function, stack::get_and_pop<Args...>(luaState));
return 0;
}
};
Expand Down Expand Up @@ -80,7 +79,7 @@ namespace lua {
template<typename T>
inline int push(lua_State* luaState, T function)
{
push(luaState, (typename function_traits<T>::Function)(function));
push(luaState, (typename traits::function_traits<T>::Function)(function));
return 1;
}

Expand Down
55 changes: 17 additions & 38 deletions include/LuaStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
namespace lua { namespace stack {

inline void dump (lua_State *L) {
#ifdef LUASTATE_DEBUG_MODE
int i;
int top = lua_gettop(L);
for (i = 1; i <= top; i++) { /* repeat for each level */
int t = lua_type(L, i);
switch (t) {

case LUA_TSTRING: /* strings */
printf("`%s'", lua_tostring(L, i));
break;
Expand All @@ -43,6 +43,7 @@ namespace lua { namespace stack {
printf(" "); /* put a separator */
}
printf("\n"); /* end the listing */
#endif
}

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -65,42 +66,42 @@ namespace lua { namespace stack {

template<>
inline int push(lua_State* luaState, int value) {
printf(" PUSH %d\n", value);
LUASTATE_DEBUG_LOG(" PUSH %d\n", value);
lua_pushinteger(luaState, value);
return 1;
}

template<>
inline int push(lua_State* luaState, LuaType::String value) {
printf(" PUSH %s\n", value);
LUASTATE_DEBUG_LOG(" PUSH %s\n", value);
lua_pushstring(luaState, value);
return 1;
}

template<>
inline int push(lua_State* luaState, LuaType::Number value) {
printf(" PUSH %lf\n", value);
LUASTATE_DEBUG_LOG(" PUSH %lf\n", value);
lua_pushnumber(luaState, value);
return 1;
}

template<>
inline int push(lua_State* luaState, LuaType::Boolean value) {
printf(" PUSH %s\n", value ? "true" : "false");
LUASTATE_DEBUG_LOG(" PUSH %s\n", value ? "true" : "false");
lua_pushboolean(luaState, value);
return 1;
}

template<>
inline int push(lua_State* luaState, LuaType::Null value) {
printf(" PUSH null\n");
LUASTATE_DEBUG_LOG(" PUSH null\n");
lua_pushnil(luaState);
return 1;
}

template<>
inline int push(lua_State* luaState, Table value) {
printf(" PUSH newTable\n");
LUASTATE_DEBUG_LOG(" PUSH newTable\n");
lua_newtable(luaState);
return 1;
}
Expand All @@ -117,38 +118,16 @@ namespace lua { namespace stack {
return 1;
}

template<typename T>
void push_helper(lua_State* luaState, const T& value)
{
int index = lua_tointeger(luaState, -1);
push(luaState, value);
lua_settable(luaState, -3);
lua_pushinteger(luaState, ++index);
}

template<typename T, typename ... Ts>
void push_helper(lua_State* luaState, const T value, const Ts... values)
{
push_helper(luaState, value);
push_helper(luaState, values...);
}

template<typename ... Args, int ... Indexes>
void push_helper(lua_State* luaState, index_tuple< Indexes... >, const std::tuple<Args...>& tup)
template<typename ... Args, size_t ... Indexes>
void push_helper(lua_State* luaState, traits::index_tuple< Indexes... >, const std::tuple<Args...>& tup)
{
push(luaState, std::get<Indexes>(tup)...);
// lua_newtable(luaState);
// lua_pushinteger(luaState, 1);
// push_helper(luaState, std::get<Indexes>(tup)... );
// lua_pop(luaState, 1);
}

template<typename ... Args>
inline int push(lua_State* luaState, const std::tuple<Args...>& tuple)
{
stack::dump(luaState);
push_helper(luaState, typename make_indexes<Args...>::type(), tuple);
stack::dump(luaState);
push_helper(luaState, typename traits::make_indexes<Args...>::type(), tuple);
return sizeof...(Args);
}

Expand Down Expand Up @@ -195,7 +174,7 @@ namespace lua { namespace stack {
//////////////////////////////////////////////////////////////////////////////////////////////////

inline void pop(lua_State* luaState, int n) {
printf(" POP %d\n", n);
LUASTATE_DEBUG_LOG(" POP %d\n", n);
lua_pop(luaState, n);
}

Expand All @@ -217,13 +196,13 @@ namespace lua { namespace stack {
class Pop {

template<std::size_t... Is>
static std::tuple<Ts...> create(lua_State* luaState, int offset, Indexes<Is...>) {
static std::tuple<Ts...> create(lua_State* luaState, int offset, traits::indexes<Is...>) {
return std::make_tuple(read<Ts>(luaState, Is + offset)...);
}

public:
static std::tuple<Ts...> getTable(lua_State* luaState, int offset) {
return create(luaState, offset, typename IndexesBuilder<I>::index());
return create(luaState, offset, typename traits::indexes_builder<I>::index());
}
};

Expand All @@ -239,7 +218,7 @@ namespace lua { namespace stack {
//////////////////////////////////////////////////////////////////////////////////////////////////

inline void get(lua_State* luaState, int index) {
printf("GET\n");
LUASTATE_DEBUG_LOG("GET\n");
lua_gettable(luaState, index);
}

Expand All @@ -248,13 +227,13 @@ namespace lua { namespace stack {

template<>
inline void get(lua_State* luaState, int index, const char* key) {
printf("GET %s\n", key);
LUASTATE_DEBUG_LOG("GET %s\n", key);
lua_getfield(luaState, index, key);
}

template<>
inline void get(lua_State* luaState, int index, int key) {
printf("GET %d\n", key);
LUASTATE_DEBUG_LOG("GET %d\n", key);
lua_rawgeti(luaState, index, key);
}

Expand Down
10 changes: 9 additions & 1 deletion include/LuaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

#include <lua.hpp>

//#define LUASTATE_DEBUG_MODE

#ifdef LUASTATE_DEBUG_MODE
# define LUASTATE_DEBUG_LOG(format, ...) printf(format, ## __VA_ARGS__)
#else
# define LUASTATE_DEBUG_LOG(format, ...)
#endif

#include "./LuaException.h"
#include "./LuaValue.h"
#include "./LuaReturn.h"
Expand Down Expand Up @@ -89,7 +97,7 @@ namespace lua {
/// @returns Number of flushed items
int flushStack() {
int count = stack::numberOfPushedValues(_luaState.get());
printf("Flushed %d elements from stack\n", count);
LUASTATE_DEBUG_LOG("Flushed %d elements from stack\n", count);
lua_settop(_luaState.get(), 0);
return count;
}
Expand Down
22 changes: 11 additions & 11 deletions include/Traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#pragma once

namespace lua {
namespace lua { namespace traits {

template <typename T>
struct function_traits
Expand All @@ -31,18 +31,18 @@ namespace lua {

//////////////////////////////////////////////////////////////////////////////////////////////////

template<int...> struct index_tuple{};
template<size_t...> struct index_tuple{};

template<int I, typename IndexTuple, typename ... Types>
template<size_t I, typename IndexTuple, typename ... Types>
struct make_indexes_impl;

template<int I, int... Indexes, typename T, typename ... Types>
template<size_t I, size_t... Indexes, typename T, typename ... Types>
struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...>
{
typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type;
};

template<int I, int... Indexes>
template<size_t I, size_t... Indexes>
struct make_indexes_impl<I, index_tuple<Indexes...> >
{
typedef index_tuple<Indexes...> type;
Expand All @@ -54,7 +54,7 @@ namespace lua {

//////////////////////////////////////////////////////////////////////////////////////////////////

template<class Ret, class... Args, int... Indexes >
template<class Ret, class... Args, size_t... Indexes >
Ret apply_helper(std::function<Ret(Args...)> pf, index_tuple< Indexes... >, std::tuple<Args...>&& tup)
{
return pf( std::forward<Args>( std::get<Indexes>(tup))... );
Expand Down Expand Up @@ -87,13 +87,13 @@ namespace lua {
//////////////////////////////////////////////////////////////////////////////////////////////////

template <std::size_t... Is>
struct Indexes {};
struct indexes {};

template <std::size_t N, std::size_t... Is>
struct IndexesBuilder : IndexesBuilder<N-1, N-1, Is...> {};
struct indexes_builder : indexes_builder<N-1, N-1, Is...> {};

template <std::size_t... Is>
struct IndexesBuilder<0, Is...> {
using index = Indexes<Is...>;
struct indexes_builder<0, Is...> {
using index = indexes<Is...>;
};
}
}}
5 changes: 2 additions & 3 deletions test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#define check(code, result) \
{ \
if (code == result) { \
printf("OK %s == %s\n", #code, #result); \
printf("line %d: OK %s == %s\n", __LINE__, #code, #result); \
} \
else { \
printf("FAIL %s == %s\n", #code, #result); \
printf("line %d: FAIL %s == %s\n", __LINE__, #code, #result); \
assert(false); \
} \
}
Expand All @@ -17,7 +17,6 @@ using namespace std::placeholders;

const char* sayHello()
{
printf("Hello!\n");
return "Hello return\n";
}

Expand Down

0 comments on commit 87b4414

Please sign in to comment.