VR-Link API Documentation for DIS
cmdSwitchArg.h
Go to the documentation of this file.
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  file:  SwitchArg.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 
00024 #ifndef DtCmdLine_SWITCH_ARG_H
00025 #define DtCmdLine_SWITCH_ARG_H
00026 
00030 
00031 #include <string>
00032 #include <vector>
00033 
00034 #include "cmdArg.h"
00035 
00036 #ifdef DtUSE_UTILITIES_NAMESPACE
00037 namespace DtUSE_UTILITIES_NAMESPACE
00038 {
00039 #endif
00040 
00041    namespace DtCmdLine {
00042 
00048 
00049       class SwitchArg : public Arg
00050       {
00051       protected:
00052 
00056          bool _value;
00057 
00058 
00059       public:
00060 
00073          SwitchArg(const std::string& flag, 
00074                    const std::string& name, 
00075                    const std::string& desc,
00076                    bool def,
00077                    Visitor* v = NULL);
00078 
00079 
00093          SwitchArg(const std::string& flag, 
00094                    const std::string& name, 
00095                    const std::string& desc,
00096                    bool def,
00097                    CmdLineInterface& parser,
00098                    Visitor* v = NULL);
00099 
00100 
00109          virtual bool processArg(int* i, std::vector<std::string>& args); 
00110 
00115          bool combinedSwitchesMatch(std::string& combined);
00116 
00120          bool getValue() const;
00121 
00124          operator bool();
00125 
00126       };
00127 
00129       //BEGIN SwitchArg.cpp
00131       inline SwitchArg::SwitchArg(const std::string& flag, 
00132                                   const std::string& name, 
00133                                   const std::string& desc, 
00134                                   bool _default,
00135                                   Visitor* v )
00136          : Arg(flag, name, desc, false, false, v),
00137          _value( _default )
00138       { }
00139 
00140       inline SwitchArg::SwitchArg(const std::string& flag, 
00141                                   const std::string& name, 
00142                                   const std::string& desc, 
00143                                   bool _default,
00144                                   CmdLineInterface& parser,
00145                                   Visitor* v )
00146          : Arg(flag, name, desc, false, false, v),
00147          _value( _default )
00148       { 
00149          parser.add( this );
00150       }
00151 
00152       inline bool SwitchArg::getValue() const { return _value; }
00153 
00154       inline SwitchArg::operator bool() { return _value; }
00155 
00156       inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
00157       {
00159          if ( combinedSwitches[0] != Arg::flagStartString()[0] )
00160             return false;
00161 
00163          if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 
00164               Arg::nameStartString() )
00165             return false;
00166 
00169          for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
00170             if ( !_flag.empty() && combinedSwitches[i] == _flag[0] ) 
00171             {
00175                //combinedSwitches.erase(i,1);
00176                combinedSwitches[i] = Arg::blankChar(); 
00177                return true;
00178             }
00179 
00181          return false;  
00182       }
00183 
00184 
00185       inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
00186       {
00187          if ( _ignoreable && Arg::ignoreRest() )
00188             return false;
00189 
00190          if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
00191          {
00195             bool ret = false;
00196             if ( argMatches( args[*i] ) )
00197                ret = true;
00198 
00199             if ( _alreadySet )
00200                throw(CmdLineParseException("Argument already set!", toString()));   
00201 
00202             _alreadySet = true;
00203 
00204             if ( _value == true )
00205                _value = false;
00206             else
00207                _value = true;
00208 
00209             _checkWithVisitor();
00210 
00211             return ret;
00212          }
00213          else
00214             return false;
00215       }
00216 
00218       //End SwitchArg.cpp
00220 
00221    } //namespace DtCmdLine
00222 
00223 #ifdef DtUSE_UTILITIES_NAMESPACE
00224 }     
00225 #endif
00226 
00227 #endif

Document ID: Generated on Mon May 14 08:06:18 EDT 2012 from SVN revision 114750
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)