XorHandler.h

Go to the documentation of this file.
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 <tclap/Arg.h>
00027 #include <tclap/PrintSensibly.h>
00028 #include <string>
00029 #include <vector>
00030 #include <algorithm>
00031 #include <iostream>
00032 
00033 namespace TCLAP {
00034 
00039 class XorHandler
00040 {
00041      protected:
00042 
00046           std::vector< std::vector<Arg*> > _orList;
00047 
00048      public:
00049 
00053           XorHandler( ) {}
00054 
00059           void add( std::vector<Arg*>& ors );
00060                
00068           int check( const Arg* a );
00069 
00073           std::string shortUsage();
00074 
00079           void printLongUsage(std::ostream& os);
00080 
00086           bool contains( const Arg* a );
00087 
00088 };
00089 
00090 
00092 //BEGIN XOR.cpp
00094 inline void XorHandler::add( std::vector<Arg*>& ors )
00095 { 
00096      _orList.push_back( ors );
00097 }
00098 
00099 inline std::string XorHandler::shortUsage()
00100 {
00101         std::string out = "";
00102      for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00103      {
00104           out += " {";
00105           for ( ArgVectorIterator it = _orList[i].begin(); 
00106                               it != _orList[i].end(); it++ )
00107                out += (*it)->shortID() + "|";
00108 
00109           out[out.length()-1] = '}';
00110      }
00111 
00112      return out;
00113 }
00114 
00115 inline void XorHandler::printLongUsage( std::ostream& os )
00116 {
00117      for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00118      {
00119           for ( ArgVectorIterator it = _orList[i].begin(); 
00120                  it != _orList[i].end(); 
00121                  it++ )
00122           {
00123                spacePrint( os, (*it)->longID(), 75, 3, 3 );
00124                spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
00125 
00126                if ( it+1 != _orList[i].end() )
00127                     spacePrint(os, "-- OR --", 75, 9);
00128           }
00129           os << std::endl << std::endl;
00130      }
00131 }
00132 
00133 inline int XorHandler::check( const Arg* a ) 
00134 {
00135      // iterate over each XOR list
00136      for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00137      {
00138           // if the XOR list contains the arg..
00139           if ( find( _orList[i].begin(), _orList[i].end(), a ) != 
00140                        _orList[i].end() )
00141           {
00142                // go through and set each arg that is not a
00143                for ( ArgVectorIterator it = _orList[i].begin(); 
00144                       it != _orList[i].end(); 
00145                       it++ )  
00146                     if ( a != (*it) )
00147                          (*it)->xorSet();
00148 
00149                // return the number of required args that have now been set
00150                return (int)_orList[i].size();
00151           }
00152      }
00153 
00154      if ( a->isRequired() )
00155           return 1;
00156      else
00157           return 0;
00158 }
00159 
00160 inline bool XorHandler::contains( const Arg* a )
00161 {
00162      for ( int i = 0; (unsigned int)i < _orList.size(); i++ )
00163           for ( ArgVectorIterator it = _orList[i].begin(); 
00164                  it != _orList[i].end(); 
00165                  it++ )  
00166                if ( a == (*it) )
00167                     return true;
00168 
00169      return false;
00170 }
00172 //END XOR.cpp
00174 
00175 } //namespace TCLAP
00176 
00177 #endif 

Generated on Sun Mar 11 15:33:24 2007 for MetalWarriors by  doxygen 1.4.7