Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add remove_cv traits #5

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions include/zll/meta/is_pointer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef ZLL_META_ISPOINTER_HPP
#define ZLL_META_ISPOINTER_HPP

#include "zll/meta/integral_constant.hpp"

namespace zll {
namespace meta {

// clang-format off
template<typename T> struct is_pointer : false_type {};
template<typename T> struct is_pointer<T*> : true_type {};
template<typename T> struct is_pointer<T* const> : true_type {};
template<typename T> struct is_pointer<T* volatile> : true_type {};
template<typename T> struct is_pointer<T* const volatile> : true_type {};
// clang-format on

} // namespace meta
} // namespace zll

#endif // ZLL_META_ISPOINTER_HPP
27 changes: 27 additions & 0 deletions include/zll/meta/remove_cv.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef ZLL_META_REMOVECV_HPP
#define ZLL_META_REMOVECV_HPP

namespace zll {
namespace meta {

// clang-format off
template <typename T> struct remove_const { typedef T type; };
template <typename T> struct remove_const<const T> { typedef T type; };
// clang-format on

// clang-format off
template <typename T> struct remove_volatile { typedef T type; };
template <typename T> struct remove_volatile<volatile T> { typedef T type; };
// clang-format on

// clang-format off
template<typename T> struct remove_cv { typedef T type; };
template<typename T> struct remove_cv<const T> { typedef T type; };
template<typename T> struct remove_cv<volatile T> { typedef T type; };
template<typename T> struct remove_cv<const volatile T> { typedef T type; };
// clang-format on

} // namespace meta
} // namespace zll

#endif // ZLL_META_ISSAME_HPP
4 changes: 4 additions & 0 deletions include/zll/type_traits.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ZLL_TYPETRAITS_HPP
#define ZLL_TYPETRAITS_HPP

#include "zll/meta/is_pointer.hpp"
#include "zll/meta/is_same.hpp"

#include "zll/meta/remove_cv.hpp"


#endif // ZLL_TYPETRAITS_HPP
4 changes: 3 additions & 1 deletion tests/unit_tests/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

unit_tests_srcs = [
'meta/integral_constant_tests.cpp',
'meta/is_pointer_tests.cpp',
'meta/is_same_tests.cpp',
'meta/remove_cv_tests.cpp',
'main.cpp',
]

Expand All @@ -13,4 +15,4 @@ unit_tests_exec = executable(
dependencies: [utest_dep, dis_dep],
)

test('zll-unit-tests', unit_tests_exec, suite: 'dis')
test('zll-unit-tests', unit_tests_exec, suite: 'zll')
20 changes: 20 additions & 0 deletions tests/unit_tests/meta/is_pointer_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "zll/meta/is_pointer.hpp"
#include "zll/meta/is_same.hpp"

#include <utest.h>

struct test_dummy {};

UTEST(meta_is_pointer, is_pointer) {
ASSERT_TRUE(zll::meta::is_pointer<test_dummy*>::value);
ASSERT_TRUE(zll::meta::is_pointer<test_dummy const* volatile>::value);
ASSERT_TRUE(zll::meta::is_pointer<void*>::value);
ASSERT_TRUE(zll::meta::is_pointer<int*>::value);
ASSERT_TRUE(zll::meta::is_pointer<int**>::value);
ASSERT_TRUE(zll::meta::is_pointer<void (*)()>::value);

ASSERT_FALSE(zll::meta::is_pointer<test_dummy>::value);
ASSERT_FALSE(zll::meta::is_pointer<test_dummy&>::value);
ASSERT_FALSE(zll::meta::is_pointer<int>::value);
ASSERT_FALSE(zll::meta::is_pointer<int[3]>::value);
}
36 changes: 36 additions & 0 deletions tests/unit_tests/meta/remove_cv_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "zll/meta/remove_cv.hpp"
#include "zll/meta/is_same.hpp"

#include <utest.h>

struct test_dummy {};
struct test_child : test_dummy {};

UTEST(meta_remove_cv, remove_const) {
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_const<int>::type, int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_const<const int>::type, int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_const<volatile int>::type, volatile int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_const<const volatile int>::type, volatile int>::value));
}

UTEST(meta_remove_cv, remove_volatile) {
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_volatile<int>::type, int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_volatile<const int>::type, const int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_volatile<volatile int>::type, int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_volatile<const volatile int>::type, const int>::value));
}

UTEST(meta_remove_cv, remove_cv) {
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_cv<int>::type, int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_cv<const int>::type, int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_cv<volatile int>::type, int>::value));
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_cv<const volatile int>::type, int>::value));
}
UTEST(meta_remove_cv, remove_cv_pointer) {
ASSERT_TRUE((zll::meta::is_same<zll::meta::remove_cv<int*>::type, int*>::value));

// remove_cv does not work on pointers
ASSERT_FALSE((zll::meta::is_same<zll::meta::remove_cv<const int*>::type, int*>::value));
ASSERT_FALSE((zll::meta::is_same<zll::meta::remove_cv<volatile int*>::type, int*>::value));
ASSERT_FALSE((zll::meta::is_same<zll::meta::remove_cv<const volatile int*>::type, int*>::value));
}