From 70a3abcb3aad4c5e74b4b9fa6ac76508ac157f55 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 5 Jan 2024 05:58:49 +0100 Subject: codec: Adding a basic csv decoder. Unfinished --- modules/codec/c++/csv.h | 47 +++++++++++++++++++++++++++++++++++++ modules/codec/c++/schema.h | 48 +++++++++++++++++++++++++++++++------- modules/codec/c++/schema_factory.h | 3 +++ 3 files changed, 90 insertions(+), 8 deletions(-) (limited to 'modules/codec/c++') diff --git a/modules/codec/c++/csv.h b/modules/codec/c++/csv.h index 67c2c1d..3727829 100644 --- a/modules/codec/c++/csv.h +++ b/modules/codec/c++/csv.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "data.h" namespace saw { @@ -7,8 +9,53 @@ namespace encode { struct Csv {}; } +namespace impl { +template +struct csv_encode { + static_assert(always_false, "Case not supported"); +}; + +template +struct csv_encode, FromDecode> { + static_assert(Dim == 1, "Only one dimension is allowed."); + static_assert(!is_array::value, "Array of an array is not allowed."); + static_assert(is_tuple::value || is_struct::value, "Only struct or tuples allowed inside a csv array"); + + using Schema = schema::Array; + + static error_or encode(const data& from, data& to){ + if constexpr (is_struct::value){ + auto eov = csv_encode::encode_header(to); + if(eov.is_error()){ + return eov; + } + } + + for(std::size_t i = 0; i < from.size(); ++i){ + auto eov = csv_encode::encode(from.at(i), to); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + } +}; + +template<> +struct csv_encode { + using Schema = schema::String; + + static error_or encode(const data& from, data& to){ + + return void_t{}; + } +}; +} + template class codec { + static_assert(is_array::value, "Only an Array is allowed as a base value"); public: template static error_or encode(const data& from, data& to){ diff --git a/modules/codec/c++/schema.h b/modules/codec/c++/schema.h index 69233fa..576f378 100644 --- a/modules/codec/c++/schema.h +++ b/modules/codec/c++/schema.h @@ -28,14 +28,6 @@ struct Union...> {}; template struct Array {}; -template struct is_array { - constexpr static bool value = false; -}; - -template struct is_array> { - constexpr static bool value = true; -}; - template struct FixedArray {}; template struct Tuple {}; @@ -55,6 +47,7 @@ class Wrapper {}; struct String {}; + struct SignedInteger {}; struct UnsignedInteger {}; struct FloatingPoint {}; @@ -106,4 +99,43 @@ struct Interface,Names>...> {}; // NOLINTEND } // namespace schema +template struct is_struct { + constexpr static bool value = false; +}; + +template struct is_struct...>> { + constexpr static bool value = true; +}; + +template struct is_string { + constexpr static bool value = false; +}; + +template <> struct is_string { + constexpr static bool value = true; +}; + +template struct is_tuple { + constexpr static bool value = false; +}; + +template struct is_tuple> { + constexpr static bool value = true; +}; + +template struct is_array { + constexpr static bool value = false; +}; + +template struct is_array> { + constexpr static bool value = true; +}; + +template struct is_primitive { + constexpr static bool value = false; +}; + +template struct is_primitive> { + constexpr static bool value = true; +}; } // namespace saw diff --git a/modules/codec/c++/schema_factory.h b/modules/codec/c++/schema_factory.h index 91094fe..47185be 100644 --- a/modules/codec/c++/schema_factory.h +++ b/modules/codec/c++/schema_factory.h @@ -39,6 +39,9 @@ struct schema_factory> { } }; +/** + * This creates the base schema + */ template constexpr schema_factory build_schema() noexcept { return {}; -- cgit v1.2.3