BASIS  version 1.2.3 (revision 2104)
XorHandler.h
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  file:  XorHandler.h
00005  * 
00006  *  Copyright (c) 2003, Michael E. Smoot .
00007  *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
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_XORHANDLER_H
00024 #define TCLAP_XORHANDLER_H
00025 
00026 #include <sbia/tclap/Arg.h>
00027 #include <string>
00028 #include <vector>
00029 #include <algorithm>
00030 #include <iostream>
00031 
00032 namespace TCLAP {
00033 
00034 /**
00035  * This class handles lists of Arg's that are to be XOR'd on the command
00036  * line.  This is used by CmdLine and you shouldn't ever use it.
00037  */
00038 class XorHandler
00039 {
00040     protected:
00041 
00042         /**
00043          * The list of of lists of Arg's to be or'd together.
00044          */
00045         std::vector< std::vector<Arg*> > _orList;
00046 
00047     public:
00048 
00049         /**
00050          * Constructor.  Does nothing.
00051          */
00052         XorHandler( ) : _orList(std::vector< std::vector<Arg*> >()) {}
00053 
00054         /**
00055          * Add a list of Arg*'s that will be orred together.
00056          * \param ors - list of Arg* that will be xor'd.
00057          */
00058         void add( std::vector<Arg*>& ors );
00059             
00060         /**
00061          * Checks whether the specified Arg is in one of the xor lists and
00062          * if it does match one, returns the size of the xor list that the
00063          * Arg matched.  If the Arg matches, then it also sets the rest of
00064          * the Arg's in the list. You shouldn't use this.  
00065          * \param a - The Arg to be checked.
00066          */
00067         int check( const Arg* a );
00068 
00069         /**
00070          * Returns the XOR specific short usage.
00071          */
00072         std::string shortUsage();
00073 
00074         /**
00075          * Prints the XOR specific long usage.
00076          * \param os - Stream to print to.
00077          */
00078         void printLongUsage(std::ostream& os);
00079 
00080         /**
00081          * Simply checks whether the Arg is contained in one of the arg
00082          * lists.
00083          * \param a - The Arg to be checked.
00084          */
00085         bool contains( const Arg* a );
00086 
00087         std::vector< std::vector<Arg*> >& getXorList(); 
00088 
00089 };
00090 
00091 
00092 //////////////////////////////////////////////////////////////////////
00093 //BEGIN XOR.cpp
00094 //////////////////////////////////////////////////////////////////////
00095 inline void XorHandler::add( std::vector<Arg*>& ors )
00096 { 
00097     _orList.push_back( ors );
00098 }
00099 
00100 inline int XorHandler::check( const Arg* a ) 
00101 {
00102     // iterate over each XOR list
00103     for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
00104     {
00105         // if the XOR list contains the arg..
00106         ArgVectorIterator ait = std::find( _orList[i].begin(), 
00107                                            _orList[i].end(), a );
00108         if ( ait != _orList[i].end() )
00109         {
00110             // first check to see if a mutually exclusive switch
00111             // has not already been set
00112             for ( ArgVectorIterator it = _orList[i].begin(); 
00113                   it != _orList[i].end(); 
00114                   it++ )
00115                 if ( a != (*it) && (*it)->isSet() )
00116                     throw(CmdLineParseException(
00117                           "Mutually exclusive argument already set!",
00118                           (*it)->toString()));
00119 
00120             // go through and set each arg that is not a
00121             for ( ArgVectorIterator it = _orList[i].begin(); 
00122                   it != _orList[i].end(); 
00123                   it++ )
00124                 if ( a != (*it) )
00125                     (*it)->xorSet();
00126 
00127             // return the number of required args that have now been set
00128             if ( (*ait)->allowMore() )
00129                 return 0;
00130             else
00131                 return static_cast<int>(_orList[i].size());
00132         }
00133     }
00134 
00135     if ( a->isRequired() )
00136         return 1;
00137     else
00138         return 0;
00139 }
00140 
00141 inline bool XorHandler::contains( const Arg* a )
00142 {
00143     for ( int i = 0; static_cast<unsigned int>(i) < _orList.size(); i++ )
00144         for ( ArgVectorIterator it = _orList[i].begin(); 
00145               it != _orList[i].end(); 
00146               it++ )    
00147             if ( a == (*it) )
00148                 return true;
00149 
00150     return false;
00151 }
00152 
00153 inline std::vector< std::vector<Arg*> >& XorHandler::getXorList() 
00154 {
00155     return _orList;
00156 }
00157 
00158 
00159 
00160 //////////////////////////////////////////////////////////////////////
00161 //END XOR.cpp
00162 //////////////////////////////////////////////////////////////////////
00163 
00164 } //namespace TCLAP
00165 
00166 #endif