From 09dfc35e79dc556e30b24ddca89c4d131e3d58cd Mon Sep 17 00:00:00 2001
From: tqcq <99722391+tqcq@users.noreply.github.com>
Date: Fri, 31 May 2024 10:51:57 +0800
Subject: [PATCH] fix optional
---
src/sled/optional.h | 882 ++++++++++++++------------------------------
1 file changed, 283 insertions(+), 599 deletions(-)
diff --git a/src/sled/optional.h b/src/sled/optional.h
index 0066508..1419fed 100644
--- a/src/sled/optional.h
+++ b/src/sled/optional.h
@@ -14,9 +14,8 @@
// .
///
-#pragma once
-#ifndef SLED_TL_OPTIONAL_HPP
-#define SLED_TL_OPTIONAL_HPP
+#ifndef TL_OPTIONAL_HPP
+#define TL_OPTIONAL_HPP
#define TL_OPTIONAL_VERSION_MAJOR 1
#define TL_OPTIONAL_VERSION_MINOR 1
@@ -32,38 +31,31 @@
#define TL_OPTIONAL_MSVC2015
#endif
-#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 \
- && !defined(__clang__))
+#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
#define TL_OPTIONAL_GCC49
#endif
-#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 \
- && !defined(__clang__))
+#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && !defined(__clang__))
#define TL_OPTIONAL_GCC54
#endif
-#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 \
- && !defined(__clang__))
+#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && !defined(__clang__))
#define TL_OPTIONAL_GCC55
#endif
-#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 \
- && !defined(__clang__))
+#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
// GCC < 5 doesn't support overloading on const&& for member functions
#define TL_OPTIONAL_NO_CONSTRR
// GCC < 5 doesn't support some standard C++11 type traits
-#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
- std::has_trivial_copy_constructor::value
-#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
- std::has_trivial_copy_assign::value
+#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::has_trivial_copy_constructor::value
+#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::has_trivial_copy_assign::value
// This one will be different for GCC 5.7 if it's ever supported
-#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \
- std::is_trivially_destructible::value
+#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value
-// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector
-// for non-copyable types
+// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks
+// std::vector for non-copyable types
#elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__))
#ifndef TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
#define TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
@@ -71,30 +63,22 @@
namespace tl {
namespace detail {
template
-struct is_trivially_copy_constructible
- : std::is_trivially_copy_constructible {};
+struct is_trivially_copy_constructible : std::is_trivially_copy_constructible {};
#ifdef _GLIBCXX_VECTOR
template
-struct is_trivially_copy_constructible>
- : std::is_trivially_copy_constructible {};
+struct is_trivially_copy_constructible> : std::is_trivially_copy_constructible {};
#endif
}// namespace detail
}// namespace tl
#endif
-#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
- tl::detail::is_trivially_copy_constructible::value
-#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
- std::is_trivially_copy_assignable::value
-#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \
- std::is_trivially_destructible::value
+#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) tl::detail::is_trivially_copy_constructible::value
+#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable::value
+#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value
#else
-#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
- std::is_trivially_copy_constructible::value
-#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
- std::is_trivially_copy_assignable::value
-#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \
- std::is_trivially_destructible::value
+#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::is_trivially_copy_constructible::value
+#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable::value
+#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value
#endif
#if __cplusplus > 201103L
@@ -102,8 +86,7 @@ struct is_trivially_copy_constructible>
#endif
// constexpr implies const in C++11, not C++14
-#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) \
- || defined(TL_OPTIONAL_GCC49))
+#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || defined(TL_OPTIONAL_GCC49))
#define TL_OPTIONAL_11_CONSTEXPR
#else
#define TL_OPTIONAL_11_CONSTEXPR constexpr
@@ -151,8 +134,7 @@ template
struct conjunction : B {};
template
-struct conjunction
- : std::conditional, B>::type {};
+struct conjunction : std::conditional, B>::type {};
#if defined(_LIBCPP_VERSION) && __cplusplus == 201103L
#define TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND
@@ -166,28 +148,22 @@ template
struct is_pointer_to_non_const_member_func : std::false_type {};
template
-struct is_pointer_to_non_const_member_func
- : std::true_type {};
+struct is_pointer_to_non_const_member_func : std::true_type {};
template
-struct is_pointer_to_non_const_member_func
- : std::true_type {};
+struct is_pointer_to_non_const_member_func : std::true_type {};
template
-struct is_pointer_to_non_const_member_func
- : std::true_type {};
+struct is_pointer_to_non_const_member_func : std::true_type {};
template
-struct is_pointer_to_non_const_member_func
- : std::true_type {};
+struct is_pointer_to_non_const_member_func : std::true_type {};
template
-struct is_pointer_to_non_const_member_func
- : std::true_type {};
+struct is_pointer_to_non_const_member_func : std::true_type {};
template
-struct is_pointer_to_non_const_member_func
- : std::true_type {};
+struct is_pointer_to_non_const_member_func : std::true_type {};
template
struct is_const_or_const_ref : std::false_type {};
@@ -204,25 +180,21 @@ struct is_const_or_const_ref : std::true_type {};
template::value
- && is_const_or_const_ref::value)>,
+ typename
+ = enable_if_t::value && is_const_or_const_ref::value)>,
#endif
typename = enable_if_t>::value>,
- int = 0>
+ int = 0>
constexpr auto
-invoke(Fn &&f, Args &&...args) noexcept(
- noexcept(std::mem_fn(f)(std::forward(args)...)))
+invoke(Fn &&f, Args &&...args) noexcept(noexcept(std::mem_fn(f)(std::forward(args)...)))
-> decltype(std::mem_fn(f)(std::forward(args)...))
{
return std::mem_fn(f)(std::forward(args)...);
}
-template>::value>>
+template>::value>>
constexpr auto
-invoke(Fn &&f, Args &&...args) noexcept(
- noexcept(std::forward(f)(std::forward(args)...)))
+invoke(Fn &&f, Args &&...args) noexcept(noexcept(std::forward(f)(std::forward(args)...)))
-> decltype(std::forward(f)(std::forward(args)...))
{
return std::forward(f)(std::forward(args)...);
@@ -233,12 +205,8 @@ template
struct invoke_result_impl;
template
-struct invoke_result_impl<
- F,
- decltype(detail::invoke(std::declval(), std::declval()...), void()),
- Us...> {
- using type =
- decltype(detail::invoke(std::declval(), std::declval()...));
+struct invoke_result_impl(), std::declval()...), void()), Us...> {
+ using type = decltype(detail::invoke(std::declval(), std::declval()...));
};
template
@@ -270,62 +238,50 @@ tag swap(T (&a)[N], T (&b)[N]);
// becomes std::swap
template
std::false_type can_swap(...) noexcept(false);
-template(), std::declval()))>
-std::true_type can_swap(int) noexcept(noexcept(swap(std::declval(),
- std::declval())));
+template(), std::declval()))>
+std::true_type can_swap(int) noexcept(noexcept(swap(std::declval(), std::declval())));
template
std::false_type uses_std(...);
template
-std::is_same(), std::declval())), tag>
-uses_std(int);
+std::is_same(), std::declval())), tag> uses_std(int);
template
struct is_std_swap_noexcept
: std::integral_constant::value
- && std::is_nothrow_move_assignable::value> {
-};
+ && std::is_nothrow_move_assignable::value> {};
template
struct is_std_swap_noexcept : is_std_swap_noexcept {};
template
-struct is_adl_swap_noexcept
- : std::integral_constant(0))> {};
+struct is_adl_swap_noexcept : std::integral_constant(0))> {};
}// namespace swap_adl_tests
template
struct is_swappable
- : std::integral_constant<
- bool,
- decltype(detail::swap_adl_tests::can_swap(0))::value
- && (!decltype(detail::swap_adl_tests::uses_std(0))::value
- || (std::is_move_assignable::value
- && std::is_move_constructible::value))> {};
+ : std::integral_constant(0))::value
+ && (!decltype(detail::swap_adl_tests::uses_std(0))::value
+ || (std::is_move_assignable::value && std::is_move_constructible::value))> {
+};
template
struct is_swappable
- : std::integral_constant<
- bool,
- decltype(detail::swap_adl_tests::can_swap(0))::value
- && (!decltype(detail::swap_adl_tests::uses_std(
- 0))::value
- || is_swappable::value)> {};
+ : std::integral_constant(0))::value
+ && (!decltype(detail::swap_adl_tests::uses_std(0))::value
+ || is_swappable::value)> {};
template
struct is_nothrow_swappable
- : std::integral_constant<
- bool,
- is_swappable::value
- && ((decltype(detail::swap_adl_tests::uses_std(0))::value
- && detail::swap_adl_tests::is_std_swap_noexcept::value)
- || (!decltype(detail::swap_adl_tests::uses_std(
- 0))::value
- && detail::swap_adl_tests::is_adl_swap_noexcept::
- value))> {};
+ : std::integral_constant::value
+ && ((decltype(detail::swap_adl_tests::uses_std(0))::value
+ && detail::swap_adl_tests::is_std_swap_noexcept::value)
+ || (!decltype(detail::swap_adl_tests::uses_std(0))::value
+ && detail::swap_adl_tests::is_adl_swap_noexcept::value))> {};
#endif
#endif
@@ -358,8 +314,7 @@ template
struct returns_void_impl;
template
-struct returns_void_impl>, U...>
- : std::is_void> {};
+struct returns_void_impl>, U...> : std::is_void> {};
template
using returns_void = returns_void_impl;
@@ -370,56 +325,40 @@ template
using disable_if_ret_void = enable_if_t::value>;
template
-using enable_forward_value = detail::enable_if_t<
- std::is_constructible::value
- && !std::is_same, in_place_t>::value
- && !std::is_same, detail::decay_t>::value>;
+using enable_forward_value
+ = detail::enable_if_t::value && !std::is_same, in_place_t>::value
+ && !std::is_same, detail::decay_t>::value>;
template
using enable_from_other = detail::enable_if_t<
- std::is_constructible::value
- && !std::is_constructible &>::value
- && !std::is_constructible &&>::value
- && !std::is_constructible &>::value
- && !std::is_constructible &&>::value
- && !std::is_convertible &, T>::value
- && !std::is_convertible &&, T>::value
- && !std::is_convertible &, T>::value
+ std::is_constructible::value && !std::is_constructible &>::value
+ && !std::is_constructible &&>::value && !std::is_constructible &>::value
+ && !std::is_constructible &&>::value && !std::is_convertible &, T>::value
+ && !std::is_convertible &&, T>::value && !std::is_convertible &, T>::value
&& !std::is_convertible &&, T>::value>;
template
-using enable_assign_forward = detail::enable_if_t<
- !std::is_same, detail::decay_t>::value
- && !detail::conjunction,
- std::is_same>>::value
- && std::is_constructible::value && std::is_assignable::value>;
+using enable_assign_forward
+ = detail::enable_if_t, detail::decay_t>::value
+ && !detail::conjunction, std::is_same>>::value
+ && std::is_constructible::value && std::is_assignable::value>;
template
using enable_assign_from_other = detail::enable_if_t<
- std::is_constructible::value
- && std::is_assignable::value
- && !std::is_constructible &>::value
- && !std::is_constructible &&>::value
- && !std::is_constructible &>::value
- && !std::is_constructible &&>::value
- && !std::is_convertible &, T>::value
- && !std::is_convertible &&, T>::value
- && !std::is_convertible &, T>::value
- && !std::is_convertible &&, T>::value
- && !std::is_assignable &>::value
- && !std::is_assignable &&>::value
- && !std::is_assignable &>::value
- && !std::is_assignable &&>::value>;
+ std::is_constructible::value && std::is_assignable::value
+ && !std::is_constructible &>::value && !std::is_constructible &&>::value
+ && !std::is_constructible &>::value && !std::is_constructible &&>::value
+ && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value
+ && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value
+ && !std::is_assignable &>::value && !std::is_assignable &&>::value
+ && !std::is_assignable &>::value && !std::is_assignable &&>::value>;
// The storage base manages the actual storage, and correctly propagates
// trivial destruction from T. This case is for when T is not trivially
// destructible.
template::value>
struct optional_storage_base {
- TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept
- : m_dummy(),
- m_has_value(false)
- {}
+ TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {}
template
TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&...u)
@@ -448,10 +387,7 @@ struct optional_storage_base {
// This case is for when T is trivially destructible.
template
struct optional_storage_base {
- TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept
- : m_dummy(),
- m_has_value(false)
- {}
+ TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {}
template
TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&...u)
@@ -533,8 +469,7 @@ struct optional_copy_base : optional_operations_base {
optional_copy_base() = default;
- optional_copy_base(const optional_copy_base &rhs)
- : optional_operations_base()
+ optional_copy_base(const optional_copy_base &rhs) : optional_operations_base()
{
if (rhs.has_value()) {
this->construct(rhs.get());
@@ -543,9 +478,9 @@ struct optional_copy_base : optional_operations_base {
}
}
- optional_copy_base(optional_copy_base &&rhs) = default;
+ optional_copy_base(optional_copy_base &&rhs) = default;
optional_copy_base &operator=(const optional_copy_base &rhs) = default;
- optional_copy_base &operator=(optional_copy_base &&rhs) = default;
+ optional_copy_base &operator=(optional_copy_base &&rhs) = default;
};
// This class manages conditionally having a trivial move constructor
@@ -566,11 +501,10 @@ template
struct optional_move_base : optional_copy_base {
using optional_copy_base::optional_copy_base;
- optional_move_base() = default;
+ optional_move_base() = default;
optional_move_base(const optional_move_base &rhs) = default;
- optional_move_base(optional_move_base &&rhs) noexcept(
- std::is_nothrow_move_constructible::value)
+ optional_move_base(optional_move_base &&rhs) noexcept(std::is_nothrow_move_constructible::value)
{
if (rhs.has_value()) {
this->construct(std::move(rhs.get()));
@@ -580,14 +514,13 @@ struct optional_move_base : optional_copy_base {
}
optional_move_base &operator=(const optional_move_base &rhs) = default;
- optional_move_base &operator=(optional_move_base &&rhs) = default;
+ optional_move_base &operator=(optional_move_base &&rhs) = default;
};
// This class manages conditionally having a trivial copy assignment operator
template
+ bool = TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) && TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)
+ && TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T)>
struct optional_copy_assign_base : optional_move_base {
using optional_move_base::optional_move_base;
};
@@ -596,7 +529,7 @@ template
struct optional_copy_assign_base : optional_move_base {
using optional_move_base::optional_move_base;
- optional_copy_assign_base() = default;
+ optional_copy_assign_base() = default;
optional_copy_assign_base(const optional_copy_assign_base &rhs) = default;
optional_copy_assign_base(optional_copy_assign_base &&rhs) = default;
@@ -607,8 +540,7 @@ struct optional_copy_assign_base : optional_move_base {
return *this;
}
- optional_copy_assign_base &
- operator=(optional_copy_assign_base &&rhs) = default;
+ optional_copy_assign_base &operator=(optional_copy_assign_base &&rhs) = default;
};
// This class manages conditionally having a trivial move assignment operator
@@ -618,9 +550,8 @@ struct optional_copy_assign_base : optional_move_base {
// move assignable
#ifndef TL_OPTIONAL_GCC49
template::value
- && std::is_trivially_move_constructible::value
- && std::is_trivially_move_assignable::value>
+ bool = std::is_trivially_destructible::value && std::is_trivially_move_constructible::value
+ && std::is_trivially_move_assignable::value>
struct optional_move_assign_base : optional_copy_assign_base {
using optional_copy_assign_base::optional_copy_assign_base;
};
@@ -633,18 +564,15 @@ template
struct optional_move_assign_base : optional_copy_assign_base