Migrate to actual C++ standard library

This commit is contained in:
BlackMark 2022-05-29 16:16:19 +02:00
parent 5282fdde46
commit 62efd1cb26
6 changed files with 21 additions and 902 deletions

238
array.hpp
View File

@ -1,238 +0,0 @@
#pragma once
#include <stddef.h>
#include "type.hpp"
#include "util.hpp"
namespace util {
template <class T, size_t N>
struct array {
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;
using iterator = pointer;
using const_iterator = const_pointer;
// using reverse_iterator = <not implemented>;
// using const_reverse_iterator = <not implemented>;
constexpr reference at(size_type pos)
{
if (pos >= N) {
return *static_cast<pointer>(nullptr);
}
return m_data[pos];
}
constexpr const_reference at(size_type pos) const
{
if (pos >= N) {
return *static_cast<pointer>(nullptr);
}
return m_data[pos];
}
constexpr reference operator[](size_type pos)
{
return m_data[pos];
}
constexpr const_reference operator[](size_type pos) const
{
return m_data[pos];
}
constexpr reference front()
{
return m_data[0];
}
constexpr const_reference front() const
{
return m_data[0];
}
constexpr reference back()
{
return m_data[N - 1];
}
constexpr const_reference back() const
{
return m_data[N - 1];
}
constexpr pointer data() noexcept
{
return m_data;
}
constexpr const_pointer data() const noexcept
{
return m_data;
}
constexpr iterator begin() noexcept
{
return &m_data[0];
}
constexpr const_iterator begin() const noexcept
{
return &m_data[0];
}
constexpr const_iterator cbegin() const noexcept
{
return &m_data[0];
}
constexpr iterator end() noexcept
{
return &m_data[N];
}
constexpr const_iterator end() const noexcept
{
return &m_data[N];
}
constexpr const_iterator cend() const noexcept
{
return &m_data[N];
}
// reverse_iterator rbegin() noexcept;
// const_reverse_iterator rbegin() const noexcept;
// const_reverse_iterator crbegin() const noexcept;
// reverse_iterator rend() noexcept;
// const_reverse_iterator rend() const noexcept;
// const_reverse_iterator crend() const noexcept;
constexpr bool empty() const noexcept
{
return N == 0;
}
constexpr size_type size() const noexcept
{
return N;
}
constexpr size_type max_size() const noexcept
{
return N;
}
constexpr void fill(const T &value)
{
for (size_t i = 0; i < N; ++i) {
m_data[i] = value;
}
}
constexpr void swap(array &other) noexcept(noexcept(swap(declval<T &>(), declval<T &>())))
{
for (size_t i = 0; i < N; ++i) {
T tmp = m_data[i];
m_data[i] = other.m_data[i];
other.m_data[i] = tmp;
}
}
//////////////////////////////////////////////////////////////////////////
friend constexpr bool operator==(const array &lhs, const array &rhs)
{
for (size_t i = 0; i < N; ++i) {
if (!(lhs[i] == rhs[i])) {
return false;
}
}
return true;
}
friend constexpr bool operator!=(const array &lhs, const array &rhs)
{
return !(lhs == rhs);
}
friend constexpr bool operator<(const array &lhs, const array &rhs)
{
for (size_t i = 0; i < N; ++i) {
if (rhs[i] < lhs[i]) {
return false;
} else if (lhs[i] < rhs[i]) {
return true;
}
}
return false;
}
friend constexpr bool operator<=(const array &lhs, const array &rhs)
{
return !(rhs < lhs);
}
friend constexpr bool operator>(const array &lhs, const array &rhs)
{
return (rhs < lhs);
}
friend constexpr bool operator>=(const array &lhs, const array &rhs)
{
return !(lhs < rhs);
}
//////////////////////////////////////////////////////////////////////////
T m_data[N];
};
//////////////////////////////////////////////////////////////////////////
namespace detail {
template <typename T, size_t N, size_t... I>
constexpr array<remove_cv_t<T>, N> to_array_impl(T (&a)[N], index_sequence<I...>)
{
return {{a[I]...}};
}
template <typename T, size_t N, size_t... I>
constexpr array<remove_cv_t<T>, N> to_array_impl(T(&&a)[N], index_sequence<I...>)
{
return {{move(a[I])...}};
}
} // namespace detail
template <typename T, size_t N>
constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N])
{
return detail::to_array_impl(a, make_index_sequence<N>{});
}
template <typename T, size_t N>
constexpr array<remove_cv_t<T>, N> to_array(T(&&a)[N])
{
return detail::to_array_impl(move(a), make_index_sequence<N>{});
}
//////////////////////////////////////////////////////////////////////////
// deduction guides
template <typename T, typename... U>
array(T, U...)->array<T, 1 + sizeof...(U)>;
} // namespace util

View File

@ -1,94 +0,0 @@
#pragma once
#include "type.hpp"
#include "util.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 (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<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(util::is_member_object_pointer_v<decltype(f)>);
static_assert(sizeof...(args) == 0);
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<util::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 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)...);
}
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>()),
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))))
{}
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 util::invoke_result_t<T &, ArgTypes...> operator()(ArgTypes &&... args) const
{
return invoke(get(), util::forward<ArgTypes>(args)...);
}
private:
T *_ptr;
};
// deduction guides
template <typename T> reference_wrapper(T &) -> reference_wrapper<T>;
// clang-format on
} // namespace util

42
new.cpp
View File

@ -1,42 +0,0 @@
#include "new.hpp"
#include <stdlib.h>
[[nodiscard]] void *operator new(size_t size)
{
return malloc(size);
}
void operator delete(void *ptr) noexcept
{
free(ptr);
}
void operator delete(void *ptr, size_t) noexcept
{
free(ptr);
}
[[nodiscard]] void *operator new[](size_t size)
{
return malloc(size);
}
void operator delete[](void *ptr) noexcept
{
free(ptr);
}
void operator delete[](void *ptr, size_t) noexcept
{
free(ptr);
}
[[nodiscard]] void *operator new(size_t, void *ptr) noexcept
{
return ptr;
}
[[nodiscard]] void *operator new[](size_t, void *ptr) noexcept
{
return ptr;
}
void operator delete(void *, void *)noexcept {}
void operator delete[](void *, void *) noexcept {}

18
new.hpp
View File

@ -1,18 +0,0 @@
#pragma once
#include <stddef.h>
[[nodiscard]] void *operator new(size_t size);
void operator delete(void *ptr) noexcept;
void operator delete(void *ptr, size_t size) noexcept;
[[nodiscard]] void *operator new[](size_t size);
void operator delete[](void *ptr) noexcept;
void operator delete[](void *ptr, size_t size) noexcept;
[[nodiscard]] void *operator new(size_t size, void *ptr) noexcept;
[[nodiscard]] void *operator new[](size_t size, void *ptr) noexcept;
void operator delete(void *ptr, void *)noexcept;
void operator delete[](void *ptr, void *) noexcept;

414
type.hpp
View File

@ -1,414 +0,0 @@
#pragma once
// 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 util {
// <type_traits>
// clang-format off
template <typename T> struct remove_reference { using type = T; };
template <typename T> struct remove_reference<T &> { using type = T; };
template <typename T> struct remove_reference<T &&> { using type = T; };
template <typename T> using remove_reference_t = typename remove_reference<T>::type;
template <typename T, T v>
struct integral_constant {
static constexpr T value = v;
using value_type = T;
using type = integral_constant; // using injected-class-name
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; } // since c++14
};
template <bool B> using bool_constant = integral_constant<bool, B>;
using false_type = bool_constant<false>;
using true_type = bool_constant<true>;
// Not part of <type_traits>, but very useful
template <typename...> struct always_false : false_type {};
template <typename... Ts> inline constexpr auto always_false_v = always_false<Ts...>::value;
template <typename T> struct is_const : false_type {};
template <typename T> struct is_const<const T> : true_type {};
template <typename T> inline constexpr bool is_const_v = is_const<T>::value;
template <typename T> struct is_reference : false_type {};
template <typename T> struct is_reference<T &> : true_type {};
template <typename T> struct is_reference<T &&> : true_type {};
template <typename T> inline constexpr bool is_reference_v = is_reference<T>::value;
template <typename T> struct is_lvalue_reference : false_type {};
template <typename T> struct is_lvalue_reference<T &> : true_type {};
template <typename T> inline constexpr bool is_lvalue_reference_v = is_lvalue_reference<T>::value;
template <typename T> struct is_function : integral_constant<bool, !is_const_v<const T> && !is_reference_v<T>> {};
template <typename T> inline constexpr bool is_function_v = is_function<T>::value;
// Must use intrinsic, because this cannot be implemented in standard C++
template <typename T> struct is_union : integral_constant<bool, __is_union(T)> {};
template <typename T> inline constexpr bool is_union_v = is_union<T>::value;
namespace detail {
template <typename T> struct type_identity { using type = T; };
template <typename T> auto try_add_pointer(int) -> type_identity<remove_reference_t<T> *>;
template <typename T> auto try_add_pointer(...) -> type_identity<T>;
template <typename T> auto try_add_lvalue_reference(int) -> type_identity<T &>;
template <typename T> auto try_add_lvalue_reference(...) -> type_identity<T>;
template <typename T> auto try_add_rvalue_reference(int) -> type_identity<T &&>;
template <typename T> auto try_add_rvalue_reference(...) -> type_identity<T>;
template <typename T> struct is_pointer_helper : false_type {};
template <typename T> struct is_pointer_helper<T *> : true_type {};
template <typename T> struct is_member_pointer_helper : false_type {};
template <typename T, class U> struct is_member_pointer_helper<T U::*> : true_type {};
template <typename T> struct is_member_function_pointer_helper : false_type {};
template <typename T, typename U> struct is_member_function_pointer_helper<T U::*> : is_function<T> {};
template <typename T> integral_constant<bool, !is_union<T>::value> is_class_helper(int T::*);
template <typename> false_type is_class_helper(...);
template <typename B> true_type test_pre_ptr_convertible(const volatile B *);
template <typename> false_type test_pre_ptr_convertible(const volatile void *);
template <typename, typename> auto test_pre_is_base_of(...) -> true_type;
template <typename B, typename D> auto test_pre_is_base_of(int) -> decltype(test_pre_ptr_convertible<B>(static_cast<D *>(nullptr)));
} // namespace detail
template <bool B, typename T, typename F> struct conditional { using type = T; };
template <typename T, typename F> struct conditional<false, T, F> { using type = F; };
template <bool B, class T, class F> using conditional_t = typename conditional<B, T, F>::type;
template <typename T> struct is_array : false_type {};
template <typename T> struct is_array<T[]> : true_type {};
template <typename T, size_t N> struct is_array<T[N]> : true_type {};
template <typename T> inline constexpr bool is_array_v = is_array<T>::value;
template <typename T> struct remove_extent { using type = T; };
template <typename T> struct remove_extent<T[]> { using type = T; };
template <typename T, size_t N> struct remove_extent<T[N]> { using type = T; };
template <typename T> using remove_extent_t = typename remove_extent<T>::type;
template <typename T> struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {};
template <typename T> using add_pointer_t = typename add_pointer<T>::type;
template <typename T> struct add_lvalue_reference : decltype(detail::try_add_lvalue_reference<T>(0)) {};
template <typename T> using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
template <typename T> struct add_rvalue_reference : decltype(detail::try_add_rvalue_reference<T>(0)) {};
template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
template <typename T> struct remove_cv { using type = T; };
template <typename T> struct remove_cv<const T> { using type = T; };
template <typename T> struct remove_cv<volatile T> { using type = T; };
template <typename T> struct remove_cv<const volatile T> { using type = T; };
template <typename T> struct remove_const { using type = T; };
template <typename T> struct remove_const<const T> { using type = T; };
template <typename T> struct remove_volatile { using type = T; };
template <typename T> struct remove_volatile<volatile T> { using type = T; };
template <typename T> using remove_cv_t = typename remove_cv<T>::type;
template <typename T> using remove_const_t = typename remove_const<T>::type;
template <typename T> using remove_volatile_t = typename remove_volatile<T>::type;
template <typename T> struct remove_cvref { using type = remove_cv_t<remove_reference_t<T>>; };
template <typename T> using remove_cvref_t = typename remove_cvref<T>::type;
template <typename T>
struct decay {
private:
using U = remove_reference_t<T>;
public:
using type = conditional_t<is_array_v<U>,
remove_extent_t<U> *,
conditional_t<is_function<U>::value,
add_pointer_t<U>,
remove_cv_t<U>
>
>;
};
template <typename T> using decay_t = typename decay<T>::type;
template <typename T, typename U> struct is_same : false_type {};
template <typename T> struct is_same<T, T> : true_type {};
template <typename T, typename U> inline constexpr auto is_same_v = is_same<T, U>::value;
template <typename T>
struct is_floating_point : integral_constant<bool,
is_same_v<float, remove_cv_t<T>> ||
is_same_v<double, remove_cv_t<T>> ||
is_same_v<long double, remove_cv_t<T>>>
{};
template <typename T> inline constexpr auto is_floating_point_v = is_floating_point<T>::value;
template <typename T>
struct is_integral : integral_constant<bool,
is_same_v<bool, remove_cv_t<T>> ||
is_same_v<char, remove_cv_t<T>> ||
is_same_v<signed char, remove_cv_t<T>> ||
is_same_v<unsigned char, remove_cv_t<T>> ||
is_same_v<wchar_t, remove_cv_t<T>> ||
is_same_v<char16_t, remove_cv_t<T>> ||
is_same_v<char32_t, remove_cv_t<T>> ||
is_same_v<short int, remove_cv_t<T>> ||
is_same_v<unsigned short int, remove_cv_t<T>> ||
is_same_v<int, remove_cv_t<T>> ||
is_same_v<unsigned int, remove_cv_t<T>> ||
is_same_v<long int, remove_cv_t<T>> ||
is_same_v<unsigned long int, remove_cv_t<T>> ||
is_same_v<long long int, remove_cv_t<T>> ||
is_same_v<unsigned long long int, remove_cv_t<T>>>
{};
template <typename T> inline constexpr auto is_integral_v = is_integral<T>::value;
template <typename T> struct is_arithmetic : integral_constant<bool, is_integral<T>::value || is_floating_point<T>::value> {};
template <typename T> inline constexpr bool is_arithmetic_v = is_arithmetic<T>::value;
template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_void_v = is_void<T>::value;
template <typename T> struct is_pointer : detail::is_pointer_helper<remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_pointer_v = is_pointer<T>::value;
template <typename T> struct is_member_pointer : detail::is_member_pointer_helper<remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_member_pointer_v = is_member_pointer<T>::value;
template <typename T> struct is_member_function_pointer : detail::is_member_function_pointer_helper<remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_member_function_pointer_v = is_member_function_pointer<T>::value;
template <typename T> struct is_member_object_pointer : integral_constant<bool, is_member_pointer_v<T> && !is_member_function_pointer_v<T>> {};
template <typename T> inline constexpr bool is_member_object_pointer_v = is_member_object_pointer<T>::value;
template <typename T> struct is_class : decltype(detail::is_class_helper<T>(nullptr)) {};
template <typename T> inline constexpr bool is_class_v = is_class<T>::value;
template <typename T> struct is_enum : integral_constant<bool,
!is_void_v<T> &&
!is_integral_v<T> &&
!is_floating_point_v<T> &&
!is_array_v<T> &&
!is_pointer_v<T> &&
!is_reference_v<T> &&
!is_member_pointer_v<T> &&
!is_union_v<T> &&
!is_class_v<T> &&
!is_function_v<T>>
{};
template <typename T> inline constexpr bool is_enum_v = is_enum<T>::value;
template <typename T> struct is_null_pointer : is_same<nullptr_t, remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_null_pointer_v = is_null_pointer<T>::value;
template <typename T>
struct is_scalar : integral_constant<bool,
is_arithmetic_v<T> ||
is_enum_v<T> ||
is_pointer_v<T> ||
is_member_pointer_v<T> ||
is_null_pointer_v<T>>
{};
template <typename T> inline constexpr bool is_scalar_v = is_scalar<T>::value;
template <typename T> struct is_object : integral_constant<bool,
is_scalar_v<T> ||
is_array_v<T> ||
is_union_v<T> ||
is_class_v<T>>
{};
template <typename T> inline constexpr bool is_object_v = is_object<T>::value;
template <typename Base, typename Derived> struct is_base_of : integral_constant<bool,
is_class_v<Base> &&
is_class_v<Derived> &&
decltype(detail::test_pre_is_base_of<Base, Derived>(0))::value>
{};
template <typename Base, typename Derived> inline constexpr bool is_base_of_v = is_base_of<Base, Derived>::value;
template <bool B, typename T = void> struct enable_if {};
template <typename T> struct enable_if<true, T> { using type = T; };
template <bool B, typename T = void> using enable_if_t = typename enable_if<B, T>::type;
} // namespace type
//////////////////////////////////////////////////////////////////////////
// Forward declarations
namespace util {
template <typename T> struct reference_wrapper;
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 {
static_assert(always_false_v<T>, "declval not allowed in an evaluated context");
}
} // namespace util
//////////////////////////////////////////////////////////////////////////
namespace util {
// Everything is nothrow_invocable, because exceptions are disabled in avr-gcc
template <typename, typename...> struct is_nothrow_invocable : true_type {};
template <typename Fn, typename... Args> inline constexpr auto is_nothrow_invocable_v = is_nothrow_invocable<Fn, Args...>::value;
namespace detail {
template <typename T> struct is_reference_wrapper : false_type {};
template <typename U> struct is_reference_wrapper<util::reference_wrapper<U>> : true_type {};
template <typename T>
struct invoke_impl {
template <typename Fn, typename... Args>
static auto call(Fn &&f, Args &&... args) -> decltype(util::forward<Fn>(f)(util::forward<Args>(args)...));
};
template <typename B, typename MT>
struct invoke_impl<MT B::*> {
template <typename T, typename Td = decay_t<T>, typename = enable_if_t<is_base_of_v<B, Td>>>
static auto get(T &&t) -> T &&;
template <typename T, typename Td = decay_t<T>, typename = enable_if_t<is_reference_wrapper<Td>::value>>
static auto get(T &&t) -> decltype(t.get());
template <typename T, typename Td = decay_t<T>,
typename = enable_if_t<!is_base_of_v<B, Td>>,
typename = enable_if_t<!is_reference_wrapper<Td>::value>>
static auto get(T &&t) -> decltype(*util::forward<T>(t));
template <typename T, typename... Args, typename MT1,
typename = enable_if_t<is_function_v<MT1>>>
static auto call(MT1 B::*pmf, T &&t, Args &&... args)
-> decltype((invoke_impl::get(util::forward<T>(t)).*pmf)(util::forward<Args>(args)...));
template <typename T>
static auto call(MT B::*pmd, T &&t) -> decltype(invoke_impl::get(util::forward<T>(t)).*pmd);
};
template <typename Fn, class... Args, class Fd = decay_t<Fn>>
auto INVOKE(Fn &&f, Args &&... args)
-> decltype(invoke_impl<Fd>::call(util::forward<Fn>(f), util::forward<Args>(args)...));
template <typename AlwaysVoid, typename, typename...> struct invoke_result {};
template <typename Fn, typename... Args>
struct invoke_result<decltype(void(INVOKE(util::declval<Fn>(), util::declval<Args>()...))), Fn, Args...> {
using type = decltype(INVOKE(util::declval<Fn>(), util::declval<Args>()...));
};
} // namespace detail
template <typename Fn, typename... ArgTypes> struct invoke_result : detail::invoke_result<void, Fn, ArgTypes...> {};
template <typename Fn, typename... ArgTypes> using invoke_result_t = typename invoke_result<Fn, ArgTypes...>::type;
// <limits>
template <typename T> struct numeric_limits {};
template <>
struct numeric_limits<bool> {
static constexpr bool min() { return false; }
static constexpr bool max() { return true; }
};
template <>
struct numeric_limits<char> {
static constexpr char min() { return CHAR_MIN; }
static constexpr char max() { return CHAR_MAX; }
};
template <>
struct numeric_limits<signed char> {
static constexpr signed char min() { return SCHAR_MIN; }
static constexpr signed char max() { return SCHAR_MAX; }
};
template <>
struct numeric_limits<unsigned char> {
static constexpr unsigned char min() { return 0; }
static constexpr unsigned char max() { return UCHAR_MAX; }
};
template <>
struct numeric_limits<short> {
static constexpr short min() { return SHRT_MIN; }
static constexpr short max() { return SHRT_MAX; }
};
template <>
struct numeric_limits<int> {
static constexpr int min() { return INT_MIN; }
static constexpr int max() { return INT_MAX; }
};
template <>
struct numeric_limits<long> {
static constexpr long int min() { return LONG_MIN; }
static constexpr long int max() { return LONG_MAX; }
};
template <>
struct numeric_limits<long long int> {
static constexpr long long int min() { return LLONG_MIN; }
static constexpr long long int max() { return LLONG_MAX; }
};
template <>
struct numeric_limits<unsigned short> {
static constexpr unsigned short min() { return 0; }
static constexpr unsigned short max() { return USHRT_MAX; }
};
template <>
struct numeric_limits<unsigned int> {
static constexpr unsigned int min() { return 0; }
static constexpr unsigned int max() { return UINT_MAX; }
};
template <>
struct numeric_limits<unsigned long int> {
static constexpr unsigned long int min() { return 0; }
static constexpr unsigned long int max() { return ULONG_MAX; }
};
template <>
struct numeric_limits<unsigned long long int> {
static constexpr unsigned long long int min() { return 0; }
static constexpr unsigned long long int max() { return ULLONG_MAX; }
};
template <>
struct numeric_limits<float> {
static constexpr float min() { return FLT_MIN; }
static constexpr float max() { return FLT_MAX; }
static constexpr float lowest() { return -FLT_MAX; }
};
template <>
struct numeric_limits<double> {
static constexpr double min() { return DBL_MIN; }
static constexpr double max() { return DBL_MAX; }
static constexpr double lowest() { return -DBL_MAX; }
};
template <>
struct numeric_limits<long double> {
static constexpr long double min() { return LDBL_MIN; }
static constexpr long double max() { return LDBL_MAX; }
static constexpr long double lowest() { return -LDBL_MAX; }
};
// clang-format on
} // namespace util

117
util.hpp
View File

@ -1,126 +1,51 @@
#pragma once
#include <stdint.h>
#include <type_traits>
#include <utility>
#include "type.hpp"
#include <cstddef>
#include <cstdint>
namespace util {
// <memory>
// clang-format off
template <typename T>
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>
util::enable_if_t<!util::is_object_v<T>, T *> addressof(T &arg) noexcept
{
return &arg;
}
// <utility>
template <typename T> util::add_rvalue_reference_t<T> declval() noexcept;
template <typename T>
inline constexpr util::remove_reference_t<T> &&move(T &&t) noexcept
{
return static_cast<util::remove_reference_t<T> &&>(t);
}
template <typename T>
inline constexpr T &&forward(util::remove_reference_t<T> &t) noexcept
{
return static_cast<T &&>(t);
}
template <typename T>
inline constexpr T &&forward(util::remove_reference_t<T> &&t) noexcept
{
static_assert(!util::is_lvalue_reference_v<T>, "Can not forward an rvalue as an lvalue.");
return static_cast<T &&>(t);
}
template <typename T, T... Ints>
struct integer_sequence {
using __type = integer_sequence;
using value_type = T;
static constexpr size_t size() noexcept
{
return sizeof...(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, T... Ints1, T... Ints2>
struct concat<T, integer_sequence<T, Ints1...>, integer_sequence<T, 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>
{};
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
template <typename T, T N>
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 <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 std::integer_sequence<T, (Offset + Ints)...> add_offset(std::integer_sequence<T, Ints...>)
{
return {};
}
} // namespace detail
template <typename T, T Offset, T N>
inline constexpr auto make_offset_integer_sequence()
{
return detail::add_offset<T, Offset>(make_integer_sequence<T, N>{});
return detail::add_offset<T, Offset>(std::make_integer_sequence<T, N>{});
}
template <size_t Offset, size_t N>
template <std::size_t Offset, std::size_t N>
inline constexpr auto make_offset_index_sequence()
{
return make_offset_integer_sequence<size_t, Offset, N>();
return make_offset_integer_sequence<std::size_t, Offset, N>();
}
template <typename Fn, size_t... Ints>
constexpr auto for_constexpr(Fn &&func, index_sequence<Ints...>)
template <typename Fn, std::size_t... Ints>
constexpr auto for_constexpr(Fn &&func, std::index_sequence<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>{}), ...);
if constexpr (std::is_void_v<std::invoke_result_t<Fn, std::integral_constant<std::size_t, 0>>>) {
(func(std::integral_constant<std::size_t, Ints>{}), ...);
} else {
if ((func(util::integral_constant<size_t, Ints>{}) && ...))
if ((func(std::integral_constant<std::size_t, Ints>{}) && ...))
return true;
return false;
}
}
// clang-format on
template <typename...>
struct always_false : std::false_type {
};
template <typename... Ts>
inline constexpr auto always_false_v = always_false<Ts...>::value;
} // namespace util