CPP-AP 3.0.1
Command-line argument parser for C++20
Loading...
Searching...
No Matches
range.hpp
Go to the documentation of this file.
1// Copyright (c) 2023-2025 Jakub MusiaƂ
2// This file is part of the CPP-AP project (https://github.com/SpectraL519/cpp-ap).
3// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
10#pragma once
11
12#include <format>
13#include <limits>
14#include <ostream>
15
16namespace ap::nargs {
17
18using count_type = std::size_t;
19
21constexpr count_type min_bound = std::numeric_limits<count_type>::min();
22
24constexpr count_type max_bound = std::numeric_limits<count_type>::max();
25
27class range {
28public:
30 constexpr range() = default;
31
36 explicit constexpr range(const count_type n) : _lower_bound(n), _upper_bound(n) {}
37
43 constexpr range(const count_type lower, const count_type upper)
44 : _lower_bound(lower), _upper_bound(upper) {
45 if (upper < lower)
46 throw std::logic_error(
47 std::format("Invalid range bounds: lower = {}, upper = {}", lower, upper)
48 );
49 }
50
51 [[nodiscard]] constexpr bool has_explicit_lower_bound() const noexcept {
52 return this->_lower_bound > min_bound;
53 }
54
55 [[nodiscard]] constexpr bool has_explicit_upper_bound() const noexcept {
56 return this->_upper_bound < max_bound;
57 }
58
59 [[nodiscard]] constexpr bool is_explicitly_bound() const noexcept {
60 return this->has_explicit_lower_bound() or this->has_explicit_upper_bound();
61 }
62
63 [[nodiscard]] constexpr bool is_exactly_bound() const noexcept {
64 return this->_lower_bound == this->_upper_bound;
65 }
66
67 constexpr bool operator==(const range& other) const = default;
68
80 [[nodiscard]] friend constexpr std::weak_ordering operator<=>(
81 const count_type n, const range& r
82 ) noexcept {
83 if (n < r._lower_bound)
84 return std::weak_ordering::less;
85
86 if (n > r._upper_bound)
87 return std::weak_ordering::greater;
88
89 return std::weak_ordering::equivalent;
90 }
91
92 friend std::ostream& operator<<(std::ostream& os, const range& r) noexcept {
93 if (not r.is_explicitly_bound()) {
94 os << "unbound";
95 return os;
96 }
97
98 if (r.is_exactly_bound()) {
99 os << r._upper_bound;
100 return os;
101 }
102
103 os << "[" << r._lower_bound << ", ";
104 if (r.has_explicit_upper_bound())
105 os << r._upper_bound << "]";
106 else
107 os << "inf)";
108
109 return os;
110 }
111
112 friend constexpr range at_least(const count_type) noexcept;
113 friend constexpr range more_than(const count_type) noexcept;
114 friend constexpr range less_than(const count_type) noexcept;
115 friend constexpr range up_to(const count_type) noexcept;
116 friend constexpr range any() noexcept;
117
118private:
121};
122
128[[nodiscard]] constexpr range at_least(const count_type n) noexcept {
129 return range(n, max_bound);
130}
131
137[[nodiscard]] constexpr range more_than(const count_type n) noexcept {
138 return range(n + 1ull, max_bound);
139}
140
146[[nodiscard]] constexpr range less_than(const count_type n) noexcept {
147 return range(min_bound, n - 1ull);
148}
149
155[[nodiscard]] constexpr range up_to(const count_type n) noexcept {
156 return range(min_bound, n);
157}
158
163[[nodiscard]] constexpr range any() noexcept {
164 return range(min_bound, max_bound);
165}
166
167} // namespace ap::nargs
Argument's number of values managing class.
Definition range.hpp:27
friend constexpr range more_than(const count_type) noexcept
range class builder function. Creates a range [n + 1, inf).
Definition range.hpp:137
constexpr bool is_explicitly_bound() const noexcept
Definition range.hpp:59
friend constexpr std::weak_ordering operator<=>(const count_type n, const range &r) noexcept
Determines the ordering of the count against a range instance.
Definition range.hpp:80
constexpr range(const count_type n)
Exact count constructor: creates range [n, n].
Definition range.hpp:36
friend constexpr range up_to(const count_type) noexcept
range class builder function. Creates a range [0, n].
Definition range.hpp:155
constexpr bool has_explicit_lower_bound() const noexcept
Definition range.hpp:51
friend constexpr range at_least(const count_type) noexcept
range class builder function. Creates a range [n, inf).
Definition range.hpp:128
friend constexpr range less_than(const count_type) noexcept
range class builder function. Creates a range [0, n - 1].
Definition range.hpp:146
count_type _upper_bound
Definition range.hpp:120
constexpr range()=default
Default constructor: creates an unbound range.
friend std::ostream & operator<<(std::ostream &os, const range &r) noexcept
Definition range.hpp:92
constexpr bool is_exactly_bound() const noexcept
Definition range.hpp:63
constexpr bool operator==(const range &other) const =default
constexpr bool has_explicit_upper_bound() const noexcept
Definition range.hpp:55
constexpr range(const count_type lower, const count_type upper)
Concrete range constructor: creates range [lower, upper].
Definition range.hpp:43
friend constexpr range any() noexcept
range class builder function. Creates a range [0, inf].
Definition range.hpp:163
count_type _lower_bound
Definition range.hpp:119
constexpr count_type min_bound
The minimum bound for the nargs::range class.
Definition range.hpp:21
std::size_t count_type
Definition range.hpp:18
constexpr count_type max_bound
The maximum bound for the nargs::range class.
Definition range.hpp:24