diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-01-05 05:58:49 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-01-05 05:58:49 +0100 |
commit | 70a3abcb3aad4c5e74b4b9fa6ac76508ac157f55 (patch) | |
tree | 07f6516c19fd4f5eda0c7a5c8cb440570eafccfb /modules/codec/c++ | |
parent | 162e5c5da90f4316725086fa6a2ae13b3c22104e (diff) |
codec: Adding a basic csv decoder. Unfinished
Diffstat (limited to 'modules/codec/c++')
-rw-r--r-- | modules/codec/c++/csv.h | 47 | ||||
-rw-r--r-- | modules/codec/c++/schema.h | 48 | ||||
-rw-r--r-- | modules/codec/c++/schema_factory.h | 3 |
3 files changed, 90 insertions, 8 deletions
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 <forstio/error.h> + #include "data.h" namespace saw { @@ -7,8 +9,53 @@ namespace encode { struct Csv {}; } +namespace impl { +template<typename Schema, typename FromDecode> +struct csv_encode { + static_assert(always_false<T>, "Case not supported"); +}; + +template<typename T, size_t Dim> +struct csv_encode<schema::Array<T,Dim>, FromDecode> { + static_assert(Dim == 1, "Only one dimension is allowed."); + static_assert(!is_array<T>::value, "Array of an array is not allowed."); + static_assert(is_tuple<T>::value || is_struct<T>::value, "Only struct or tuples allowed inside a csv array"); + + using Schema = schema::Array<T,Dim>; + + static error_or<void> encode(const data<Schema, FromDecode>& from, data<Schema, encode::Csv>& to){ + if constexpr (is_struct<T>::value){ + auto eov = csv_encode<T>::encode_header(to); + if(eov.is_error()){ + return eov; + } + } + + for(std::size_t i = 0; i < from.size(); ++i){ + auto eov = csv_encode<T>::encode(from.at(i), to); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + } +}; + +template<> +struct csv_encode<schema::String, FromDecode> { + using Schema = schema::String; + + static error_or<void> encode(const data<Schema, FromDecode>& from, data<Schema, encode::Csv>& to){ + + return void_t{}; + } +}; +} + template<typename Schema> class codec<Schema, encode::Csv> { + static_assert(is_array<Schema>::value, "Only an Array is allowed as a base value"); public: template<typename FromEncode> static error_or<void> encode(const data<Schema, FromEncode>& from, data<Schema,encode::Csv>& 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<Member<V, K>...> {}; template <typename T, size_t Dim = 1> struct Array {}; -template <class T> struct is_array { - constexpr static bool value = false; -}; - -template <class T, size_t Dim> struct is_array<schema::Array<T,Dim>> { - constexpr static bool value = true; -}; - template<typename T, size_t... S> struct FixedArray {}; template <typename... T> struct Tuple {}; @@ -55,6 +47,7 @@ class Wrapper {}; struct String {}; + struct SignedInteger {}; struct UnsignedInteger {}; struct FloatingPoint {}; @@ -106,4 +99,43 @@ struct Interface<Member<Function<Requests, Responses>,Names>...> {}; // NOLINTEND } // namespace schema +template <class T> struct is_struct { + constexpr static bool value = false; +}; + +template <class... V, string_literal... K> struct is_struct<schema::Struct<schema::Member<V,K>...>> { + constexpr static bool value = true; +}; + +template <class T> struct is_string { + constexpr static bool value = false; +}; + +template <> struct is_string<schema::String> { + constexpr static bool value = true; +}; + +template <class T> struct is_tuple { + constexpr static bool value = false; +}; + +template <class... T> struct is_tuple<schema::Tuple<T...>> { + constexpr static bool value = true; +}; + +template <class T> struct is_array { + constexpr static bool value = false; +}; + +template <class T, size_t Dim> struct is_array<schema::Array<T,Dim>> { + constexpr static bool value = true; +}; + +template <class T> struct is_primitive { + constexpr static bool value = false; +}; + +template <typename T, size_t N> struct is_primitive<schema::Primitive<T,N>> { + 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<schema::Tuple<T...>> { } }; +/** + * This creates the base schema + */ template<typename T> constexpr schema_factory<T> build_schema() noexcept { return {}; |