sparrow ..
Loading...
Searching...
No Matches
nullable.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <algorithm>
18#include <compare>
19#include <concepts>
20#include <exception>
21#if defined(__cpp_lib_format)
22# include <format>
23# include <ostream>
24#endif
25#include <type_traits>
26#include <variant>
27
30
31
32#if defined(SPARROW_CONSTEXPR)
33# error "SPARROW_CONSTEXPR already defined"
34#endif
35
36// clang workaround: clang instantiates the constructor in SFINAE context,
37// which is incompatible with the implementation of standard libraries which
38// are not libc++. This leads to wrong compilation errors. Making the constructor
39// not constexpr prevents the compiler from instantiating it.
40#if defined(__clang__) && not defined(_LIBCPP_VERSION)
41# define SPARROW_CONSTEXPR
42#else
43# define SPARROW_CONSTEXPR constexpr
44#endif
45
46namespace sparrow
47{
48 template <class T, mpl::boolean_like B>
49 class nullable;
50
51 template <class T>
52 struct is_nullable : std::false_type
53 {
54 };
55
56 template <class T, mpl::boolean_like B>
57 struct is_nullable<nullable<T, B>> : std::true_type
58 {
59 };
60
61 template <class T>
62 inline constexpr bool is_nullable_v = is_nullable<T>::value;
63
64 template <class N, class T>
65 concept nullable_of = is_nullable_v<N> && std::same_as<typename N::stored_value_type, T>;
66
67 template <class N, class T>
69 && std::convertible_to<typename N::stored_value_type, T>;
70
71 /*
72 * Matches a range of nullables objects.
73 *
74 * A range is considered a range of nullables if it is a range and its value type is a nullable.
75 *
76 * @tparam RangeOfNullables The range to check.
77 */
78 template <class RangeOfNullables>
79 concept range_of_nullables = std::ranges::range<RangeOfNullables>
81
82 /*
83 * Default traits for the nullable class. These traits should be specialized
84 * for proxy classes whose reference and const_reference types are not
85 * defined as usual. For instance:
86 *
87 * @code{.cpp}
88 * struct nullable_traits<string_proxy>
89 * {
90 * using value_type = std::string;
91 * using reference = string_proxy;
92 * using const_reference = std::string_view;
93 * using rvalue_reverence = std::string&&;
94 * using const_rvalue_reference = const std::string&&;
95 * };
96 * @endcode
97 */
98 template <class T>
100 {
101 using value_type = T;
102 using reference = std::add_lvalue_reference_t<value_type>;
103 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
106 };
107
108 template <class T>
110 {
111 using value_type = T;
112 using reference = std::add_lvalue_reference_t<value_type>;
113 using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
116 };
117
124 class bad_nullable_access : public std::exception
125 {
126 public:
127
128 bad_nullable_access() noexcept = default;
129 bad_nullable_access(const bad_nullable_access&) noexcept = default;
130 bad_nullable_access& operator=(const bad_nullable_access&) noexcept = default;
131
139 [[nodiscard]] const char* what() const noexcept override
140 {
141 return message;
142 }
143
144 private:
145
146 static constexpr const char* message = "Invalid access to nullable underlying value";
147 };
148
157 {
161 constexpr explicit nullval_t(int)
162 {
163 }
164 };
165
178 inline constexpr nullval_t nullval(0);
179
180 namespace impl
181 {
185 template <class T, class TArgs, class U, class UArgs>
186 concept both_constructible_from_cref = std::constructible_from<T, mpl::add_const_lvalue_reference_t<TArgs>>
187 and std::constructible_from<U, mpl::add_const_lvalue_reference_t<UArgs>>;
188
189 template <class To1, class From1, class To2, class From2>
190 concept both_convertible_from_cref = std::convertible_to<mpl::add_const_lvalue_reference_t<From1>, To1>
191 and std::convertible_to<mpl::add_const_lvalue_reference_t<From2>, To2>;
192
193 template <class T, class... Args>
194 concept constructible_from_one = (std::constructible_from<T, Args> || ...);
195
196 template <class T, class... Args>
197 concept convertible_from_one = (std::convertible_to<Args, T> || ...);
198
199 template <class T, class... Args>
201
202 template <class T, class Arg>
204
205 // We prefer std::is_assignable_v to std::assignable_from<To, From> because
206 // std::assignable_from requires the existence of an implicit conversion
207 // from From to To
208 template <class To, class... Args>
209 concept assignable_from_one = (std::is_assignable_v<std::add_lvalue_reference<To>, Args> && ...);
210
211 template <class T, class Arg>
213
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>&&>;
216
217 template <class T, class Targs, class U, class UArgs>
218 concept both_constructible_from_cond_ref = std::constructible_from<T, conditional_ref_t<T, Targs>>
219 and std::constructible_from<U, conditional_ref_t<U, UArgs>>;
220
221 template <class To1, class From1, class To2, class From2>
222 concept both_convertible_from_cond_ref = std::convertible_to<conditional_ref_t<To1, From1>, To1>
223 and std::convertible_to<conditional_ref_t<To2, From2>, To2>;
224
225 template <class To1, class From1, class To2, class From2>
226 concept both_assignable_from_cref = std::is_assignable_v<
227 std::add_lvalue_reference_t<To1>,
229 and std::is_assignable_v<
230 std::add_lvalue_reference_t<To2>,
232
233 template <class To1, class From1, class To2, class From2>
234 concept both_assignable_from_cond_ref = std::is_assignable_v<
235 std::add_lvalue_reference_t<To1>,
237 and std::is_assignable_v<
238 std::add_lvalue_reference_t<To2>,
240 }
241
288 template <class T, mpl::boolean_like B = bool>
290 {
291 public:
292
307
319 template <std::default_initializable U = T, std::default_initializable BB = B>
320 constexpr nullable() noexcept
321 : m_value()
322 , m_null_flag(false)
323 {
324 }
325
337 template <std::default_initializable U = T, std::default_initializable BB = B>
338 constexpr nullable(nullval_t) noexcept
339 : m_value()
340 , m_null_flag(false)
341 {
342 }
343
356 template <class U>
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>()))
360 )
361 : m_value(std::forward<U>(value))
362 , m_null_flag(true)
363 {
364 }
365
376 constexpr nullable(const self_type& rhs) = default;
377
392 template <class TO, mpl::boolean_like BO>
397 )
398 : m_value(rhs.get())
399 , m_null_flag(rhs.null_flag())
400 {
401 }
402
403#ifdef __clang__
424 template <class TO, mpl::boolean_like BO>
425 requires(impl::both_constructible_from_cref<T, TO, B, BO> and std::same_as<std::decay_t<T>, bool>)
427 const nullable<TO, BO>& rhs
428 )
429 : m_value(rhs.get())
430 , m_null_flag(rhs.null_flag())
431 {
432 }
433#endif
434
445 constexpr nullable(self_type&& rhs) noexcept = default;
446
461 template <class TO, mpl::boolean_like BO>
466 )
467 : m_value(std::move(rhs).get())
468 , m_null_flag(std::move(rhs).null_flag())
469 {
470 }
471
472#ifdef __clang__
494 template <class TO, mpl::boolean_like BO>
496 and std::same_as<std::decay_t<T>, bool>)
499 )
500 : m_value(std::move(rhs).get())
501 , m_null_flag(std::move(rhs).null_flag())
502 {
503 }
504#endif
505
517 : m_value(std::move(value))
518 , m_null_flag(std::move(null_flag))
519 {
520 }
521
532 constexpr nullable(std::add_lvalue_reference_t<T> value, std::add_lvalue_reference_t<B> null_flag)
533 : m_value(value)
534 , m_null_flag(null_flag)
535 {
536 }
537
547 constexpr nullable(value_type&& value, std::add_lvalue_reference_t<B> null_flag)
548 : m_value(std::move(value))
549 , m_null_flag(null_flag)
550 {
551 }
552
562 constexpr nullable(std::add_lvalue_reference_t<T> value, flag_type&& null_flag)
563 : m_value(value)
564 , m_null_flag(std::move(null_flag))
565 {
566 }
567
578 constexpr self_type& operator=(nullval_t) noexcept
579 {
580 m_null_flag = false;
581 return *this;
582 }
583
597 template <class TO>
598 requires(not std::same_as<self_type, TO> and std::assignable_from<std::add_lvalue_reference_t<T>, TO>)
599 constexpr self_type& operator=(TO&& rhs) noexcept
600 {
601 m_value = std::forward<TO>(rhs);
602 m_null_flag = true;
603 return *this;
604 }
605
617 constexpr self_type& operator=(const self_type& rhs) noexcept
618 {
619 m_value = rhs.get();
620 m_null_flag = rhs.null_flag();
621 return *this;
622 }
623
639 template <class TO, mpl::boolean_like BO>
640 requires(
644 )
645 constexpr self_type& operator=(const nullable<TO, BO>& rhs) noexcept
646 {
647 m_value = rhs.get();
648 m_null_flag = rhs.null_flag();
649 return *this;
650 }
651
663 constexpr self_type& operator=(self_type&& rhs) noexcept
664 {
665 m_value = std::move(rhs).get();
666 m_null_flag = std::move(rhs).null_flag();
667 return *this;
668 }
669
685 template <class TO, mpl::boolean_like BO>
686 requires(
690 )
691 constexpr self_type& operator=(nullable<TO, BO>&& rhs) noexcept
692 {
693 m_value = std::move(rhs).get();
694 m_null_flag = std::move(rhs).null_flag();
695 return *this;
696 }
697
706 constexpr explicit operator bool() const noexcept;
707
716 [[nodiscard]] constexpr bool has_value() const noexcept;
717
726 [[nodiscard]] constexpr flag_reference null_flag() & noexcept;
727
735 [[nodiscard]] constexpr flag_const_reference null_flag() const& noexcept;
736
745 [[nodiscard]] constexpr flag_rvalue_reference null_flag() && noexcept;
746
755 [[nodiscard]] constexpr flag_const_rvalue_reference null_flag() const&& noexcept;
756
766 [[nodiscard]] constexpr reference get() & noexcept;
767
776 [[nodiscard]] constexpr const_reference get() const& noexcept;
777
786 [[nodiscard]] constexpr rvalue_reference get() && noexcept;
787
796 [[nodiscard]] constexpr const_rvalue_reference get() const&& noexcept;
797
808 [[nodiscard]] constexpr reference value() &;
809
820 [[nodiscard]] constexpr const_reference value() const&;
821
833 [[nodiscard]] constexpr rvalue_reference value() &&;
834
845 [[nodiscard]] constexpr const_rvalue_reference value() const&&;
846
858 template <class U>
859 [[nodiscard]] constexpr value_type value_or(U&& default_value) const&;
860
872 template <class U>
873 [[nodiscard]] constexpr value_type value_or(U&& default_value) &&;
874
883 void swap(self_type& other) noexcept;
884
892 void reset() noexcept;
893
894 private:
895
901 void throw_if_null() const;
902
903 T m_value;
904 B m_null_flag;
905
906 template <class TO, mpl::boolean_like BO>
907 friend class nullable;
908 };
909
921 template <class T, class B>
922 constexpr void swap(nullable<T, B>& lhs, nullable<T, B>& rhs) noexcept;
923
935 template <class T, class B>
936 constexpr bool operator==(const nullable<T, B>& lhs, nullval_t) noexcept;
937
950 template <class T, mpl::boolean_like B>
951 constexpr std::strong_ordering operator<=>(const nullable<T, B>& lhs, nullval_t dummy) noexcept;
952
966 template <class T, class B, class U>
967 requires(!is_nullable_v<U> && mpl::weakly_equality_comparable_with<T, U>)
968 constexpr bool operator==(const nullable<T, B>& lhs, const U& rhs) noexcept;
969
984 template <class T, class B, class U>
985 requires(!is_nullable_v<U> && std::three_way_comparable_with<U, T>)
986 constexpr std::compare_three_way_result_t<T, U>
987 operator<=>(const nullable<T, B>& lhs, const U& rhs) noexcept;
988
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;
1007
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>
1026 operator<=>(const nullable<T, B>& lhs, const nullable<U, UB>& rhs) noexcept;
1027
1040 template <class T, mpl::boolean_like B = bool>
1041 constexpr nullable<T, B> make_nullable(T&& value, B&& flag = true);
1042
1056 template <std::ranges::range R, typename T = typename std::ranges::range_value_t<R>::value_type>
1057 requires(nullable_of<std::ranges::range_value_t<R>, T>)
1058 constexpr void zero_null_values(R& range, const T& default_value = T{});
1059
1073 template <class... T>
1074 requires(sizeof...(T) > 0 && (is_nullable_v<T> && ...))
1075 class nullable_variant : public std::variant<T...>
1076 {
1077 public:
1078
1079 using base_type = std::variant<T...>;
1080 using base_type::base_type;
1081
1082 constexpr nullable_variant(const nullable_variant&) = default;
1083 constexpr nullable_variant(nullable_variant&&) noexcept = default;
1084
1094 constexpr nullable_variant& operator=(const nullable_variant&);
1095
1105 constexpr nullable_variant& operator=(nullable_variant&&) noexcept;
1106
1114 constexpr explicit operator bool() const;
1115
1123 constexpr bool has_value() const;
1124 };
1125
1126 template <class T>
1127 struct is_nullable_variant : std::false_type
1128 {
1129 };
1130
1131 template <class... T>
1132 struct is_nullable_variant<nullable_variant<T...>> : std::true_type
1133 {
1134 };
1135
1136 template <class T>
1138}
1139
1140namespace std
1141{
1142 namespace mpl = sparrow::mpl;
1143
1144 // Specialization of basic_common_reference for nullable proxies so
1145 // we can use ranges algorithm on iterators returning nullable
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>
1148 struct basic_common_reference<sparrow::nullable<T, TB>, sparrow::nullable<U, UB>, TQual, UQual>
1149 {
1150 using type = sparrow::
1151 nullable<std::common_reference_t<TQual<T>, UQual<U>>, std::common_reference_t<TQual<TB>, UQual<UB>>>;
1152 };
1153}
1154
1155namespace sparrow
1156{
1157 /***************************
1158 * nullable implementation *
1159 ***************************/
1160
1161 template <class T, mpl::boolean_like B>
1162 constexpr nullable<T, B>::operator bool() const noexcept
1163 {
1164 return m_null_flag;
1165 }
1166
1167 template <class T, mpl::boolean_like B>
1168 constexpr bool nullable<T, B>::has_value() const noexcept
1169 {
1170 return m_null_flag;
1171 }
1172
1173 template <class T, mpl::boolean_like B>
1174 constexpr auto nullable<T, B>::null_flag() & noexcept -> flag_reference
1175 {
1176 return m_null_flag;
1177 }
1178
1179 template <class T, mpl::boolean_like B>
1180 constexpr auto nullable<T, B>::null_flag() const& noexcept -> flag_const_reference
1181 {
1182 return m_null_flag;
1183 }
1184
1185 template <class T, mpl::boolean_like B>
1186 constexpr auto nullable<T, B>::null_flag() && noexcept -> flag_rvalue_reference
1187 {
1188 if constexpr (std::is_reference_v<B>)
1189 {
1190 return m_null_flag;
1191 }
1192 else
1193 {
1194 return flag_rvalue_reference(m_null_flag);
1195 }
1196 }
1197
1198 template <class T, mpl::boolean_like B>
1199 constexpr auto nullable<T, B>::null_flag() const&& noexcept -> flag_const_rvalue_reference
1200 {
1201 if constexpr (std::is_reference_v<B>)
1202 {
1203 return m_null_flag;
1204 }
1205 else
1206 {
1207 return flag_const_rvalue_reference(m_null_flag);
1208 }
1209 }
1210
1211 template <class T, mpl::boolean_like B>
1212 constexpr auto nullable<T, B>::get() & noexcept -> reference
1213 {
1214 return m_value;
1215 }
1216
1217 template <class T, mpl::boolean_like B>
1218 constexpr auto nullable<T, B>::get() const& noexcept -> const_reference
1219 {
1220 return m_value;
1221 }
1222
1223 template <class T, mpl::boolean_like B>
1224 constexpr auto nullable<T, B>::get() && noexcept -> rvalue_reference
1225 {
1226 if constexpr (std::is_reference_v<T>)
1227 {
1228 return m_value;
1229 }
1230 else
1231 {
1232 return rvalue_reference(m_value);
1233 }
1234 }
1235
1236 template <class T, mpl::boolean_like B>
1237 constexpr auto nullable<T, B>::get() const&& noexcept -> const_rvalue_reference
1238 {
1239 if constexpr (std::is_reference_v<T>)
1240 {
1241 return m_value;
1242 }
1243 else
1244 {
1245 return const_rvalue_reference(m_value);
1246 }
1247 }
1248
1249 template <class T, mpl::boolean_like B>
1250 constexpr auto nullable<T, B>::value() & -> reference
1251 {
1252 throw_if_null();
1253 return get();
1254 }
1255
1256 template <class T, mpl::boolean_like B>
1257 constexpr auto nullable<T, B>::value() const& -> const_reference
1258 {
1259 throw_if_null();
1260 return get();
1261 }
1262
1263 template <class T, mpl::boolean_like B>
1265 {
1266 throw_if_null();
1267 return std::move(*this).get();
1268 }
1269
1270 template <class T, mpl::boolean_like B>
1272 {
1273 throw_if_null();
1274 return std::move(*this).get();
1275 }
1276
1277 template <class T, mpl::boolean_like B>
1278 template <class U>
1279 constexpr auto nullable<T, B>::value_or(U&& default_value) const& -> value_type
1280 {
1281 return *this ? get() : value_type(std::forward<U>(default_value));
1282 }
1283
1284 template <class T, mpl::boolean_like B>
1285 template <class U>
1286 constexpr auto nullable<T, B>::value_or(U&& default_value) && -> value_type
1287 {
1288 return *this ? get() : value_type(std::forward<U>(default_value));
1289 }
1290
1291 template <class T, mpl::boolean_like B>
1292 void nullable<T, B>::swap(self_type& other) noexcept
1293 {
1294 using std::swap;
1295 swap(m_value, other.m_value);
1296 swap(m_null_flag, other.m_null_flag);
1297 }
1298
1299 template <class T, mpl::boolean_like B>
1301 {
1302 m_null_flag = false;
1303 }
1304
1305 template <class T, mpl::boolean_like B>
1306 void nullable<T, B>::throw_if_null() const
1307 {
1308 if (!m_null_flag)
1309 {
1310 throw bad_nullable_access{};
1311 }
1312 }
1313
1314 template <class T, class B>
1315 constexpr void swap(nullable<T, B>& lhs, nullable<T, B>& rhs) noexcept
1316 {
1317 lhs.swap(rhs);
1318 }
1319
1320 template <class T, class B>
1321 constexpr bool operator==(const nullable<T, B>& lhs, nullval_t) noexcept
1322 {
1323 return !lhs;
1324 }
1325
1326 template <class T, class B>
1327 constexpr std::strong_ordering operator<=>(const nullable<T, B>& lhs, nullval_t) noexcept
1328 {
1329 return lhs <=> false;
1330 }
1331
1332 template <class T, class B, class U>
1333 requires(!is_nullable_v<U> && mpl::weakly_equality_comparable_with<T, U>)
1334 constexpr bool operator==(const nullable<T, B>& lhs, const U& rhs) noexcept
1335 {
1336 return lhs && (lhs.get() == rhs);
1337 }
1338
1339 template <class T, class B, class U>
1340 requires(!is_nullable_v<U> && std::three_way_comparable_with<U, T>)
1341 constexpr std::compare_three_way_result_t<T, U> operator<=>(const nullable<T, B>& lhs, const U& rhs) noexcept
1342 {
1343 return lhs ? lhs.get() <=> rhs : std::strong_ordering::less;
1344 }
1345
1346 template <class T, class B, class U, class UB>
1347 requires(mpl::weakly_equality_comparable_with<T, U>)
1348 constexpr bool operator==(const nullable<T, B>& lhs, const nullable<U, UB>& rhs) noexcept
1349 {
1350 return rhs ? lhs == rhs.get() : !lhs;
1351 }
1352
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>
1355 operator<=>(const nullable<T, B>& lhs, const nullable<U, UB>& rhs) noexcept
1356 {
1357 return (lhs && rhs) ? lhs.get() <=> rhs.get() : bool(lhs) <=> bool(rhs);
1358 }
1359
1360 template <class T, mpl::boolean_like B>
1361 constexpr nullable<T, B> make_nullable(T&& value, B&& flag)
1362 {
1363 return nullable<T, B>(std::forward<T>(value), std::forward<B>(flag));
1364 }
1365
1366 template <std::ranges::range R, typename T>
1367 requires(nullable_of<std::ranges::range_value_t<R>, T>)
1368 constexpr void zero_null_values(R& range, const T& default_value)
1369 {
1370 for (auto nullable_value : range)
1371 {
1372 if (!nullable_value.has_value())
1373 {
1374 nullable_value.get() = default_value;
1375 }
1376 }
1377 }
1378
1379 /***********************************
1380 * nullable_variant implementation *
1381 ***********************************/
1382
1383 template <class... T>
1384 requires(sizeof...(T) > 0 && (is_nullable_v<T> && ...))
1385 constexpr nullable_variant<T...>& nullable_variant<T...>::operator=(const nullable_variant& rhs)
1386 {
1387 base_type::operator=(rhs);
1388 return *this;
1389 }
1390
1391 template <class... T>
1392 requires(sizeof...(T) > 0 && (is_nullable_v<T> && ...))
1393 constexpr nullable_variant<T...>& nullable_variant<T...>::operator=(nullable_variant&& rhs) noexcept
1394 {
1395 base_type::operator=(std::move(rhs));
1396 return *this;
1397 }
1398
1399 template <class... T>
1400 requires(sizeof...(T) > 0 && (is_nullable_v<T> && ...))
1401 constexpr nullable_variant<T...>::operator bool() const
1402 {
1403 return has_value();
1404 }
1405
1406 template <class... T>
1407 requires(sizeof...(T) > 0 && (is_nullable_v<T> && ...))
1408 constexpr bool nullable_variant<T...>::has_value() const
1409 {
1410 return std::visit(
1411 [](const auto& v)
1412 {
1413 return v.has_value();
1414 },
1415#if SPARROW_GCC_11_2_WORKAROUND
1416 static_cast<const base_type&>(*this)
1417#else
1418 *this
1419#endif
1420 );
1421 }
1422}
1423
1424#if defined(__cpp_lib_format)
1425
1426template <typename T, sparrow::mpl::boolean_like B>
1427struct std::formatter<sparrow::nullable<T, B>>
1428{
1429 constexpr auto parse(format_parse_context& ctx)
1430 {
1431 auto pos = ctx.begin();
1432 while (pos != ctx.end() && *pos != '}')
1433 {
1434 m_format_string.push_back(*pos);
1435 ++pos;
1436 }
1437 m_format_string.push_back('}');
1438 return pos;
1439 }
1440
1441 auto format(const sparrow::nullable<T, B>& n, std::format_context& ctx) const
1442 {
1443 if (n.has_value())
1444 {
1445 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(n.get()));
1446 }
1447 else
1448 {
1449 return std::format_to(ctx.out(), "{}", "null");
1450 }
1451 }
1452
1453 std::string m_format_string = "{:";
1454};
1455
1456namespace sparrow
1457{
1458 template <typename T, mpl::boolean_like B>
1459 std::ostream& operator<<(std::ostream& os, const nullable<T, B>& value)
1460 {
1461 os << std::format("{}", value);
1462 return os;
1463 }
1464}
1465
1466template <class... T>
1467struct std::formatter<sparrow::nullable_variant<T...>>
1468{
1469 constexpr auto parse(format_parse_context& ctx)
1470 {
1471 auto pos = ctx.begin();
1472 while (pos != ctx.end() && *pos != '}')
1473 {
1474 m_format_string.push_back(*pos);
1475 ++pos;
1476 }
1477 m_format_string.push_back('}');
1478 return pos;
1479 }
1480
1481 auto format(const sparrow::nullable_variant<T...>& variant, std::format_context& ctx) const
1482 {
1483 if (variant.has_value())
1484 {
1485 return std::visit(
1486 [&](const auto& value)
1487 {
1488 return std::vformat_to(ctx.out(), m_format_string, std::make_format_args(value));
1489 },
1490 variant
1491 );
1492 }
1493 else
1494 {
1495 return std::format_to(ctx.out(), "{}", "null");
1496 }
1497 }
1498
1499 std::string m_format_string = "{:";
1500};
1501
1502namespace sparrow
1503{
1504 template <class... T>
1505 std::ostream& operator<<(std::ostream& os, const nullable_variant<T...>& value)
1506 {
1507 os << std::format("{}", value);
1508 return os;
1509 }
1510}
1511
1512template <>
1513struct std::formatter<sparrow::nullval_t>
1514{
1515 constexpr auto parse(format_parse_context& ctx)
1516 {
1517 return ctx.begin(); // Simple implementation
1518 }
1519
1520 auto format(const sparrow::nullval_t&, std::format_context& ctx) const
1521 {
1522 return std::format_to(ctx.out(), "nullval");
1523 }
1524};
1525
1526#endif
1527
1528namespace sparrow
1529{
1530 inline std::ostream& operator<<(std::ostream& os, const nullval_t&)
1531 {
1532 constexpr std::string_view nullval_str = "nullval";
1533 os << nullval_str;
1534 return os;
1535 }
1536}
1537
1538#undef SPARROW_CONSTEXPR
Exception thrown when accessing a null nullable value.
Definition nullable.hpp:125
const char * what() const noexcept override
Gets the descriptive error message.
Definition nullable.hpp:139
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.
constexpr nullable(std::add_lvalue_reference_t< T > value, flag_type &&null_flag)
Constructor from value reference and moved flag.
Definition nullable.hpp:562
constexpr nullable(value_type &&value, flag_type &&null_flag)
Constructor from value and flag.
Definition nullable.hpp:516
constexpr nullable() noexcept
Default constructor creating a null nullable.
Definition nullable.hpp:320
friend class nullable
Definition nullable.hpp:907
constexpr nullable(nullval_t) noexcept
Constructor from nullval_t creating a null nullable.
Definition nullable.hpp:338
typename value_traits::rvalue_reference rvalue_reference
Definition nullable.hpp:299
constexpr nullable(value_type &&value, std::add_lvalue_reference_t< B > null_flag)
Constructor from moved value and flag reference.
Definition nullable.hpp:547
constexpr self_type & operator=(const self_type &rhs) noexcept
Default copy assignment operator.
Definition nullable.hpp:617
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
Definition nullable.hpp:306
constexpr self_type & operator=(nullval_t) noexcept
Assignment from nullval_t, setting nullable to null state.
Definition nullable.hpp:578
typename flag_traits::rvalue_reference flag_rvalue_reference
Definition nullable.hpp:305
constexpr self_type & operator=(self_type &&rhs) noexcept
Default move assignment operator.
Definition nullable.hpp:663
constexpr nullable(std::add_lvalue_reference_t< T > value, std::add_lvalue_reference_t< B > null_flag)
Constructor from lvalue references (for reference semantics).
Definition nullable.hpp:532
nullable_traits< inner_const_reference > value_traits
Definition nullable.hpp:295
typename value_traits::const_rvalue_reference const_rvalue_reference
Definition nullable.hpp:300
Concepts used to disambiguate the nullable class constructors.
Definition nullable.hpp:186
std::conditional_t< std::is_reference_v< T >, const std::decay_t< U > &, std::decay_t< U > && > conditional_ref_t
Definition nullable.hpp:215
typename add_const_lvalue_reference< T >::type add_const_lvalue_reference_t
Convenience alias for add_const_lvalue_reference.
Definition mp_utils.hpp:766
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
Definition memory.hpp:474
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
Definition nullable.hpp:62
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
Definition nullable.hpp:43
const_reference const_rvalue_reference
Definition nullable.hpp:115
std::add_lvalue_reference_t< value_type > reference
Definition nullable.hpp:112
std::add_lvalue_reference_t< std::add_const_t< value_type > > const_reference
Definition nullable.hpp:113
const value_type && const_rvalue_reference
Definition nullable.hpp:105
std::add_lvalue_reference_t< value_type > reference
Definition nullable.hpp:102
value_type && rvalue_reference
Definition nullable.hpp:104
std::add_lvalue_reference_t< std::add_const_t< value_type > > const_reference
Definition nullable.hpp:103
Sentinel type to indicate a nullable value is null.
Definition nullable.hpp:157
constexpr nullval_t(int)
Private constructor to prevent default construction.
Definition nullable.hpp:161
sparrow:: nullable< std::common_reference_t< TQual< T >, UQual< U > >, std::common_reference_t< TQual< TB >, UQual< UB > > > type