Merge type repo into util

This commit is contained in:
BlackMark 2020-05-16 17:39:34 +02:00
parent f1bb696e6e
commit 81b3ae244c
3 changed files with 33 additions and 32 deletions

View File

@ -1,9 +1,8 @@
#pragma once #pragma once
#include "type.hpp"
#include "util.hpp" #include "util.hpp"
#include "../type/type.hpp"
namespace util { namespace util {
// <functional> // <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> template <typename T, typename Type, typename T1, typename... Args>
constexpr decltype(auto) INVOKE(Type T::*f, T1 &&t1, Args &&... args) constexpr decltype(auto) INVOKE(Type T::*f, T1 &&t1, Args &&... args)
{ {
if constexpr (type::is_member_function_pointer_v<decltype(f)>) { if constexpr (util::is_member_function_pointer_v<decltype(f)>) {
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)(util::forward<Args>(args)...); 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)...); return (t1.get().*f)(util::forward<Args>(args)...);
else else
return ((*util::forward<T1>(t1)).*f)(util::forward<Args>(args)...); return ((*util::forward<T1>(t1)).*f)(util::forward<Args>(args)...);
} else { } 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); 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; 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; return t1.get().*f;
else else
return (*util::forward<T1>(t1)).*f; return (*util::forward<T1>(t1)).*f;
@ -49,8 +48,8 @@ template <typename T> void FUN(T &&) = delete;
} // namespace detail } // namespace detail
template <typename Fn, typename... Args> template <typename Fn, typename... Args>
constexpr type::invoke_result_t<Fn, Args...> invoke(Fn &&f, Args &&... args) constexpr util::invoke_result_t<Fn, Args...> invoke(Fn &&f, Args &&... args)
noexcept(type::is_nothrow_invocable_v<Fn, Args...>) noexcept(util::is_nothrow_invocable_v<Fn, Args...>)
{ {
return detail::INVOKE(util::forward<Fn>(f), util::forward<Args>(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> template <typename T>
class reference_wrapper { class reference_wrapper {
public: public:
// types
using type = T;
// construct/copy/destroy // construct/copy/destroy
template <typename U, template <typename U,
typename = decltype(detail::FUN<T>(util::declval<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)))) constexpr reference_wrapper(U &&u) noexcept(noexcept(detail::FUN<T>(util::forward<U>(u))))
: _ptr(util::addressof(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; } constexpr T &get() const noexcept { return *_ptr; }
template <typename... ArgTypes> 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)...); return invoke(get(), util::forward<ArgTypes>(args)...);
} }
// types
using type = T;
private: private:
T *_ptr; T *_ptr;
}; };

View File

@ -2,14 +2,16 @@
// Fix for limits.h not exposing LLONG_MIN, LLONG_MIN, and ULLONG_MAX to C++ context // Fix for limits.h not exposing LLONG_MIN, LLONG_MIN, and ULLONG_MAX to C++ context
#ifdef __cplusplus #ifdef __cplusplus
#ifndef __STDC_VERSION__
#define __STDC_VERSION__ 201112L #define __STDC_VERSION__ 201112L
#endif #endif
#endif
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <stddef.h> #include <stddef.h>
namespace type { namespace util {
// <type_traits> // <type_traits>
@ -244,14 +246,14 @@ template <bool B, typename T = void> using enable_if_t = typename enable_if<B, T
namespace util { namespace util {
template <typename T> struct reference_wrapper; template <typename T> struct reference_wrapper;
template <typename T> inline constexpr T &&forward(type::remove_reference_t<T> &t) noexcept; template <typename T> inline constexpr T &&forward(util::remove_reference_t<T> &t) noexcept;
template <typename T> type::add_rvalue_reference_t<T> declval() noexcept; template <typename T> util::add_rvalue_reference_t<T> declval() noexcept;
} // namespace util } // namespace util
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
namespace type { namespace util {
// Everything is nothrow_invocable, because exceptions are disabled in avr-gcc // Everything is nothrow_invocable, because exceptions are disabled in avr-gcc
template <typename, typename...> struct is_nothrow_invocable : true_type {}; template <typename, typename...> struct is_nothrow_invocable : true_type {};
@ -407,4 +409,4 @@ struct numeric_limits<long double> {
}; };
// clang-format on // clang-format on
} // namespace type } // namespace util

View File

@ -2,7 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include "../type/type.hpp" #include "type.hpp"
namespace util { namespace util {
@ -11,37 +11,37 @@ namespace util {
// clang-format off // clang-format off
template <typename T> 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))); return reinterpret_cast<T *>(&const_cast<char &>(reinterpret_cast<const volatile char &>(arg)));
} }
template <typename T> 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; return &arg;
} }
// <utility> // <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> 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> 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); return static_cast<T &&>(t);
} }
template <typename 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); return static_cast<T &&>(t);
} }
@ -112,10 +112,10 @@ inline constexpr auto make_offset_index_sequence()
template <typename Fn, size_t... Ints> template <typename Fn, size_t... Ints>
auto for_constexpr(Fn &&func, index_sequence<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>>>) { if constexpr (util::is_void_v<util::invoke_result_t<Fn, util::integral_constant<size_t, 0>>>) {
(func(type::integral_constant<size_t, Ints>{}), ...); (func(util::integral_constant<size_t, Ints>{}), ...);
} else { } else {
if ((func(type::integral_constant<size_t, Ints>{}) && ...)) if ((func(util::integral_constant<size_t, Ints>{}) && ...))
return true; return true;
return false; return false;
} }