tclap 1.2.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ValuesConstraint.h
Go to the documentation of this file.
1
2
3 /******************************************************************************
4 *
5 * file: ValuesConstraint.h
6 *
7 * Copyright (c) 2005, Michael E. Smoot
8 * All rights reverved.
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_VALUESCONSTRAINT_H
24 #define TCLAP_VALUESCONSTRAINT_H
25
26 #include <string>
27 #include <vector>
28 #include <tclap/Constraint.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #else
33 #define HAVE_SSTREAM
34 #endif
35
36 #if defined(HAVE_SSTREAM)
37 #include <sstream>
38 #elif defined(HAVE_STRSTREAM)
39 #include <strstream>
40 #else
41 #error "Need a stringstream (sstream or strstream) to compile!"
42 #endif
43
44 namespace TCLAP {
45
50 template<class T>
51 class ValuesConstraint : public Constraint<T>
52 {
53
54 public:
55
60 ValuesConstraint(std::vector<T>& allowed);
61
65 virtual ~ValuesConstraint() {}
66
70 virtual std::string description() const;
71
75 virtual std::string shortID() const;
76
82 virtual bool check(const T& value) const;
83
84 protected:
85
89 std::vector<T> _allowed;
90
94 std::string _typeDesc;
95
96 };
97
98 template<class T>
99 ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
100 : _allowed(allowed),
101 _typeDesc("")
102 {
103 for ( unsigned int i = 0; i < _allowed.size(); i++ )
104 {
105
106 #if defined(HAVE_SSTREAM)
107 std::ostringstream os;
108 #elif defined(HAVE_STRSTREAM)
109 std::ostrstream os;
110 #else
111 #error "Need a stringstream (sstream or strstream) to compile!"
112 #endif
113
114 os << _allowed[i];
115
116 std::string temp( os.str() );
117
118 if ( i > 0 )
119 _typeDesc += "|";
120 _typeDesc += temp;
121 }
122 }
123
124 template<class T>
125 bool ValuesConstraint<T>::check( const T& val ) const
126 {
127 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
128 return false;
129 else
130 return true;
131 }
132
133 template<class T>
134 std::string ValuesConstraint<T>::shortID() const
135 {
136 return _typeDesc;
137 }
138
139 template<class T>
140 std::string ValuesConstraint<T>::description() const
141 {
142 return _typeDesc;
143 }
144
145
146 } //namespace TCLAP
147 #endif
148
TCLAP::ValuesConstraint
A Constraint that constrains the Arg to only those values specified in the constraint.
Definition: ValuesConstraint.h:51
TCLAP::ValuesConstraint::ValuesConstraint
ValuesConstraint(std::vector< T > &allowed)
Constructor.
Definition: ValuesConstraint.h:99
TCLAP::ValuesConstraint::description
virtual std::string description() const
Returns a description of the Constraint.
Definition: ValuesConstraint.h:140
TCLAP::Constraint
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:38
TCLAP::ValuesConstraint::shortID
virtual std::string shortID() const
Returns the short ID for the Constraint.
Definition: ValuesConstraint.h:134
TCLAP::ValuesConstraint::_allowed
std::vector< T > _allowed
The list of valid values.
Definition: ValuesConstraint.h:89
Constraint.h
TCLAP::ValuesConstraint::_typeDesc
std::string _typeDesc
The string used to describe the allowed values of this constraint.
Definition: ValuesConstraint.h:94
TCLAP
Definition: Arg.h:57
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:125
TCLAP::ValuesConstraint::~ValuesConstraint
virtual ~ValuesConstraint()
Virtual destructor.
Definition: ValuesConstraint.h:65