tclap  1.2.2
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 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_VALUESCONSTRAINT_H
24 #define TCLAP_VALUESCONSTRAINT_H
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <string>
31 #include <vector>
32 #include <tclap/Constraint.h>
33 #include <tclap/sstream.h>
34 
35 namespace TCLAP {
36 
41 template<class T>
42 class ValuesConstraint : public Constraint<T>
43 {
44 
45  public:
46 
51  ValuesConstraint(std::vector<T>& allowed);
52 
56  virtual ~ValuesConstraint() {}
57 
61  virtual std::string description() const;
62 
66  virtual std::string shortID() const;
67 
73  virtual bool check(const T& value) const;
74 
75  protected:
76 
80  std::vector<T> _allowed;
81 
85  std::string _typeDesc;
86 
87 };
88 
89 template<class T>
90 ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
91 : _allowed(allowed),
92  _typeDesc("")
93 {
94  for ( unsigned int i = 0; i < _allowed.size(); i++ )
95  {
96 
97 #if defined(HAVE_SSTREAM)
99 #elif defined(HAVE_STRSTREAM)
100  std::ostrstream os;
101 #else
102 #error "Need a stringstream (sstream or strstream) to compile!"
103 #endif
104 
105  os << _allowed[i];
106 
107  std::string temp( os.str() );
108 
109  if ( i > 0 )
110  _typeDesc += "|";
111  _typeDesc += temp;
112  }
113 }
114 
115 template<class T>
116 bool ValuesConstraint<T>::check( const T& val ) const
117 {
118  if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
119  return false;
120  else
121  return true;
122 }
123 
124 template<class T>
125 std::string ValuesConstraint<T>::shortID() const
126 {
127  return _typeDesc;
128 }
129 
130 template<class T>
132 {
133  return _typeDesc;
134 }
135 
136 
137 } //namespace TCLAP
138 #endif
139 
A Constraint that constrains the Arg to only those values specified in the constraint.
ValuesConstraint(std::vector< T > &allowed)
Constructor.
virtual std::string description() const
Returns a description of the Constraint.
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:38
virtual std::string shortID() const
Returns the short ID for the Constraint.
std::vector< T > _allowed
The list of valid values.
std::ostringstream ostringstream
Definition: sstream.h:38
std::string _typeDesc
The string used to describe the allowed values of this constraint.
Definition: Arg.h:58
virtual bool check(const T &value) const
The method used to verify that the value parsed from the command line meets the constraint.
virtual ~ValuesConstraint()
Virtual destructor.