BASIS  version 1.2.3 (revision 2104)
ValuesConstraint.h
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 <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  * A Constraint that constrains the Arg to only those values specified
00044  * in the constraint.
00045  */
00046 template<class T>
00047 class ValuesConstraint : public Constraint<T>
00048 {
00049 
00050     public:
00051 
00052         /**
00053          * Constructor. 
00054          * \param allowed - vector of allowed values. 
00055          */
00056         ValuesConstraint(std::vector<T>& allowed);  
00057 
00058         /**
00059          * Virtual destructor.
00060          */
00061         virtual ~ValuesConstraint() {}
00062 
00063         /**
00064          * Returns a description of the Constraint. 
00065          */
00066         virtual std::string description() const;
00067 
00068         /**
00069          * Returns the short ID for the Constraint.
00070          */
00071         virtual std::string shortID() const;
00072 
00073         /**
00074          * The method used to verify that the value parsed from the command
00075          * line meets the constraint.
00076          * \param value - The value that will be checked. 
00077          */
00078         virtual bool check(const T& value) const;
00079     
00080     protected:
00081 
00082         /**
00083          * The list of valid values. 
00084          */
00085         std::vector<T> _allowed;
00086 
00087         /**
00088          * The string used to describe the allowed values of this constraint.
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 } //namespace TCLAP
00143 #endif 
00144