C++ Add new type vector_bool flexbuffers (#4410)

* Add new type vector_bool

* Update stl_emulation.h

Fix Is same typo

* Update stl_emulation.h
This commit is contained in:
rouzier 2017-08-14 11:44:56 -04:00 committed by Wouter van Oortmerssen
parent b4e91091ec
commit 76744a4345
3 changed files with 16 additions and 5 deletions

View File

@ -77,16 +77,17 @@ enum Type {
TYPE_VECTOR_FLOAT4 = 24, TYPE_VECTOR_FLOAT4 = 24,
TYPE_BLOB = 25, TYPE_BLOB = 25,
TYPE_BOOL = 26, 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 IsInline(Type t) { return t <= TYPE_FLOAT || t == TYPE_BOOL; }
inline bool IsTypedVectorElementType(Type t) { 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) { 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) { inline bool IsFixedTypedVector(Type t) {
@ -1240,6 +1241,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
assert(flatbuffers::is_scalar<T>::value); assert(flatbuffers::is_scalar<T>::value);
return flatbuffers::is_floating_point<T>::value return flatbuffers::is_floating_point<T>::value
? TYPE_FLOAT ? TYPE_FLOAT
: flatbuffers::is_same<T, bool>::value ? TYPE_BOOL
: (flatbuffers::is_unsigned<T>::value ? TYPE_UINT : TYPE_INT); : (flatbuffers::is_unsigned<T>::value ? TYPE_UINT : TYPE_INT);
} }

View File

@ -91,11 +91,13 @@ inline void vector_emplace_back(std::vector<T> *vector, V &&data) {
#if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */) #if !(defined(_MSC_VER) && _MSC_VER <= 1700 /* MSVC2012 */)
#ifndef FLATBUFFERS_CPP98_STL #ifndef FLATBUFFERS_CPP98_STL
template <typename T> using is_scalar = std::is_scalar<T>; template <typename T> using is_scalar = std::is_scalar<T>;
template <typename T, typename U> using is_same = std::is_same<T,U>;
template <typename T> using is_floating_point = std::is_floating_point<T>; template <typename T> using is_floating_point = std::is_floating_point<T>;
template <typename T> using is_unsigned = std::is_unsigned<T>; template <typename T> using is_unsigned = std::is_unsigned<T>;
#else #else
// Map C++ TR1 templates defined by stlport. // Map C++ TR1 templates defined by stlport.
template <typename T> using is_scalar = std::tr1::is_scalar<T>; template <typename T> using is_scalar = std::tr1::is_scalar<T>;
template <typename T, typename U> using is_same = std::tr1::is_same<T,U>;
template <typename T> using is_floating_point = template <typename T> using is_floating_point =
std::tr1::is_floating_point<T>; std::tr1::is_floating_point<T>;
template <typename T> using is_unsigned = std::tr1::is_unsigned<T>; template <typename T> using is_unsigned = std::tr1::is_unsigned<T>;
@ -103,6 +105,7 @@ inline void vector_emplace_back(std::vector<T> *vector, V &&data) {
#else #else
// MSVC 2010 doesn't support C++11 aliases. // MSVC 2010 doesn't support C++11 aliases.
template <typename T> struct is_scalar : public std::is_scalar<T> {}; template <typename T> struct is_scalar : public std::is_scalar<T> {};
template <typename T, typename U> struct is_same : public std::is_same<T,U> {};
template <typename T> struct is_floating_point : template <typename T> struct is_floating_point :
public std::is_floating_point<T> {}; public std::is_floating_point<T> {};
template <typename T> struct is_unsigned : public std::is_unsigned<T> {}; template <typename T> struct is_unsigned : public std::is_unsigned<T> {};

View File

@ -1608,6 +1608,8 @@ void FlexBuffersTest() {
int ints[] = { 1, 2, 3 }; int ints[] = { 1, 2, 3 };
slb.Vector("bar", ints, 3); slb.Vector("bar", ints, 3);
slb.FixedTypedVector("bar3", ints, 3); slb.FixedTypedVector("bar3", ints, 3);
bool bools[] = {true, false, true, false};
slb.Vector("bools", bools, 4);
slb.Bool("bool", true); slb.Bool("bool", true);
slb.Double("foo", 100); slb.Double("foo", 100);
slb.Map("mymap", [&]() { slb.Map("mymap", [&]() {
@ -1624,10 +1626,12 @@ void FlexBuffersTest() {
slb3.IndirectFloat(4.0f); slb3.IndirectFloat(4.0f);
uint8_t blob[] = { 77 }; uint8_t blob[] = { 77 };
slb3.Blob(blob, 1); slb3.Blob(blob, 1);
slb3 += false;
}, slb2); }, slb2);
int ints[] = { 1, 2, 3 }; int ints[] = { 1, 2, 3 };
slb2.Vector("bar", ints, 3); slb2.Vector("bar", ints, 3);
slb2.FixedTypedVector("bar3", ints, 3); slb2.FixedTypedVector("bar3", ints, 3);
slb.Bool("bool", true);
slb2.Double("foo", 100); slb2.Double("foo", 100);
slb2.Map("mymap", [](flexbuffers::Builder& slb3) { slb2.Map("mymap", [](flexbuffers::Builder& slb3) {
slb3.String("foo", "Fred"); // Testing key and string reuse. slb3.String("foo", "Fred"); // Testing key and string reuse.
@ -1641,7 +1645,7 @@ void FlexBuffersTest() {
printf("\n"); printf("\n");
auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap(); auto map = flexbuffers::GetRoot(slb.GetBuffer()).AsMap();
TEST_EQ(map.size(), 6); TEST_EQ(map.size(), 7);
auto vec = map["vec"].AsVector(); auto vec = map["vec"].AsVector();
TEST_EQ(vec.size(), 5); TEST_EQ(vec.size(), 5);
TEST_EQ(vec[0].AsInt64(), -100); TEST_EQ(vec[0].AsInt64(), -100);
@ -1665,11 +1669,13 @@ void FlexBuffersTest() {
TEST_EQ(tvec3.size(), 3); TEST_EQ(tvec3.size(), 3);
TEST_EQ(tvec3[2].AsInt8(), 3); TEST_EQ(tvec3[2].AsInt8(), 3);
TEST_EQ(map["bool"].AsBool(), true); 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["foo"].AsUInt8(), 100);
TEST_EQ(map["unknown"].IsNull(), true); TEST_EQ(map["unknown"].IsNull(), true);
auto mymap = map["mymap"].AsMap(); auto mymap = map["mymap"].AsMap();
// These should be equal by pointer equality, since key and value are shared. // 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()); TEST_EQ(mymap.Values()[0].AsString().c_str(), vec[1].AsString().c_str());
// We can mutate values in the buffer. // We can mutate values in the buffer.
TEST_EQ(vec[0].MutateInt(-99), true); TEST_EQ(vec[0].MutateInt(-99), true);