From b6053e1ddcc7fa2dbcbda6c18dd844528bc28ce6 Mon Sep 17 00:00:00 2001 From: Tobias Zindl Date: Fri, 6 Oct 2023 18:47:36 +0200 Subject: [PATCH] :sparkles: add enable_if trait --- include/zll/meta/enable_if.hpp | 24 +++++++++++++++ include/zll/type_traits.hpp | 2 ++ tests/unit_tests/meson.build | 1 + tests/unit_tests/meta/enable_if_tests.cpp | 36 +++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 include/zll/meta/enable_if.hpp create mode 100644 tests/unit_tests/meta/enable_if_tests.cpp diff --git a/include/zll/meta/enable_if.hpp b/include/zll/meta/enable_if.hpp new file mode 100644 index 0000000..0714cd4 --- /dev/null +++ b/include/zll/meta/enable_if.hpp @@ -0,0 +1,24 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#ifndef ZLL_META_ENABLEIF_HPP +#define ZLL_META_ENABLEIF_HPP + +namespace zll { +namespace meta { + +template +struct enable_if {}; + +template +struct enable_if { + typedef T type; +}; + +} // namespace meta +} // namespace zll + +#endif // ZLL_META_ENABLEIF_HPP diff --git a/include/zll/type_traits.hpp b/include/zll/type_traits.hpp index eeee223..e200975 100644 --- a/include/zll/type_traits.hpp +++ b/include/zll/type_traits.hpp @@ -7,6 +7,8 @@ #ifndef ZLL_TYPETRAITS_HPP #define ZLL_TYPETRAITS_HPP +#include "zll/meta/enable_if.hpp" + #include "zll/meta/is_pointer.hpp" #include "zll/meta/is_same.hpp" diff --git a/tests/unit_tests/meson.build b/tests/unit_tests/meson.build index fcfe88b..cc1dc27 100644 --- a/tests/unit_tests/meson.build +++ b/tests/unit_tests/meson.build @@ -1,5 +1,6 @@ unit_tests_srcs = [ + 'meta/enable_if_tests.cpp', 'meta/integral_constant_tests.cpp', 'meta/is_pointer_tests.cpp', 'meta/is_same_tests.cpp', diff --git a/tests/unit_tests/meta/enable_if_tests.cpp b/tests/unit_tests/meta/enable_if_tests.cpp new file mode 100644 index 0000000..d81791c --- /dev/null +++ b/tests/unit_tests/meta/enable_if_tests.cpp @@ -0,0 +1,36 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "zll/meta/enable_if.hpp" +#include "zll/meta/is_same.hpp" +#include "zll/meta/is_pointer.hpp" + +#include + +namespace test_types { +enum test_values { f = 0, i, p }; +} + +template +typename zll::meta::enable_if::value, test_types::test_values>::type test_function() { + return test_types::i; +} + +template +typename zll::meta::enable_if::value, test_types::test_values>::type test_function() { + return test_types::f; +} + +template +typename zll::meta::enable_if::value, test_types::test_values>::type test_function() { + return test_types::p; +} + +UTEST(meta_enable_if, sfinae) { + ASSERT_EQ(test_function(), test_types::i); + ASSERT_EQ(test_function(), test_types::f); + ASSERT_EQ(test_function(), test_types::p); +}