Implement reference_wrapper and invoke to allow invoke_result_t implementation

This commit is contained in:
BlackMark 2020-05-16 16:55:12 +02:00
parent bc40515636
commit f1bb696e6e
2 changed files with 137 additions and 26 deletions

95
func.hpp Normal file
View File

@ -0,0 +1,95 @@
#pragma once
#include "util.hpp"
#include "../type/type.hpp"
namespace util {
// <functional>
// clang-format off
namespace detail {
template <typename> constexpr bool is_reference_wrapper_v = false;
template <typename U> constexpr bool is_reference_wrapper_v<reference_wrapper<U>> = true;
template <typename T, typename Type, typename T1, typename... Args>
constexpr decltype(auto) INVOKE(Type T::*f, T1 &&t1, Args &&... args)
{
if constexpr (type::is_member_function_pointer_v<decltype(f)>) {
if constexpr (type::is_base_of_v<T, type::decay_t<T1>>)
return (util::forward<T1>(t1).*f)(util::forward<Args>(args)...);
else if constexpr (is_reference_wrapper_v<type::decay_t<T1>>)
return (t1.get().*f)(util::forward<Args>(args)...);
else
return ((*util::forward<T1>(t1)).*f)(util::forward<Args>(args)...);
} else {
static_assert(type::is_member_object_pointer_v<decltype(f)>);
static_assert(sizeof...(args) == 0);
if constexpr (type::is_base_of_v<T, type::decay_t<T1>>)
return util::forward<T1>(t1).*f;
else if constexpr (is_reference_wrapper_v<type::decay_t<T1>>)
return t1.get().*f;
else
return (*util::forward<T1>(t1)).*f;
}
}
template <typename Fn, typename... Args>
constexpr decltype(auto) INVOKE(Fn &&f, Args &&... args)
{
return util::forward<Fn>(f)(util::forward<Args>(args)...);
}
template <typename T> constexpr T &FUN(T &t) noexcept { return t; }
template <typename T> void FUN(T &&) = delete;
} // namespace detail
template <typename Fn, typename... Args>
constexpr type::invoke_result_t<Fn, Args...> invoke(Fn &&f, Args &&... args)
noexcept(type::is_nothrow_invocable_v<Fn, Args...>)
{
return detail::INVOKE(util::forward<Fn>(f), util::forward<Args>(args)...);
}
template <typename T>
class reference_wrapper {
public:
// construct/copy/destroy
template <typename U,
typename = decltype(detail::FUN<T>(util::declval<U>()),
type::enable_if_t<!type::is_same_v<reference_wrapper, type::remove_cvref_t<U>>>())>
constexpr reference_wrapper(U &&u) noexcept(noexcept(detail::FUN<T>(util::forward<U>(u))))
: _ptr(util::addressof(detail::FUN<T>(util::forward<U>(u))))
{}
reference_wrapper(const reference_wrapper &) noexcept = default;
// assignment
reference_wrapper &operator=(const reference_wrapper &) noexcept = default;
// access
constexpr operator T &() const noexcept { return *_ptr; }
constexpr T &get() const noexcept { return *_ptr; }
template <typename... ArgTypes>
constexpr type::invoke_result_t<T &, ArgTypes...> operator()(ArgTypes &&... args) const
{
return invoke(get(), util::forward<ArgTypes>(args)...);
}
// types
using type = T;
private:
T *_ptr;
};
// deduction guides
template <typename T> reference_wrapper(T &) -> reference_wrapper<T>;
// clang-format on
} // namespace util

View File

@ -6,6 +6,26 @@
namespace util {
// <memory>
// clang-format off
template <typename T>
type::enable_if_t<type::is_object_v<T>, T *> addressof(T &arg) noexcept
{
return reinterpret_cast<T *>(&const_cast<char &>(reinterpret_cast<const volatile char &>(arg)));
}
template <typename T>
type::enable_if_t<!type::is_object_v<T>, T *> addressof(T &arg) noexcept
{
return &arg;
}
// <utility>
template <typename T> type::add_rvalue_reference_t<T> declval() noexcept;
template <typename T>
inline constexpr type::remove_reference_t<T> &&move(T &&t) noexcept
{
@ -36,33 +56,26 @@ struct integer_sequence {
}
};
template <size_t... Ints>
using index_sequence = integer_sequence<size_t, Ints...>;
template <size_t... Ints> using index_sequence = integer_sequence<size_t, Ints...>;
namespace detail {
template <typename T, typename Seq1, typename Seq2>
struct concat;
template <typename T, typename Seq1, typename Seq2> struct concat;
template <typename T, T... Ints1, T... Ints2>
struct concat<T, integer_sequence<T, Ints1...>, integer_sequence<T, Ints2...>>
: integer_sequence<T, Ints1..., (sizeof...(Ints1) + Ints2)...> {
};
: integer_sequence<T, Ints1..., (sizeof...(Ints1) + Ints2)...>
{};
// uint64_t because this must be able to hold all possible sizes and cannot be T, because then the specialization for 0
// and 1 are not possible
template <typename T, uint64_t N>
struct gen_integer_sequence : detail::concat<T, typename gen_integer_sequence<T, N / 2>::__type,
typename gen_integer_sequence<T, N - N / 2>::__type> {
};
typename gen_integer_sequence<T, N - N / 2>::__type>
{};
template <typename T>
struct gen_integer_sequence<T, 0> : integer_sequence<T> {
};
template <typename T>
struct gen_integer_sequence<T, 1> : integer_sequence<T, 0> {
};
template <typename T> struct gen_integer_sequence<T, 0> : integer_sequence<T> {};
template <typename T> struct gen_integer_sequence<T, 1> : integer_sequence<T, 0> {};
} // namespace detail
@ -71,21 +84,16 @@ struct make_integer_sequence : detail::gen_integer_sequence<T, N> {
static_assert(N >= 0, "Integer sequence cannot be negative");
};
template <size_t N>
using make_index_sequence = make_integer_sequence<size_t, N>;
template <size_t N> using make_index_sequence = make_integer_sequence<size_t, N>;
template <typename... T>
using index_sequence_for = make_index_sequence<sizeof...(T)>;
template <typename... T> using index_sequence_for = make_index_sequence<sizeof...(T)>;
// Not part of <utility>, but very useful
namespace detail {
template <typename T, T Offset, T... Ints>
inline constexpr integer_sequence<T, (Offset + Ints)...> add_offset(integer_sequence<T, Ints...>)
{
return {};
}
inline constexpr integer_sequence<T, (Offset + Ints)...> add_offset(integer_sequence<T, Ints...>) { return {}; }
} // namespace detail
@ -101,10 +109,18 @@ inline constexpr auto make_offset_index_sequence()
return make_offset_integer_sequence<size_t, Offset, N>();
}
template <class Fn, size_t... Ints>
void for_constexpr(Fn &&func, index_sequence<Ints...>)
template <typename Fn, size_t... Ints>
auto for_constexpr(Fn &&func, index_sequence<Ints...>)
{
if constexpr (type::is_void_v<type::invoke_result_t<Fn, type::integral_constant<size_t, 0>>>) {
(func(type::integral_constant<size_t, Ints>{}), ...);
} else {
if ((func(type::integral_constant<size_t, Ints>{}) && ...))
return true;
return false;
}
}
// clang-format on
} // namespace util