From d2fa0c9b46050a3d472337c528171a048751c960 Mon Sep 17 00:00:00 2001 From: Michael Tao Date: Sun, 15 Oct 2023 21:30:38 -0400 Subject: [PATCH 1/2] fixing variant test names to metaprogramming --- tests/test_variant_metaprogramming.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_variant_metaprogramming.cpp b/tests/test_variant_metaprogramming.cpp index eb29269e24..871a769a37 100644 --- a/tests/test_variant_metaprogramming.cpp +++ b/tests/test_variant_metaprogramming.cpp @@ -73,7 +73,7 @@ size_t TestRefType::get_index(const Input& input) } // namespace wmtk::utils::metaprogramming -TEST_CASE("test_variant_multiprogramming", "[metaprogramming]") +TEST_CASE("test_variant_metaprogramming", "[metaprogramming]") { static_assert(std::is_same_v); static_assert(std::is_same_v>); @@ -135,7 +135,7 @@ TEST_CASE("test_variant_multiprogramming", "[metaprogramming]") c_ref); } -TEST_CASE("test_variant_multiprogramming_cache", "[metaprogramming]") +TEST_CASE("test_variant_metaprogramming_cache", "[metaprogramming]") { A a(0); B b(2); From 5ac1610388c345bf71c1f379f14d960c1879a32d Mon Sep 17 00:00:00 2001 From: Michael Tao Date: Sun, 15 Oct 2023 21:41:36 -0400 Subject: [PATCH 2/2] implementation of tuple helpers for removing voids and making things unique --- src/wmtk/utils/CMakeLists.txt | 3 + src/wmtk/utils/metaprogramming/CMakeLists.txt | 12 ++ .../metaprogramming/tuple/CMakeLists.txt | 7 + .../tuple/concatenate_types.hpp | 27 ++++ .../tuple/get_unique_remove_void_types.hpp | 16 ++ .../tuple/get_unique_types.hpp | 74 +++++++++ .../tuple/remove_void_types.hpp | 49 ++++++ tests/CMakeLists.txt | 1 + tests/test_tuple_metaprogramming.cpp | 151 ++++++++++++++++++ 9 files changed, 340 insertions(+) create mode 100644 src/wmtk/utils/metaprogramming/CMakeLists.txt create mode 100644 src/wmtk/utils/metaprogramming/tuple/CMakeLists.txt create mode 100644 src/wmtk/utils/metaprogramming/tuple/concatenate_types.hpp create mode 100644 src/wmtk/utils/metaprogramming/tuple/get_unique_remove_void_types.hpp create mode 100644 src/wmtk/utils/metaprogramming/tuple/get_unique_types.hpp create mode 100644 src/wmtk/utils/metaprogramming/tuple/remove_void_types.hpp create mode 100644 tests/test_tuple_metaprogramming.cpp diff --git a/src/wmtk/utils/CMakeLists.txt b/src/wmtk/utils/CMakeLists.txt index ca0947f014..28c8f9da99 100644 --- a/src/wmtk/utils/CMakeLists.txt +++ b/src/wmtk/utils/CMakeLists.txt @@ -16,3 +16,6 @@ set(SRC_FILES TupleInspector.hpp ) target_sources(wildmeshing_toolkit PRIVATE ${SRC_FILES}) + + +add_subdirectory(metaprogramming) diff --git a/src/wmtk/utils/metaprogramming/CMakeLists.txt b/src/wmtk/utils/metaprogramming/CMakeLists.txt new file mode 100644 index 0000000000..ba8c0deffa --- /dev/null +++ b/src/wmtk/utils/metaprogramming/CMakeLists.txt @@ -0,0 +1,12 @@ +set(SRC_FILES + DerivedReferenceWrapperVariantTraits.hpp + ReferenceWrappedFunctorReturnCache.hpp + ReferenceWrappedFunctorReturnType.hpp + ReferenceWrapperVariant.hpp + as_variant.hpp + unwrap_ref.hpp + ) + +target_sources(wildmeshing_toolkit PRIVATE ${SRC_FILES}) + +add_subdirectory(tuple) diff --git a/src/wmtk/utils/metaprogramming/tuple/CMakeLists.txt b/src/wmtk/utils/metaprogramming/tuple/CMakeLists.txt new file mode 100644 index 0000000000..d2113036cb --- /dev/null +++ b/src/wmtk/utils/metaprogramming/tuple/CMakeLists.txt @@ -0,0 +1,7 @@ +set(SRC_FILES + concatenate_types.hpp + get_unique_remove_void_types.hpp + get_unique_types.hpp + remove_void_types.hpp + ) +target_sources(wildmeshing_toolkit PRIVATE ${SRC_FILES}) diff --git a/src/wmtk/utils/metaprogramming/tuple/concatenate_types.hpp b/src/wmtk/utils/metaprogramming/tuple/concatenate_types.hpp new file mode 100644 index 0000000000..1221f7ed47 --- /dev/null +++ b/src/wmtk/utils/metaprogramming/tuple/concatenate_types.hpp @@ -0,0 +1,27 @@ +#pragma once +#include + +namespace wmtk::utils::metaprogramming::tuple { + + +// creates a tuple that concatenates the types passed in + + +// generic case so we can specialize with tuples passed in +template +struct concatenate_types +{ +}; + +// main case where we pass in two tuples +template +struct concatenate_types, std::tuple> +{ + using type = std::tuple; +}; + + +// alias to underlying type +template +using concatenate_types_t = typename concatenate_types::type; +} // namespace wmtk::utils::metaprogramming::tuple diff --git a/src/wmtk/utils/metaprogramming/tuple/get_unique_remove_void_types.hpp b/src/wmtk/utils/metaprogramming/tuple/get_unique_remove_void_types.hpp new file mode 100644 index 0000000000..44f30123dc --- /dev/null +++ b/src/wmtk/utils/metaprogramming/tuple/get_unique_remove_void_types.hpp @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include "get_unique_types.hpp" +#include "remove_void_types.hpp" + + +namespace wmtk::utils::metaprogramming::tuple { + + +// given a set of types, a tuple without the voids and makes unique types +template +using get_unique_remove_void_types_t = // get_unique_t; + typename detail::remove_void_tuple>::type; +} + diff --git a/src/wmtk/utils/metaprogramming/tuple/get_unique_types.hpp b/src/wmtk/utils/metaprogramming/tuple/get_unique_types.hpp new file mode 100644 index 0000000000..ab10338c8d --- /dev/null +++ b/src/wmtk/utils/metaprogramming/tuple/get_unique_types.hpp @@ -0,0 +1,74 @@ +#pragma once +#include +#include +#include "concatenate_types.hpp" + +namespace wmtk::utils::metaprogramming::tuple { + + +// given a tuple, extracts a tuple with only unique types in the input + +// strategy is to recursively extract the last copy of any given type + + +// alternates between get_unique_types with +// main_unique_tuple_types> to combine Ts... and T,Ts... as necessary + +namespace detail { + + +// basic initializations - by default return empty in case the input is empty (which is not caught +// by later cases) +template +struct get_unique_types +{ + using type = std::tuple<>; +}; +// basic initialization, expects a T and a Tuple +template +struct get_unique_types_tuple +{ +}; +// convenience to avoid typename for get_unique_types... +template +using get_unique_types_t = typename get_unique_types::type; + + +// actual base case - single type is unique so we return that type +template +struct get_unique_types +{ + using type = std::tuple; +}; + + +// from a list of types, check if the first is unique compared to the rest +template +struct get_unique_types +{ + using type = typename get_unique_types_tuple>::type; +}; + + +// do the meat of the implementation - check if T is unique in T,Ts... +// if so then add it to the unique of the rest +template +struct get_unique_types_tuple> +{ + // is t unique among T,Ts... + constexpr static bool t_is_unique = (!std::is_same_v && ...); + // if its unique then create {T} otherwise {} + using adding_t_type = std::conditional_t, std::tuple<>>; + + // concatenate adding_t_type \cup unique(Ts...) + using type = concatenate_types_t>; +}; + + +} // namespace detail + +// main access point +template +using get_unique_types_t = detail::get_unique_types_t; + +} // namespace wmtk::utils::metaprogramming::tuple diff --git a/src/wmtk/utils/metaprogramming/tuple/remove_void_types.hpp b/src/wmtk/utils/metaprogramming/tuple/remove_void_types.hpp new file mode 100644 index 0000000000..e8a0a4eacd --- /dev/null +++ b/src/wmtk/utils/metaprogramming/tuple/remove_void_types.hpp @@ -0,0 +1,49 @@ +#pragma once +#include +#include "concatenate_types.hpp" + + +namespace wmtk::utils::metaprogramming::tuple { +namespace detail { +template +struct remove_void +{ + using type = std::tuple<>; +}; +template <> +struct remove_void +{ + using type = std::tuple<>; +}; +template +struct remove_void +{ + using type = std::tuple; +}; + +template +using remove_void_t = typename remove_void::type; + +template +struct remove_void_tuple +{ +}; +template +struct remove_void_tuple> +{ + constexpr static bool t_not_void = !std::is_same_v; + using adding_t_type = std::conditional_t, std::tuple<>>; + + using type = concatenate_types_t>; +}; + +template +struct remove_void +{ + using type = typename remove_void_tuple>::type; +}; +} // namespace detail + +template +using remove_void_types_t = detail::remove_void_t; +} // namespace wmtk::utils::metaprogramming::tuple diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6999911404..36131dac07 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,6 +27,7 @@ set(TEST_SOURCES test_3d_operations.cpp test_multi_mesh.cpp test_variant_metaprogramming.cpp + test_tuple_metaprogramming.cpp tools/DEBUG_PointMesh.hpp diff --git a/tests/test_tuple_metaprogramming.cpp b/tests/test_tuple_metaprogramming.cpp new file mode 100644 index 0000000000..7e87af4e5f --- /dev/null +++ b/tests/test_tuple_metaprogramming.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include + +TEST_CASE("test_concatenate_metaprogramming", "[tuple]") +{ + + { + using a = std::tuple<>; + using b = std::tuple<>; + using c = wmtk::utils::metaprogramming::tuple::concatenate_types_t; + static_assert(std::is_same_v>); + } + { + using a = std::tuple<>; + using b = std::tuple; + using c = wmtk::utils::metaprogramming::tuple::concatenate_types_t; + static_assert(std::is_same_v>); + } + { + using a = std::tuple; + using b = std::tuple<>; + using c = wmtk::utils::metaprogramming::tuple::concatenate_types_t; + static_assert(std::is_same_v>); + } + { + using a = std::tuple; + using b = std::tuple; + using c = wmtk::utils::metaprogramming::tuple::concatenate_types_t; + static_assert(std::is_same_v>); + } + +} +TEST_CASE("test_remove_void_metaprogramming", "[tuple]") +{ + + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t<>; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::remove_void_types_t; + static_assert(std::is_same_v>); + } + +} +TEST_CASE("test_get_unique_metaprogramming", "[tuple]") +{ + + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_types_t<>; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_types_t; + static_assert(std::is_same_v>); + } + +} +TEST_CASE("test_get_unique_remove_void_types_metaprogramming", "[tuple]") +{ + + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t<>; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + { + using type = wmtk::utils::metaprogramming::tuple::get_unique_remove_void_types_t; + static_assert(std::is_same_v>); + } + +}