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 <sbia/tclap/Constraint.h>
00029
00030 #include <sbia/basis/config.h>
00031
00032 #if defined(HAVE_SSTREAM)
00033 #include <sstream>
00034 #elif defined(HAVE_STRSTREAM)
00035 #include <strstream>
00036 #else
00037 #error "Need a stringstream (sstream or strstream) to compile!"
00038 #endif
00039
00040 namespace TCLAP {
00041
00042
00043
00044
00045
00046 template<class T>
00047 class ValuesConstraint : public Constraint<T>
00048 {
00049
00050 public:
00051
00052
00053
00054
00055
00056 ValuesConstraint(std::vector<T>& allowed);
00057
00058
00059
00060
00061 virtual ~ValuesConstraint() {}
00062
00063
00064
00065
00066 virtual std::string description() const;
00067
00068
00069
00070
00071 virtual std::string shortID() const;
00072
00073
00074
00075
00076
00077
00078 virtual bool check(const T& value) const;
00079
00080 protected:
00081
00082
00083
00084
00085 std::vector<T> _allowed;
00086
00087
00088
00089
00090 std::string _typeDesc;
00091
00092 };
00093
00094 template<class T>
00095 ValuesConstraint<T>::ValuesConstraint(std::vector<T>& allowed)
00096 : _allowed(allowed),
00097 _typeDesc("")
00098 {
00099 for ( unsigned int i = 0; i < _allowed.size(); i++ )
00100 {
00101
00102 #if defined(HAVE_SSTREAM)
00103 std::ostringstream os;
00104 #elif defined(HAVE_STRSTREAM)
00105 std::ostrstream os;
00106 #else
00107 #error "Need a stringstream (sstream or strstream) to compile!"
00108 #endif
00109
00110 os << _allowed[i];
00111
00112 std::string temp( os.str() );
00113
00114 if ( i > 0 )
00115 _typeDesc += "|";
00116 _typeDesc += temp;
00117 }
00118 }
00119
00120 template<class T>
00121 bool ValuesConstraint<T>::check( const T& val ) const
00122 {
00123 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() )
00124 return false;
00125 else
00126 return true;
00127 }
00128
00129 template<class T>
00130 std::string ValuesConstraint<T>::shortID() const
00131 {
00132 return _typeDesc;
00133 }
00134
00135 template<class T>
00136 std::string ValuesConstraint<T>::description() const
00137 {
00138 return _typeDesc;
00139 }
00140
00141
00142 }
00143 #endif
00144