diff options
-rw-r--r-- | src/codec-json/json.h | 40 | ||||
-rw-r--r-- | src/codec-json/json.tmpl.h | 17 | ||||
-rw-r--r-- | src/codec/data.h | 2 | ||||
-rw-r--r-- | src/codec/forst.h | 13 | ||||
-rw-r--r-- | src/core/error.cpp | 4 | ||||
-rw-r--r-- | src/core/error.h | 4 | ||||
-rw-r--r-- | src/core/templates.h | 2 | ||||
-rw-r--r-- | tests/.nix/derivation.nix | 2 | ||||
-rw-r--r-- | tests/SConstruct | 2 | ||||
-rw-r--r-- | tests/codec-json.cpp | 22 | ||||
-rw-r--r-- | tests/core.cpp | 2 |
11 files changed, 77 insertions, 33 deletions
diff --git a/src/codec-json/json.h b/src/codec-json/json.h index f23aff9..c8456b5 100644 --- a/src/codec-json/json.h +++ b/src/codec-json/json.h @@ -4,40 +4,43 @@ #include <forstio/core/common.h> #include <forstio/codec/data.h> -#include "json.tmpl.h" - #include <algorithm> namespace saw { namespace encode { struct Json {}; } +} + +#include "json.tmpl.h" + +namespace saw { template<typename Schema> class data<Schema, encode::Json> { private: - own<buffer> buffer_; + ring_buffer buffer_; public: - - data(): - buffer_{heap<ring_buffer>()} - {} + data():buffer_{}{} buffer& get_buffer(){ - assert(buffer_); - return *buffer_; + return buffer_; + } + + void push(uint8_t val){ + buffer_.push(val); } std::size_t get_size() const { - return buffer_.size(); + return buffer_.read_composite_length(); } uint8_t& at(std::size_t i){ - return buffer_.at(i); + return buffer_.read(i); } const uint8_t& at(std::size_t i) const { - return buffer_.at(i); + return buffer_.read(i); } }; @@ -57,7 +60,7 @@ public: /** * Default constructor */ - config(){} + codec(){} /** * Constructor @@ -68,16 +71,15 @@ public: SAW_DEFAULT_MOVE(codec); template <typename FromEncoding> - ErrorOr<void> encode(const data<Schema, FromEncoding>& from_encode, data<Schema, encode::Json>& to_encode){ + error_or<void> encode(const data<Schema, FromEncoding>& from_encode, data<Schema, encode::Json>& to_encode){ // To Be encoded - - - return Void {}; + return impl::json_encode<Schema, Schema, FromEncoding>::encode(from_encode, to_encode); } template <typename ToEncoding> - ErrorOr<void> decode(const data<Schema, encode::Json>& from_decode, data<Schema, ToEncoding>& to_decode){ - return Void {}; + error_or<void> decode(const data<Schema, encode::Json>& from_decode, data<Schema, ToEncoding>& to_decode){ + return void_t {}; } }; } + diff --git a/src/codec-json/json.tmpl.h b/src/codec-json/json.tmpl.h index 3699e10..a19e3dc 100644 --- a/src/codec-json/json.tmpl.h +++ b/src/codec-json/json.tmpl.h @@ -5,23 +5,23 @@ namespace saw { namespace impl { template<typename Schema, typename RootSchema, typename FromEncode> -class json_encode_impl { +class json_encode { static_assert(always_false<Schema, RootSchema, FromEncode>, "This schema type is not being handle by the json encoding."); }; template<typename T, size_t N, typename RootSchema, typename FromEncode> -class json_encode_impl<saw::schema::Primitive<T,N>, RootSchema, FromEncode> { - static ErrorOr<void> encode(const data<Schema, FromEncode>& from, data<Schema, encode::Json>& to, size_t ptr) { +struct json_encode<saw::schema::Primitive<T,N>, RootSchema, FromEncode> { + static error_or<void> encode(const data<saw::schema::Primitive<T,N>, FromEncode>& from, data<saw::schema::Primitive<T,N>, encode::Json>& to) { auto val = from.get(); std::array<uint8_t, 256> data; - auto tc_result = std::to_chars(reinterpret_cast<int8_t>(data.data()), reinrepret_cast<int8_t>(data.data())+data.size(), val); + auto tc_result = std::to_chars(reinterpret_cast<char*>(data.data()), reinterpret_cast<char*>(data.data())+data.size(), val); if(tc_result.ec != std::errc{}){ return make_error<err::critical>(); } size_t bytes_written = 0; - for(auto ptr = data.data(); ptr != tc_result.ptr; ++ptr){ + for(char* ptr = reinterpret_cast<char*>(data.data()); ptr != tc_result.ptr; ++ptr){ ++bytes_written; } @@ -31,11 +31,12 @@ class json_encode_impl<saw::schema::Primitive<T,N>, RootSchema, FromEncode> { return std::move(err); } - for(auto ptr = data.data(); ptr != tc_result.ptr; ++ptr){ - buff.push(ptr[0]); + for(char* ptr = reinterpret_cast<char*>(data.data()); ptr != tc_result.ptr; ++ptr){ + uint8_t* un_ptr = reinterpret_cast<uint8_t*>(ptr); + buff.push(un_ptr[0]); } - return Void{}; + return void_t{}; } }; } diff --git a/src/codec/data.h b/src/codec/data.h index 30b5239..895a2fb 100644 --- a/src/codec/data.h +++ b/src/codec/data.h @@ -11,6 +11,8 @@ namespace saw { namespace encode { struct Native {}; } +template<typename Schema, typename Encode> +class codec; /* * Helper for the basic message container, so the class doesn't have to be * specialized 10 times. diff --git a/src/codec/forst.h b/src/codec/forst.h new file mode 100644 index 0000000..fee5fa6 --- /dev/null +++ b/src/codec/forst.h @@ -0,0 +1,13 @@ +#pragma once + +#include "data.h" + +namespace saw { +namespace encode { +struct Forst {}; +} + +class data<schema::String, encode::Forst> { + +}; +} diff --git a/src/core/error.cpp b/src/core/error.cpp index 7258740..ef8dc57 100644 --- a/src/core/error.cpp +++ b/src/core/error.cpp @@ -32,7 +32,7 @@ const std::string_view error::get_message() const { } bool error::failed() const { - return this->is_error<err::no_error>(); + return this->is_type<err::no_error>(); } bool error::is_critical() const { @@ -104,7 +104,7 @@ error_or<error::code> error_registry::search_or_register_id(const std::string_vi auto& err = err_or_id.get_error(); - if(err.is_error<err::not_found>()){ + if(err.is_type<err::not_found>()){ size_t new_index = infos.size(); if(new_index == std::numeric_limits<error::code>::max()){ return make_error<err::out_of_memory>("Error registry ids are exhausted"); diff --git a/src/core/error.h b/src/core/error.h index 2ed7cd4..32eab1e 100644 --- a/src/core/error.h +++ b/src/core/error.h @@ -48,7 +48,7 @@ public: code get_id() const; template<typename T> - bool is_error() const; + bool is_type() const; }; template<typename T> @@ -225,7 +225,7 @@ private: }; template<typename T> -bool error::is_error() const { +bool error::is_type() const { return error_code_ == impl::get_template_id<T>(); } diff --git a/src/core/templates.h b/src/core/templates.h index 9b4bcac..6c0a74e 100644 --- a/src/core/templates.h +++ b/src/core/templates.h @@ -1,5 +1,7 @@ #pragma once +#include "string_literal.h" + namespace saw { template <class T, class... TL> struct parameter_pack_index; diff --git a/tests/.nix/derivation.nix b/tests/.nix/derivation.nix index d5a698c..e6a109a 100644 --- a/tests/.nix/derivation.nix +++ b/tests/.nix/derivation.nix @@ -21,6 +21,8 @@ stdenvNoCC.mkDerivation { forstio.core forstio.async forstio.test + forstio.codec + forstio.codec-json ]; outputs = ["out" "dev"]; diff --git a/tests/SConstruct b/tests/SConstruct index 98eccdb..4434d20 100644 --- a/tests/SConstruct +++ b/tests/SConstruct @@ -46,7 +46,7 @@ env_vars.Add('prefix', env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], CPPDEFINES=['SAW_UNIX'], CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], - LIBS=['forstio-core', 'forstio-async']) + LIBS=['forstio-core', 'forstio-async', 'forstio-test']) env.__class__.add_source_files = add_kel_source_files env.Tool('compilation_db'); env.cdb = env.CompilationDatabase('compile_commands.json'); diff --git a/tests/codec-json.cpp b/tests/codec-json.cpp new file mode 100644 index 0000000..d82db14 --- /dev/null +++ b/tests/codec-json.cpp @@ -0,0 +1,22 @@ +#include <forstio/test/suite.h> +#include <forstio/codec/json/json.h> + +namespace { +SAW_TEST("Int32 write"){ + using namespace saw; + data<schema::Int32, encode::Native> native_int; + data<schema::Int32, encode::Json> json_int; + + native_int.set(44123); + + codec<schema::Int32, encode::Json> json_codec; + + error_or<void> eov = json_codec.encode(native_int, json_int); + SAW_EXPECT(eov.is_value(), "Encoding error"); + + std::string_view str_view = "44123"; + for(std::size_t i = 0; i < str_view.size(); ++i){ + SAW_EXPECT( json_int.at(i) == str_view[i], "Value is not being encoded correctly" ); + } +} +} diff --git a/tests/core.cpp b/tests/core.cpp index af19b42..0ddfcd6 100644 --- a/tests/core.cpp +++ b/tests/core.cpp @@ -1,7 +1,7 @@ #include <forstio/test/suite.h> namespace { -SAW_TEST { +SAW_TEST("Test") { } } |