21#if defined(__cpp_lib_format)
32#if defined(SPARROW_CONSTEXPR)
33# error "SPARROW_CONSTEXPR already defined"
40#if defined(__clang__) && not defined(_LIBCPP_VERSION)
41# define SPARROW_CONSTEXPR
43# define SPARROW_CONSTEXPR constexpr
48 template <
class T, mpl::
boolean_like B>
56 template <
class T, mpl::
boolean_like B>
64 template <
class N,
class T>
67 template <
class N,
class T>
69 && std::convertible_to<typename N::stored_value_type, T>;
78 template <
class RangeOfNullables>
102 using reference = std::add_lvalue_reference_t<value_type>;
112 using reference = std::add_lvalue_reference_t<value_type>;
139 [[nodiscard]] const
char*
what() const noexcept
override
146 static constexpr const char* message =
"Invalid access to nullable underlying value";
185 template <
class T,
class TArgs,
class U,
class UArgs>
187 and std::constructible_from<U, mpl::add_const_lvalue_reference_t<UArgs>>;
189 template <
class To1,
class From1,
class To2,
class From2>
191 and std::convertible_to<mpl::add_const_lvalue_reference_t<From2>, To2>;
193 template <
class T,
class... Args>
196 template <
class T,
class... Args>
199 template <
class T,
class... Args>
202 template <
class T,
class Arg>
208 template <
class To,
class... Args>
211 template <
class T,
class Arg>
214 template <
class T,
class U>
215 using conditional_ref_t = std::conditional_t<std::is_reference_v<T>,
const std::decay_t<U>&, std::decay_t<U>&&>;
217 template <
class T,
class Targs,
class U,
class UArgs>
219 and std::constructible_from<U, conditional_ref_t<U, UArgs>>;
221 template <
class To1,
class From1,
class To2,
class From2>
223 and std::convertible_to<conditional_ref_t<To2, From2>, To2>;
225 template <
class To1,
class From1,
class To2,
class From2>
227 std::add_lvalue_reference_t<To1>,
229 and std::is_assignable_v<
230 std::add_lvalue_reference_t<To2>,
233 template <
class To1,
class From1,
class To2,
class From2>
235 std::add_lvalue_reference_t<To1>,
237 and std::is_assignable_v<
238 std::add_lvalue_reference_t<To2>,
288 template <
class T, mpl::
boolean_like B =
bool>
319 template <std::default_initializable U = T, std::default_initializable BB = B>
337 template <std::default_initializable U = T, std::default_initializable BB = B>
357 requires(not std::same_as<self_type, std::decay_t<U>> and std::constructible_from<T, U &&>)
358 explicit(not std::convertible_to<U&&, T>)
constexpr nullable(U&&
value)
noexcept(
359 noexcept(T(std::declval<U>()))
392 template <
class TO, mpl::
boolean_like BO>
424 template <
class TO, mpl::
boolean_like BO>
461 template <
class TO, mpl::
boolean_like BO>
494 template <
class TO, mpl::
boolean_like BO>
496 and std::same_as<std::decay_t<T>,
bool>)
577 template <
class U,
class V>
578 requires(std::same_as<std::remove_cvref_t<U>, T> && std::same_as<std::remove_cvref_t<V>, B>
579 && std::is_const_v<std::remove_reference_t<U>>
580 && not std::is_const_v<std::remove_reference_t<V>> && not std::is_reference_v<T>
581 && not std::is_reference_v<B> && std::is_lvalue_reference_v<U &&>
582 && std::is_lvalue_reference_v<V &&>)
602 template <
class U,
class V>
603 requires(std::same_as<std::remove_cvref_t<U>, T> && std::same_as<std::remove_cvref_t<V>, B>
604 && not std::is_reference_v<T> && not std::is_reference_v<B>
605 && (std::is_const_v<std::remove_reference_t<U>>
606 || std::is_const_v<std::remove_reference_t<V>>) )
643 requires(not std::same_as<self_type, TO> and std::assignable_from<std::add_lvalue_reference_t<T>, TO>)
646 m_value = std::forward<TO>(
rhs);
665 m_null_flag =
rhs.null_flag();
684 template <
class TO, mpl::
boolean_like BO>
693 m_null_flag =
rhs.null_flag();
710 m_value = std::move(
rhs).get();
711 m_null_flag = std::move(
rhs).null_flag();
730 template <
class TO, mpl::
boolean_like BO>
738 m_value = std::move(
rhs).get();
739 m_null_flag = std::move(
rhs).null_flag();
751 constexpr explicit operator bool() const noexcept;
761 [[nodiscard]] constexpr
bool has_value() const noexcept;
946 void throw_if_null() const;
951 template <class TO,
mpl::boolean_like BO>
966 template <class T, class B>
980 template <class T, class B>
995 template <class T,
mpl::boolean_like B>
996 constexpr
std::strong_ordering operator<=>(const
nullable<T, B>& lhs,
nullval_t dummy) noexcept;
1011 template <class T, class B, class U>
1013 constexpr
bool operator==(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
1029 template <class T, class B, class U>
1031 constexpr
std::compare_three_way_result_t<T, U>
1032 operator<=>(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
1049 template <class T, class B, class U, class UB>
1050 requires(
mpl::weakly_equality_comparable_with<T, U>)
1051 constexpr
bool operator==(const
nullable<T, B>& lhs, const
nullable<U, UB>&
rhs) noexcept;
1069 template <class T, class B,
std::three_way_comparable_with<T> U, class UB>
1070 constexpr
std::compare_three_way_result_t<T, U>
1085 template <class T,
mpl::boolean_like B =
bool>
1118 template <
class... T>
1125 using base_type::base_type;
1159 constexpr explicit operator
bool() const;
1176 template <
class... T>
1191 template <
class T, mpl::
boolean_like TB,
class U, mpl::
boolean_like UB,
template <
class>
class TQual,
template <
class>
class UQual>
1192 requires std::common_reference_with<T, U> && std::common_reference_with<TB, UB>
1196 nullable<std::common_reference_t<TQual<T>, UQual<U>>, std::common_reference_t<TQual<TB>, UQual<UB>>>;
1206 template <
class T, mpl::
boolean_like B>
1212 template <
class T, mpl::
boolean_like B>
1218 template <
class T, mpl::
boolean_like B>
1224 template <
class T, mpl::
boolean_like B>
1230 template <
class T, mpl::
boolean_like B>
1233 if constexpr (std::is_reference_v<B>)
1243 template <
class T, mpl::
boolean_like B>
1246 if constexpr (std::is_reference_v<B>)
1256 template <
class T, mpl::
boolean_like B>
1262 template <
class T, mpl::
boolean_like B>
1268 template <
class T, mpl::
boolean_like B>
1271 if constexpr (std::is_reference_v<T>)
1281 template <
class T, mpl::
boolean_like B>
1284 if constexpr (std::is_reference_v<T>)
1294 template <
class T, mpl::
boolean_like B>
1301 template <
class T, mpl::
boolean_like B>
1308 template <
class T, mpl::
boolean_like B>
1312 return std::move(*this).get();
1315 template <
class T, mpl::
boolean_like B>
1319 return std::move(*this).get();
1322 template <
class T, mpl::
boolean_like B>
1326 return *
this ?
get() :
value_type(std::forward<U>(default_value));
1329 template <
class T, mpl::
boolean_like B>
1333 return *
this ?
get() :
value_type(std::forward<U>(default_value));
1336 template <
class T, mpl::
boolean_like B>
1340 swap(m_value, other.m_value);
1341 swap(m_null_flag, other.m_null_flag);
1344 template <
class T, mpl::
boolean_like B>
1347 m_null_flag =
false;
1350 template <
class T, mpl::
boolean_like B>
1351 void nullable<T, B>::throw_if_null()
const
1359 template <
class T,
class B>
1365 template <
class T,
class B>
1371 template <
class T,
class B>
1374 return lhs <=>
false;
1377 template <
class T,
class B,
class U>
1381 return lhs && (lhs.
get() == rhs);
1384 template <
class T,
class B,
class U>
1388 return lhs ? lhs.
get() <=> rhs : std::strong_ordering::less;
1391 template <
class T,
class B,
class U,
class UB>
1392 requires(mpl::weakly_equality_comparable_with<T, U>)
1395 return rhs ? lhs == rhs.
get() : !lhs;
1398 template <
class T,
class B, std::three_way_comparable_with<T> U,
class UB>
1399 constexpr std::compare_three_way_result_t<T, U>
1402 return (lhs && rhs) ? lhs.
get() <=> rhs.
get() : bool(lhs) <=> bool(rhs);
1405 template <
class T, mpl::
boolean_like B>
1408 return nullable<T, B>(std::forward<T>(value), std::forward<B>(flag));
1411 template <std::ranges::range R,
typename T>
1412 requires(nullable_of<std::ranges::range_value_t<R>, T>)
1415 for (
auto nullable_value : range)
1417 if (!nullable_value.has_value())
1419 nullable_value.get() = default_value;
1428 template <
class... T>
1432 base_type::operator=(rhs);
1436 template <
class... T>
1440 base_type::operator=(std::move(rhs));
1444 template <
class... T>
1451 template <
class... T>
1458 return v.has_value();
1460#if SPARROW_GCC_11_2_WORKAROUND
1469#if defined(__cpp_lib_format)
1471template <
typename T, sparrow::mpl::
boolean_like B>
1472struct std::formatter<
sparrow::nullable<T, B>>
1474 constexpr auto parse(format_parse_context& ctx)
1476 auto pos = ctx.begin();
1477 while (pos != ctx.end() && *pos !=
'}')
1479 m_format_string.push_back(*pos);
1482 m_format_string.push_back(
'}');
1486 auto format(
const sparrow::nullable<T, B>& n, std::format_context& ctx)
const
1490 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(n.
get()));
1494 return std::format_to(ctx.out(),
"{}",
"null");
1498 std::string m_format_string =
"{:";
1503 template <
typename T, mpl::
boolean_like B>
1504 std::ostream&
operator<<(std::ostream& os,
const nullable<T, B>& value)
1506 os << std::format(
"{}", value);
1511template <
class... T>
1512struct std::formatter<sparrow::nullable_variant<T...>>
1514 constexpr auto parse(format_parse_context& ctx)
1516 auto pos = ctx.begin();
1517 while (pos != ctx.end() && *pos !=
'}')
1519 m_format_string.push_back(*pos);
1522 m_format_string.push_back(
'}');
1526 auto format(
const sparrow::nullable_variant<T...>& variant, std::format_context& ctx)
const
1531 [&](
const auto& value)
1533 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(value));
1540 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(
"null"));
1544 std::string m_format_string =
"{:";
1549 template <
class... T>
1550 std::ostream&
operator<<(std::ostream& os,
const nullable_variant<T...>& value)
1552 os << std::format(
"{}", value);
1558struct std::formatter<sparrow::nullval_t>
1560 constexpr auto parse(format_parse_context& ctx)
1565 auto format(
const sparrow::nullval_t&, std::format_context& ctx)
const
1567 return std::format_to(ctx.out(),
"nullval");
1577 constexpr std::string_view nullval_str =
"nullval";
1583#undef SPARROW_CONSTEXPR
Exception thrown when accessing a null nullable value.
const char * what() const noexcept override
Gets the descriptive error message.
bad_nullable_access() noexcept=default
Variant of nullable types with has_value() convenience method.
constexpr nullable_variant(nullable_variant &&) noexcept=default
constexpr nullable_variant(const nullable_variant &)=default
constexpr bool has_value() const
Checks whether the active alternative contains a valid value.
std::variant< T... > base_type
constexpr nullable(const self_type &rhs)=default
Default copy constructor.
nullable_traits< B > flag_traits
constexpr nullable(std::add_lvalue_reference_t< T > value, flag_type &&null_flag)
Constructor from value reference and moved flag.
constexpr nullable(U &value, V &null_flag)
Constructor from const value and non-const flag reference (for non-reference types only).
typename value_traits::const_reference const_reference
constexpr nullable(value_type &&value, flag_type &&null_flag)
Constructor from value and flag.
constexpr nullable() noexcept
Default constructor creating a null nullable.
typename flag_traits::reference flag_reference
constexpr nullable(nullval_t) noexcept
Constructor from nullval_t creating a null nullable.
typename flag_traits::const_reference flag_const_reference
typename flag_traits::value_type flag_type
constexpr nullable(U &&value, V &&null_flag)
Constructor from two forwarding references (for value types with const qualifiers).
nullable< inner_const_reference, B > self_type
constexpr reference value() &
inner_const_reference stored_value_type
constexpr bool has_value() const noexcept
void swap(self_type &other) noexcept
constexpr reference get() &noexcept
typename value_traits::rvalue_reference rvalue_reference
typename value_traits::value_type value_type
constexpr nullable(value_type &&value, std::add_lvalue_reference_t< B > null_flag)
Constructor from moved value and flag reference.
constexpr self_type & operator=(const self_type &rhs) noexcept
Default copy assignment operator.
constexpr value_type value_or(U &&default_value) const &
constexpr nullable(self_type &&rhs) noexcept=default
Default move constructor.
typename flag_traits::const_rvalue_reference flag_const_rvalue_reference
constexpr self_type & operator=(nullval_t) noexcept
Assignment from nullval_t, setting nullable to null state.
constexpr flag_reference null_flag() &noexcept
typename flag_traits::rvalue_reference flag_rvalue_reference
constexpr self_type & operator=(self_type &&rhs) noexcept
Default move assignment operator.
typename value_traits::reference reference
constexpr nullable(std::add_lvalue_reference_t< T > value, std::add_lvalue_reference_t< B > null_flag)
Constructor from lvalue references (for reference semantics).
nullable_traits< inner_const_reference > value_traits
typename value_traits::const_rvalue_reference const_rvalue_reference
Concepts used to disambiguate the nullable class constructors.
std::ostream & operator<<(std::ostream &stream, primesum::uint128_t n)
The __int128_t type (GCC/Clang) is not well supported by the C++ standard library (in 2016) so we hav...
std::conditional_t< std::is_reference_v< T >, const std::decay_t< U > &, std::decay_t< U > && > conditional_ref_t
typename add_const_lvalue_reference< T >::type add_const_lvalue_reference_t
Convenience alias for add_const_lvalue_reference.
constexpr std::compare_three_way_result_t< typename cloning_ptr< T1 >::pointer, typename cloning_ptr< T2 >::pointer > operator<=>(const cloning_ptr< T1 > &lhs, const cloning_ptr< T2 > &rhs) noexcept
constexpr void zero_null_values(R &range, const T &default_value=T{})
Sets null values in a range to a default value.
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
constexpr nullval_t nullval(0)
constexpr bool is_nullable_variant_v
constexpr bool is_nullable_v
SPARROW_API void swap(ArrowArray &lhs, ArrowArray &rhs) noexcept
Swaps the contents of the two ArrowArray objects.
constexpr nullable< T, B > make_nullable(T &&value, B &&flag=true)
Creates a nullable object with deduced types.
Extensions to the C++ standard library.
#define SPARROW_CONSTEXPR
const_reference const_rvalue_reference
reference rvalue_reference
std::add_lvalue_reference_t< value_type > reference
std::add_lvalue_reference_t< std::add_const_t< value_type > > const_reference
const value_type && const_rvalue_reference
std::add_lvalue_reference_t< value_type > reference
value_type && rvalue_reference
std::add_lvalue_reference_t< std::add_const_t< value_type > > const_reference
Sentinel type to indicate a nullable value is null.
constexpr nullval_t(int)
Private constructor to prevent default construction.
sparrow:: nullable< std::common_reference_t< TQual< T >, UQual< U > >, std::common_reference_t< TQual< TB >, UQual< UB > > > type