VR-Link API Documentation for HLA 1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
61  bool myDefault;
62 
63  public:
64 
77  SwitchArg(const std::string& flag,
78  const std::string& name,
79  const std::string& desc,
80  bool def,
81  Visitor* v = NULL);
82 
83 
97  SwitchArg(const std::string& flag,
98  const std::string& name,
99  const std::string& desc,
100  bool def,
101  CmdLineInterface& parser,
102  Visitor* v = NULL);
103 
104 
113  virtual bool processArg(int* i, std::vector<std::string>& args);
114 
119  bool combinedSwitchesMatch(std::string& combined);
120 
124  bool getValue() const;
125 
129  bool getDefault() const;
130 
133  operator bool();
134 
135  };
136 
138  //BEGIN SwitchArg.cpp
140  inline SwitchArg::SwitchArg(const std::string& flag,
141  const std::string& name,
142  const std::string& desc,
143  bool _default,
144  Visitor* v )
145  : Arg(flag, name, desc, false, false, v)
146  , _value( _default )
147  , myDefault( _default )
148  { }
149 
150  inline SwitchArg::SwitchArg(const std::string& flag,
151  const std::string& name,
152  const std::string& desc,
153  bool _default,
154  CmdLineInterface& parser,
155  Visitor* v )
156  : Arg(flag, name, desc, false, false, v)
157  , _value( _default )
158  , myDefault( _default )
159  {
160  parser.add( this );
161  }
162 
163  inline bool SwitchArg::getValue() const { return _value; }
164 
165  inline bool SwitchArg::getDefault() const { return myDefault; }
166 
167  inline SwitchArg::operator bool() { return _value; }
168 
169  inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches )
170  {
172  if ( combinedSwitches[0] != Arg::flagStartString()[0] )
173  return false;
174 
176  if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) ==
178  return false;
179 
182  for ( unsigned int i = 1; i < combinedSwitches.length(); i++ )
183  if ( !_flag.empty() && combinedSwitches[i] == _flag[0] )
184  {
188  //combinedSwitches.erase(i,1);
189  combinedSwitches[i] = Arg::blankChar();
190  return true;
191  }
192 
194  return false;
195  }
196 
197 
198  inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
199  {
200  if ( _ignoreable && Arg::ignoreRest() )
201  return false;
202 
203  if ( argMatches( args[*i] ) || combinedSwitchesMatch( args[*i] ) )
204  {
208  bool ret = false;
209  if ( argMatches( args[*i] ) )
210  ret = true;
211 
212  if ( _alreadySet )
213  throw(CmdLineParseException("Argument already set!", toString()));
214 
215  _alreadySet = true;
216 
217  if ( myDefault == true )
218  _value = false;
219  else
220  _value = true;
221 
223 
224  return ret;
225  }
226  else
227  return false;
228  }
229 
231  //End SwitchArg.cpp
233 
234  } //namespace DtCmdLine
235 
236 #ifdef DtUSE_UTILITIES_NAMESPACE
237 }
238 #endif
239 
240 #endif

Document ID: Generated on Mon Feb 6 18:36:34 EST 2017 from SVN revision 173107
Copyright © 2005-2016 VT MÄK. All Rights Reserved (www.mak.com)