diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h index 2858e6485..d649a3410 100644 --- a/include/flatbuffers/flexbuffers.h +++ b/include/flatbuffers/flexbuffers.h @@ -77,16 +77,17 @@ enum Type { TYPE_VECTOR_FLOAT4 = 24, TYPE_BLOB = 25, TYPE_BOOL = 26, + TYPE_VECTOR_BOOL = 36, // To Allow the same type of conversion of type to vector type }; inline bool IsInline(Type t) { return t <= TYPE_FLOAT || t == TYPE_BOOL; } inline bool IsTypedVectorElementType(Type t) { - return t >= TYPE_INT && t <= TYPE_STRING; + return (t >= TYPE_INT && t <= TYPE_STRING) || t == TYPE_BOOL; } inline bool IsTypedVector(Type t) { - return t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING; + return (t >= TYPE_VECTOR_INT && t <= TYPE_VECTOR_STRING) || t == TYPE_VECTOR_BOOL; } inline bool IsFixedTypedVector(Type t) { @@ -1240,7 +1241,8 @@ class Builder FLATBUFFERS_FINAL_CLASS { assert(flatbuffers::is_scalar::value); return flatbuffers::is_floating_point::value ? TYPE_FLOAT - : (flatbuffers::is_unsigned::value ? TYPE_UINT : TYPE_INT); + : flatbuffers::is_same::value ? TYPE_BOOL + : (flatbuffers::is_unsigned::value ? TYPE_UINT : TYPE_INT); } struct Value { diff --git a/include/flatbuffers/stl_emulation.h b/include/flatbuffers/stl_emulation.h index 7906648ba..c025eca1a 100644 --- a/include/flatbuffers/stl_emulation.h +++ b/include/flatbuffers/stl_emulation.h @@ -91,11 +91,13 @@ inline void vector_emplace_back(std::vector *vector, V &&data) { #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */) #ifndef FLATBUFFERS_CPP98_STL template using is_scalar = std::is_scalar; + template using is_same = std::is_same; template using is_floating_point = std::is_floating_point; template using is_unsigned = std::is_unsigned; #else // Map C++ TR1 templates defined by stlport. template using is_scalar = std::tr1::is_scalar; + template using is_same = std::tr1::is_same; template using is_floating_point = std::tr1::is_floating_point; template using is_unsigned = std::tr1::is_unsigned; @@ -103,6 +105,7 @@ inline void vector_emplace_back(std::vector *vector, V &&data) { #else // MSVC 2010 doesn't support C++11 aliases. template struct is_scalar : public std::is_scalar {}; + template struct is_same : public std::is_same {}; template struct is_floating_point : public std::is_floating_point {}; template struct is_unsigned : public std::is_unsigned {}; diff --git a/tests/test.cpp b/tests/test.cpp index d60ba1cc1..03ccf062c 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1608,6 +1608,8 @@ void FlexBuffersTest() { int ints[] = { 1, 2, 3 }; slb.Vector("bar", ints, 3); slb.FixedTypedVector("bar3", ints, 3); + bool bools[] = {true, false, true, false}; + slb.Vector("bools", bools, 4); slb.Bool("bool", true); slb.Double("foo", 100); slb.Map("mymap", [&]() { @@ -1624,10 +1626,12 @@ void FlexBuffersTest() { slb3.IndirectFloat(4.0f); uint8_t blob[] = { 77 }; slb3.Blob(blob, 1); + slb3 += false; }, slb2); int ints[] = { 1, 2, 3 }; slb2.Vector("bar", ints, 3); slb2.FixedTypedVector("bar3", ints, 3); + slb.Bool("bool", true); slb2.Double("foo", 100); slb2.Map("mymap", [](flexbuffers::Builder& slb3) { slb3.String("foo", "Fred"); // Testing key and string reuse. @@ -1641,7 +1645,7 @@ void FlexBuffersTest() { printf("\n"); auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap(); - TEST_EQ(map.size(), 6); + TEST_EQ(map.size(), 7); auto vec = map["vec"].AsVector(); TEST_EQ(vec.size(), 5); TEST_EQ(vec[0].AsInt64(), -100); @@ -1665,11 +1669,13 @@ void FlexBuffersTest() { TEST_EQ(tvec3.size(), 3); TEST_EQ(tvec3[2].AsInt8(), 3); TEST_EQ(map["bool"].AsBool(), true); + auto tvecb = map["bools"].AsTypedVector(); + TEST_EQ(tvecb.ElementType(), flexbuffers::TYPE_BOOL); TEST_EQ(map["foo"].AsUInt8(), 100); TEST_EQ(map["unknown"].IsNull(), true); auto mymap = map["mymap"].AsMap(); // These should be equal by pointer equality, since key and value are shared. - TEST_EQ(mymap.Keys()[0].AsKey(), map.Keys()[3].AsKey()); + TEST_EQ(mymap.Keys()[0].AsKey(), map.Keys()[4].AsKey()); TEST_EQ(mymap.Values()[0].AsString().c_str(), vec[1].AsString().c_str()); // We can mutate values in the buffer. TEST_EQ(vec[0].MutateInt(-99), true);