tclap: ValuesConstraint.h Source File

tclap 1.2.5

ValuesConstraint.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3
4
5 /******************************************************************************
6 *
7 * file: ValuesConstraint.h
8 *
9 * Copyright (c) 2005, Michael E. Smoot
10 * Copyright (c) 2017, Google LLC
11 * All rights reserved.
12 *
13 * See the file COPYING in the top directory of this distribution for
14 * more information.
15 *
16 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 *****************************************************************************/
25
26 #ifndef TCLAP_VALUESCONSTRAINT_H
27 #define TCLAP_VALUESCONSTRAINT_H
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <string>
34 #include <vector>
35 #include <tclap/Constraint.h>
36 #include <tclap/sstream.h>
37
38 namespace TCLAP {
39
44 template<class T>
45 class ValuesConstraint : public Constraint<T>
46 {
47
48 public:
49
54 ValuesConstraint(std::vector<T>const& allowed);
55
59 virtual ~ValuesConstraint() {}
60
64 virtual std::string description() const;
65
69 virtual std::string shortID() const;
70
76 virtual bool check(const T& value) const;
77
78 protected:
79
83 std::vector<T> _allowed;
84
88 std::string _typeDesc;
89
90 };
91
92 template<class T>
93 ValuesConstraint<T>::ValuesConstraint(std::vector<T> const& allowed)
94 : _allowed(allowed),
95 _typeDesc("")
96 {
97 for ( unsigned int i = 0; i < _allowed.size(); i++ )
98 {
99 std::ostringstream os;
100 os << _allowed[i];
101
102 std::string temp( os.str() );
103
104 if ( i > 0 )
105 _typeDesc += "|";
106 _typeDesc += temp;
107 }
108 }
109
110 template<class T>
111 bool ValuesConstraint<T>::check( const T& val ) const
112 {
113 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
114 return false;
115 else
116 return true;
117 }
118
119 template<class T>
120 std::string ValuesConstraint<T>::shortID() const
121 {
122 return _typeDesc;
123 }
124
125 template<class T>
126 std::string ValuesConstraint<T>::description() const
127 {
128 return _typeDesc;
129 }
130
131
132 } //namespace TCLAP
133 #endif
134
Constraint.h
TCLAP::Constraint
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:43
TCLAP::ValuesConstraint
A Constraint that constrains the Arg to only those values specified in the constraint.
Definition: ValuesConstraint.h:46
TCLAP::ValuesConstraint::ValuesConstraint
ValuesConstraint(std::vector< T >const &allowed)
Constructor.
Definition: ValuesConstraint.h:93
TCLAP::ValuesConstraint::_typeDesc
std::string _typeDesc
The string used to describe the allowed values of this constraint.
Definition: ValuesConstraint.h:88
TCLAP::ValuesConstraint::check
virtual bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.
Definition: ValuesConstraint.h:111
TCLAP::ValuesConstraint::description
virtual std::string description() const
Returns a description of the Constraint.
Definition: ValuesConstraint.h:126
TCLAP::ValuesConstraint::~ValuesConstraint
virtual ~ValuesConstraint()
Virtual destructor.
Definition: ValuesConstraint.h:59
TCLAP::ValuesConstraint::_allowed
std::vector< T > _allowed
The list of valid values.
Definition: ValuesConstraint.h:83
TCLAP::ValuesConstraint::shortID
virtual std::string shortID() const
Returns the short ID for the Constraint.
Definition: ValuesConstraint.h:120
TCLAP
Definition: Arg.h:48
TCLAP::ostringstream
std::ostringstream ostringstream
Definition: sstream.h:38
sstream.h