diff options
author | Louis-Paul CORDIER <lp.cordier@dynamixyz.com> | 2017-10-03 06:38:38 +0900 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2017-10-02 14:38:38 -0700 |
commit | 971a68110e4fc1bace10fcb6deeb189e7e1a34ce (patch) | |
tree | 4c90475787ab9cb6e7456c78fdc9ae92bc7df9e9 | |
parent | 7a6b2bf521e95097a92ec848001531b2dcf0f3fa (diff) | |
download | flatbuffers-971a68110e4fc1bace10fcb6deeb189e7e1a34ce.tar.gz flatbuffers-971a68110e4fc1bace10fcb6deeb189e7e1a34ce.tar.bz2 flatbuffers-971a68110e4fc1bace10fcb6deeb189e7e1a34ce.zip |
Add template version for As method. (#4443)
* Add template version for As method.
* Fix compilation error on Linux.
* Fix linux issue (2).
* Linux fix (3)
* Add few tests.
-rw-r--r-- | include/flatbuffers/flexbuffers.h | 27 | ||||
-rw-r--r-- | tests/test.cpp | 7 |
2 files changed, 34 insertions, 0 deletions
diff --git a/include/flatbuffers/flexbuffers.h b/include/flatbuffers/flexbuffers.h index 644fa04a..3050c2ff 100644 --- a/include/flatbuffers/flexbuffers.h +++ b/include/flatbuffers/flexbuffers.h @@ -579,6 +579,8 @@ class Reference { } } + template<typename T> T As(); + // Experimental: Mutation functions. // These allow scalars in an already created buffer to be updated in-place. // Since by default scalars are stored in the smallest possible space, @@ -688,6 +690,31 @@ class Reference { Type type_; }; +// Template specialization for As(). +template<> inline bool Reference::As<bool>() { return AsBool(); } + +template<> inline int8_t Reference::As<int8_t>() { return AsInt8(); } +template<> inline int16_t Reference::As<int16_t>() { return AsInt16(); } +template<> inline int32_t Reference::As<int32_t>() { return AsInt32(); } +template<> inline int64_t Reference::As<int64_t>() { return AsInt64(); } + +template<> inline uint8_t Reference::As<uint8_t>() { return AsUInt8(); } +template<> inline uint16_t Reference::As<uint16_t>() { return AsUInt16(); } +template<> inline uint32_t Reference::As<uint32_t>() { return AsUInt32(); } +template<> inline uint64_t Reference::As<uint64_t>() { return AsUInt64(); } + +template<> inline double Reference::As<double>() { return AsDouble(); } +template<> inline float Reference::As<float>() { return AsFloat(); } + +template<> inline String Reference::As<String>() { return AsString(); } +template<> inline std::string Reference::As<std::string>() { return AsString().str(); } + +template<> inline Blob Reference::As<Blob>() { return AsBlob(); } +template<> inline Vector Reference::As<Vector>() { return AsVector(); } +template<> inline TypedVector Reference::As<TypedVector>() { return AsTypedVector(); } +template<> inline FixedTypedVector Reference::As<FixedTypedVector>() { return AsFixedTypedVector(); } +template<> inline Map Reference::As<Map>() { return AsMap(); } + inline uint8_t PackedType(BitWidth bit_width, Type type) { return static_cast<uint8_t>(bit_width | (type << 2)); } diff --git a/tests/test.cpp b/tests/test.cpp index a698b128..04299436 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1687,6 +1687,13 @@ void FlexBuffersTest() { TEST_EQ(vec[2].AsString().IsTheEmptyString(), true); // Wrong Type. TEST_EQ_STR(vec[2].AsString().c_str(), ""); // This still works though. TEST_EQ_STR(vec[2].ToString().c_str(), "4.0"); // Or have it converted. + + // Few tests for templated version of As. + TEST_EQ(vec[0].As<int64_t>(), -100); + TEST_EQ_STR(vec[1].As<std::string>().c_str(), "Fred"); + TEST_EQ(vec[1].As<int64_t>(), 0); // Number parsing failed. + TEST_EQ(vec[2].As<double>(), 4.0); + // Test that the blob can be accessed. TEST_EQ(vec[3].IsBlob(), true); auto blob = vec[3].AsBlob(); |