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 &&>)
313 explicit(not std::convertible_to<U&&, T>
314 )
constexpr nullable(U&&
value)
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>
422 m_null_flag =
rhs.null_flag();
428 m_value = std::move(
rhs).
get();
429 m_null_flag = std::move(
rhs).null_flag();
433 template <
class TO, mpl::
boolean_like BO>
441 m_value = std::move(
rhs).get();
442 m_null_flag = std::move(
rhs).null_flag();
446 constexpr explicit operator bool() const noexcept;
447 [[nodiscard]] constexpr
bool has_value() const noexcept;
475 void throw_if_null() const;
480 template <class TO,
mpl::boolean_like BO>
484 template <class T, class B>
487 template <class T, class B>
490 template <class T,
mpl::boolean_like B>
493 template <class T, class B, class U>
494 constexpr
bool operator==(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
496 template <class T, class B, class U>
498 constexpr
std::compare_three_way_result_t<T, U>
499 operator<=>(const
nullable<T, B>& lhs, const U&
rhs) noexcept;
501 template <class T, class B, class U, class UB>
504 template <class T, class B,
std::three_way_comparable_with<T> U, class UB>
505 constexpr
std::compare_three_way_result_t<T, U>
510 template <class T,
mpl::boolean_like B =
bool>
518 template <class... T>
525 using base_type::base_type;
533 constexpr
explicit operator
bool() const;
544 template <
class T, mpl::
boolean_like TB,
class U, mpl::
boolean_like UB,
template <
class>
class TQual,
template <
class>
class UQual>
548 nullable<std::common_reference_t<TQual<T>, UQual<U>>, std::common_reference_t<TQual<TB>, UQual<UB>>>;
558 template <
class T, mpl::
boolean_like B>
564 template <
class T, mpl::
boolean_like B>
570 template <
class T, mpl::
boolean_like B>
576 template <
class T, mpl::
boolean_like B>
582 template <
class T, mpl::
boolean_like B>
585 if constexpr (std::is_reference_v<B>)
595 template <
class T, mpl::
boolean_like B>
598 if constexpr (std::is_reference_v<B>)
608 template <
class T, mpl::
boolean_like B>
614 template <
class T, mpl::
boolean_like B>
620 template <
class T, mpl::
boolean_like B>
623 if constexpr (std::is_reference_v<T>)
633 template <
class T, mpl::
boolean_like B>
636 if constexpr (std::is_reference_v<T>)
646 template <
class T, mpl::
boolean_like B>
653 template <
class T, mpl::
boolean_like B>
660 template <
class T, mpl::
boolean_like B>
664 return std::move(*this).get();
667 template <
class T, mpl::
boolean_like B>
671 return std::move(*this).get();
674 template <
class T, mpl::
boolean_like B>
678 return *
this ?
get() :
value_type(std::forward<U>(default_value));
681 template <
class T, mpl::
boolean_like B>
685 return *
this ?
get() :
value_type(std::forward<U>(default_value));
688 template <
class T, mpl::
boolean_like B>
692 swap(m_value, other.m_value);
693 swap(m_null_flag, other.m_null_flag);
696 template <
class T, mpl::
boolean_like B>
702 template <
class T, mpl::
boolean_like B>
703 void nullable<T, B>::throw_if_null()
const
711 template <
class T,
class B>
717 template <
class T,
class B>
723 template <
class T,
class B>
726 return lhs <=>
false;
729 template <
class T,
class B,
class U>
732 return lhs && (lhs.get() == rhs);
735 template <
class T,
class B,
class U>
739 return lhs ? lhs.
get() <=> rhs : std::strong_ordering::less;
742 template <
class T,
class B,
class U,
class UB>
745 return rhs ? lhs == rhs.
get() : !lhs;
748 template <
class T,
class B, std::three_way_comparable_with<T> U,
class UB>
749 constexpr std::compare_three_way_result_t<T, U>
752 return (lhs && rhs) ? lhs.
get() <=> rhs.get() : bool(lhs) <=> bool(rhs);
755 template <
class T, mpl::
boolean_like B>
758 return nullable<T, B>(std::forward<T>(value), std::forward<B>(flag));
765 template <
class... T>
769 base_type::operator=(rhs);
773 template <
class... T>
777 base_type::operator=(std::move(rhs));
781 template <
class... T>
788 template <
class... T>
795 return v.has_value();
802#if defined(__cpp_lib_format)
804template <
typename T, sparrow::mpl::
boolean_like B>
805struct std::formatter<
sparrow::nullable<T, B>>
807 constexpr auto parse(format_parse_context& ctx)
809 auto pos = ctx.begin();
810 while (pos != ctx.end() && *pos !=
'}')
812 m_format_string.push_back(*pos);
815 m_format_string.push_back(
'}');
819 auto format(
const sparrow::nullable<T, B>& n, std::format_context& ctx)
const
823 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(n.
get()));
827 return std::format_to(ctx.out(),
"{}",
"null");
831 std::string m_format_string =
"{:";
834template <
typename T, sparrow::mpl::
boolean_like B>
835std::ostream&
operator<<(std::ostream& os,
const sparrow::nullable<T, B>& value)
837 os << std::format(
"{}", value);
842struct std::formatter<sparrow::nullable_variant<T...>>
844 constexpr auto parse(format_parse_context& ctx)
846 auto pos = ctx.begin();
847 while (pos != ctx.end() && *pos !=
'}')
849 m_format_string.push_back(*pos);
852 m_format_string.push_back(
'}');
856 auto format(
const sparrow::nullable_variant<T...>& variant, std::format_context& ctx)
const
861 [&](
const auto& value)
863 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(value));
870 return std::format_to(ctx.out(),
"{}",
"null");
874 std::string m_format_string =
"{:";
878std::ostream&
operator<<(std::ostream& os,
const sparrow::nullable_variant<T...>& value)
880 os << std::format(
"{}", value);
885struct std::formatter<sparrow::nullval_t>
887 constexpr auto parse(format_parse_context& ctx)
892 auto format(
const sparrow::nullval_t&, std::format_context& ctx)
const
894 return std::format_to(ctx.out(),
"nullval");
902 constexpr std::string_view nullval_str =
"nullval";
907#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
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