Skip to content

Commit

Permalink
✨ add is_function trait
Browse files Browse the repository at this point in the history
  • Loading branch information
zie87 committed Oct 13, 2023
1 parent 1c5512d commit eb69ff2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/zll/meta/traits/is_function.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ZLL_META_TRAITS_ISFUNCTION_HPP
#define ZLL_META_TRAITS_ISFUNCTION_HPP

#include "zll/meta/integral_constant.hpp"
#include "zll/meta/traits/is_const.hpp"
#include "zll/meta/traits/is_reference.hpp"

namespace zll {
namespace meta {

namespace detail {

template <typename T>
struct is_function_helper {
static const bool value = !(zll::meta::is_const<const T>::value) && !(zll::meta::is_reference<T>::value);
};

} // namespace detail

template <typename T>
struct is_function : zll::meta::bool_constant<detail::is_function_helper<T>::value> {};

} // namespace meta
} // namespace zll

#endif // ZLL_META_TRAITS_ISFUNCTION_HPP
1 change: 1 addition & 0 deletions tests/unit_tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ unit_tests_srcs = [
'meta/traits/is_const_tests.cpp',
'meta/traits/is_enum_tests.cpp',
'meta/traits/is_floating_point_tests.cpp',
'meta/traits/is_function_tests.cpp',
'meta/traits/is_integral_tests.cpp',
'meta/traits/is_member_pointer_tests.cpp',
'meta/traits/is_object_tests.cpp',
Expand Down
31 changes: 31 additions & 0 deletions tests/unit_tests/meta/traits/is_function_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <zll/meta/traits/is_function.hpp>
#include <zll/utils/typeof_macro.hpp>

#include <utest.h>

struct test_struct {
int fun() const;
};

int test_function();

template <typename T>
struct test_trait {};
template <typename T, typename U>
struct test_trait<U T::*> {
typedef U member_type;
};

UTEST(meta_traits_is_function, is_function) {
ASSERT_TRUE((zll::meta::is_function<int(int)>::value));
ASSERT_TRUE((zll::meta::is_function<ZLL_TYPE_OF(test_function)>::value));

typedef test_trait<ZLL_TYPE_OF(&test_struct::fun)>::member_type fun_type;
ASSERT_TRUE((zll::meta::is_function<fun_type>::value));
}

UTEST(meta_traits_is_function, is_not_function) {
ASSERT_FALSE((zll::meta::is_function<int>::value));
ASSERT_FALSE((zll::meta::is_function<test_struct>::value));
}

0 comments on commit eb69ff2

Please sign in to comment.