20#if defined(__cpp_lib_format)
31#if defined(SPARROW_CONSTEXPR)
32# error "SPARROW_CONSTEXPR already defined"
39#if defined(__clang__) && not defined(_LIBCPP_VERSION)
40# define SPARROW_CONSTEXPR
42# define SPARROW_CONSTEXPR constexpr
47 template <
class T, mpl::
boolean_like B>
55 template <
class T, mpl::
boolean_like B>
63 template <
class N,
class T>
66 template <
class N,
class T>
76 template <
class RangeOfNullables>
100 using reference = std::add_lvalue_reference_t<value_type>;
110 using reference = std::add_lvalue_reference_t<value_type>;
128 [[nodiscard]] const
char*
what() const noexcept
override
135 static constexpr const char* message =
"Invalid access to nullable underlying value";
160 template <
class T,
class TArgs,
class U,
class UArgs>
162 and std::constructible_from<U, mpl::add_const_lvalue_reference_t<UArgs>>;
164 template <
class To1,
class From1,
class To2,
class From2>
166 and std::convertible_to<mpl::add_const_lvalue_reference_t<From2>, To2>;
168 template <
class T,
class... Args>
171 template <
class T,
class... Args>
174 template <
class T,
class... Args>
177 template <
class T,
class Arg>
183 template <
class To,
class... Args>
186 template <
class T,
class Arg>
189 template <
class T,
class U>
190 using conditional_ref_t = std::conditional_t<std::is_reference_v<T>,
const std::decay_t<U>&, std::decay_t<U>&&>;
192 template <
class T,
class Targs,
class U,
class UArgs>
194 and std::constructible_from<U, conditional_ref_t<U, UArgs>>;
196 template <
class To1,
class From1,
class To2,
class From2>
198 and std::convertible_to<conditional_ref_t<To2, From2>, To2>;
200 template <
class To1,
class From1,
class To2,
class From2>
202 std::add_lvalue_reference_t<To1>,
204 and std::is_assignable_v<
205 std::add_lvalue_reference_t<To2>,
208 template <
class To1,
class From1,
class To2,
class From2>
210 std::add_lvalue_reference_t<To1>,
212 and std::is_assignable_v<
213 std::add_lvalue_reference_t<To2>,
276 template <
class T, mpl::
boolean_like B =
bool>
295 template <std::default_initializable U = T, std::default_initializable BB = B>
302 template <std::default_initializable U = T, std::default_initializable BB = B>
310 requires(not std::same_as<self_type, std::decay_t<U>> and std::constructible_from<T, U &&>)
311 explicit(not std::convertible_to<U&&, T>)
constexpr nullable(U&&
value)
noexcept(
312 noexcept(T(std::declval<U>()))
321 template <
class TO, mpl::
boolean_like BO>
333 template <
class TO, mpl::
boolean_like BO>
346 template <class TO,
mpl::boolean_like BO>
347 requires(
impl::both_constructible_from_cond_ref<T, TO, B, BO>
348 and not
impl::initializable_from_refs<T,
nullable<TO, BO>>)
358 template <
class TO, mpl::
boolean_like BO>
360 and std::same_as<std::decay_t<T>,
bool>)
401 requires(not std::same_as<self_type, TO> and std::assignable_from<std::add_lvalue_reference_t<T>, TO>)
404 m_value = std::forward<TO>(
rhs);
412 m_null_flag =
rhs.null_flag();
416 template <
class TO, mpl::
boolean_like BO>
425 m_null_flag =
rhs.null_flag();
431 m_value = std::move(
rhs).get();
432 m_null_flag = std::move(
rhs).null_flag();
436 template <
class TO, mpl::
boolean_like BO>
444 m_value = std::move(
rhs).get();
445 m_null_flag = std::move(
rhs).null_flag();
449 constexpr explicit operator bool() const noexcept;
450 [[nodiscard]] constexpr
bool has_value() const noexcept;
478 void throw_if_null() const;
483 template <class TO,
mpl::boolean_like BO>
487 template <class T, class B>
490 template <class T, class B>
493 template <class T,
mpl::boolean_like B>
496 template <class T, class B, class U>
498 constexpr
bool operator==(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
500 template <class T, class B, class U>
502 constexpr
std::compare_three_way_result_t<T, U>
503 operator<=>(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
505 template <class T, class B, class U, class UB>
506 requires(
mpl::weakly_equality_comparable_with<T, U>)
509 template <class T, class B,
std::three_way_comparable_with<T> U, class UB>
510 constexpr
std::compare_three_way_result_t<T, U>
515 template <class T,
mpl::boolean_like B =
bool>
527 template <
class... T>
534 using base_type::base_type;
542 constexpr explicit operator
bool() const;
553 template <
class T, mpl::
boolean_like TB,
class U, mpl::
boolean_like UB,
template <
class>
class TQual,
template <
class>
class UQual>
554 requires std::common_reference_with<T, U> && std::common_reference_with<TB, UB>
558 nullable<std::common_reference_t<TQual<T>, UQual<U>>, std::common_reference_t<TQual<TB>, UQual<UB>>>;
568 template <
class T, mpl::
boolean_like B>
574 template <
class T, mpl::
boolean_like B>
580 template <
class T, mpl::
boolean_like B>
586 template <
class T, mpl::
boolean_like B>
592 template <
class T, mpl::
boolean_like B>
595 if constexpr (std::is_reference_v<B>)
605 template <
class T, mpl::
boolean_like B>
608 if constexpr (std::is_reference_v<B>)
618 template <
class T, mpl::
boolean_like B>
624 template <
class T, mpl::
boolean_like B>
630 template <
class T, mpl::
boolean_like B>
633 if constexpr (std::is_reference_v<T>)
643 template <
class T, mpl::
boolean_like B>
646 if constexpr (std::is_reference_v<T>)
656 template <
class T, mpl::
boolean_like B>
663 template <
class T, mpl::
boolean_like B>
670 template <
class T, mpl::
boolean_like B>
674 return std::move(*this).get();
677 template <
class T, mpl::
boolean_like B>
681 return std::move(*this).get();
684 template <
class T, mpl::
boolean_like B>
688 return *
this ?
get() :
value_type(std::forward<U>(default_value));
691 template <
class T, mpl::
boolean_like B>
695 return *
this ?
get() :
value_type(std::forward<U>(default_value));
698 template <
class T, mpl::
boolean_like B>
702 swap(m_value, other.m_value);
703 swap(m_null_flag, other.m_null_flag);
706 template <
class T, mpl::
boolean_like B>
712 template <
class T, mpl::
boolean_like B>
713 void nullable<T, B>::throw_if_null()
const
721 template <
class T,
class B>
727 template <
class T,
class B>
733 template <
class T,
class B>
736 return lhs <=>
false;
739 template <
class T,
class B,
class U>
743 return lhs && (lhs.
get() == rhs);
746 template <
class T,
class B,
class U>
750 return lhs ? lhs.
get() <=> rhs : std::strong_ordering::less;
753 template <
class T,
class B,
class U,
class UB>
754 requires(mpl::weakly_equality_comparable_with<T, U>)
757 return rhs ? lhs == rhs.
get() : !lhs;
760 template <
class T,
class B, std::three_way_comparable_with<T> U,
class UB>
761 constexpr std::compare_three_way_result_t<T, U>
764 return (lhs && rhs) ? lhs.
get() <=> rhs.
get() : bool(lhs) <=> bool(rhs);
767 template <
class T, mpl::
boolean_like B>
770 return nullable<T, B>(std::forward<T>(value), std::forward<B>(flag));
773 template <std::ranges::range R,
typename T>
774 requires(nullable_of<std::ranges::range_value_t<R>, T>)
777 for (
auto nullable_value : range)
779 if (!nullable_value.has_value())
781 nullable_value.get() = default_value;
790 template <
class... T>
794 base_type::operator=(rhs);
798 template <
class... T>
802 base_type::operator=(std::move(rhs));
806 template <
class... T>
813 template <
class... T>
820 return v.has_value();
822#if SPARROW_GCC_11_2_WORKAROUND
831#if defined(__cpp_lib_format)
833template <
typename T, sparrow::mpl::
boolean_like B>
834struct std::formatter<
sparrow::nullable<T, B>>
836 constexpr auto parse(format_parse_context& ctx)
838 auto pos = ctx.begin();
839 while (pos != ctx.end() && *pos !=
'}')
841 m_format_string.push_back(*pos);
844 m_format_string.push_back(
'}');
848 auto format(
const sparrow::nullable<T, B>& n, std::format_context& ctx)
const
852 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(n.
get()));
856 return std::format_to(ctx.out(),
"{}",
"null");
860 std::string m_format_string =
"{:";
863template <
typename T, sparrow::mpl::
boolean_like B>
864std::ostream&
operator<<(std::ostream& os,
const sparrow::nullable<T, B>& value)
866 os << std::format(
"{}", value);
871struct std::formatter<sparrow::nullable_variant<T...>>
873 constexpr auto parse(format_parse_context& ctx)
875 auto pos = ctx.begin();
876 while (pos != ctx.end() && *pos !=
'}')
878 m_format_string.push_back(*pos);
881 m_format_string.push_back(
'}');
885 auto format(
const sparrow::nullable_variant<T...>& variant, std::format_context& ctx)
const
890 [&](
const auto& value)
892 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(value));
899 return std::format_to(ctx.out(),
"{}",
"null");
903 std::string m_format_string =
"{:";
907std::ostream&
operator<<(std::ostream& os,
const sparrow::nullable_variant<T...>& value)
909 os << std::format(
"{}", value);
914struct std::formatter<sparrow::nullval_t>
916 constexpr auto parse(format_parse_context& ctx)
921 auto format(
const sparrow::nullval_t&, std::format_context& ctx)
const
923 return std::format_to(ctx.out(),
"nullval");
931 constexpr std::string_view nullval_str =
"nullval";
936#undef SPARROW_CONSTEXPR
const char * what() const noexcept override
bad_nullable_access() noexcept=default
constexpr nullable_variant(nullable_variant &&) noexcept=default
constexpr nullable_variant(const nullable_variant &)=default
constexpr bool has_value() const
std::variant< T... > base_type
The nullable class models a value or a reference that can be "null", or missing, like values traditio...
nullable_traits< bool > flag_traits
explicit(not std::convertible_to< U &&, inner_value_type >) const expr nullable(U &&value) noexcept(noexcept(inner_value_type(std::declval< U >())))
constexpr nullable(std::add_lvalue_reference_t< T > value, flag_type &&null_flag)
typename value_traits::const_reference const_reference
constexpr nullable(value_type &&value, flag_type &&null_flag)
constexpr nullable() noexcept
typename flag_traits::reference flag_reference
constexpr nullable(nullval_t) noexcept
typename flag_traits::const_reference flag_const_reference
typename flag_traits::value_type flag_type
nullable< inner_value_type, bool > self_type
constexpr reference value() &
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)
constexpr self_type & operator=(const self_type &rhs) noexcept
constexpr value_type value_or(U &&default_value) const &
constexpr nullable(const self_type &)=default
typename flag_traits::const_rvalue_reference flag_const_rvalue_reference
constexpr self_type & operator=(nullval_t) noexcept
constexpr flag_reference null_flag() &noexcept
typename flag_traits::rvalue_reference flag_rvalue_reference
constexpr self_type & operator=(self_type &&rhs) noexcept
typename value_traits::reference reference
constexpr nullable(std::add_lvalue_reference_t< T > value, std::add_lvalue_reference_t< B > null_flag)
nullable_traits< inner_value_type > value_traits
constexpr nullable(self_type &&) noexcept=default
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
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{})
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_v
constexpr nullable< T, B > make_nullable(T &&value, B &&flag=true)
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)
#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
nullval_t is an empty class used to indicate that a nullable is null.
sparrow:: nullable< std::common_reference_t< TQual< T >, UQual< U > >, std::common_reference_t< TQual< TB >, UQual< UB > > > type