3 /******************************************************************************
5 * file: ValuesConstraint.h
7 * Copyright (c) 2005, Michael E. Smoot
10 * See the file COPYING in the top directory of this distribution for
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.
21 *****************************************************************************/
23 #ifndef TCLAP_VALUESCONSTRAINT_H
24 #define TCLAP_VALUESCONSTRAINT_H
28 #include <tclap/Constraint.h>
36 #if defined(HAVE_SSTREAM)
38 #elif defined(HAVE_STRSTREAM)
41 #error "Need a stringstream (sstream or strstream) to compile!"
47 * A Constraint that constrains the Arg to only those values specified
51 class ValuesConstraint : public Constraint<T>
58 * \param allowed - vector of allowed values.
60 ValuesConstraint(std::vector<T>& allowed);
65 virtual ~ValuesConstraint() {}
68 * Returns a description of the Constraint.
70 virtual std::string description() const;
73 * Returns the short ID for the Constraint.
75 virtual std::string shortID() const;
78 * The method used to verify that the value parsed from the command
79 * line meets the constraint.
80 * \param value - The value that will be checked.
82 virtual bool check(const T& value) const;
87 * The list of valid values.
89 std::vector<T> _allowed;
92 * The string used to describe the allowed values of this constraint.
94 std::string _typeDesc;
99 ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
103 for ( unsigned int i = 0; i < _allowed.size(); i++ )
106 #if defined(HAVE_SSTREAM)
107 std::ostringstream os;
108 #elif defined(HAVE_STRSTREAM)
111 #error "Need a stringstream (sstream or strstream) to compile!"
116 std::string temp( os.str() );
125 bool ValuesConstraint<T>::check( const T& val ) const
127 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
134 std::string ValuesConstraint<T>::shortID() const
140 std::string ValuesConstraint<T>::description() const