tclap 1.4.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 <sstream>
31#include <string>
32#include <vector>
33
34namespace TCLAP {
35
40template <class T>
41class ValuesConstraint : public Constraint<T> {
42public:
47 explicit ValuesConstraint(const std::vector<T> &allowed);
48
52 virtual ~ValuesConstraint() {}
53
57 virtual std::string description() const;
58
62 virtual std::string shortID() const;
63
69 virtual bool check(const T &value) const;
70
71protected:
75 std::vector<T> _allowed;
76
80 std::string _typeDesc;
81};
82
83template <class T>
84ValuesConstraint<T>::ValuesConstraint(const std::vector<T> &allowed)
85 : _allowed(allowed), _typeDesc("") {
86 for (unsigned int i = 0; i < _allowed.size(); i++) {
87 std::ostringstream os;
88 os << _allowed[i];
89
90 std::string temp(os.str());
91
92 if (i > 0) _typeDesc += "|";
93 _typeDesc += temp;
94 }
95}
96
97template <class T>
98bool ValuesConstraint<T>::check(const T &val) const {
99 return std::find(_allowed.begin(), _allowed.end(), val) != _allowed.end();
100}
101
102template <class T>
103std::string ValuesConstraint<T>::shortID() const {
104 return _typeDesc;
105}
106
107template <class T>
109 return _typeDesc;
110}
111
112} // namespace TCLAP
113#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.
std::string _typeDesc
The string used to describe the allowed values of this constraint.
virtual bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.
virtual std::string description() const
Returns a description of the Constraint.
virtual ~ValuesConstraint()
Virtual destructor.
ValuesConstraint(const std::vector< T > &allowed)
Constructor.
std::vector< T > _allowed
The list of valid values.
virtual std::string shortID() const
Returns the short ID for the Constraint.
Definition Arg.h:52