tclap 1.6.0
ValuesConstraint.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: ValuesConstraint.h
6 *
7 * Copyright (c) 2005, Michael E. Smoot
8 * Copyright (c) 2017, Google LLC
9 * All rights reserved.
10 *
11 * See the file COPYING in the top directory of this distribution for
12 * more information.
13 *
14 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 *****************************************************************************/
23
24#ifndef TCLAP_VALUES_CONSTRAINT_H
25#define TCLAP_VALUES_CONSTRAINT_H
26
27#include <tclap/Constraint.h>
28#include <tclap/sstream.h>
29
30#include <algorithm>
31#include <ostream>
32#include <sstream>
33#include <string>
34#include <vector>
35
36namespace TCLAP {
37
38namespace detail {
39// Satisfied iff T can be written to an ostream via operator<< (what the
40// ValuesConstraint constructor, below, uses to build its description).
41template <typename T>
43 requires(std::ostream &os, const T &val) { os << val; };
44} // namespace detail
45
50template <class T>
51class ValuesConstraint : public Constraint<T> {
52public:
57 explicit ValuesConstraint(const std::vector<T> &allowed)
59
63 ~ValuesConstraint() override = default;
64
68 [[nodiscard]] std::string description() const override;
69
73 [[nodiscard]] std::string shortID() const override;
74
80 bool check(const T &value) const override;
81
82protected:
86 std::vector<T> _allowed;
87
91 std::string _typeDesc;
92};
93
94template <class T>
95ValuesConstraint<T>::ValuesConstraint(const std::vector<T> &allowed)
97 : _allowed(allowed), _typeDesc("") {
98 bool first = true;
99 for (const T &value : _allowed) {
100 std::ostringstream os;
101 os << value;
102
103 if (!first) _typeDesc += "|";
104 _typeDesc += os.str();
105 first = false;
106 }
107}
108
109template <class T>
110bool ValuesConstraint<T>::check(const T &val) const {
111 return std::ranges::find(_allowed, val) != _allowed.end();
112}
113
114template <class T>
115std::string ValuesConstraint<T>::shortID() const {
116 return _typeDesc;
117}
118
119template <class T>
121 return _typeDesc;
122}
123
124} // namespace TCLAP
125#endif // TCLAP_VALUES_CONSTRAINT_H
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:41
A Constraint that constrains the Arg to only those values specified in 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.
ValuesConstraint(const std::vector< T > &allowed)
Constructor.
std::string _typeDesc
The string used to describe the allowed values of this constraint.
std::string shortID() const override
Returns the short ID for the Constraint.
std::vector< T > _allowed
The list of valid values.
~ValuesConstraint() override=default
Virtual destructor.
std::string description() const override
Returns a description of the Constraint.
Definition Arg.h:54