Merge type repo into util
This commit is contained in:
parent
f1bb696e6e
commit
81b3ae244c
29
func.hpp
29
func.hpp
@ -1,9 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "type.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
#include "../type/type.hpp"
|
||||
|
||||
namespace util {
|
||||
|
||||
// <functional>
|
||||
@ -18,19 +17,19 @@ template <typename U> constexpr bool is_reference_wrapper_v<reference_wrapper<U>
|
||||
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>>)
|
||||
if constexpr (util::is_member_function_pointer_v<decltype(f)>) {
|
||||
if constexpr (util::is_base_of_v<T, util::decay_t<T1>>)
|
||||
return (util::forward<T1>(t1).*f)(util::forward<Args>(args)...);
|
||||
else if constexpr (is_reference_wrapper_v<type::decay_t<T1>>)
|
||||
else if constexpr (is_reference_wrapper_v<util::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(util::is_member_object_pointer_v<decltype(f)>);
|
||||
static_assert(sizeof...(args) == 0);
|
||||
if constexpr (type::is_base_of_v<T, type::decay_t<T1>>)
|
||||
if constexpr (util::is_base_of_v<T, util::decay_t<T1>>)
|
||||
return util::forward<T1>(t1).*f;
|
||||
else if constexpr (is_reference_wrapper_v<type::decay_t<T1>>)
|
||||
else if constexpr (is_reference_wrapper_v<util::decay_t<T1>>)
|
||||
return t1.get().*f;
|
||||
else
|
||||
return (*util::forward<T1>(t1)).*f;
|
||||
@ -49,8 +48,8 @@ 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...>)
|
||||
constexpr util::invoke_result_t<Fn, Args...> invoke(Fn &&f, Args &&... args)
|
||||
noexcept(util::is_nothrow_invocable_v<Fn, Args...>)
|
||||
{
|
||||
return detail::INVOKE(util::forward<Fn>(f), util::forward<Args>(args)...);
|
||||
}
|
||||
@ -58,10 +57,13 @@ constexpr type::invoke_result_t<Fn, Args...> invoke(Fn &&f, Args &&... args)
|
||||
template <typename T>
|
||||
class reference_wrapper {
|
||||
public:
|
||||
// types
|
||||
using type = T;
|
||||
|
||||
// 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>>>())>
|
||||
util::enable_if_t<!util::is_same_v<reference_wrapper, util::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))))
|
||||
{}
|
||||
@ -75,14 +77,11 @@ class reference_wrapper {
|
||||
constexpr T &get() const noexcept { return *_ptr; }
|
||||
|
||||
template <typename... ArgTypes>
|
||||
constexpr type::invoke_result_t<T &, ArgTypes...> operator()(ArgTypes &&... args) const
|
||||
constexpr util::invoke_result_t<T &, ArgTypes...> operator()(ArgTypes &&... args) const
|
||||
{
|
||||
return invoke(get(), util::forward<ArgTypes>(args)...);
|
||||
}
|
||||
|
||||
// types
|
||||
using type = T;
|
||||
|
||||
private:
|
||||
T *_ptr;
|
||||
};
|
||||
|
12
type.hpp
12
type.hpp
@ -2,14 +2,16 @@
|
||||
|
||||
// Fix for limits.h not exposing LLONG_MIN, LLONG_MIN, and ULLONG_MAX to C++ context
|
||||
#ifdef __cplusplus
|
||||
#ifndef __STDC_VERSION__
|
||||
#define __STDC_VERSION__ 201112L
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
namespace type {
|
||||
namespace util {
|
||||
|
||||
// <type_traits>
|
||||
|
||||
@ -244,14 +246,14 @@ template <bool B, typename T = void> using enable_if_t = typename enable_if<B, T
|
||||
namespace util {
|
||||
|
||||
template <typename T> struct reference_wrapper;
|
||||
template <typename T> inline constexpr T &&forward(type::remove_reference_t<T> &t) noexcept;
|
||||
template <typename T> type::add_rvalue_reference_t<T> declval() noexcept;
|
||||
template <typename T> inline constexpr T &&forward(util::remove_reference_t<T> &t) noexcept;
|
||||
template <typename T> util::add_rvalue_reference_t<T> declval() noexcept;
|
||||
|
||||
} // namespace util
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace type {
|
||||
namespace util {
|
||||
|
||||
// Everything is nothrow_invocable, because exceptions are disabled in avr-gcc
|
||||
template <typename, typename...> struct is_nothrow_invocable : true_type {};
|
||||
@ -407,4 +409,4 @@ struct numeric_limits<long double> {
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
} // namespace type
|
||||
} // namespace util
|
||||
|
24
util.hpp
24
util.hpp
@ -2,7 +2,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../type/type.hpp"
|
||||
#include "type.hpp"
|
||||
|
||||
namespace util {
|
||||
|
||||
@ -11,37 +11,37 @@ namespace util {
|
||||
// clang-format off
|
||||
|
||||
template <typename T>
|
||||
type::enable_if_t<type::is_object_v<T>, T *> addressof(T &arg) noexcept
|
||||
util::enable_if_t<util::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
|
||||
util::enable_if_t<!util::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> util::add_rvalue_reference_t<T> declval() noexcept;
|
||||
|
||||
template <typename T>
|
||||
inline constexpr type::remove_reference_t<T> &&move(T &&t) noexcept
|
||||
inline constexpr util::remove_reference_t<T> &&move(T &&t) noexcept
|
||||
{
|
||||
return static_cast<type::remove_reference_t<T> &&>(t);
|
||||
return static_cast<util::remove_reference_t<T> &&>(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline constexpr T &&forward(type::remove_reference_t<T> &t) noexcept
|
||||
inline constexpr T &&forward(util::remove_reference_t<T> &t) noexcept
|
||||
{
|
||||
return static_cast<T &&>(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline constexpr T &&forward(type::remove_reference_t<T> &&t) noexcept
|
||||
inline constexpr T &&forward(util::remove_reference_t<T> &&t) noexcept
|
||||
{
|
||||
static_assert(!type::is_lvalue_reference_v<T>, "Can not forward an rvalue as an lvalue.");
|
||||
static_assert(!util::is_lvalue_reference_v<T>, "Can not forward an rvalue as an lvalue.");
|
||||
return static_cast<T &&>(t);
|
||||
}
|
||||
|
||||
@ -112,10 +112,10 @@ inline constexpr auto make_offset_index_sequence()
|
||||
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>{}), ...);
|
||||
if constexpr (util::is_void_v<util::invoke_result_t<Fn, util::integral_constant<size_t, 0>>>) {
|
||||
(func(util::integral_constant<size_t, Ints>{}), ...);
|
||||
} else {
|
||||
if ((func(type::integral_constant<size_t, Ints>{}) && ...))
|
||||
if ((func(util::integral_constant<size_t, Ints>{}) && ...))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user