tclap 1.4.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 <functional>
29#include <sstream>
30#include <string>
31
32namespace TCLAP {
33namespace internal {
34
35// A single owned threshold supplies validation and both forms of help text.
36template <class T, class Compare>
38public:
39 ComparisonRule(const T &bound, const std::string &relation,
40 const std::string &symbol)
41 : _bound(bound), _relation(relation), _shortId() {
42 std::ostringstream out;
43 out << '[' << symbol << _bound << ']';
44 _shortId = out.str();
45 }
46
47 bool operator()(const T &value) const { return Compare()(value, _bound); }
48 std::string description() const {
49 std::ostringstream out;
50 out << "value must be " << _relation << ' ' << _bound;
51 return out.str();
52 }
53 std::string shortID() const { return _shortId; }
54
55private:
56 T _bound;
57 std::string _relation;
58 std::string _shortId;
59};
60
61template <class T, class Compare>
63 : public PredicateConstraint<T, ComparisonRule<T, Compare>,
64 RuleConstraintText> {
65protected:
66 ComparisonConstraintBase(const T &bound, const std::string &relation,
67 const std::string &symbol)
68 : PredicateConstraint<T, ComparisonRule<T, Compare>,
70 ComparisonRule<T, Compare>(bound, relation, symbol),
72};
73
74} // namespace internal
75
83template <class T>
85 : public internal::ComparisonConstraintBase<T, std::less<T> > {
86public:
87 explicit LessThanConstraint(const T &bound)
88 : internal::ComparisonConstraintBase<T, std::less<T> >(
89 bound, "less than", "<") {}
90};
91
93template <class T>
95 : public internal::ComparisonConstraintBase<T, std::less_equal<T> > {
96public:
97 explicit LessThanOrEqualConstraint(const T &bound)
98 : internal::ComparisonConstraintBase<T, std::less_equal<T> >(
99 bound, "less than or equal to", "<=") {}
100};
101
103template <class T>
105 : public internal::ComparisonConstraintBase<T, std::greater<T> > {
106public:
107 explicit GreaterThanConstraint(const T &bound)
108 : internal::ComparisonConstraintBase<T, std::greater<T> >(
109 bound, "greater than", ">") {}
110};
111
113template <class T>
115 : public internal::ComparisonConstraintBase<T, std::greater_equal<T> > {
116public:
117 explicit GreaterThanOrEqualConstraint(const T &bound)
118 : internal::ComparisonConstraintBase<T, std::greater_equal<T> >(
119 bound, "greater than or equal to", ">=") {}
120};
121
122} // namespace TCLAP
123
124#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(const T &bound, const std::string &relation, const std::string &symbol)
bool operator()(const T &value) const
ComparisonRule(const T &bound, const std::string &relation, const std::string &symbol)
Definition Arg.h:52