From 8d1f860c8f96b0b366729cf18fc08f59ccac3934 Mon Sep 17 00:00:00 2001 From: fraillt Date: Fri, 17 Feb 2017 16:21:23 +0200 Subject: [PATCH] default values --- include/Deserializer.h | 14 ++++++++++++++ include/Serializer.h | 26 ++++++++++++++++++++++++++ tests/SerializerObjectsTests.cpp | 1 - 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/include/Deserializer.h b/include/Deserializer.h index c2aecac..cf4e739 100644 --- a/include/Deserializer.h +++ b/include/Deserializer.h @@ -58,6 +58,20 @@ public: return *this; } + /* + * default overloads + */ + template + Deserializer& defaultV(T& v, const std::array& defValues) { + size_t index; + _reader.template readBits(index, calcRequiredBits({}, N + 1)); + if (index) + v = defValues[index-1]; + else + ProcessAnyType::serialize(*this, v); + }; + + /* * text overloads */ diff --git a/include/Serializer.h b/include/Serializer.h index 3090bc1..38ae1f0 100644 --- a/include/Serializer.h +++ b/include/Serializer.h @@ -57,6 +57,17 @@ public: return *this; } + /* + * default overloads + */ + template + Serializer& defaultV(const T& v, const std::array& defValues) { + auto index = findDefault(v, defValues); + _writter.template writeBits(index, calcRequiredBits({}, N + 1)); + if (!index) + ProcessAnyType::serialize(*this, v); + }; + /* * text overloads */ @@ -211,5 +222,20 @@ auto getRangeValue(const T& v, const RangeSpec& r) { return static_cast(ratio * maxUint); }; +/* + * functions for default + */ + +template +size_t findDefault(const T& v, const std::array& defValues) { + auto index{1u}; + for (auto& d:defValues) { + if (d == v) + return index; + ++index; + } + return 0u; +}; + #endif //TMP_SERIALIZER_H diff --git a/tests/SerializerObjectsTests.cpp b/tests/SerializerObjectsTests.cpp index 80bbcf9..067145f 100644 --- a/tests/SerializerObjectsTests.cpp +++ b/tests/SerializerObjectsTests.cpp @@ -59,7 +59,6 @@ SERIALIZE(Y) return s; } - TEST(SerializeObject, GeneralConceptTest) { //std::string buf; SerializationContext ctx;