From 62efd1cb26b5e36a49743ac14ac8da25a3f1142c Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sun, 29 May 2022 16:16:19 +0200 Subject: [PATCH] Migrate to actual C++ standard library --- array.hpp | 238 ------------------------------- func.hpp | 94 ------------- new.cpp | 42 ------ new.hpp | 18 --- type.hpp | 414 ------------------------------------------------------ util.hpp | 117 +++------------ 6 files changed, 21 insertions(+), 902 deletions(-) delete mode 100644 array.hpp delete mode 100644 func.hpp delete mode 100644 new.cpp delete mode 100644 new.hpp delete mode 100644 type.hpp diff --git a/array.hpp b/array.hpp deleted file mode 100644 index 388878a..0000000 --- a/array.hpp +++ /dev/null @@ -1,238 +0,0 @@ -#pragma once - -#include - -#include "type.hpp" -#include "util.hpp" - -namespace util { - -template -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 = ; - // using const_reverse_iterator = ; - - constexpr reference at(size_type pos) - { - if (pos >= N) { - return *static_cast(nullptr); - } - - return m_data[pos]; - } - - constexpr const_reference at(size_type pos) const - { - if (pos >= N) { - return *static_cast(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(), declval()))) - { - 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 -constexpr array, N> to_array_impl(T (&a)[N], index_sequence) -{ - return {{a[I]...}}; -} - -template -constexpr array, N> to_array_impl(T(&&a)[N], index_sequence) -{ - return {{move(a[I])...}}; -} - -} // namespace detail - -template -constexpr array, N> to_array(T (&a)[N]) -{ - return detail::to_array_impl(a, make_index_sequence{}); -} - -template -constexpr array, N> to_array(T(&&a)[N]) -{ - return detail::to_array_impl(move(a), make_index_sequence{}); -} - -////////////////////////////////////////////////////////////////////////// -// deduction guides - -template -array(T, U...)->array; - -} // namespace util diff --git a/func.hpp b/func.hpp deleted file mode 100644 index 4b6b8e0..0000000 --- a/func.hpp +++ /dev/null @@ -1,94 +0,0 @@ -#pragma once - -#include "type.hpp" -#include "util.hpp" - -namespace util { - -// - -// clang-format off - -namespace detail { - -template constexpr bool is_reference_wrapper_v = false; -template constexpr bool is_reference_wrapper_v> = true; - -template -constexpr decltype(auto) INVOKE(Type T::*f, T1 &&t1, Args &&... args) -{ - if constexpr (util::is_member_function_pointer_v) { - if constexpr (util::is_base_of_v>) - return (util::forward(t1).*f)(util::forward(args)...); - else if constexpr (is_reference_wrapper_v>) - return (t1.get().*f)(util::forward(args)...); - else - return ((*util::forward(t1)).*f)(util::forward(args)...); - } else { - static_assert(util::is_member_object_pointer_v); - static_assert(sizeof...(args) == 0); - if constexpr (util::is_base_of_v>) - return util::forward(t1).*f; - else if constexpr (is_reference_wrapper_v>) - return t1.get().*f; - else - return (*util::forward(t1)).*f; - } -} - -template -constexpr decltype(auto) INVOKE(Fn &&f, Args &&... args) -{ - return util::forward(f)(util::forward(args)...); -} - -template constexpr T &FUN(T &t) noexcept { return t; } -template void FUN(T &&) = delete; - -} // namespace detail - -template -constexpr util::invoke_result_t invoke(Fn &&f, Args &&... args) - noexcept(util::is_nothrow_invocable_v) -{ - return detail::INVOKE(util::forward(f), util::forward(args)...); -} - -template -class reference_wrapper { - public: - // types - using type = T; - - // construct/copy/destroy - template (util::declval()), - util::enable_if_t>>())> - constexpr reference_wrapper(U &&u) noexcept(noexcept(detail::FUN(util::forward(u)))) - : _ptr(util::addressof(detail::FUN(util::forward(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 - constexpr util::invoke_result_t operator()(ArgTypes &&... args) const - { - return invoke(get(), util::forward(args)...); - } - - private: - T *_ptr; -}; - -// deduction guides -template reference_wrapper(T &) -> reference_wrapper; - -// clang-format on - -} // namespace util diff --git a/new.cpp b/new.cpp deleted file mode 100644 index a070e4d..0000000 --- a/new.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "new.hpp" - -#include - -[[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 {} diff --git a/new.hpp b/new.hpp deleted file mode 100644 index 06c3e59..0000000 --- a/new.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include - -[[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; diff --git a/type.hpp b/type.hpp deleted file mode 100644 index 4474ca1..0000000 --- a/type.hpp +++ /dev/null @@ -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 -#include -#include - -namespace util { - -// - -// clang-format off -template struct remove_reference { using type = T; }; -template struct remove_reference { using type = T; }; -template struct remove_reference { using type = T; }; -template using remove_reference_t = typename remove_reference::type; - -template -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 using bool_constant = integral_constant; -using false_type = bool_constant; -using true_type = bool_constant; - -// Not part of , but very useful -template struct always_false : false_type {}; -template inline constexpr auto always_false_v = always_false::value; - -template struct is_const : false_type {}; -template struct is_const : true_type {}; -template inline constexpr bool is_const_v = is_const::value; - -template struct is_reference : false_type {}; -template struct is_reference : true_type {}; -template struct is_reference : true_type {}; -template inline constexpr bool is_reference_v = is_reference::value; - -template struct is_lvalue_reference : false_type {}; -template struct is_lvalue_reference : true_type {}; -template inline constexpr bool is_lvalue_reference_v = is_lvalue_reference::value; - -template struct is_function : integral_constant && !is_reference_v> {}; -template inline constexpr bool is_function_v = is_function::value; - -// Must use intrinsic, because this cannot be implemented in standard C++ -template struct is_union : integral_constant {}; -template inline constexpr bool is_union_v = is_union::value; - -namespace detail { - - template struct type_identity { using type = T; }; - - template auto try_add_pointer(int) -> type_identity *>; - template auto try_add_pointer(...) -> type_identity; - - template auto try_add_lvalue_reference(int) -> type_identity; - template auto try_add_lvalue_reference(...) -> type_identity; - - template auto try_add_rvalue_reference(int) -> type_identity; - template auto try_add_rvalue_reference(...) -> type_identity; - - template struct is_pointer_helper : false_type {}; - template struct is_pointer_helper : true_type {}; - - template struct is_member_pointer_helper : false_type {}; - template struct is_member_pointer_helper : true_type {}; - - template struct is_member_function_pointer_helper : false_type {}; - template struct is_member_function_pointer_helper : is_function {}; - - template integral_constant::value> is_class_helper(int T::*); - template false_type is_class_helper(...); - - template true_type test_pre_ptr_convertible(const volatile B *); - template false_type test_pre_ptr_convertible(const volatile void *); - - template auto test_pre_is_base_of(...) -> true_type; - template auto test_pre_is_base_of(int) -> decltype(test_pre_ptr_convertible(static_cast(nullptr))); - -} // namespace detail - -template struct conditional { using type = T; }; -template struct conditional { using type = F; }; -template using conditional_t = typename conditional::type; - -template struct is_array : false_type {}; -template struct is_array : true_type {}; -template struct is_array : true_type {}; -template inline constexpr bool is_array_v = is_array::value; - -template struct remove_extent { using type = T; }; -template struct remove_extent { using type = T; }; -template struct remove_extent { using type = T; }; -template using remove_extent_t = typename remove_extent::type; - -template struct add_pointer : decltype(detail::try_add_pointer(0)) {}; -template using add_pointer_t = typename add_pointer::type; - -template struct add_lvalue_reference : decltype(detail::try_add_lvalue_reference(0)) {}; -template using add_lvalue_reference_t = typename add_lvalue_reference::type; - -template struct add_rvalue_reference : decltype(detail::try_add_rvalue_reference(0)) {}; -template using add_rvalue_reference_t = typename add_rvalue_reference::type; - -template struct remove_cv { using type = T; }; -template struct remove_cv { using type = T; }; -template struct remove_cv { using type = T; }; -template struct remove_cv { using type = T; }; -template struct remove_const { using type = T; }; -template struct remove_const { using type = T; }; -template struct remove_volatile { using type = T; }; -template struct remove_volatile { using type = T; }; -template using remove_cv_t = typename remove_cv::type; -template using remove_const_t = typename remove_const::type; -template using remove_volatile_t = typename remove_volatile::type; - -template struct remove_cvref { using type = remove_cv_t>; }; -template using remove_cvref_t = typename remove_cvref::type; - -template -struct decay { -private: - using U = remove_reference_t; -public: - using type = conditional_t, - remove_extent_t *, - conditional_t::value, - add_pointer_t, - remove_cv_t - > - >; -}; -template using decay_t = typename decay::type; - -template struct is_same : false_type {}; -template struct is_same : true_type {}; -template inline constexpr auto is_same_v = is_same::value; - -template -struct is_floating_point : integral_constant> || - is_same_v> || - is_same_v>> -{}; -template inline constexpr auto is_floating_point_v = is_floating_point::value; - -template -struct is_integral : integral_constant> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v> || - is_same_v>> -{}; -template inline constexpr auto is_integral_v = is_integral::value; - -template struct is_arithmetic : integral_constant::value || is_floating_point::value> {}; -template inline constexpr bool is_arithmetic_v = is_arithmetic::value; - -template struct is_void : is_same> {}; -template inline constexpr bool is_void_v = is_void::value; - -template struct is_pointer : detail::is_pointer_helper> {}; -template inline constexpr bool is_pointer_v = is_pointer::value; - -template struct is_member_pointer : detail::is_member_pointer_helper> {}; -template inline constexpr bool is_member_pointer_v = is_member_pointer::value; - -template struct is_member_function_pointer : detail::is_member_function_pointer_helper> {}; -template inline constexpr bool is_member_function_pointer_v = is_member_function_pointer::value; - -template struct is_member_object_pointer : integral_constant && !is_member_function_pointer_v> {}; -template inline constexpr bool is_member_object_pointer_v = is_member_object_pointer::value; - -template struct is_class : decltype(detail::is_class_helper(nullptr)) {}; -template inline constexpr bool is_class_v = is_class::value; - -template struct is_enum : integral_constant && - !is_integral_v && - !is_floating_point_v && - !is_array_v && - !is_pointer_v && - !is_reference_v && - !is_member_pointer_v && - !is_union_v && - !is_class_v && - !is_function_v> -{}; -template inline constexpr bool is_enum_v = is_enum::value; - -template struct is_null_pointer : is_same> {}; -template inline constexpr bool is_null_pointer_v = is_null_pointer::value; - -template -struct is_scalar : integral_constant || - is_enum_v || - is_pointer_v || - is_member_pointer_v || - is_null_pointer_v> -{}; -template inline constexpr bool is_scalar_v = is_scalar::value; - -template struct is_object : integral_constant || - is_array_v || - is_union_v || - is_class_v> -{}; -template inline constexpr bool is_object_v = is_object::value; - -template struct is_base_of : integral_constant && - is_class_v && - decltype(detail::test_pre_is_base_of(0))::value> -{}; -template inline constexpr bool is_base_of_v = is_base_of::value; - -template struct enable_if {}; -template struct enable_if { using type = T; }; -template using enable_if_t = typename enable_if::type; - -} // namespace type - -////////////////////////////////////////////////////////////////////////// -// Forward declarations - -namespace util { - -template struct reference_wrapper; -template inline constexpr T &&forward(util::remove_reference_t &t) noexcept; -template util::add_rvalue_reference_t declval() noexcept { - static_assert(always_false_v, "declval not allowed in an evaluated context"); -} - -} // namespace util - -////////////////////////////////////////////////////////////////////////// - -namespace util { - -// Everything is nothrow_invocable, because exceptions are disabled in avr-gcc -template struct is_nothrow_invocable : true_type {}; -template inline constexpr auto is_nothrow_invocable_v = is_nothrow_invocable::value; - -namespace detail { - -template struct is_reference_wrapper : false_type {}; -template struct is_reference_wrapper> : true_type {}; - -template -struct invoke_impl { - template - static auto call(Fn &&f, Args &&... args) -> decltype(util::forward(f)(util::forward(args)...)); -}; - -template -struct invoke_impl { - template , typename = enable_if_t>> - static auto get(T &&t) -> T &&; - - template , typename = enable_if_t::value>> - static auto get(T &&t) -> decltype(t.get()); - - template , - typename = enable_if_t>, - typename = enable_if_t::value>> - static auto get(T &&t) -> decltype(*util::forward(t)); - - template >> - static auto call(MT1 B::*pmf, T &&t, Args &&... args) - -> decltype((invoke_impl::get(util::forward(t)).*pmf)(util::forward(args)...)); - - template - static auto call(MT B::*pmd, T &&t) -> decltype(invoke_impl::get(util::forward(t)).*pmd); -}; - -template > -auto INVOKE(Fn &&f, Args &&... args) - -> decltype(invoke_impl::call(util::forward(f), util::forward(args)...)); - -template struct invoke_result {}; -template -struct invoke_result(), util::declval()...))), Fn, Args...> { - using type = decltype(INVOKE(util::declval(), util::declval()...)); -}; - -} // namespace detail - -template struct invoke_result : detail::invoke_result {}; -template using invoke_result_t = typename invoke_result::type; - -// - -template struct numeric_limits {}; - -template <> -struct numeric_limits { - static constexpr bool min() { return false; } - static constexpr bool max() { return true; } -}; - -template <> -struct numeric_limits { - static constexpr char min() { return CHAR_MIN; } - static constexpr char max() { return CHAR_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr signed char min() { return SCHAR_MIN; } - static constexpr signed char max() { return SCHAR_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr unsigned char min() { return 0; } - static constexpr unsigned char max() { return UCHAR_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr short min() { return SHRT_MIN; } - static constexpr short max() { return SHRT_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr int min() { return INT_MIN; } - static constexpr int max() { return INT_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr long int min() { return LONG_MIN; } - static constexpr long int max() { return LONG_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr long long int min() { return LLONG_MIN; } - static constexpr long long int max() { return LLONG_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr unsigned short min() { return 0; } - static constexpr unsigned short max() { return USHRT_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr unsigned int min() { return 0; } - static constexpr unsigned int max() { return UINT_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr unsigned long int min() { return 0; } - static constexpr unsigned long int max() { return ULONG_MAX; } -}; - -template <> -struct numeric_limits { - static constexpr unsigned long long int min() { return 0; } - static constexpr unsigned long long int max() { return ULLONG_MAX; } -}; - -template <> -struct numeric_limits { - 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 { - 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 { - 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 diff --git a/util.hpp b/util.hpp index 680e2ce..cb60d30 100644 --- a/util.hpp +++ b/util.hpp @@ -1,126 +1,51 @@ #pragma once -#include +#include +#include -#include "type.hpp" +#include +#include namespace util { -// - -// clang-format off - -template -util::enable_if_t, T *> addressof(T &arg) noexcept -{ - return reinterpret_cast(&const_cast(reinterpret_cast(arg))); -} - -template -util::enable_if_t, T *> addressof(T &arg) noexcept -{ - return &arg; -} - -// - -template util::add_rvalue_reference_t declval() noexcept; - -template -inline constexpr util::remove_reference_t &&move(T &&t) noexcept -{ - return static_cast &&>(t); -} - -template -inline constexpr T &&forward(util::remove_reference_t &t) noexcept -{ - return static_cast(t); -} - -template -inline constexpr T &&forward(util::remove_reference_t &&t) noexcept -{ - static_assert(!util::is_lvalue_reference_v, "Can not forward an rvalue as an lvalue."); - return static_cast(t); -} - -template -struct integer_sequence { - using __type = integer_sequence; - using value_type = T; - - static constexpr size_t size() noexcept - { - return sizeof...(Ints); - } -}; - -template using index_sequence = integer_sequence; - -namespace detail { - -template struct concat; - -template -struct concat, integer_sequence> - : integer_sequence -{}; - -// 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 -struct gen_integer_sequence : detail::concat::__type, - typename gen_integer_sequence::__type> -{}; - -template struct gen_integer_sequence : integer_sequence {}; -template struct gen_integer_sequence : integer_sequence {}; - -} // namespace detail - -template -struct make_integer_sequence : detail::gen_integer_sequence { - static_assert(N >= 0, "Integer sequence cannot be negative"); -}; - -template using make_index_sequence = make_integer_sequence; - -template using index_sequence_for = make_index_sequence; - -// Not part of , but very useful - namespace detail { template -inline constexpr integer_sequence add_offset(integer_sequence) { return {}; } +inline constexpr std::integer_sequence add_offset(std::integer_sequence) +{ + return {}; +} } // namespace detail template inline constexpr auto make_offset_integer_sequence() { - return detail::add_offset(make_integer_sequence{}); + return detail::add_offset(std::make_integer_sequence{}); } -template +template inline constexpr auto make_offset_index_sequence() { - return make_offset_integer_sequence(); + return make_offset_integer_sequence(); } -template -constexpr auto for_constexpr(Fn &&func, index_sequence) +template +constexpr auto for_constexpr(Fn &&func, std::index_sequence) { - if constexpr (util::is_void_v>>) { - (func(util::integral_constant{}), ...); + if constexpr (std::is_void_v>>) { + (func(std::integral_constant{}), ...); } else { - if ((func(util::integral_constant{}) && ...)) + if ((func(std::integral_constant{}) && ...)) return true; return false; } } -// clang-format on +template +struct always_false : std::false_type { +}; +template +inline constexpr auto always_false_v = always_false::value; } // namespace util