VR-Link API Documentation for HLA 1516
cmdSwitchArg.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: SwitchArg.h
5  *
6  * Copyright (c) 2003, Michael E. Smoot .
7  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8  * All rights reverved.
9  *
10  * See the file COPYING in the top directory of this distribution for
11  * more information.
12  *
13  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19  * DEALINGS IN THE SOFTWARE.
20  *
21  *****************************************************************************/
22 
23 
24 #ifndef DtCmdLine_SWITCH_ARG_H
25 #define DtCmdLine_SWITCH_ARG_H
26 
30 
31 #include <string>
32 #include <vector>
33 
34 #include "cmdArg.h"
35 
36 #ifdef DtUSE_UTILITIES_NAMESPACE
37 namespace DtUSE_UTILITIES_NAMESPACE
38 {
39 #endif
40 
41  namespace DtCmdLine {
42 
48 
49  class SwitchArg : public Arg
50  {
51  protected:
52 
56  bool _value;
57 
58 
59  public:
60 
73  SwitchArg(const std::string& flag,
74  const std::string& name,
75  const std::string& desc,
76  bool def,
77  Visitor* v = NULL);
78 
79 
93  SwitchArg(const std::string& flag,
94  const std::string& name,
95  const std::string& desc,
96  bool def,
97  CmdLineInterface& parser,
98  Visitor* v = NULL);
99 
100 
109  virtual bool processArg(int* i, std::vector<std::string>& args);
110 
115  bool combinedSwitchesMatch(std::string& combined);
116 
120  bool getValue() const;
121 
124  operator bool();
125 
126  };
127 
129  //BEGIN SwitchArg.cpp
131  inline SwitchArg::SwitchArg(const std::string& flag,
132  const std::string& name,
133  const std::string& desc,
134  bool _default,
135  Visitor* v )
136  : Arg(flag, name, desc, false, false, v),
137  _value( _default )
138  { }
139 
140  inline SwitchArg::SwitchArg(const std::string& flag,
141  const std::string& name,
142  const std::string& desc,
143  bool _default,
144  CmdLineInterface& parser,
145  Visitor* v )
146  : Arg(flag, name, desc, false, false, v),
147  _value( _default )
148  {
149  parser.add( this );
150  }
151 
152  inline bool SwitchArg::getValue() const { return _value; }
153 
154  inline SwitchArg::operator bool() { return _value; }
155 
156  inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
157  {
159  if ( combinedSwitches[0] != Arg::flagStartString()[0] )
160  return false;
161 
163  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
165  return false;
166 
169  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
170  if ( !_flag.empty() && combinedSwitches[i] == _flag[0] )
171  {
175  //combinedSwitches.erase(i,1);
176  combinedSwitches[i] = Arg::blankChar();
177  return true;
178  }
179 
181  return false;
182  }
183 
184 
185  inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
186  {
187  if ( _ignoreable && Arg::ignoreRest() )
188  return false;
189 
190  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
191  {
195  bool ret = false;
196  if ( argMatches( args[*i] ) )
197  ret = true;
198 
199  if ( _alreadySet )
200  throw(CmdLineParseException("Argument already set!", toString()));
201 
202  _alreadySet = true;
203 
204  if ( _value == true )
205  _value = false;
206  else
207  _value = true;
208 
210 
211  return ret;
212  }
213  else
214  return false;
215  }
216 
218  //End SwitchArg.cpp
220 
221  } //namespace DtCmdLine
222 
223 #ifdef DtUSE_UTILITIES_NAMESPACE
224 }
225 #endif
226 
227 #endif

Document ID: Generated on Wed Oct 23 22:25:48 EDT 2013 from SVN revision 132765
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)