diff options
Diffstat (limited to 'modules/codec/c++/csv.h')
-rw-r--r-- | modules/codec/c++/csv.h | 47 |
1 files changed, 47 insertions, 0 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){ |