UniversalIndentGUI 1.2.0
ValuesConstraint.h
Go to the documentation of this file.
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 { 
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 } //namespace TCLAP
00146 #endif 
00147 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines