4#if !defined(PQXX_HEADER_PRE)
5# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
11#include "pqxx/internal/array-composite.hxx"
12#include "pqxx/internal/concat.hxx"
25 template<
typename TYPE>
26 constexpr bool extends_down_to(TYPE
const &)
const noexcept
30 template<
typename TYPE>
31 constexpr bool extends_up_to(TYPE
const &)
const noexcept
53 throw argument_error{
"Got null value as an inclusive range bound."};
56 [[nodiscard]]
constexpr TYPE
const &get()
const &
noexcept
63 noexcept(
noexcept(value < m_value))
65 return not(value < m_value);
70 noexcept(
noexcept(value < m_value))
72 return not(m_value < value);
92 throw argument_error{
"Got null value as an exclusive range bound."};
95 [[nodiscard]]
constexpr TYPE
const &get()
const &
noexcept
102 noexcept(
noexcept(m_value < value))
104 return m_value < value;
109 noexcept(
noexcept(value < m_value))
111 return value < m_value;
142 std::declval<inclusive_bound<TYPE>
const &>()}) and
144 std::declval<exclusive_bound<TYPE>
const &>()})) =
default;
148 constexpr bool operator==(
range_bound const &rhs)
const
149 noexcept(
noexcept(*this->
value() == *rhs.
value()))
159 constexpr bool operator!=(
range_bound const &rhs)
const
160 noexcept(
noexcept(*
this == rhs))
162 return not(*
this == rhs);
170 return not std::holds_alternative<no_bound>(m_bound);
176 return std::holds_alternative<inclusive_bound<TYPE>>(m_bound);
182 return std::holds_alternative<exclusive_bound<TYPE>>(m_bound);
190 value))) { return bound.extends_down_to(value); },
198 [&
value](
auto const &bound)
noexcept(
noexcept(
204 [[nodiscard]]
constexpr TYPE
const *
value() const & noexcept
207 [](
auto const &bound)
noexcept {
208 using bound_t = std::decay_t<
decltype(bound)>;
209 if constexpr (std::is_same_v<bound_t, no_bound>)
210 return static_cast<TYPE
const *
>(
nullptr);
252 m_lower{lower}, m_upper{upper}
258 "Range's lower bound (", *lower.
value(),
259 ") is greater than its upper bound (", *upper.
value(),
").")};
268 m_upper{exclusive_bound<TYPE>{TYPE{}}}
271 constexpr bool operator==(range
const &rhs)
const noexcept(
272 noexcept(this->lower_bound() == rhs.lower_bound()) and
273 noexcept(this->upper_bound() == rhs.upper_bound()) and
274 noexcept(this->empty()))
276 return (this->lower_bound() == rhs.lower_bound() and
277 this->upper_bound() == rhs.upper_bound()) or
278 (this->empty() and rhs.empty());
281 constexpr bool operator!=(range
const &rhs)
const
282 noexcept(
noexcept(*
this == rhs))
284 return not(*
this == rhs);
287 range(range
const &) =
default;
288 range(range &&) =
default;
289 range &operator=(range
const &) =
default;
290 range &operator=(range &&) =
default;
301 constexpr bool empty() const noexcept(
302 noexcept(m_lower.is_exclusive()) and noexcept(m_lower.is_limited()) and
303 noexcept(*m_lower.value() < *m_upper.value()))
305 return (m_lower.is_exclusive() or m_upper.is_exclusive()) and
306 m_lower.is_limited() and m_upper.is_limited() and
307 not(*m_lower.value() < *m_upper.value());
311 constexpr bool contains(TYPE value)
const noexcept(
312 noexcept(m_lower.extends_down_to(value)) and
313 noexcept(m_upper.extends_up_to(value)))
315 return m_lower.extends_down_to(value) and m_upper.extends_up_to(value);
323 noexcept(
noexcept((*
this & other) == other))
325 return (*
this & other) == other;
329 lower_bound() const & noexcept
333 [[nodiscard]]
constexpr range_bound<TYPE>
const &
334 upper_bound() const & noexcept
345 if (not this->lower_bound().is_limited())
346 lower = other.lower_bound();
347 else if (not other.lower_bound().is_limited())
348 lower = this->lower_bound();
349 else if (*this->lower_bound().value() < *other.lower_bound().value())
350 lower = other.lower_bound();
351 else if (*other.lower_bound().value() < *this->lower_bound().value())
352 lower = this->lower_bound();
353 else if (this->lower_bound().is_exclusive())
354 lower = this->lower_bound();
356 lower = other.lower_bound();
359 if (not this->upper_bound().is_limited())
360 upper = other.upper_bound();
361 else if (not other.upper_bound().is_limited())
362 upper = this->upper_bound();
363 else if (*other.upper_bound().value() < *this->upper_bound().value())
364 upper = other.upper_bound();
365 else if (*this->upper_bound().value() < *other.upper_bound().value())
366 upper = this->upper_bound();
367 else if (this->upper_bound().is_exclusive())
368 upper = this->upper_bound();
370 upper = other.upper_bound();
373 lower.is_limited() and upper.is_limited() and
374 (*upper.value() < *lower.value()))
377 return {lower, upper};
384 if (lower_bound().is_inclusive())
386 else if (lower_bound().is_exclusive())
389 if (upper_bound().is_inclusive())
391 else if (upper_bound().is_exclusive())
394 return {lower, upper};
405 [[nodiscard]]
static inline zview
412 into_buf(
char *begin,
char *end,
range<TYPE> const &value)
416 if ((end - begin) <= internal::ssize(s_empty))
418 char *here = begin + s_empty.copy(begin, std::size(s_empty));
428 (
static_cast<char>(value.lower_bound().is_inclusive() ?
'[' :
'('));
429 TYPE
const *lower{value.lower_bound().value()};
431 if (lower !=
nullptr)
434 TYPE
const *upper{value.upper_bound().value()};
436 if (upper !=
nullptr)
438 if ((end - here) < 2)
441 static_cast<char>(value.upper_bound().is_inclusive() ?
']' :
')');
449 if (std::size(text) < 3)
451 bool left_inc{
false};
454 case '[': left_inc =
true;
break;
461 (std::size(text) != std::size(s_empty)) or
462 (text[1] !=
'm' and text[1] !=
'M') or
463 (text[2] !=
'p' and text[2] !=
'P') or
464 (text[3] !=
't' and text[3] !=
'T') or
465 (text[4] !=
'y' and text[4] !=
'Y'))
475 std::size_t index{0};
477 static constexpr std::size_t last{1};
481 std::optional<TYPE> lower, upper;
483 auto const field_parser{
484 pqxx::internal::specialize_parse_composite_field<std::optional<TYPE>>(
485 pqxx::internal::encoding_group::UTF8)};
486 field_parser(index, text, pos, lower, last);
487 field_parser(index, text, pos, upper, last);
490 if (pos != std::size(text))
492 char const closing{text[pos - 1]};
493 if (closing !=
')' and closing !=
']')
495 bool const right_inc{closing ==
']'};
513 return {lower_bound, upper_bound};
516 [[nodiscard]]
static inline constexpr std::size_t
519 TYPE
const *lower{value.lower_bound().value()},
520 *upper{value.upper_bound().value()};
521 std::size_t
const lsz{
526 return std::size(s_empty) + 1;
528 return 1 + lsz + 1 + usz + 2;
532 static constexpr zview s_empty{
"empty"_zv};
533 static constexpr auto s_overrun{
"Not enough space in buffer for range."_zv};
536 static std::string err_bad_input(std::string_view text)
538 return internal::concat(
"Invalid range input: '", text,
"'");
An exclusive boundary value to a pqxx::range.
Definition range.hxx:82
constexpr bool extends_up_to(TYPE const &value) const noexcept(noexcept(value< m_value))
Would this bound, as an upper bound, include value?
Definition range.hxx:108
constexpr bool extends_down_to(TYPE const &value) const noexcept(noexcept(m_value< value))
Would this bound, as a lower bound, include value?
Definition range.hxx:101
An inclusive boundary value to a pqxx::range.
Definition range.hxx:43
constexpr bool extends_down_to(TYPE const &value) const noexcept(noexcept(value< m_value))
Would this bound, as a lower bound, include value?
Definition range.hxx:62
constexpr bool extends_up_to(TYPE const &value) const noexcept(noexcept(value< m_value))
Would this bound, as an upper bound, include value?
Definition range.hxx:69
A range boundary value.
Definition range.hxx:121
constexpr bool extends_up_to(TYPE const &value) const
Would this bound, as an upper bound, include value?
Definition range.hxx:195
constexpr bool is_exclusive() const noexcept
Is this boundary an exclusive one?
Definition range.hxx:180
constexpr bool is_limited() const noexcept
Is this a finite bound?
Definition range.hxx:168
constexpr TYPE const * value() const &noexcept
Return bound value, or nullptr if it's not limited.
Definition range.hxx:204
constexpr bool extends_down_to(TYPE const &value) const
Would this bound, as a lower bound, include value?
Definition range.hxx:186
constexpr bool is_inclusive() const noexcept
Is this boundary an inclusive one?
Definition range.hxx:174
A C++ equivalent to PostgreSQL's range types.
Definition range.hxx:240
constexpr range operator&(range const &other) const
Intersection of two ranges.
Definition range.hxx:342
constexpr bool contains(TYPE value) const noexcept(noexcept(m_lower.extends_down_to(value)) and noexcept(m_upper.extends_up_to(value)))
Does this range encompass value?
Definition range.hxx:311
constexpr bool contains(range< TYPE > const &other) const noexcept(noexcept((*this &other)==other))
Does this range encompass all of other?
Definition range.hxx:322
constexpr range(range_bound< TYPE > lower, range_bound< TYPE > upper)
Create a range.
Definition range.hxx:251
constexpr bool empty() const noexcept(noexcept(m_lower.is_exclusive()) and noexcept(m_lower.is_limited()) and noexcept(*m_lower.value()< *m_upper.value()))
Is this range clearly empty?
Definition range.hxx:301
constexpr range() noexcept(noexcept(exclusive_bound< TYPE >{TYPE{}}))
Create an empty range.
Definition range.hxx:266
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38
Invalid argument passed to libpqxx, similar to std::invalid_argument.
Definition except.hxx:266
Value conversion failed, e.g. when converting "Hello" to int.
Definition except.hxx:283
Could not convert value to string: not enough buffer space.
Definition except.hxx:313
Something is out of range, similar to std::out_of_range.
Definition except.hxx:326
std::string concat(TYPE... item)
Efficiently combine a bunch of items into one big string.
Definition concat.hxx:31
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
std::vector< std::string_view > to_buf(char *here, char const *end, TYPE... value)
Convert multiple values to strings inside a single buffer.
Definition strconv.hxx:492
std::size_t size_buffer(TYPE const &...value) noexcept
Estimate how much buffer space is needed to represent values as a string.
Definition strconv.hxx:525
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition strconv.hxx:514
zview generic_to_buf(char *begin, char *end, TYPE const &value)
Implement string_traits<TYPE>::to_buf by calling into_buf.
Definition strconv.hxx:586
T from_string(field const &value)
Convert a field's value to type T.
Definition field.hxx:532
An unlimited boundary value to a pqxx::range.
Definition range.hxx:24
Nullness traits describing a type which does not have a null value.
Definition strconv.hxx:113
Traits describing a type's "null value," if any.
Definition strconv.hxx:91
Traits class for use in string conversions.
Definition strconv.hxx:153