Skip to content

Commit

Permalink
✨ add is_class trait
Browse files Browse the repository at this point in the history
  • Loading branch information
zie87 committed Oct 6, 2023
1 parent c30682b commit 3592095
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/zll/meta/detail/constraints_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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_DETAIL_CONSTRAINTSTYPE_HPP
#define ZLL_META_DETAIL_CONSTRAINTSTYPE_HPP

namespace zll {
namespace meta {
namespace detail {

template <typename T>
struct larger_than {
T body[2];
};

typedef char no_type;
typedef larger_than<no_type> yes_type;

} // namespace detail
} // namespace meta
} // namespace zll

#endif // ZLL_META_DETAIL_CONSTRAINTSTYPE_HPP
39 changes: 39 additions & 0 deletions include/zll/meta/is_class.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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_ISCLASS_HPP
#define ZLL_META_ISCLASS_HPP

#include "zll/meta/enable_if.hpp"
#include "zll/meta/integral_constant.hpp"
#include "zll/meta/is_union.hpp"

#include "zll/meta/detail/constraints_type.hpp"

#include <cstdlib>

namespace zll {
namespace meta {

namespace detail {

struct is_class_helper {
template <typename T>
static typename enable_if<!(is_union<T>::value), yes_type>::type test(int T::*);

template <typename>
static no_type test(...);
};

} // namespace detail

template <typename T>
struct is_class : bool_constant<(sizeof(detail::is_class_helper::test<T>(NULL)) == sizeof(detail::yes_type))> {};

} // namespace meta
} // namespace zll

#endif // ZLL_META_ISCLASS_HPP
1 change: 1 addition & 0 deletions tests/unit_tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
unit_tests_srcs = [
'meta/enable_if_tests.cpp',
'meta/integral_constant_tests.cpp',
'meta/is_class_tests.cpp',
'meta/is_pointer_tests.cpp',
'meta/is_same_tests.cpp',
'meta/is_union_tests.cpp',
Expand Down
29 changes: 29 additions & 0 deletions tests/unit_tests/meta/is_class_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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/is_class.hpp"

#include <utest.h>

struct test_struct {};
class test_class {};

union test_union {
class clazz {};
};

UTEST(meta_is_class, is_class) {
ASSERT_TRUE(zll::meta::is_class<test_class>::value);
ASSERT_TRUE(zll::meta::is_class<const test_class>::value);
ASSERT_TRUE(zll::meta::is_class<test_struct>::value);
ASSERT_TRUE(zll::meta::is_class<const test_struct>::value);

ASSERT_FALSE(zll::meta::is_class<test_struct*>::value);
ASSERT_FALSE(zll::meta::is_class<test_struct&>::value);
ASSERT_FALSE(zll::meta::is_class<test_union>::value);

ASSERT_TRUE(zll::meta::is_class<test_union::clazz>::value);
}

0 comments on commit 3592095

Please sign in to comment.