Skip to content

Commit

Permalink
✨ add is_union trait
Browse files Browse the repository at this point in the history
  • Loading branch information
zie87 committed Oct 6, 2023
1 parent b6053e1 commit fd1b77a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/zll/meta/is_union.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef ZLL_META_ISUNION_HPP
#define ZLL_META_ISUNION_HPP

#include "zll/utils/intrinsics_macros.hpp"
#include "zll/meta/integral_constant.hpp"

namespace zll {
namespace meta {

template <typename T>
struct is_union : bool_constant<ZLL_INTRINSICS_IS_UNION(T)> {};

} // namespace meta
} // namespace zll

#endif // ZLL_META_ISUNION_HPP
1 change: 1 addition & 0 deletions include/zll/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

#include "zll/meta/remove_cv.hpp"

Expand Down
6 changes: 6 additions & 0 deletions include/zll/utils/intrinsics_macros.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef ZLL_UTILS_INTRINSICMACROS_HPP
#define ZLL_UTILS_INTRINSICMACROS_HPP

#define ZLL_INTRINSICS_IS_UNION(t) __is_union(t)

#endif // ZLL_UTILS_INTRINSICMACROS_HPP
1 change: 1 addition & 0 deletions tests/unit_tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ unit_tests_srcs = [
'meta/integral_constant_tests.cpp',
'meta/is_pointer_tests.cpp',
'meta/is_same_tests.cpp',
'meta/is_union_tests.cpp',
'meta/remove_cv_tests.cpp',
'main.cpp',
]
Expand Down
24 changes: 24 additions & 0 deletions tests/unit_tests/meta/is_union_tests.cpp
Original file line number Diff line number Diff line change
@@ -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/.
*/

#include "zll/meta/is_union.hpp"

#include <utest.h>

typedef union {
char a;
int b;
} test_union;

struct test_struct {
test_union u;
};

UTEST(meta_is_union, sfinae) {
ASSERT_TRUE(zll::meta::is_union<test_union>::value);
ASSERT_FALSE(zll::meta::is_union<test_struct>::value);
ASSERT_FALSE(zll::meta::is_union<int>::value);
}

0 comments on commit fd1b77a

Please sign in to comment.