OpcUaCanOpen
CANopen OPC-UA server
muParserToken.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2004-2013 Ingo Berg
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy of this
11  software and associated documentation files (the "Software"), to deal in the Software
12  without restriction, including without limitation the rights to use, copy, modify,
13  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14  permit persons to whom the Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in all copies or
17  substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 #ifndef MU_PARSER_TOKEN_H
27 #define MU_PARSER_TOKEN_H
28 
29 #include <cassert>
30 #include <string>
31 #include <stack>
32 #include <vector>
33 #include <memory>
34 
35 #include "muParserError.h"
36 #include "muParserCallback.h"
37 
42 namespace mu
43 {
60  template<typename TBase, typename TString>
62  {
63  private:
64 
67  void *m_pTok;
68  int m_iIdx;
69  TString m_strTok;
70  TString m_strVal;
72  std::unique_ptr<ParserCallback> m_pCallback;
73 
74  public:
75 
76  //---------------------------------------------------------------------------
85  ,m_iType(tpVOID)
86  ,m_pTok(0)
87  ,m_iIdx(-1)
88  ,m_strTok()
89  ,m_strVal()
90  ,m_fVal(0)
91  ,m_pCallback()
92  {}
93 
94  //------------------------------------------------------------------------------
102  ParserToken(const ParserToken &a_Tok)
103  {
104  Assign(a_Tok);
105  }
106 
107  //------------------------------------------------------------------------------
115  {
116  Assign(a_Tok);
117  return *this;
118  }
119 
120  //------------------------------------------------------------------------------
125  void Assign(const ParserToken &a_Tok)
126  {
127  m_iCode = a_Tok.m_iCode;
128  m_pTok = a_Tok.m_pTok;
129  m_strTok = a_Tok.m_strTok;
130  m_iIdx = a_Tok.m_iIdx;
131  m_strVal = a_Tok.m_strVal;
132  m_iType = a_Tok.m_iType;
133  m_fVal = a_Tok.m_fVal;
134  // create new callback object if a_Tok has one
135  m_pCallback.reset(a_Tok.m_pCallback.get() ? a_Tok.m_pCallback->Clone() : 0);
136  }
137 
138  //------------------------------------------------------------------------------
149  ParserToken& Set(ECmdCode a_iType, const TString &a_strTok=TString())
150  {
151  // The following types can't be set this way, they have special Set functions
152  assert(a_iType!=cmVAR);
153  assert(a_iType!=cmVAL);
154  assert(a_iType!=cmFUNC);
155 
156  m_iCode = a_iType;
157  m_iType = tpVOID;
158  m_pTok = 0;
159  m_strTok = a_strTok;
160  m_iIdx = -1;
161 
162  return *this;
163  }
164 
165  //------------------------------------------------------------------------------
167  ParserToken& Set(const ParserCallback &a_pCallback, const TString &a_sTok)
168  {
169  assert(a_pCallback.GetAddr());
170 
171  m_iCode = a_pCallback.GetCode();
172  m_iType = tpVOID;
173  m_strTok = a_sTok;
174  m_pCallback.reset(new ParserCallback(a_pCallback));
175 
176  m_pTok = 0;
177  m_iIdx = -1;
178 
179  return *this;
180  }
181 
182  //------------------------------------------------------------------------------
188  ParserToken& SetVal(TBase a_fVal, const TString &a_strTok=TString())
189  {
190  m_iCode = cmVAL;
191  m_iType = tpDBL;
192  m_fVal = a_fVal;
193  m_strTok = a_strTok;
194  m_iIdx = -1;
195 
196  m_pTok = 0;
197  m_pCallback.reset(0);
198 
199  return *this;
200  }
201 
202  //------------------------------------------------------------------------------
208  ParserToken& SetVar(TBase *a_pVar, const TString &a_strTok)
209  {
210  m_iCode = cmVAR;
211  m_iType = tpDBL;
212  m_strTok = a_strTok;
213  m_iIdx = -1;
214  m_pTok = (void*)a_pVar;
215  m_pCallback.reset(0);
216  return *this;
217  }
218 
219  //------------------------------------------------------------------------------
225  ParserToken& SetString(const TString &a_strTok, std::size_t a_iSize)
226  {
227  m_iCode = cmSTRING;
228  m_iType = tpSTR;
229  m_strTok = a_strTok;
230  m_iIdx = static_cast<int>(a_iSize);
231 
232  m_pTok = 0;
233  m_pCallback.reset(0);
234  return *this;
235  }
236 
237  //------------------------------------------------------------------------------
244  void SetIdx(int a_iIdx)
245  {
246  if (m_iCode!=cmSTRING || a_iIdx<0)
248 
249  m_iIdx = a_iIdx;
250  }
251 
252  //------------------------------------------------------------------------------
260  int GetIdx() const
261  {
262  if (m_iIdx<0 || m_iCode!=cmSTRING )
264 
265  return m_iIdx;
266  }
267 
268  //------------------------------------------------------------------------------
275  {
276  if (m_pCallback.get())
277  {
278  return m_pCallback->GetCode();
279  }
280  else
281  {
282  return m_iCode;
283  }
284  }
285 
286  //------------------------------------------------------------------------------
288  {
289  if (m_pCallback.get())
290  {
291  return m_pCallback->GetType();
292  }
293  else
294  {
295  return m_iType;
296  }
297  }
298 
299  //------------------------------------------------------------------------------
300  int GetPri() const
301  {
302  if ( !m_pCallback.get())
304 
305  if ( m_pCallback->GetCode()!=cmOPRT_BIN && m_pCallback->GetCode()!=cmOPRT_INFIX)
307 
308  return m_pCallback->GetPri();
309  }
310 
311  //------------------------------------------------------------------------------
313  {
314  if (m_pCallback.get()==NULL || m_pCallback->GetCode()!=cmOPRT_BIN)
316 
317  return m_pCallback->GetAssociativity();
318  }
319 
320  //------------------------------------------------------------------------------
336  {
337  return (m_pCallback.get()) ? (generic_fun_type)m_pCallback->GetAddr() : 0;
338  }
339 
340  //------------------------------------------------------------------------------
346  TBase GetVal() const
347  {
348  switch (m_iCode)
349  {
350  case cmVAL: return m_fVal;
351  case cmVAR: return *((TBase*)m_pTok);
352  default: throw ParserError(ecVAL_EXPECTED);
353  }
354  }
355 
356  //------------------------------------------------------------------------------
362  TBase* GetVar() const
363  {
364  if (m_iCode!=cmVAR)
366 
367  return (TBase*)m_pTok;
368  }
369 
370  //------------------------------------------------------------------------------
375  int GetArgCount() const
376  {
377  assert(m_pCallback.get());
378 
379  if (!m_pCallback->GetAddr())
381 
382  return m_pCallback->GetArgc();
383  }
384 
385  //------------------------------------------------------------------------------
394  const TString& GetAsString() const
395  {
396  return m_strTok;
397  }
398  };
399 } // namespace mu
400 
401 #endif