appkit  1.5.1
RoadNarrows Robotics Application Kit
Token.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: Token.h
10 //
11 /*! \file
12  *
13  * \brief Simple, token container class 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_TOKEN_H
57 #define _RNR_TOKEN_H
58 
59 #include <stdlib.h>
60 
61 #include <iostream>
62 #include <string>
63 #include <vector>
64 
65 #include "rnr/appkit/LogBook.h"
66 
67 /*!
68  * \brief RoadNarrows Robotics
69  */
70 namespace rnr
71 {
72  /*!
73  * \brief Commands
74  */
75  namespace cmd
76  {
77  //--------------------------------------------------------------------------
78  // Token Class
79  //--------------------------------------------------------------------------
80 
81  /*!
82  * \brief Parsed token container class.
83  */
84  class Token
85  {
86  public:
87  std::string m_strValue; ///< token string value
88  size_t m_lineNum; ///< line number
89  size_t m_posStart; ///< line start position of token
90  size_t m_posEnd; ///< line end position of token
91 
92  /*!
93  * \brief Default constructor.
94  */
95  Token();
96 
97  /*!
98  * \brief Initialization constructor.
99  *
100  * \param strValue Token value.
101  */
102  Token(const std::string &strValue);
103 
104  /*!
105  * \brief Initialization constructor.
106  *
107  * \param strValue Token value.
108  * \param lineNum Line number. Set to 0 if unknown or not associated
109  * with I/O input.
110  * \param posStart Token start position in line.
111  * \param posEnd Token end position in line.
112  */
113  Token(const std::string &strValue,
114  const size_t lineNum,
115  const size_t posStart,
116  const size_t posEnd);
117 
118  /*!
119  * \brief Copy constructor.
120  *
121  * \param src Source object.
122  */
123  Token(const Token &src);
124 
125  /*!
126  * \brief Destructor.
127  */
128  virtual ~Token();
129 
130  /*!
131  * \brief Assignment operator.
132  *
133  * \param rhs Right-hand side object.
134  *
135  * \return *this
136  */
137  Token &operator=(const Token &rhs);
138 
139 
140  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
141  // Attribute Methods
142  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
143 
144  /*!
145  * \brief Return token string.
146  *
147  * \return String.
148  */
149  const std::string &value() const
150  {
151  return m_strValue;
152  }
153 
154  /*!
155  * \brief Return input line number where token was located.
156  *
157  * \return Line number.
158  */
159  const size_t linenum() const
160  {
161  return m_lineNum;
162  }
163 
164  /*!
165  * \brief Return input line position where token was located.
166  *
167  * \param [out] posStart Starting character position of token.
168  * \param [out] posEnd Ending character position of token.
169  */
170  void position(size_t &posStart, size_t &posEnd) const
171  {
172  posStart = m_posStart;
173  posEnd = m_posEnd;
174  }
175 
176 
177  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
178  // Output Methods and Operators
179  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
180 
181  /*!
182  * \brief Output annotated token embedded location in the input line.
183  *
184  * \par Output:
185  * [linenum] line of text where <b>token</b> was generated
186  *
187  * \param os Output stream.
188  * \param strLine Line whence token was generated.
189  * \param bLoc If true, then include line number and token characater
190  * positions.
191  *
192  * \return Reference to output stream.
193  */
194  std::ostream &printAnnotated(std::ostream &os,
195  const std::string &strLine,
196  const bool bLoc = false);
197 
198  /*!
199  * \brief Insert object into output stream.
200  *
201  * \param os Output stream.
202  * \param tok Object to insert.
203  *
204  * \return Reference to output stream.
205  */
206  friend std::ostream &operator<<(std::ostream &os, const Token &tok);
207 
208  /*!
209  * \brief Insert object into LogBook pending entry.
210  *
211  * \param log LogBook stream.
212  * \param tok Object to insert.
213  *
214  * \return Reference to LogBook.
215  */
216  friend LogBook &operator<<(LogBook &log, const Token &tok);
217  }; // class Token
218 
219  //
220  // Types
221  //
222  typedef std::vector<Token> TokenVec; ///< vector of tokens type
223 
224  } // namespace cmd
225 } // namespace rnr
226 
227 #endif // _RNR_TOKEN_H
virtual ~Token()
Destructor.
Definition: Token.cxx:112
void position(size_t &posStart, size_t &posEnd) const
Return input line position where token was located.
Definition: Token.h:170
const size_t linenum() const
Return input line number where token was located.
Definition: Token.h:159
size_t m_lineNum
line number
Definition: Token.h:88
size_t m_posEnd
line end position of token
Definition: Token.h:90
size_t m_posStart
line start position of token
Definition: Token.h:89
std::vector< Token > TokenVec
vector of tokens type
Definition: Token.h:222
std::string m_strValue
token string value
Definition: Token.h:87
const std::string & value() const
Return token string.
Definition: Token.h:149
LogBook class interface.
Token()
Default constructor.
Definition: Token.cxx:81
Token & operator=(const Token &rhs)
Assignment operator.
Definition: Token.cxx:116
Parsed token container class.
Definition: Token.h:84
RoadNarrows Robotics.
Definition: Camera.h:74
friend std::ostream & operator<<(std::ostream &os, const Token &tok)
Insert object into output stream.
std::ostream & printAnnotated(std::ostream &os, const std::string &strLine, const bool bLoc=false)
Output annotated token embedded location in the input line.
Definition: Token.cxx:126