-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Split up code into multiple files - Better names for internal/helper classes - Expose function_traits, member_pointer_traits, functor_traits which are used by callable_traits and might be useful individually - Handle cv qualification more consistently - Handle member function qualifiers - Use constexpr instead of const - Handle class static constant definitions correctly
- Loading branch information
Showing
9 changed files
with
470 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
|
||
test_src = Split(''' | ||
test/driver.cpp | ||
test/driver.cpp | ||
test/function_test.cpp | ||
test/callable_test.cpp | ||
''') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
#if !defined(CALLABLE_FUNCTION_HPP_INCLUDED) | ||
#define CALLABLE_FUNCTION_HPP_INCLUDED | ||
|
||
#include "helpers.hpp" | ||
#include <cstddef> | ||
|
||
namespace detail { | ||
|
||
namespace { | ||
|
||
/** Define traits for a function type */ | ||
template<typename Fun> | ||
struct function_traits; | ||
|
||
template<typename Ret, typename... Args> | ||
struct function_traits<Ret (Args...)> { | ||
typedef Ret function_type(Args...); | ||
typedef Ret return_type; | ||
static constexpr size_t argc = types_count<Args...>::value; | ||
|
||
template<size_t N> | ||
using argument_type = typename types_n<N, Args...>::type; | ||
}; | ||
|
||
template<typename Ret, typename... Args> | ||
const size_t function_traits<Ret (Args...)>::argc; | ||
|
||
} // namespace | ||
|
||
} // namespace detail | ||
|
||
|
||
template<typename Func> | ||
struct function_traits : detail::function_traits<detail::remove_cvref_t<Func>> { | ||
}; | ||
|
||
template<typename Func> | ||
struct function_traits<Func*> : detail::function_traits<detail::remove_cvref_t<Func>> { | ||
}; | ||
|
||
#endif // CALLABLE_FUNCTION_HPP_INCLUDED | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
#if !defined(CALLABLE_FUNCTOR_HPP_INCLUDED) | ||
#define CALLABLE_FUNCTOR_HPP_INCLUDED | ||
|
||
#include "helpers.hpp" | ||
#include "function.hpp" | ||
#include "member_function.hpp" | ||
|
||
namespace detail { | ||
|
||
template<typename Class> | ||
using call_operator_traits = member_function_traits<decltype(&Class::operator())>; | ||
|
||
// classes with operator() | ||
template<typename Class> | ||
struct functor_traits : function_traits<typename call_operator_traits<Class>::function_type> { | ||
typedef call_operator_traits<Class> call_operator; | ||
}; | ||
|
||
} // namespace detail | ||
|
||
|
||
template<typename Class> | ||
struct functor_traits : detail::functor_traits<detail::remove_cvref_t<Class>> { | ||
}; | ||
|
||
#endif // CALLABLE_FUNCTOR_HPP_INCLUDED | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
#if !defined(CALLABLE_HELPERS_HPP_INCLUDED) | ||
#define CALLABLE_HELPERS_HPP_INCLUDED | ||
|
||
#include <cstddef> | ||
|
||
namespace detail { | ||
|
||
/** Remove reference and cv qualification */ | ||
template<typename T> | ||
using remove_cvref_t = typename std::remove_cv< typename std::remove_reference<T>::type >::type; | ||
|
||
|
||
/** Count the number of types given to the template */ | ||
template<typename... Types> | ||
struct types_count; | ||
|
||
template<> | ||
struct types_count<> { | ||
static constexpr std::size_t value = 0; | ||
}; | ||
|
||
|
||
template<typename Type, typename... Types> | ||
struct types_count<Type, Types...> { | ||
static constexpr std::size_t value = types_count<Types...>::value + 1; | ||
}; | ||
|
||
|
||
/** Get the nth type given to the template */ | ||
template<std::size_t n, typename... Types> | ||
struct types_n; | ||
|
||
template<std::size_t N, typename Type, typename... Types> | ||
struct types_n<N, Type, Types...> : types_n<N-1, Types...> { | ||
}; | ||
|
||
template<typename Type, typename... Types> | ||
struct types_n<0, Type, Types...> { | ||
typedef Type type; | ||
}; | ||
|
||
|
||
/** Test if a type is in a list given types */ | ||
template<typename Q, typename... Ts> | ||
struct types_has; | ||
|
||
template<typename Q> | ||
struct types_has<Q> { | ||
static constexpr bool value = false; | ||
}; | ||
|
||
template<typename Q, typename... Ts> | ||
struct types_has<Q, Q, Ts...> { | ||
static constexpr bool value = true; | ||
}; | ||
|
||
template<typename Q, typename T, typename... Ts> | ||
struct types_has<Q, T, Ts...> : types_has<Q, Ts...> { | ||
}; | ||
|
||
|
||
} // namespace detail | ||
|
||
#endif // CALLABLE_HELPERS_HPP_INCLUDED | ||
|
Oops, something went wrong.