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>)
598 requires(not std::same_as<self_type, TO> and std::assignable_from<std::add_lvalue_reference_t<T>, TO>)
601 m_value = std::forward<TO>(
rhs);
620 m_null_flag =
rhs.null_flag();
639 template <
class TO, mpl::
boolean_like BO>
648 m_null_flag =
rhs.null_flag();
665 m_value = std::move(
rhs).get();
666 m_null_flag = std::move(
rhs).null_flag();
685 template <
class TO, mpl::
boolean_like BO>
693 m_value = std::move(
rhs).get();
694 m_null_flag = std::move(
rhs).null_flag();
706 constexpr explicit operator bool() const noexcept;
716 [[nodiscard]] constexpr
bool has_value() const noexcept;
901 void throw_if_null() const;
906 template <class TO,
mpl::boolean_like BO>
921 template <class T, class B>
935 template <class T, class B>
950 template <class T,
mpl::boolean_like B>
951 constexpr
std::strong_ordering operator<=>(const
nullable<T, B>& lhs,
nullval_t dummy) noexcept;
966 template <class T, class B, class U>
968 constexpr
bool operator==(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
984 template <class T, class B, class U>
986 constexpr
std::compare_three_way_result_t<T, U>
987 operator<=>(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
1004 template <class T, class B, class U, class UB>
1005 requires(
mpl::weakly_equality_comparable_with<T, U>)
1006 constexpr
bool operator==(const
nullable<T, B>& lhs, const
nullable<U, UB>&
rhs) noexcept;
1024 template <class T, class B,
std::three_way_comparable_with<T> U, class UB>
1025 constexpr
std::compare_three_way_result_t<T, U>
1040 template <class T,
mpl::boolean_like B =
bool>
1073 template <
class... T>
1080 using base_type::base_type;
1114 constexpr explicit operator
bool() const;
1131 template <
class... T>
1146 template <
class T, mpl::
boolean_like TB,
class U, mpl::
boolean_like UB,
template <
class>
class TQual,
template <
class>
class UQual>
1147 requires std::common_reference_with<T, U> && std::common_reference_with<TB, UB>
1151 nullable<std::common_reference_t<TQual<T>, UQual<U>>, std::common_reference_t<TQual<TB>, UQual<UB>>>;
1161 template <
class T, mpl::
boolean_like B>
1167 template <
class T, mpl::
boolean_like B>
1173 template <
class T, mpl::
boolean_like B>
1179 template <
class T, mpl::
boolean_like B>
1185 template <
class T, mpl::
boolean_like B>
1188 if constexpr (std::is_reference_v<B>)
1198 template <
class T, mpl::
boolean_like B>
1201 if constexpr (std::is_reference_v<B>)
1211 template <
class T, mpl::
boolean_like B>
1217 template <
class T, mpl::
boolean_like B>
1223 template <
class T, mpl::
boolean_like B>
1226 if constexpr (std::is_reference_v<T>)
1236 template <
class T, mpl::
boolean_like B>
1239 if constexpr (std::is_reference_v<T>)
1249 template <
class T, mpl::
boolean_like B>
1256 template <
class T, mpl::
boolean_like B>
1263 template <
class T, mpl::
boolean_like B>
1267 return std::move(*this).get();
1270 template <
class T, mpl::
boolean_like B>
1274 return std::move(*this).get();
1277 template <
class T, mpl::
boolean_like B>
1281 return *
this ?
get() :
value_type(std::forward<U>(default_value));
1284 template <
class T, mpl::
boolean_like B>
1288 return *
this ?
get() :
value_type(std::forward<U>(default_value));
1291 template <
class T, mpl::
boolean_like B>
1295 swap(m_value, other.m_value);
1296 swap(m_null_flag, other.m_null_flag);
1299 template <
class T, mpl::
boolean_like B>
1302 m_null_flag =
false;
1305 template <
class T, mpl::
boolean_like B>
1306 void nullable<T, B>::throw_if_null()
const
1314 template <
class T,
class B>
1320 template <
class T,
class B>
1326 template <
class T,
class B>
1329 return lhs <=>
false;
1332 template <
class T,
class B,
class U>
1336 return lhs && (lhs.
get() == rhs);
1339 template <
class T,
class B,
class U>
1343 return lhs ? lhs.
get() <=> rhs : std::strong_ordering::less;
1346 template <
class T,
class B,
class U,
class UB>
1347 requires(mpl::weakly_equality_comparable_with<T, U>)
1350 return rhs ? lhs == rhs.
get() : !lhs;
1353 template <
class T,
class B, std::three_way_comparable_with<T> U,
class UB>
1354 constexpr std::compare_three_way_result_t<T, U>
1357 return (lhs && rhs) ? lhs.
get() <=> rhs.
get() : bool(lhs) <=> bool(rhs);
1360 template <
class T, mpl::
boolean_like B>
1363 return nullable<T, B>(std::forward<T>(value), std::forward<B>(flag));
1366 template <std::ranges::range R,
typename T>
1367 requires(nullable_of<std::ranges::range_value_t<R>, T>)
1370 for (
auto nullable_value : range)
1372 if (!nullable_value.has_value())
1374 nullable_value.get() = default_value;
1383 template <
class... T>
1387 base_type::operator=(rhs);
1391 template <
class... T>
1395 base_type::operator=(std::move(rhs));
1399 template <
class... T>
1406 template <
class... T>
1413 return v.has_value();
1415#if SPARROW_GCC_11_2_WORKAROUND
1424#if defined(__cpp_lib_format)
1426template <
typename T, sparrow::mpl::
boolean_like B>
1427struct std::formatter<
sparrow::nullable<T, B>>
1429 constexpr auto parse(format_parse_context& ctx)
1431 auto pos = ctx.begin();
1432 while (pos != ctx.end() && *pos !=
'}')
1434 m_format_string.push_back(*pos);
1437 m_format_string.push_back(
'}');
1441 auto format(
const sparrow::nullable<T, B>& n, std::format_context& ctx)
const
1445 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(n.
get()));
1449 return std::format_to(ctx.out(),
"{}",
"null");
1453 std::string m_format_string =
"{:";
1458 template <
typename T, mpl::
boolean_like B>
1459 std::ostream&
operator<<(std::ostream& os,
const nullable<T, B>& value)
1461 os << std::format(
"{}", value);
1466template <
class... T>
1467struct std::formatter<sparrow::nullable_variant<T...>>
1469 constexpr auto parse(format_parse_context& ctx)
1471 auto pos = ctx.begin();
1472 while (pos != ctx.end() && *pos !=
'}')
1474 m_format_string.push_back(*pos);
1477 m_format_string.push_back(
'}');
1481 auto format(
const sparrow::nullable_variant<T...>& variant, std::format_context& ctx)
const
1486 [&](
const auto& value)
1488 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(value));
1495 return std::format_to(ctx.out(),
"{}",
"null");
1499 std::string m_format_string =
"{:";
1504 template <
class... T>
1505 std::ostream&
operator<<(std::ostream& os,
const nullable_variant<T...>& value)
1507 os << std::format(
"{}", value);
1513struct std::formatter<sparrow::nullval_t>
1515 constexpr auto parse(format_parse_context& ctx)
1520 auto format(
const sparrow::nullval_t&, std::format_context& ctx)
const
1522 return std::format_to(ctx.out(),
"nullval");
1532 constexpr std::string_view nullval_str =
"nullval";
1538#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.
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
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::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.
SPARROW_API void swap(ArrowArray &lhs, ArrowArray &rhs)
Swaps the contents of the two ArrowArray objects.
constexpr nullval_t nullval(0)
constexpr bool is_nullable_variant_v
constexpr bool is_nullable_v
std::ostream & operator<<(std::ostream &os, const nullval_t &)
constexpr nullable< T, B > make_nullable(T &&value, B &&flag=true)
Creates a nullable object with deduced types.
#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