00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef TCLAP_VALUESCONSTRAINT_H
00024 #define TCLAP_VALUESCONSTRAINT_H
00025
00026 #include <string>
00027 #include <vector>
00028 #include <tclap/Constraint.h>
00029
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #else
00033 #define HAVE_SSTREAM
00034 #endif
00035
00036 #if defined(HAVE_SSTREAM)
00037 #include <sstream>
00038 #elif defined(HAVE_STRSTREAM)
00039 #include <strstream>
00040 #else
00041 #error "Need a stringstream (sstream or strstream) to compile!"
00042 #endif
00043
00044 namespace TCLAP {
00045
00050 template<class T>
00051 class ValuesConstraint : public Constraint<T>
00052 {
00053
00054 public:
00055
00060 ValuesConstraint(std::vector<T>& allowed);
00061
00065 virtual ~ValuesConstraint() {}
00066
00070 virtual std::string description() const;
00071
00075 virtual std::string shortID() const;
00076
00082 virtual bool check(const T& value) const;
00083
00084 protected:
00085
00089 std::vector<T> _allowed;
00090
00094 std::string _typeDesc;
00095
00096 };
00097
00098 template<class T>
00099 ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
00100 : _allowed(allowed)
00101 {
00102 for ( unsigned int i = 0; i < _allowed.size(); i++ )
00103 {
00104
00105 #if defined(HAVE_SSTREAM)
00106 std::ostringstream os;
00107 #elif defined(HAVE_STRSTREAM)
00108 std::ostrstream os;
00109 #else
00110 #error "Need a stringstream (sstream or strstream) to compile!"
00111 #endif
00112
00113 os << _allowed[i];
00114
00115 std::string temp( os.str() );
00116
00117 if ( i > 0 )
00118 _typeDesc += "|";
00119 _typeDesc += temp;
00120 }
00121 }
00122
00123 template<class T>
00124 bool ValuesConstraint<T>::check( const T& val ) const
00125 {
00126 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
00127 return false;
00128 else
00129 return true;
00130 }
00131
00132 template<class T>
00133 std::string ValuesConstraint<T>::shortID() const
00134 {
00135 return _typeDesc;
00136 }
00137
00138 template<class T>
00139 std::string ValuesConstraint<T>::description() const
00140 {
00141 return _typeDesc;
00142 }
00143
00144
00145 }
00146 #endif
00147