From 7779119ea724987128ebd4bb4c0433dee8b5f877 Mon Sep 17 00:00:00 2001 From: Claudius Holeksa Date: Wed, 14 Jun 2023 12:51:20 +0200 Subject: c++: Let test run in test-case derivation and ammend changed schema dimension changes --- src/codec-json/json.tmpl.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++ src/codec/data.h | 25 +++++++++++++-- src/codec/schema.h | 6 ++-- 3 files changed, 102 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/codec-json/json.tmpl.h b/src/codec-json/json.tmpl.h index 38959b2..0d1c7f0 100644 --- a/src/codec-json/json.tmpl.h +++ b/src/codec-json/json.tmpl.h @@ -227,5 +227,81 @@ struct json_encode...>, RootSchema, FromEnc return void_t{}; } }; + +template +struct json_encode...>, RootSchema, FromEncode> { + + template + static error_or encode_element(const data...>, FromEncode>& from, buffer& to){ + if(from.template holds_alternative::literal>()){ + // Encode the name + { + std::string_view view = parameter_key_pack_type::literal.view(); + error err = to.push('"'); + if(!err.template is_type()){ + return err; + } + err = to.push(*reinterpret_cast(view.data()), view.size()); + if(!err.template is_type()){ + return err; + } + err = to.push('"'); + if(!err.template is_type()){ + return err; + } + } + // Add the separator + { + auto eov_ele = to.push(':'); + if(!eov_ele.template is_type()){ + return eov_ele; + } + } + + // Encode the value + auto eov = json_encode::type, RootSchema, FromEncode>::encode(from.template get::literal>(), to); + } + // Go to the next element + if constexpr ( (i+1) < sizeof...(T)){ + { + auto eov_ele = encode_element(from, to); + if(eov_ele.is_error()){ + return eov_ele; + } + } + + + } + + return void_t{}; + } + static error_or encode(const data...>, FromEncode>& from, buffer& to) { + { + auto err = to.push('{'); + if(!err.template is_type()){ + return err; + } + } + if constexpr ( sizeof...(T) > 0 ){ + auto eov = encode_element<0>(from, to); + if(eov.is_error()){ + return eov; + } + } + { + auto err = to.push('}'); + if(!err.template is_type()){ + return err; + } + } + + return void_t{}; + } +}; + +template +struct json_decode; + + } } diff --git a/src/codec/data.h b/src/codec/data.h index 469e214..5e6fd25 100644 --- a/src/codec/data.h +++ b/src/codec/data.h @@ -75,6 +75,27 @@ private: static_assert(always_false, "Type not supported"); }; +template +class data...>, encode::Native> { +private: + std::variant...> value_; +public: + data() = default; + + SAW_DEFAULT_COPY(data); + SAW_DEFAULT_MOVE(data); + + template + bool holds_alternative() const { + return std::holds_alternative::value>(value_); + } + + template + data::value>::type, encode::Native>& get(){ + return std::get::value>(value_); + } +}; + template class data...>, encode::Native> { private: @@ -135,8 +156,8 @@ public: } }; -template -class data, encode::Native> { +template +class data, encode::Native> { private: std::vector> value_; public: diff --git a/src/codec/schema.h b/src/codec/schema.h index df9f443..2a548df 100644 --- a/src/codec/schema.h +++ b/src/codec/schema.h @@ -26,17 +26,17 @@ template struct Union { template struct Union...> {}; -template struct Array {}; +template struct Array {}; template struct is_array { constexpr static bool value = false; }; -template struct is_array> { +template struct is_array> { constexpr static bool value = true; }; -template struct FixedArray {}; +template struct FixedArray {}; template struct Tuple {}; -- cgit v1.2.3