CPP-AP 2.2.6
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 <optional>
13#include <ostream>
14
15namespace ap::nargs {
16
18class range {
19public:
20 using count_type = std::size_t;
21
23 range() : _lower_bound(_default_bound), _upper_bound(_default_bound) {}
24
29 explicit range(const count_type n) : _lower_bound(n), _upper_bound(n) {}
30
36 range(const count_type lower_bound, const count_type upper_bound)
37 : _lower_bound(lower_bound), _upper_bound(upper_bound) {}
38
39 range(const range&) = default;
40 range(range&&) = default;
41
42 range& operator=(const range&) = default;
43 range& operator=(range&&) = default;
44
45 ~range() = default;
46
48 [[nodiscard]] bool is_bound() const noexcept {
49 return this->_lower_bound.has_value() or this->_upper_bound.has_value();
50 }
51
65 [[nodiscard]] std::weak_ordering ordering(const range::count_type n) const noexcept {
66 if (not (this->_lower_bound.has_value() or this->_upper_bound.has_value()))
67 return std::weak_ordering::equivalent;
68
69 if (this->_lower_bound.has_value() and this->_upper_bound.has_value()) {
70 if (n < this->_lower_bound.value())
71 return std::weak_ordering::less;
72
73 if (n > this->_upper_bound.value())
74 return std::weak_ordering::greater;
75
76 return std::weak_ordering::equivalent;
77 }
78
79 if (this->_lower_bound.has_value())
80 return (n < this->_lower_bound.value())
81 ? std::weak_ordering::less
82 : std::weak_ordering::equivalent;
83
84 return (n > this->_upper_bound.value())
85 ? std::weak_ordering::greater
86 : std::weak_ordering::equivalent;
87 }
88
89 friend std::ostream& operator<<(std::ostream& os, const range& r) noexcept {
90 if (r._lower_bound.has_value() and r._lower_bound == r._upper_bound) {
91 os << r._lower_bound.value();
92 return os;
93 }
94
95 if (not r._lower_bound.has_value() and not r._upper_bound.has_value()) {
96 os << "unbound";
97 return os;
98 }
99
100 os << "[" << r._lower_bound.value_or(0ull) << ", ";
101 if (r._upper_bound.has_value())
102 os << r._upper_bound.value() << "]";
103 else
104 os << "inf)";
105
106 return os;
107 }
108
109 friend range at_least(const count_type) noexcept;
110 friend range more_than(const count_type) noexcept;
111 friend range less_than(const count_type) noexcept;
112 friend range up_to(const count_type) noexcept;
113 friend range any() noexcept;
114
115private:
121 range(const std::optional<count_type> lower_bound, const std::optional<count_type> upper_bound)
122 : _lower_bound(lower_bound), _upper_bound(upper_bound) {}
123
124 std::optional<count_type> _lower_bound;
125 std::optional<count_type> _upper_bound;
126
127 static constexpr count_type _default_bound = 1ull;
128};
129
135[[nodiscard]] inline range at_least(const range::count_type n) noexcept {
136 return range(n, std::nullopt);
137}
138
144[[nodiscard]] inline range more_than(const range::count_type n) noexcept {
145 return range(n + 1, std::nullopt);
146}
147
153[[nodiscard]] inline range less_than(const range::count_type n) noexcept {
154 return range(std::nullopt, n - 1);
155}
156
162[[nodiscard]] inline range up_to(const range::count_type n) noexcept {
163 return range(std::nullopt, n);
164}
165
170[[nodiscard]] inline range any() noexcept {
171 return range(std::nullopt, std::nullopt);
172}
173
174} // namespace ap::nargs
Argument's number of values managing class.
Definition range.hpp:18
range()
Default constructor: creates range [1, 1].
Definition range.hpp:23
range(const count_type n)
Exact count constructor: creates range [n, n].
Definition range.hpp:29
friend range at_least(const count_type) noexcept
range class builder function. Creates a range [n, inf].
Definition range.hpp:135
friend range any() noexcept
range class builder function. Creates a range [0, inf].
Definition range.hpp:170
std::weak_ordering ordering(const range::count_type n) const noexcept
Determines the ordering of the count against a range instance.
Definition range.hpp:65
friend range less_than(const count_type) noexcept
range class builder function. Creates a range [0, n - 1].
Definition range.hpp:153
friend range more_than(const count_type) noexcept
range class builder function. Creates a range [n + 1, inf].
Definition range.hpp:144
range(const count_type lower_bound, const count_type upper_bound)
Concrete range constructor: creates range [lower_bound, upper_bound].
Definition range.hpp:36
friend range up_to(const count_type) noexcept
range class builder function. Creates a range [0, n].
Definition range.hpp:162
bool is_bound() const noexcept
Returns true if at least one bound (lower, upper) is set. Otherwise returns false
Definition range.hpp:48
range up_to(const range::count_type n) noexcept
range class builder function. Creates a range [0, n].
Definition range.hpp:162
range more_than(const range::count_type n) noexcept
range class builder function. Creates a range [n + 1, inf].
Definition range.hpp:144
range any() noexcept
range class builder function. Creates a range [0, inf].
Definition range.hpp:170
range less_than(const range::count_type n) noexcept
range class builder function. Creates a range [0, n - 1].
Definition range.hpp:153
range at_least(const range::count_type n) noexcept
range class builder function. Creates a range [n, inf].
Definition range.hpp:135