diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-04-20 17:12:55 +0200 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-04-20 17:12:55 +0200 |
commit | 0d32f9b2c73690fa188d0f5920dfa9cbe3005200 (patch) | |
tree | 7728773c6309e5977fe7d0efd1c718c33147316b | |
parent | d659724e48ce4a703a99b3414b97d582da7cd173 (diff) |
Added Unit printing
-rw-r--r-- | modules/codec-unit/c++/unit_print.hpp | 54 | ||||
-rw-r--r-- | modules/codec-unit/tests/codec-unit.cpp | 6 |
2 files changed, 60 insertions, 0 deletions
diff --git a/modules/codec-unit/c++/unit_print.hpp b/modules/codec-unit/c++/unit_print.hpp new file mode 100644 index 0000000..724e9d6 --- /dev/null +++ b/modules/codec-unit/c++/unit_print.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include <iostream> + +namespace std { +template<typename UT, int64_t UE> +inline ostream& operator<<(ostream& o, const saw::schema::UnitElement<UT,UE>&){ + o<<UT::short_name; + if constexpr (UE != 1){ + o<<'^'<<'('<<UE<<')'; + } + return o; +} +} + +namespace saw { +namespace impl { +template<typename... T> +struct unit_print { + static_assert(always_false<T...>, "Template type not supported"); +}; + +template<typename UT, int64_t UE, typename... UTL, int64_t... UEL> +struct unit_print<schema::UnitElement<UT,UE>, schema::UnitElement<UTL,UEL>...> { + static std::ostream& print(std::ostream& o){ + schema::UnitElement<UT,UE> element; + + std::ostream& o_ret = o << element; + + if constexpr (sizeof...(UTL) > 0) { + std::ostream& o_ret_next = o_ret << ' ' << '*' << ' '; + return unit_print<schema::UnitElement<UTL,UEL>...>::print(o_ret_next); + } + + return o_ret<<']'; + } +}; +} +} + +namespace std { +template<typename Store, typename Encode, typename... T> +inline ostream& operator<<(ostream& o, const saw::data<saw::schema::Unit<Store,T...>, Encode>& unit); + +template<typename StorageT, typename Encode, typename... UnitT, int64_t... UnitE> +inline ostream& operator<<(ostream& o, const saw::data<saw::schema::Unit<StorageT,saw::schema::UnitElement<UnitT,UnitE>...>, Encode>& unit){ + o << unit.handle().get(); + if constexpr (sizeof...(UnitT) > 0) { + auto& o_ret = o << ' '<<'['; + return saw::impl::unit_print<saw::schema::UnitElement<UnitT,UnitE>...>::print(o_ret); + } + return o; +} +} diff --git a/modules/codec-unit/tests/codec-unit.cpp b/modules/codec-unit/tests/codec-unit.cpp index 99645bd..6ffc4fc 100644 --- a/modules/codec-unit/tests/codec-unit.cpp +++ b/modules/codec-unit/tests/codec-unit.cpp @@ -3,6 +3,7 @@ #include <forstio/codec/schema.hpp> #include "../c++/unit.hpp" +#include "../c++/unit_print.hpp" namespace { namespace sch { @@ -60,4 +61,9 @@ SAW_TEST("Codec Unit/Division"){ SAW_EXPECT(c == data<sch::Scalar<sch::Int64>>{5u}, "Expected result 5"); } + +SAW_TEST("Codec Unit/Print"){ + using namespace saw; + // TODO +} } |