Skip to content

Commit 51db8c6

Browse files
authored
Merge pull request #60 from satoren/error_with_vector_bool
Fix compile error at push with vector<bool> #59
2 parents 13701eb + 4a5d369 commit 51db8c6

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

include/kaguya/lua_ref_table.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ namespace kaguya
438438
{
439439
typedef std::vector<T, A> get_type;
440440
typedef const std::vector<T, A>& push_type;
441-
442441
struct checkTypeForEach
443442
{
444443
checkTypeForEach(bool& valid) :valid_(valid) {}
@@ -488,6 +487,20 @@ namespace kaguya
488487
}
489488
return LuaStackRef(l, index).values<T>();
490489
}
490+
#if KAGUYA_USE_CPP11
491+
typedef std::vector<T, A>&& move_push_type;
492+
static int push(lua_State* l, move_push_type v)
493+
{
494+
lua_createtable(l, int(v.size()), 0);
495+
int count = 1;//array is 1 origin in Lua
496+
for (typename std::vector<T, A>::iterator it = v.begin(); it != v.end(); ++it)
497+
{
498+
util::one_push(l, static_cast<T&&>(*it));
499+
lua_rawseti(l, -2, count++);
500+
}
501+
return 1;
502+
}
503+
#endif
491504
static int push(lua_State* l, push_type v)
492505
{
493506
lua_createtable(l, int(v.size()), 0);

test/test_07_vector_map_to_luatable.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,26 @@ KAGUYA_TEST_FUNCTION_DEF(vector_to_table)(kaguya::State& state)
2424
std::vector<double> v; v.push_back(3); v.push_back(13); v.push_back(2); v.push_back(99);
2525
state["v"] = v;
2626
TEST_CHECK(state("assert(v[1] == 3 and v[2] == 13 and v[3] == 2 and v[4] == 99)"));
27+
}
28+
KAGUYA_TEST_FUNCTION_DEF(vector_bool_from_table)(kaguya::State& state)
29+
{
30+
state("arraytablefn =function() return {true,false,false,true,false,true} end");
31+
std::vector<bool> b = state["arraytablefn"]();
32+
TEST_EQUAL(b.size(), 6);
33+
TEST_EQUAL(b[0], true);
34+
TEST_EQUAL(b[1], false);
35+
TEST_EQUAL(b[2], false);
36+
TEST_EQUAL(b[3], true);
37+
TEST_EQUAL(b[4], false);
38+
TEST_EQUAL(b[5], true);
39+
TEST_CHECK(state["arraytablefn"]().typeTest<std::vector<bool> >());
40+
}
2741

42+
KAGUYA_TEST_FUNCTION_DEF(vector_bool_to_table)(kaguya::State& state)
43+
{
44+
std::vector<bool> v; v.push_back(true); v.push_back(false); v.push_back(false); v.push_back(true);
45+
state["v"] = v;
46+
TEST_CHECK(state("assert(v[1] == true and v[2] == false and v[3] == false and v[4] == true)"));
2847
}
2948
#endif
3049

test/test_11_cxx11_feature.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ KAGUYA_TEST_FUNCTION_DEF(invoke_test)(kaguya::State&)
341341
}
342342

343343

344+
345+
#ifndef KAGUYA_NO_STD_VECTOR_TO_TABLE
346+
347+
KAGUYA_TEST_FUNCTION_DEF(vector_to_table_with_move)(kaguya::State& state)
348+
{
349+
std::vector<double> v; v.push_back(3); v.push_back(13); v.push_back(2); v.push_back(99);
350+
state["v"] = std::move(v);
351+
TEST_CHECK(state("assert(v[1] == 3 and v[2] == 13 and v[3] == 2 and v[4] == 99)"));
352+
}
353+
#endif
354+
344355
KAGUYA_TEST_GROUP_END(test_11_cxx11_feature)
345356

346357

0 commit comments

Comments
 (0)