From f27d397e9ab2d5c020b04dde4361fffa933f103c Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Thu, 5 Sep 2024 12:51:35 +0200 Subject: Implemented basic class and operations --- modules/codec-unit/c++/unit.hpp | 12 +++- modules/codec-unit/c++/unit.tmpl.hpp | 32 ++++++++-- modules/codec-unit/c++/unit_transform.hpp | 102 ++++++++++++++++++++++++++---- modules/codec-unit/tests/codec-unit.cpp | 56 ++++++++++++++-- 4 files changed, 181 insertions(+), 21 deletions(-) diff --git a/modules/codec-unit/c++/unit.hpp b/modules/codec-unit/c++/unit.hpp index 986ca8f..0e7bf74 100644 --- a/modules/codec-unit/c++/unit.hpp +++ b/modules/codec-unit/c++/unit.hpp @@ -3,6 +3,8 @@ #include "unit_schema.hpp" #include "unit_transform.hpp" +#include + namespace saw { template class data, encode::Native> { @@ -19,7 +21,15 @@ public: data operator+(const data& rhs) const; data operator-(const data& rhs) const; + + bool operator==(const data& rhs) const; + + template + data::Schema, encode::Native> operator*(const data& rhs) const; + + template + data::Schema, encode::Native> operator/(const data& rhs) const; }; } -#include "unit.tmpl.h" +#include "unit.tmpl.hpp" diff --git a/modules/codec-unit/c++/unit.tmpl.hpp b/modules/codec-unit/c++/unit.tmpl.hpp index 7412199..08edc4f 100644 --- a/modules/codec-unit/c++/unit.tmpl.hpp +++ b/modules/codec-unit/c++/unit.tmpl.hpp @@ -3,17 +3,41 @@ namespace saw { template data, encode::Native> data, encode::Native>::operator+(const -data, encode::Native>& rhs){ +data, encode::Native>& rhs) const { auto add = dat_ + rhs.dat_; - return {add}; + return add; } template data, encode::Native> data, encode::Native>::operator-(const -data, encode::Native>& rhs){ +data, encode::Native>& rhs) const { auto sub = dat_ - rhs.dat_; - return {sub}; + return sub; +} + +template +bool +data, encode::Native>::operator==(const data& rhs) const { + return dat_ == rhs.dat_; +} + +template +template +data, UnitRhs>::Schema, encode::Native> +data, encode::Native>::operator*(const +data& rhs) const { + auto mult = dat_ * rhs.dat_; + return mult; +} + +template +template +data, UnitRhs>::Schema, encode::Native> +data, encode::Native>::operator/(const +data& rhs) const { + auto div = dat_ / rhs.dat_; + return div; } } diff --git a/modules/codec-unit/c++/unit_transform.hpp b/modules/codec-unit/c++/unit_transform.hpp index 3cae3c5..2e1ace4 100644 --- a/modules/codec-unit/c++/unit_transform.hpp +++ b/modules/codec-unit/c++/unit_transform.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include "unit_schema.hpp" @@ -13,40 +15,116 @@ template class unit_redux_list { static_assert(sizeof...(T) == 0u, "Template type not supported"); - using reduced_type = unit_redux_list<>; + using Type = unit_redux_list<>; +}; + +template +struct unit_redux_list, schema::UnitElement...> { + using Type = typename unit_matching, schema::UnitElement...>, unit_redux_list<>>::TypeList; +}; + +template +class unit_matching_reduce { +public: + static_assert(always_false, "Template type not supported"); +}; + +/** + * Iterate over the left list to check against the left element. + */ +template +class unit_matching_reduce, unit_redux_list,schema::UnitElement...>, unit_redux_list...>> { +public: + static constexpr bool is_same = std::is_same_v; + + // Match T,E against T0,E0 + using ReducedType = typename std::conditional, schema::UnitElement>::type; + using ReducedTypeList = typename std::conditional...>, unit_redux_list..., schema::UnitElement>>::type; + + using NextMatcher = unit_matching_reduce...>, ReducedTypeList>; + using Type = typename NextMatcher::Type; + using TypeList = typename NextMatcher::TypeList; + static constexpr int64_t Num = NextMatcher::Num; +}; + +/** + * Final step after going through the list. This contains the final match result. + * On the left is the accumulated type and on the right its's a list of non matching types + */ +template +class unit_matching_reduce, unit_redux_list<>, unit_redux_list...>> { +public: + using Type = schema::UnitElement; + using TypeList = unit_redux_list...>; + static constexpr int64_t Num = E; }; template class unit_matching { - static_assert(is_always_false, "Template type not supported"); + static_assert(always_false, "Template type not supported"); }; template class unit_matching, unit_redux_list> { - using Type = unit_redux_list; +public: + using TypeList = unit_redux_list; }; template -class unit_matching, unit_redux_list> { - using Type = +class unit_matching, unit_redux_list> { +public: + // static constexpr bool is_same = std::is_same_v; + + using MatchReducer = unit_matching_reduce, unit_redux_list<>>; + + using ReducedType = typename MatchReducer::Type; + using ReducedTypeList = typename MatchReducer::TypeList; + static constexpr int64_t Num = MatchReducer::Num; + + using TypeRightList = typename std::conditional, unit_redux_list>::type; + using TypeList = typename unit_matching::TypeList; +}; + +template +class unit_add_base { + static_assert(always_false, "Template type not supported"); +}; + +template +class unit_add_base> { +public: + using Schema = schema::Unit; }; } -template +template struct unit_element_reduction { - using Schema = typename impl::unit_matching, impl::unit_redux_list<>>::Schema; + using TypeList = typename impl::unit_matching, impl::unit_redux_list<>>::TypeList; + using Schema = typename impl::unit_add_base::Schema; }; template struct unit_multiplication { - static_assert(is_always_false, "Template type not supported"); + static_assert(always_false, "Template type not supported"); +}; + +template +struct unit_multiplication< + schema::Unit...>,schema::Unit...> +> { + using Schema = typename unit_element_reduction..., schema::UnitElement...>::Schema; +}; + +template +struct unit_division { + static_assert(always_false, "Template type not supported"); }; -template -class unit_multiplication< - schema::Unit...>,schema::Unit...> +template +struct unit_division< + schema::Unit...>,schema::Unit...> > { - using Schema = typename unit_reduction..., schema::UnitElement...>::Schema; + using Schema = typename unit_element_reduction..., schema::UnitElement...>::Schema; }; } diff --git a/modules/codec-unit/tests/codec-unit.cpp b/modules/codec-unit/tests/codec-unit.cpp index 0c88e9d..39e590d 100644 --- a/modules/codec-unit/tests/codec-unit.cpp +++ b/modules/codec-unit/tests/codec-unit.cpp @@ -2,15 +2,63 @@ #include +#include "../c++/unit.hpp" + namespace { namespace sch { using namespace saw::schema; + +struct FanMeterEle {}; + +using FanMeter = Unit< + Int64, + UnitElement +>; + +using FanSquareMeter = Unit< + Int64, + UnitElement +>; +} + + +SAW_TEST("Codec Unit/Addition"){ + using namespace saw; + + data a{{5}}, b{{4}}; + + auto c = a + b; + + SAW_EXPECT(c == data{9u}, "Expected result 9"); +} + +SAW_TEST("Codec Unit/Subtraction"){ + using namespace saw; + + data a{{5}}, b{{4}}; + + auto c = a - b; + + SAW_EXPECT(c == data{1u}, "Expected result 1"); } -/* -SAW_TEST("Dummy Test"){ +SAW_TEST("Codec Unit/Multiplication"){ using namespace saw; - SAW_EXPECT( false, "Dummy" ); + + data a{{5}}, b{{4}}; + + auto c = a * b; + + SAW_EXPECT(c == data{20u}, "Expected result 20"); +} + +SAW_TEST("Codec Unit/Division"){ + using namespace saw; + + data a{{20}}, b{{4}}; + + auto c = a / b; + + SAW_EXPECT(c == data>{5u}, "Expected result 5"); } -*/ } -- cgit v1.2.3