Skip to content

Commit

Permalink
Merge pull request #60 from satoren/error_with_vector_bool
Browse files Browse the repository at this point in the history
Fix compile error at push with vector<bool> #59
  • Loading branch information
satoren authored Apr 14, 2017
2 parents 13701eb + 4a5d369 commit 51db8c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion include/kaguya/lua_ref_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ namespace kaguya
{
typedef std::vector<T, A> get_type;
typedef const std::vector<T, A>& push_type;

struct checkTypeForEach
{
checkTypeForEach(bool& valid) :valid_(valid) {}
Expand Down Expand Up @@ -488,6 +487,20 @@ namespace kaguya
}
return LuaStackRef(l, index).values<T>();
}
#if KAGUYA_USE_CPP11
typedef std::vector<T, A>&& move_push_type;
static int push(lua_State* l, move_push_type v)
{
lua_createtable(l, int(v.size()), 0);
int count = 1;//array is 1 origin in Lua
for (typename std::vector<T, A>::iterator it = v.begin(); it != v.end(); ++it)
{
util::one_push(l, static_cast<T&&>(*it));
lua_rawseti(l, -2, count++);
}
return 1;
}
#endif
static int push(lua_State* l, push_type v)
{
lua_createtable(l, int(v.size()), 0);
Expand Down
19 changes: 19 additions & 0 deletions test/test_07_vector_map_to_luatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,26 @@ KAGUYA_TEST_FUNCTION_DEF(vector_to_table)(kaguya::State& state)
std::vector<double> v; v.push_back(3); v.push_back(13); v.push_back(2); v.push_back(99);
state["v"] = v;
TEST_CHECK(state("assert(v[1] == 3 and v[2] == 13 and v[3] == 2 and v[4] == 99)"));
}
KAGUYA_TEST_FUNCTION_DEF(vector_bool_from_table)(kaguya::State& state)
{
state("arraytablefn =function() return {true,false,false,true,false,true} end");
std::vector<bool> b = state["arraytablefn"]();
TEST_EQUAL(b.size(), 6);
TEST_EQUAL(b[0], true);
TEST_EQUAL(b[1], false);
TEST_EQUAL(b[2], false);
TEST_EQUAL(b[3], true);
TEST_EQUAL(b[4], false);
TEST_EQUAL(b[5], true);
TEST_CHECK(state["arraytablefn"]().typeTest<std::vector<bool> >());
}

KAGUYA_TEST_FUNCTION_DEF(vector_bool_to_table)(kaguya::State& state)
{
std::vector<bool> v; v.push_back(true); v.push_back(false); v.push_back(false); v.push_back(true);
state["v"] = v;
TEST_CHECK(state("assert(v[1] == true and v[2] == false and v[3] == false and v[4] == true)"));
}
#endif

Expand Down
11 changes: 11 additions & 0 deletions test/test_11_cxx11_feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ KAGUYA_TEST_FUNCTION_DEF(invoke_test)(kaguya::State&)
}



#ifndef KAGUYA_NO_STD_VECTOR_TO_TABLE

KAGUYA_TEST_FUNCTION_DEF(vector_to_table_with_move)(kaguya::State& state)
{
std::vector<double> v; v.push_back(3); v.push_back(13); v.push_back(2); v.push_back(99);
state["v"] = std::move(v);
TEST_CHECK(state("assert(v[1] == 3 and v[2] == 13 and v[3] == 2 and v[4] == 99)"));
}
#endif

KAGUYA_TEST_GROUP_END(test_11_cxx11_feature)


Expand Down

0 comments on commit 51db8c6

Please sign in to comment.