From 40588e30f6ab1d200180ba03e7e0823d20b642bc Mon Sep 17 00:00:00 2001 From: Tobias Zindl Date: Thu, 5 Oct 2023 22:27:16 +0200 Subject: [PATCH] :sparkles: add integral constant --- include/zll/meta/integral_constant.hpp | 27 +++++++++++++++ tests/unit_tests/meson.build | 2 +- .../meta/integral_constant_tests.cpp | 33 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 include/zll/meta/integral_constant.hpp create mode 100644 tests/unit_tests/meta/integral_constant_tests.cpp diff --git a/include/zll/meta/integral_constant.hpp b/include/zll/meta/integral_constant.hpp new file mode 100644 index 0000000..3cb3f32 --- /dev/null +++ b/include/zll/meta/integral_constant.hpp @@ -0,0 +1,27 @@ +#ifndef ZLL_META_INTEGRALCONSTANT_HPP +#define ZLL_META_INTEGRALCONSTANT_HPP + +namespace zll { +namespace meta { + +template +struct integral_constant { + typedef T value_type; + typedef integral_constant type; + + static const value_type value = VALUE_V; + + operator value_type() const { return VALUE_V; } + value_type operator()() const { return VALUE_V; } +}; + +template +struct bool_constant : integral_constant {}; + +typedef bool_constant true_type; +typedef bool_constant false_type; + +} // namespace meta +} // namespace zll + +#endif // ZLL_META_INTEGRALCONSTANT_HPP diff --git a/tests/unit_tests/meson.build b/tests/unit_tests/meson.build index cec2190..cf821f8 100644 --- a/tests/unit_tests/meson.build +++ b/tests/unit_tests/meson.build @@ -1,5 +1,6 @@ unit_tests_srcs = [ + 'meta/integral_constant_tests.cpp', 'main.cpp', ] @@ -9,7 +10,6 @@ unit_tests_exec = executable( unit_tests_srcs, include_directories: unit_tests_inc_dir, dependencies: [utest_dep, dis_dep], - override_options: ['cpp_std=c++20', 'warning_level=3'], ) test('zll-unit-tests', unit_tests_exec, suite: 'dis') diff --git a/tests/unit_tests/meta/integral_constant_tests.cpp b/tests/unit_tests/meta/integral_constant_tests.cpp new file mode 100644 index 0000000..abd0449 --- /dev/null +++ b/tests/unit_tests/meta/integral_constant_tests.cpp @@ -0,0 +1,33 @@ +#include "zll/meta/integral_constant.hpp" +#include "zll/debug/static_assert.hpp" + +#include + +UTEST(meta_integral_constant, int_constant) { + typedef zll::meta::integral_constant two_t; + + const two_t two = two_t(); + ASSERT_EQ(two_t::value, 2); + ASSERT_EQ(two(), 2); + const int val = two; + + ASSERT_EQ(val, 2); + ZLL_STATIC_ASSERT((two_t::value == 2)); +} + +UTEST(meta_integral_constant, bool_constant) { + { + const zll::meta::true_type true_v; + ASSERT_TRUE((zll::meta::true_type::value)); + ASSERT_TRUE(true_v); + ASSERT_TRUE(true_v()); + ZLL_STATIC_ASSERT((zll::meta::true_type::value)); + } + { + const zll::meta::false_type false_v; + ASSERT_FALSE((zll::meta::false_type::value)); + ASSERT_FALSE(false_v); + ASSERT_FALSE(false_v()); + ZLL_STATIC_ASSERT(!(zll::meta::false_type::value)); + } +}