diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-01-08 19:03:09 +0100 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-01-08 19:03:09 +0100 |
| commit | 87db03e9ffa559eaf5f6b970ad42776f33ae34b2 (patch) | |
| tree | 08cb174ef076709c5a8d35c919a3554df532756b | |
| parent | 043325c3a8ac3c4856a7b6944918e15cb7c36938 (diff) | |
| download | forstio-forstio-87db03e9ffa559eaf5f6b970ad42776f33ae34b2.tar.gz | |
dangling
| -rw-r--r-- | modules/codec/c++/data.hpp | 3 | ||||
| -rw-r--r-- | modules/codec/c++/data_print.hpp | 94 | ||||
| -rw-r--r-- | modules/codec/tests/args.cpp | 2 | ||||
| -rw-r--r-- | modules/codec/tests/forst.cpp | 18 | ||||
| -rw-r--r-- | modules/codec/tests/print.cpp | 57 | ||||
| -rw-r--r-- | modules/core/c++/test/suite.hpp | 2 |
6 files changed, 172 insertions, 4 deletions
diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index dca98ac..585501b 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -82,8 +82,9 @@ struct native_data_type<schema::Primitive<schema::FloatingPoint, 8>> { using type = double; }; +// Give a variable for setting the default encoding. Maybe add a static assert to guarantee that this encoding supports immediate access? #ifndef FORSTIO_DEFAULT_DATA_ENCODING -#define FORSTIO_DEFAULT_DATA_ENCODING encode::Native +#define FORSTIO_DEFAULT_DATA_ENCODING ::saw::encode::Native #endif template<typename T, typename Encoding = FORSTIO_DEFAULT_DATA_ENCODING> diff --git a/modules/codec/c++/data_print.hpp b/modules/codec/c++/data_print.hpp new file mode 100644 index 0000000..8eba274 --- /dev/null +++ b/modules/codec/c++/data_print.hpp @@ -0,0 +1,94 @@ +#pragma once + +#include "data.hpp" +#include <iostream> + +namespace saw { +namespace impl { +struct data_print_tab_helper { + static void apply(std::ostream& o, uint64_t tabs){ + for(uint64_t i = 0u; i < tabs; ++i){ + o<<'\t'; + } + } +}; + +template<typename Schema, typename Encode> +struct data_print_std_helper { + static void apply(std::ostream& o, const data<Schema,Encode>& dat){ + // Blueprint + (void)o; + (void)dat; + } +}; + +template<typename T, uint64_t N, typename Encode> +struct data_print_std_helper<schema::Primitive<T,N>, Encode> { + static void apply(std::ostream& o, const data<schema::Primitive<T,N>,Encode>& dat, uint64_t tabs){ + (void) tabs; + // Blueprint + o<<dat.get(); + } +}; + +template<typename Encode> +struct data_print_std_helper<schema::String, Encode> { + static void apply(std::ostream& o, const data<schema::String,Encode>& dat, uint64_t tabs){ + (void) tabs; + // Blueprint + auto size = dat.size(); + o<<"\""; + for(saw::data<schema::UInt64> i{0u}; i < size; ++i){ + o<<dat.at(i); + } + o<<"\""; + } +}; +template<typename T, uint64_t N, typename Encode> +struct data_print_std_helper<schema::Array<T,N>, Encode> { + static void apply(std::ostream& o, const data<schema::Array<T,N>,Encode>& dat, uint64_t tabs){ + (void) tabs; + // Blueprint + auto size = dat.size(); + o<<"\""; + for(saw::data<schema::UInt64> i{0u}; i < size; ++i){ + o<<dat.at(i); + } + o<<"\""; + } +}; + +template<typename... Members, typename Encode> +struct data_print_std_helper<schema::Struct<Members...>, Encode> { + template<uint64_t i> + static void apply(std::ostream& o, const data<schema::Struct<Members...>,Encode>& dat, uint64_t tabs){ + if constexpr (i < sizeof...(Members)){ + using Memb = parameter_pack_type<i,Members...>::type; + + data_print_tab_helper::apply(o,tabs); + o<<"- \""<<Memb::KeyLiteral.view()<<"\" : "; + data_print_std_helper<typename Memb::ValueType,Encode>::apply(o,dat.template get<Memb::KeyLiteral>(), tabs); + o<<"\n"; + apply<i+1u>(o,dat,tabs); + } + } + + static void apply(std::ostream& o, const data<schema::Struct<Members...>,Encode>& dat, uint64_t tabs){ + data_print_tab_helper::apply(o,tabs); + + apply<0u>(o,dat,tabs+1u); + } +}; + +} +} + +namespace std { +template<typename Schema, typename Encode> +inline ostream& operator<<(ostream& o, const saw::data<Schema, Encode>& dat){ + + saw::impl::data_print_std_helper<Schema,Encode>::apply(o, dat, 0u); + + return o; +} +} diff --git a/modules/codec/tests/args.cpp b/modules/codec/tests/args.cpp index f42056a..2b5b14c 100644 --- a/modules/codec/tests/args.cpp +++ b/modules/codec/tests/args.cpp @@ -40,7 +40,7 @@ SAW_TEST("Codec Args Decode Basic"){ codec<sch::Args<sch::ArgsStruct, sch::ArgsTuple>, encode::Args> args_codec; auto eov = args_codec.decode(dat_args, dat_nat); - SAW_EOV_EXPECT(eov, "Couldn't decode data. "); + SAW_EXPECT_EOV(eov, "Couldn't decode data"); auto& prog = dat_nat.template get<"program">(); auto& str = dat_nat.template get<"args">(); diff --git a/modules/codec/tests/forst.cpp b/modules/codec/tests/forst.cpp index 1d9c0c3..2bf6442 100644 --- a/modules/codec/tests/forst.cpp +++ b/modules/codec/tests/forst.cpp @@ -18,7 +18,7 @@ using TestStruct = Struct< using TestArray = Array< TestStruct >; - +/* SAW_TEST("Codec Forst Layer Info"){ using namespace saw; @@ -82,5 +82,21 @@ SAW_TEST("Codec Forst Primitive"){ SAW_EXPECT(val == foo, "Unexpected value"); } + +SAW_TEST("Codec Forst Struct of Primitives"){ + using namespace saw; + + data<TestStruct, encode::KelForst> test_dat; + + auto& test_a = test_dat.template get<"string">(); + auto& test_b = test_dat.template get<"number">(); + auto& test_c = test_dat.template get<"signed">(); + + test_b.set(50u); + test_c.set(-23); + test_a.set("cheese"); + +} +*/ } } diff --git a/modules/codec/tests/print.cpp b/modules/codec/tests/print.cpp new file mode 100644 index 0000000..695f7a0 --- /dev/null +++ b/modules/codec/tests/print.cpp @@ -0,0 +1,57 @@ +#include <forstio/test/suite.hpp> +#include "../c++/data_print.hpp" + +namespace { +namespace sch { +using namespace saw::schema; + +using TestStruct = Struct< + Member<Float64, "foo">, + Member<UInt64, "bar">, + Member<Int64, "baz">, + Member<String, "banana"> +>; +} + +/* +SAW_TEST("Math/Basic"){ + using namespace saw; + + data<sch::Vector<sch::Float64,2u>> a; + data<sch::Vector<sch::Float64,2u>> b; + + auto c = a + b; +} +*/ + +SAW_TEST("Data Print/Primitive Float64"){ + using namespace saw; + + + data<sch::Float64> a{40.1}; + + std::stringstream oss; + oss<<a; + + auto str = oss.str(); + SAW_EXPECT(str == std::string_view{"40.1"}, std::string{"Unexpected value: "} + str); +} + +SAW_TEST("Data Print/Struct"){ + using namespace saw; + + + data<sch::TestStruct> a; + + auto& banana = a.template get<"banana">(); + + banana.set("banned"); + + std::stringstream oss; + oss<<a; + + auto str = oss.str(); + std::cout<<str; + //SAW_EXPECT(str == std::string_view{"40.1"}, std::string{"Unexpected value: "} + str); +} +} diff --git a/modules/core/c++/test/suite.hpp b/modules/core/c++/test/suite.hpp index 7be7e87..f1d0662 100644 --- a/modules/core/c++/test/suite.hpp +++ b/modules/core/c++/test/suite.hpp @@ -43,7 +43,7 @@ public: throw std::runtime_error{std::string{msg}};\ } -#define SAW_EOV_EXPECT(eov_expr, msg_split) \ +#define SAW_EXPECT_EOV(eov_expr, msg_split) \ if( (eov_expr.is_error()) ){ \ auto& err = eov_expr.get_error(); \ std::stringstream sstr; \ |
