tclap 1.4.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 <sstream>
29#include <string>
30
31namespace TCLAP {
32
41template <class T>
42class RangeConstraint : public Constraint<T> {
43public:
49 RangeConstraint(const T &min, const T &max)
50 : _min(min), _max(max), _shortId() {
51 std::ostringstream os;
52 os << '[' << _min << '-' << _max << ']';
53 _shortId = os.str();
54 }
55
56 std::string description() const {
57 std::ostringstream os;
58 os << "value must be between " << _min << " and " << _max
59 << " (inclusive)";
60 return os.str();
61 }
62
63 std::string shortID() const { return _shortId; }
64
65 bool check(const T &value) const {
66 // Positive comparisons also reject unordered floating-point values.
67 return _min <= value && value <= _max;
68 }
69
70private:
71 T _min;
72 T _max;
73 std::string _shortId;
74};
75
76} // namespace TCLAP
77
78#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 ...
bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.
RangeConstraint(const T &min, const T &max)
Constructor.
std::string description() const
Returns a description of the Constraint.
std::string shortID() const
Returns the short ID for the Constraint.
Definition Arg.h:52