appkit  1.5.1
RoadNarrows Robotics Application Kit
CmdExtArg.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: ExtArg.h
10 //
11 /*! \file
12  *
13  * \brief Command line extended argument interface.
14  *
15  * \author Robin Knight (robin.knight@roadnarrows.com)
16  *
17  * \par Copyright
18  * \h_copy 2016-2017. RoadNarrows LLC.\n
19  * http://www.roadnarrows.com\n
20  * All Rights Reserved
21  *
22  * \par License:
23  * MIT
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Permission is hereby granted, without written agreement and without
29  * license or royalty fees, to use, copy, modify, and distribute this
30  * software and its documentation for any purpose, provided that
31  * (1) The above copyright notice and the following two paragraphs
32  * appear in all copies of the source code and (2) redistributions
33  * including binaries reproduces these notices in the supporting
34  * documentation. Substantial modifications to this software may be
35  * copyrighted by their authors and need not follow the licensing terms
36  * described here, provided that the new terms are clearly indicated in
37  * all files where they apply.
38  *
39  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
40  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
41  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
42  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
43  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
44  * THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
48  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
49  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  *
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 #ifndef _RNR_CMD_EXT_ARG_H
57 #define _RNR_CMD_EXT_ARG_H
58 
59 #include <unistd.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62 
63 #include <iostream>
64 #include <string>
65 #include <vector>
66 
67 #include "rnr/appkit/LogBook.h"
68 
69 /*!
70  * \brief RoadNarrows Robotics
71  */
72 namespace rnr
73 {
74  /*!
75  * \brief Commands
76  */
77  namespace cmd
78  {
79 
80  //--------------------------------------------------------------------------
81  // CmdExtArg Class
82  //--------------------------------------------------------------------------
83 
84  /*!
85  * \brief Command EXTended ARGument class holding parsed command context and
86  * the raw and converted argmument values.
87  *
88  * Extended arguments are an alternative command interface to the standard
89  * simple command string arguments.
90  */
91  class CmdExtArg
92  {
93  public:
94  /*!
95  * \brief Converted types.
96  */
97  enum CvtType
98  {
99  CvtTypeUndef, ///< unknown or uninitialized type
100  CvtTypeString, ///< string type
101  CvtTypeEnum, ///< index into literal enumeration list
102  CvtTypeBoolean, ///< boolean type
103  CvtTypeInteger, ///< integer type
104  CvtTypeFpn ///< floating-point number type
105  };
106 
107  /*!
108  * \brief Default constructor.
109  */
110  CmdExtArg();
111 
112  /*!
113  * \brief Initialization constructor.
114  *
115  * \param nCmdUid Command definition unique id.
116  * \param nFormIndex Form definition index.
117  * \param nArgIndex Argument definition index.
118  * \param nArgInstance Argument instance (FUTURE).
119  * \param strArg Source argument string.
120  */
121  CmdExtArg(const int &nCmdUid,
122  const int &nFormIndex,
123  const int &nArgIndex,
124  const int &nArgInstance,
125  const std::string &strArg);
126 
127  /*!
128  * \brief Copy constructor.
129  *
130  * \param src Source object.
131  */
132  CmdExtArg(const CmdExtArg &src);
133 
134  /*!
135  * \brief Destructor.
136  */
137  virtual ~CmdExtArg();
138 
139  /*!
140  * \brief Assignment operator.
141  *
142  * \param rhs Right-hand side object.
143  *
144  * \return *this
145  */
146  CmdExtArg &operator=(const CmdExtArg &rhs);
147 
148 
149  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
150  // Parsed Command Context Methods
151  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
152 
153  /*!
154  * \brief Get the argument's associated parsed command unique id.
155  *
156  * \return Unique id.
157  */
158  int uid() const { return m_nCmdUid; }
159 
160  /*!
161  * \brief Get the argument's associated parsed form definition index.
162  *
163  * \return Form index.
164  */
165  int formIndex() const { return m_nFormIndex; }
166 
167  /*!
168  * \brief Get the argument's associated parsed argument definition index.
169  *
170  * \return Argument index.
171  */
172  int argIndex() const { return m_nArgIndex; }
173 
174  /*!
175  * \brief Get the argument's instance number of (uid, formindex, argindex)
176  * parsed argument definition index.
177  *
178  * FUTURE
179  *
180  * \return Argument index.
181  */
182  int argInstance() const { return m_nArgInstance; }
183 
184 
185  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
186  // Argument Access Methods
187  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
188 
189  /*!
190  * \brief Get raw source argument string (or a pirate grunting).
191  *
192  * \return String.
193  */
194  const std::string &arg() const { return m_strArg; }
195 
196  /*!
197  * \brief Check if converted value is valid.
198  *
199  * \return Returns true or false.
200  */
201  bool isValid() const { return m_eCvtType != CvtTypeUndef; }
202 
203  /*!
204  * \brief Get the converted argument type.
205  *
206  * \return CmdExtArg::CvtType value.
207  */
208  CvtType type() const { return m_eCvtType; }
209 
210  /*!
211  * \brief Get the converted string value.
212  *
213  * \return Returns string.
214  */
215  const std::string &s() const { return m_strCvtVal; }
216 
217  /*!
218  * \brief Get the converted enumeration index value.
219  *
220  * \return Returns index.
221  */
222  long e() const { return m_lCvtVal; }
223 
224  /*!
225  * \brief Get the converted boolean value.
226  *
227  * \return Returns bool.
228  */
229  bool b() const { return m_bCvtVal; }
230 
231  /*!
232  * \brief Get the converted integer value.
233  *
234  * \return Returns long.
235  */
236  long i() const { return m_lCvtVal; }
237 
238  /*!
239  * \brief Get the converted floating-point number value.
240  *
241  * \return Returns double.
242  */
243  double f() const { return m_fCvtVal; }
244 
245 
246  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
247  // Comparison Operators
248  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
249 
250  /*!
251  * \{
252  *
253  * \brief Comparison operator this == rval.
254  *
255  * \param rval Rvalue object.
256  *
257  * \return Returns true if equal, false otherwise.
258  */
259  bool operator==(const CmdExtArg &rval) const;
260 
261  bool operator==(const std::string &rval) const;
262 
263  bool operator==(const char* const &rval) const;
264 
265  bool operator==(const bool &rval) const;
266 
267  bool operator==(const long &rval) const;
268 
269  bool operator==(const double &rval) const;
270  /*!
271  * \}
272  */
273 
274  /*!
275  * \{
276  *
277  * \brief Comparison operator this != rval.
278  *
279  * \param rval Rvalue object.
280  *
281  * \return Returns true if not equal, false otherwise.
282  */
283  bool operator!=(const CmdExtArg &rval) const { return !(*this == rval); }
284 
285  bool operator!=(const std::string &rval) const {return !(*this == rval);}
286 
287  bool operator!=(const char* const &rval) const {return !(*this == rval);}
288 
289  bool operator!=(const bool &rval) const { return !(*this == rval); }
290 
291  bool operator!=(const long &rval) const { return !(*this == rval); }
292 
293  bool operator!=(const double &rval) const { return !(*this == rval); }
294  /*!
295  * \}
296  */
297 
298 
299  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
300  // Output Methods and Operators
301  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
302 
303  /*!
304  * \brief Insert object into output stream.
305  *
306  * \param os Output stream.
307  * \param arg Object to insert.
308  *
309  * \return Reference to output stream.
310  */
311  friend std::ostream &operator<<(std::ostream &os, const CmdExtArg &arg);
312 
313  /*!
314  * \brief Insert object into LogBook pending entry.
315  *
316  * \param log LogBook stream.
317  * \param arg Object to insert.
318  *
319  * \return Reference to LogBook.
320  */
321  friend LogBook &operator<<(LogBook &log, const CmdExtArg &arg);
322 
323  //
324  // Friends
325  //
326  friend class CmdArgDef;
327 
328  protected:
329  // command context
330  int m_nCmdUid; ///< command definition unique id
331  int m_nFormIndex; ///< form definition index
332  int m_nArgIndex; ///< argument definition index
333  int m_nArgInstance; ///< argument instance (FUTURE)
334 
335  // raw value
336  std::string m_strArg; ///< argument unconverted raw value string
337 
338  // converted type and value
339  CvtType m_eCvtType; ///< converted type
340  std::string m_strCvtVal; ///< converted string value
341  bool m_bCvtVal; ///< converted boolean value
342  long m_lCvtVal; ///< converted integer/index value
343  double m_fCvtVal; ///< converted float-point number value
344 
345  /*!
346  * \brief Set the argument string value.
347  *
348  * \param strArg Source argument string.
349  */
350  void arg(const std::string &strArg)
351  {
352  m_strArg = strArg;
353  }
354 
355  /*!
356  * \brief Set the converted string value.
357  *
358  * \param strVal Value to set.
359  */
360  void s(const std::string &strVal);
361 
362  /*!
363  * \brief Set the converted index into literal enum list value.
364  *
365  * \note A literal has both a string and integer enumeration. So any
366  * relevant operators and methods supports both interface.
367  *
368  * \param eVal Value to set.
369  */
370  void e(const long eVal);
371 
372  /*!
373  * \brief Set the converted boolean value.
374  *
375  * \param bVal Value to set.
376  */
377  void b(const bool bVal);
378 
379  /*!
380  * \brief Set the converted integer value.
381  *
382  * \param lVal Value to set.
383  */
384  void i(const long lVal);
385 
386  /*!
387  * \brief Set the converted floating-point number value.
388  *
389  * \param fVal Value to set.
390  */
391  void f(const double fVal);
392  }; // class CmdExtArg
393 
394  //
395  // Types
396  //
397  typedef std::vector<CmdExtArg> CmdExtArgVec; ///< vector of ext args type
398 
399  } // namespace cmd
400 } // namespace rnr
401 
402 #endif // _RNR_CMD_EXT_ARG_H
CvtType m_eCvtType
converted type
Definition: CmdExtArg.h:339
std::string m_strArg
argument unconverted raw value string
Definition: CmdExtArg.h:336
Command EXTended ARGument class holding parsed command context and the raw and converted argmument va...
Definition: CmdExtArg.h:91
CvtType
Converted types.
Definition: CmdExtArg.h:97
const std::string & s() const
Get the converted string value.
Definition: CmdExtArg.h:215
unknown or uninitialized type
Definition: CmdExtArg.h:99
int m_nCmdUid
command definition unique id
Definition: CmdExtArg.h:330
int m_nArgIndex
argument definition index
Definition: CmdExtArg.h:332
CvtType type() const
Get the converted argument type.
Definition: CmdExtArg.h:208
int argInstance() const
Get the argument&#39;s instance number of (uid, formindex, argindex) parsed argument definition index...
Definition: CmdExtArg.h:182
int formIndex() const
Get the argument&#39;s associated parsed form definition index.
Definition: CmdExtArg.h:165
Command argument compiled definition class.
Definition: CmdArgDef.h:89
long i() const
Get the converted integer value.
Definition: CmdExtArg.h:236
std::string m_strCvtVal
converted string value
Definition: CmdExtArg.h:340
int uid() const
Get the argument&#39;s associated parsed command unique id.
Definition: CmdExtArg.h:158
double m_fCvtVal
converted float-point number value
Definition: CmdExtArg.h:343
int m_nArgInstance
argument instance (FUTURE)
Definition: CmdExtArg.h:333
LogBook class interface.
bool m_bCvtVal
converted boolean value
Definition: CmdExtArg.h:341
floating-point number type
Definition: CmdExtArg.h:104
CmdExtArg & operator=(const CmdExtArg &rhs)
Assignment operator.
Definition: CmdExtArg.cxx:127
bool b() const
Get the converted boolean value.
Definition: CmdExtArg.h:229
std::vector< CmdExtArg > CmdExtArgVec
vector of ext args type
Definition: CmdExtArg.h:397
bool isValid() const
Check if converted value is valid.
Definition: CmdExtArg.h:201
index into literal enumeration list
Definition: CmdExtArg.h:101
int m_nFormIndex
form definition index
Definition: CmdExtArg.h:331
virtual ~CmdExtArg()
Destructor.
Definition: CmdExtArg.cxx:123
const std::string & arg() const
Get raw source argument string (or a pirate grunting).
Definition: CmdExtArg.h:194
double f() const
Get the converted floating-point number value.
Definition: CmdExtArg.h:243
bool operator==(const CmdExtArg &rval) const
Comparison operator this == rval.
Definition: CmdExtArg.cxx:172
friend std::ostream & operator<<(std::ostream &os, const CmdExtArg &arg)
Insert object into output stream.
CmdExtArg()
Default constructor.
Definition: CmdExtArg.cxx:80
int argIndex() const
Get the argument&#39;s associated parsed argument definition index.
Definition: CmdExtArg.h:172
long e() const
Get the converted enumeration index value.
Definition: CmdExtArg.h:222
RoadNarrows Robotics.
Definition: Camera.h:74
bool operator!=(const CmdExtArg &rval) const
Comparison operator this != rval.
Definition: CmdExtArg.h:283
void arg(const std::string &strArg)
Set the argument string value.
Definition: CmdExtArg.h:350
long m_lCvtVal
converted integer/index value
Definition: CmdExtArg.h:342