$search
00001 00002 00003 /****************************************************************************** 00004 * 00005 * file: ValuesConstraint.h 00006 * 00007 * Copyright (c) 2005, Michael E. Smoot 00008 * All rights reverved. 00009 * 00010 * See the file COPYING in the top directory of this distribution for 00011 * more information. 00012 * 00013 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 00014 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00015 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00016 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00017 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00018 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00019 * DEALINGS IN THE SOFTWARE. 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 _typeDesc("") 00102 { 00103 for ( unsigned int i = 0; i < _allowed.size(); i++ ) 00104 { 00105 00106 #if defined(HAVE_SSTREAM) 00107 std::ostringstream os; 00108 #elif defined(HAVE_STRSTREAM) 00109 std::ostrstream os; 00110 #else 00111 #error "Need a stringstream (sstream or strstream) to compile!" 00112 #endif 00113 00114 os << _allowed[i]; 00115 00116 std::string temp( os.str() ); 00117 00118 if ( i > 0 ) 00119 _typeDesc += "|"; 00120 _typeDesc += temp; 00121 } 00122 } 00123 00124 template<class T> 00125 bool ValuesConstraint<T>::check( const T& val ) const 00126 { 00127 if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() ) 00128 return false; 00129 else 00130 return true; 00131 } 00132 00133 template<class T> 00134 std::string ValuesConstraint<T>::shortID() const 00135 { 00136 return _typeDesc; 00137 } 00138 00139 template<class T> 00140 std::string ValuesConstraint<T>::description() const 00141 { 00142 return _typeDesc; 00143 } 00144 00145 00146 } //namespace TCLAP 00147 #endif 00148