tclap 1.6.0
ComparisonConstraint.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: ComparisonConstraint.h
6 *
7 * Copyright (c) 2026, Google LLC
8 * All rights reserved.
9 *
10 * See the file COPYING in the top directory of this distribution for
11 * more information.
12 *
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *****************************************************************************/
22
23#ifndef TCLAP_COMPARISON_CONSTRAINT_H
24#define TCLAP_COMPARISON_CONSTRAINT_H
25
27
28#include <concepts>
29#include <functional>
30#include <ostream>
31#include <sstream>
32#include <string>
33#include <utility>
34
35namespace TCLAP {
36namespace internal {
37
38template <class T>
40 std::totally_ordered<T> &&
41 requires(std::ostream &out, const T &value) { out << value; };
42
43// A single owned threshold supplies validation and both forms of help text.
44template <class T, class Compare>
47public:
48 template <class Bound>
49 requires std::constructible_from<T, Bound &&>
50 ComparisonRule(Bound &&bound, const std::string &relation,
51 const std::string &symbol)
52 : _bound(std::forward<Bound>(bound)), _relation(relation), _shortId() {
53 std::ostringstream out;
54 out << '[' << symbol << _bound << ']';
55 _shortId = out.str();
56 }
57
58 bool operator()(const T &value) const { return Compare()(value, _bound); }
59 std::string description() const {
60 std::ostringstream out;
61 out << "value must be " << _relation << ' ' << _bound;
62 return out.str();
63 }
64 std::string shortID() const { return _shortId; }
65
66private:
67 T _bound;
68 std::string _relation;
69 std::string _shortId;
70};
71
72template <class T, class Compare>
73 requires OrderedConstraintValue<T>
75 : public PredicateConstraint<T, ComparisonRule<T, Compare>,
76 RuleConstraintText> {
77protected:
78 template <class Bound>
79 requires std::constructible_from<T, Bound &&>
80 ComparisonConstraintBase(Bound &&bound, const std::string &relation,
81 const std::string &symbol)
82 : PredicateConstraint<T, ComparisonRule<T, Compare>,
84 ComparisonRule<T, Compare>(std::forward<Bound>(bound), relation,
85 symbol),
87};
88
89} // namespace internal
90
99template <class T>
100 requires internal::OrderedConstraintValue<T>
102 : public internal::ComparisonConstraintBase<T, std::less<T> > {
103public:
104 template <class Bound>
105 requires std::constructible_from<T, Bound &&>
106 explicit LessThanConstraint(Bound &&bound)
107 : internal::ComparisonConstraintBase<T, std::less<T> >(
108 std::forward<Bound>(bound), "less than", "<") {}
109};
110
112template <class T>
113 requires internal::OrderedConstraintValue<T>
115 : public internal::ComparisonConstraintBase<T, std::less_equal<T> > {
116public:
117 template <class Bound>
118 requires std::constructible_from<T, Bound &&>
119 explicit LessThanOrEqualConstraint(Bound &&bound)
120 : internal::ComparisonConstraintBase<T, std::less_equal<T> >(
121 std::forward<Bound>(bound), "less than or equal to", "<=") {}
122};
123
125template <class T>
126 requires internal::OrderedConstraintValue<T>
128 : public internal::ComparisonConstraintBase<T, std::greater<T> > {
129public:
130 template <class Bound>
131 requires std::constructible_from<T, Bound &&>
132 explicit GreaterThanConstraint(Bound &&bound)
133 : internal::ComparisonConstraintBase<T, std::greater<T> >(
134 std::forward<Bound>(bound), "greater than", ">") {}
135};
136
138template <class T>
139 requires internal::OrderedConstraintValue<T>
141 : public internal::ComparisonConstraintBase<T, std::greater_equal<T> > {
142public:
143 template <class Bound>
144 requires std::constructible_from<T, Bound &&>
145 explicit GreaterThanOrEqualConstraint(Bound &&bound)
146 : internal::ComparisonConstraintBase<T, std::greater_equal<T> >(
147 std::forward<Bound>(bound), "greater than or equal to", ">=") {}
148};
149
150} // namespace TCLAP
151
152#endif // TCLAP_COMPARISON_CONSTRAINT_H
Accepts values strictly greater than an owned lower bound.
Accepts values greater than or equal to an owned lower bound.
Accepts values strictly less than an owned upper bound.
Accepts values less than or equal to an owned upper bound.
Adapts an owned function or const function object to Constraint<T>.
ComparisonConstraintBase(Bound &&bound, const std::string &relation, const std::string &symbol)
bool operator()(const T &value) const
ComparisonRule(Bound &&bound, const std::string &relation, const std::string &symbol)
Definition Arg.h:54