20#if defined(__cpp_lib_format)
30#if defined(SPARROW_CONSTEXPR)
31# error "SPARROW_CONSTEXPR already defined"
38#if defined(__clang__) && not defined(_LIBCPP_VERSION)
39# define SPARROW_CONSTEXPR
41# define SPARROW_CONSTEXPR constexpr
46 template <
class T, mpl::
boolean_like B>
54 template <
class T, mpl::
boolean_like B>
62 template <
class N,
class T>
65 template <
class N,
class T>
75 template <
class RangeOfNullables>
99 using reference = std::add_lvalue_reference_t<value_type>;
109 using reference = std::add_lvalue_reference_t<value_type>;
127 [[nodiscard]] const
char*
what() const noexcept
override
134 static constexpr const char* message =
"Invalid access to nullable underlying value";
159 template <
class T,
class TArgs,
class U,
class UArgs>
161 and std::constructible_from<U, mpl::add_const_lvalue_reference_t<UArgs>>;
163 template <
class To1,
class From1,
class To2,
class From2>
165 and std::convertible_to<mpl::add_const_lvalue_reference_t<From2>, To2>;
167 template <
class T,
class... Args>
170 template <
class T,
class... Args>
173 template <
class T,
class... Args>
176 template <
class T,
class Arg>
182 template <
class To,
class... Args>
185 template <
class T,
class Arg>
188 template <
class T,
class U>
189 using conditional_ref_t = std::conditional_t<std::is_reference_v<T>,
const std::decay_t<U>&, std::decay_t<U>&&>;
191 template <
class T,
class Targs,
class U,
class UArgs>
193 and std::constructible_from<U, conditional_ref_t<U, UArgs>>;
195 template <
class To1,
class From1,
class To2,
class From2>
197 and std::convertible_to<conditional_ref_t<To2, From2>, To2>;
199 template <
class To1,
class From1,
class To2,
class From2>
201 std::add_lvalue_reference_t<To1>,
203 and std::is_assignable_v<
204 std::add_lvalue_reference_t<To2>,
207 template <
class To1,
class From1,
class To2,
class From2>
209 std::add_lvalue_reference_t<To1>,
211 and std::is_assignable_v<
212 std::add_lvalue_reference_t<To2>,
278 template <
class T, mpl::
boolean_like B =
bool>
297 template <std::default_initializable U = T, std::default_initializable BB = B>
304 template <std::default_initializable U = T, std::default_initializable BB = B>
312 requires(not std::same_as<self_type, std::decay_t<U>> and std::constructible_from<T, U &&>)
314 )
noexcept(
noexcept(T(std::declval<U>())))
322 template <
class TO, mpl::
boolean_like BO>
333 template <
class TO, mpl::
boolean_like BO>
345 template <class TO,
mpl::boolean_like BO>
346 requires(
impl::both_constructible_from_cond_ref<T, TO, B, BO>
347 and not
impl::initializable_from_refs<T,
nullable<TO, BO>>)
356 template <
class TO, mpl::
boolean_like BO>
358 and std::same_as<std::decay_t<T>,
bool>)
398 requires(not std::same_as<self_type, TO> and std::assignable_from<std::add_lvalue_reference_t<T>, TO>)
401 m_value = std::forward<TO>(
rhs);
409 m_null_flag =
rhs.null_flag();
413 template <
class TO, mpl::
boolean_like BO>
418 m_null_flag =
rhs.null_flag();
424 m_value = std::move(
rhs).
get();
425 m_null_flag = std::move(
rhs).null_flag();
429 template <
class TO, mpl::
boolean_like BO>
433 m_value = std::move(
rhs).get();
434 m_null_flag = std::move(
rhs).null_flag();
438 constexpr explicit operator bool() const noexcept;
439 [[nodiscard]] constexpr
bool has_value() const noexcept;
467 void throw_if_null() const;
472 template <class TO,
mpl::boolean_like BO>
476 template <class T, class B>
479 template <class T, class B>
482 template <class T,
mpl::boolean_like B>
485 template <class T, class B, class U>
486 constexpr
bool operator==(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
488 template <class T, class B, class U>
490 constexpr
std::compare_three_way_result_t<T, U>
491 operator<=>(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
493 template <class T, class B, class U, class UB>
496 template <class T, class B,
std::three_way_comparable_with<T> U, class UB>
497 constexpr
std::compare_three_way_result_t<T, U>
502 template <class T,
mpl::boolean_like B =
bool>
510 template <class... T>
517 using base_type::base_type;
525 constexpr
explicit operator
bool() const;
536 template <
class T, mpl::
boolean_like TB,
class U, mpl::
boolean_like UB,
template <
class>
class TQual,
template <
class>
class UQual>
540 nullable<std::common_reference_t<TQual<T>, UQual<U>>, std::common_reference_t<TQual<TB>, UQual<UB>>>;
550 template <
class T, mpl::
boolean_like B>
556 template <
class T, mpl::
boolean_like B>
562 template <
class T, mpl::
boolean_like B>
568 template <
class T, mpl::
boolean_like B>
574 template <
class T, mpl::
boolean_like B>
577 if constexpr (std::is_reference_v<B>)
587 template <
class T, mpl::
boolean_like B>
590 if constexpr (std::is_reference_v<B>)
600 template <
class T, mpl::
boolean_like B>
606 template <
class T, mpl::
boolean_like B>
612 template <
class T, mpl::
boolean_like B>
615 if constexpr (std::is_reference_v<T>)
625 template <
class T, mpl::
boolean_like B>
628 if constexpr (std::is_reference_v<T>)
638 template <
class T, mpl::
boolean_like B>
645 template <
class T, mpl::
boolean_like B>
652 template <
class T, mpl::
boolean_like B>
656 return std::move(*this).get();
659 template <
class T, mpl::
boolean_like B>
663 return std::move(*this).get();
666 template <
class T, mpl::
boolean_like B>
670 return *
this ?
get() :
value_type(std::forward<U>(default_value));
673 template <
class T, mpl::
boolean_like B>
677 return *
this ?
get() :
value_type(std::forward<U>(default_value));
680 template <
class T, mpl::
boolean_like B>
684 swap(m_value, other.m_value);
685 swap(m_null_flag, other.m_null_flag);
688 template <
class T, mpl::
boolean_like B>
694 template <
class T, mpl::
boolean_like B>
695 void nullable<T, B>::throw_if_null()
const
703 template <
class T,
class B>
709 template <
class T,
class B>
715 template <
class T,
class B>
718 return lhs <=>
false;
721 template <
class T,
class B,
class U>
724 return lhs && (lhs.get() == rhs);
727 template <
class T,
class B,
class U>
731 return lhs ? lhs.
get() <=> rhs : std::strong_ordering::less;
734 template <
class T,
class B,
class U,
class UB>
737 return rhs ? lhs == rhs.
get() : !lhs;
740 template <
class T,
class B, std::three_way_comparable_with<T> U,
class UB>
741 constexpr std::compare_three_way_result_t<T, U>
744 return (lhs && rhs) ? lhs.
get() <=> rhs.get() : bool(lhs) <=> bool(rhs);
747 template <
class T, mpl::
boolean_like B>
750 return nullable<T, B>(std::forward<T>(value), std::forward<B>(flag));
757 template <
class... T>
761 base_type::operator=(rhs);
765 template <
class... T>
769 base_type::operator=(std::move(rhs));
773 template <
class... T>
780 template <
class... T>
787 return v.has_value();
794#if defined(__cpp_lib_format)
796template <
typename T, sparrow::mpl::
boolean_like B>
797struct std::formatter<
sparrow::nullable<T, B>>
799 constexpr auto parse(format_parse_context& ctx)
801 auto pos = ctx.begin();
802 while (pos != ctx.end() && *pos !=
'}')
804 m_format_string.push_back(*pos);
807 m_format_string.push_back(
'}');
811 auto format(
const sparrow::nullable<T, B>& n, std::format_context& ctx)
const
815 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(n.
get()));
819 return std::format_to(ctx.out(),
"{}",
"null");
823 std::string m_format_string =
"{:";
826template <
typename T, sparrow::mpl::
boolean_like B>
827std::ostream&
operator<<(std::ostream& os,
const sparrow::nullable<T, B>& value)
829 os << std::format(
"{}", value);
834struct std::formatter<sparrow::nullable_variant<T...>>
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_variant<T...>& variant, std::format_context& ctx)
const
853 [&](
const auto& value)
855 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(value));
862 return std::format_to(ctx.out(),
"{}",
"null");
866 std::string m_format_string =
"{:";
870std::ostream&
operator<<(std::ostream& os,
const sparrow::nullable_variant<T...>& value)
872 os << std::format(
"{}", value);
879#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
constexpr self_type & operator=(nullval_t)
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 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=(self_type &&rhs)
constexpr flag_reference null_flag() &noexcept
typename flag_traits::rvalue_reference flag_rvalue_reference
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
constexpr self_type & operator=(const self_type &rhs)
Concepts used to disambiguate the nullable class constructors.
static constexpr bool is_nullable_v
std::conditional_t< std::is_reference_v< T >, const std::decay_t< U > &, std::decay_t< U > && > conditional_ref_t
constexpr bool is_type_instance_of_v
true if T is a concrete type template instanciation of U which is a type template.
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
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
std::ostream & operator<<(std::ostream &stream, T n)
constexpr nullable< T, B > make_nullable(T &&value, B &&flag=true)
#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