diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-06-26 09:39:34 +0200 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-06-26 09:39:34 +0200 |
commit | 729307460e77f62a532ee9841dcaed9c47f46419 (patch) | |
tree | 0b52ddbfa47d9d148907de90e7a2987d72ed7d73 /modules/core | |
parent | 51b50882d2906b83c5275c732a56ff333ae6696f (diff) |
Added better structure for the data server
Diffstat (limited to 'modules/core')
-rw-r--r-- | modules/core/c++/reduce_templates.hpp | 41 | ||||
-rw-r--r-- | modules/core/c++/templates.hpp | 14 | ||||
-rw-r--r-- | modules/core/tests/core.cpp | 23 |
3 files changed, 78 insertions, 0 deletions
diff --git a/modules/core/c++/reduce_templates.hpp b/modules/core/c++/reduce_templates.hpp new file mode 100644 index 0000000..ef5fa4c --- /dev/null +++ b/modules/core/c++/reduce_templates.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "templates.hpp" + +namespace saw { + +namespace impl { +template<typename C, typename T> +struct tmpl_group_reduce_match { + static constexpr bool has_type = false; + using type = saw::tmpl_group<C>; +}; + +template<typename T0, typename TL0, typename... TL> +struct tmpl_group_reduce_match<T0, tmpl_group<TL0,TL...>> { + static constexpr bool has_type = std::is_same_v<T0,TL0> or tmpl_group_reduce_match<T0, tmpl_group<TL...>>::has_type; + + using type = typename std::conditional<has_type, tmpl_group<TL0,TL...>, tmpl_group<T0, TL0, TL...>>::type; +}; + +template<typename T> +struct tmpl_group_reduce { + using reduced_type = T; +}; + +/** + * Reducing in outer loop + */ +template<typename... TL, typename T0> +struct tmpl_group_reduce<tmpl_group<T0,TL...>> { + using reduced_inner_list = typename tmpl_group_reduce<tmpl_group<TL...>>::reduced_type; + + using reduced_type = typename tmpl_group_reduce_match<T0,reduced_inner_list>::type; +}; +} + +template<typename T> +struct tmpl_reduce { + using type = typename impl::tmpl_group_reduce<T>::reduced_type; +}; +} diff --git a/modules/core/c++/templates.hpp b/modules/core/c++/templates.hpp index acbaeb0..70836ae 100644 --- a/modules/core/c++/templates.hpp +++ b/modules/core/c++/templates.hpp @@ -5,6 +5,20 @@ namespace saw { +/** + * This type is meant for grouping of template types + */ +template <class... T> +struct tmpl_group {}; + +template<typename T, typename U> +struct tmpl_concat; + +template<typename... T, typename... U> +struct tmpl_concat<tmpl_group<T...>, tmpl_group<U...>> { + using type = tmpl_group<T..., U...>; +}; + template <class T, class... TL> struct parameter_pack_index; template <class T, class... TL> struct parameter_pack_index<T, T, TL...> { diff --git a/modules/core/tests/core.cpp b/modules/core/tests/core.cpp index b1ce741..f418a07 100644 --- a/modules/core/tests/core.cpp +++ b/modules/core/tests/core.cpp @@ -1,6 +1,7 @@ #include "../c++/test/suite.hpp" #include "../c++/id.hpp" #include "../c++/string_literal.hpp" +#include "../c++/reduce_templates.hpp" namespace { SAW_TEST("ID functionality") { @@ -37,4 +38,26 @@ SAW_TEST("String Literal Append"){ SAW_EXPECT(c == "foobar", "CT String sum is not \"foobar\""); } + +SAW_TEST("Template Group Reduction"){ + using namespace saw; + + struct foo { + std::string name = "foo"; + }; + struct bar { + std::string name = "bar"; + }; + struct baz { + std::string name = "baz"; + }; + + using grp = tmpl_group<foo, bar, baz, foo, bar, foo>; + using red_grp = tmpl_group<baz, bar, foo>; + + using alg_red_grp = tmpl_reduce<grp>::type; + + static_assert(std::is_same_v<alg_red_grp, red_grp>, "Should be same type"); + SAW_EXPECT((std::is_same_v<alg_red_grp, red_grp>), "Should be same type"); +} } |