tclap 1.6.0
RangeConstraint.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: RangeConstraint.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_RANGE_CONSTRAINT_H
24#define TCLAP_RANGE_CONSTRAINT_H
25
26#include <tclap/Constraint.h>
27
28#include <concepts>
29#include <ostream>
30#include <sstream>
31#include <string>
32#include <utility>
33
34namespace TCLAP {
35
41template <typename T>
42 requires std::totally_ordered<T> &&
43 requires(std::ostream &out, const T &value) { out << value; }
44class RangeConstraint : public Constraint<T> {
45public:
51 RangeConstraint(T min, T max)
52 : _min(std::move(min)), _max(std::move(max)), _shortId() {
53 std::ostringstream os;
54 os << '[' << _min << '-' << _max << ']';
55 _shortId = os.str();
56 }
57
58 [[nodiscard]] std::string description() const override {
59 std::ostringstream os;
60 os << "value must be between " << _min << " and " << _max
61 << " (inclusive)";
62 return os.str();
63 }
64
65 [[nodiscard]] std::string shortID() const override { return _shortId; }
66
67 bool check(const T &value) const override {
68 // Positive comparisons also reject unordered floating-point values.
69 return _min <= value && value <= _max;
70 }
71
72private:
73 T _min;
74 T _max;
75 std::string _shortId;
76};
77
78} // namespace TCLAP
79
80#endif // TCLAP_RANGE_CONSTRAINT_H
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:41
A Constraint that requires a value to fall within a closed [min, max] range, for the common "between ...
std::string shortID() const override
Returns the short ID for the Constraint.
bool check(const T &value) const override
The method used to verify that the value parsed from the command line meets the constraint.
std::string description() const override
Returns a description of the Constraint.
RangeConstraint(T min, T max)
Constructor.
Definition Arg.h:54