File: mpIToken.cpp

package info (click to toggle)
muparserx 4.0.12-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: cpp: 10,953; ansic: 28; makefile: 8
file content (325 lines) | stat: -rw-r--r-- 10,079 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
               __________                                 ____  ___
    _____  __ _\______   \_____ _______  ______ __________\   \/  /
   /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     / 
  |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \ 
  |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
        \/                     \/           \/     \/           \_/
                                       Copyright (C) 2016 Ingo Berg
                                       All rights reserved.

  muParserX - A C++ math parser library with array and string support
  Copyright (c) 2016, Ingo Berg
  All rights reserved.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright notice, 
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice, 
     this list of conditions and the following disclaimer in the documentation 
     and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
*/
#include "mpIToken.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <cassert>

#include "mpIPrecedence.h"

MUP_NAMESPACE_START

#ifdef MUP_LEAKAGE_REPORT
  std::list<IToken*> IToken::s_Tokens;
#endif

#ifndef MUP_USE_WIDE_STRING

  //---------------------------------------------------------------------------
  /** \brief Overloaded streaming operator for outputting the value type 
             into an std::ostream. 
      \param a_Stream The stream object
      \param a_Val The value object to be streamed

    This function is only present if MUP_USE_WIDE_STRING is not defined.
  */
  std::ostream& operator<<(std::ostream &a_Stream, const IToken &tok)
  {
    return a_Stream << tok.ToString();
  }  

#else

  //---------------------------------------------------------------------------
  /** \brief Overloaded streaming operator for outputting the value type 
             into an std::ostream. 
      \param a_Stream The stream object
      \param a_Val The value object to be streamed

    This function is only present if MUP_USE_WIDE_STRING is defined.
  */
  std::wostream& operator<<(std::wostream &a_Stream, const IToken &tok)
  {
    return a_Stream << tok.ToString();
  }  
#endif

#ifdef MUP_LEAKAGE_REPORT

  void IToken::LeakageReport()
  {
    using namespace std;

    console() << "\n";     
    console() << "Memory leakage report:\n\n";     
    if (IToken::s_Tokens.size())
    {
      list<IToken*>::const_iterator item = IToken::s_Tokens.begin();
      std::vector<int> stat(cmCOUNT, 0);
      for (; item!=IToken::s_Tokens.end(); ++item)
      {
        console() << "Addr: 0x" << hex << *item << "  Ident: \"" << (*item)->GetIdent() << "\"";
        console() << "\tCode: " << g_sCmdCode[(*item)->GetCode()] << "\n"; 
        stat[(*item)->GetCode()]++;
      }
      console() << "Leaked tokens: " << dec << (int)IToken::s_Tokens.size() << std::endl;
      
      for (int i=0; i<cmCOUNT; ++i)
        console() << g_sCmdCode[i] << ":" << stat[i] << "\n";

      console() << endl;
    }
    else
    {
      console() << "No tokens leaked!\n";
    }
  }

#endif

  //------------------------------------------------------------------------------
  IToken::IToken(ECmdCode a_iCode)
    :m_eCode(a_iCode)
    ,m_sIdent()
    ,m_nPosExpr(-1)
    ,m_nRefCount(0)
    ,m_flags(0)
  {
#ifdef MUP_LEAKAGE_REPORT
    IToken::s_Tokens.push_back(this);
#endif
  }

  //------------------------------------------------------------------------------
  IToken::IToken(ECmdCode a_iCode, string_type a_sIdent)
    :m_eCode(a_iCode)
    ,m_sIdent(a_sIdent)
    ,m_nPosExpr(-1)
    ,m_nRefCount(0)
    ,m_flags(0)
  {
#ifdef MUP_LEAKAGE_REPORT
    IToken::s_Tokens.push_back(this);
#endif
  }

  /** \brief Destructor (trivial). */
  IToken::~IToken()
  {
#ifdef MUP_LEAKAGE_REPORT
    std::list<IToken*>::iterator it = std::find(IToken::s_Tokens.begin(), IToken::s_Tokens.end(), this);
    IToken::s_Tokens.remove(this);
#endif
  }

  //------------------------------------------------------------------------------
  /** \brief Copy constructor. 
      \param ref The token to copy basic state information from.

    The copy constructor must be implemented in order not to screw up
    the reference count of the created object. CC's are used in the
    Clone function and they would start with a reference count != 0 
    introducing memory leaks if the default CC where used.
  */
  IToken::IToken(const IToken &ref)
  {
    m_eCode  = ref.m_eCode;
    m_sIdent = ref.m_sIdent;
    m_flags  = ref.m_flags;
    m_nPosExpr = ref.m_nPosExpr;

    // The following items must be initialised 
    // (rather than just beeing copied)
    m_nRefCount = 0;
  }

  //------------------------------------------------------------------------------
  void IToken::ResetRef()
  {
    m_nRefCount = 0;
  }

  //------------------------------------------------------------------------------
  void IToken::Release()
  {
    delete this;
  }

  //------------------------------------------------------------------------------
  string_type IToken::ToString() const
  {
    return AsciiDump();
  }

  //------------------------------------------------------------------------------
  int IToken::GetExprPos() const
  {
    return m_nPosExpr;
  }

  //------------------------------------------------------------------------------
  void IToken::SetExprPos(int nPos)
  {
    m_nPosExpr = nPos;
  }

  //------------------------------------------------------------------------------
  /** \brief return the token code. 
     
      \sa ECmdCode
  */
  ECmdCode IToken::GetCode() const
  {
    return m_eCode;
  }

  //------------------------------------------------------------------------------
  /** \brief Return the token identifier string. */
  const string_type& IToken::GetIdent() const
  {
    return m_sIdent;
  }

  //------------------------------------------------------------------------------
  void IToken::SetIdent(const string_type &a_sIdent)
  {
    m_sIdent = a_sIdent;
  }

  //------------------------------------------------------------------------------
  string_type IToken::AsciiDump() const
  {
    stringstream_type ss;
    ss << g_sCmdCode[m_eCode];
    return ss.str().c_str();
  }

  //------------------------------------------------------------------------------
  void IToken::IncRef() const
  {
    ++m_nRefCount;
  }

  //------------------------------------------------------------------------------
  long IToken::DecRef() const
  {
    return --m_nRefCount;
  }

  //------------------------------------------------------------------------------
  long IToken::GetRef() const
  {
    return m_nRefCount;
  }

  //---------------------------------------------------------------------------
  void IToken::AddFlags(int flags)
  {
    m_flags |= flags;
  }

  //---------------------------------------------------------------------------
  bool IToken::IsFlagSet(int flags) const
  {
    return (m_flags & flags)==flags;
  }

  //---------------------------------------------------------------------------
  ICallback* IToken::AsICallback()
  {
    return nullptr;
  }

  //---------------------------------------------------------------------------
  IValue* IToken::AsIValue()
  {
    return nullptr;
  }

  //---------------------------------------------------------------------------
  IPrecedence* IToken::AsIPrecedence()
  {
    return nullptr;
  }

  //------------------------------------------------------------------------------
  void IToken::Compile(const string_type & /*sArg*/)
  {
  }

  //---------------------------------------------------------------------------
  //
  // Generic token implementation
  //
  //---------------------------------------------------------------------------

  GenericToken::GenericToken(ECmdCode a_iCode, string_type a_sIdent)
    :IToken(a_iCode, a_sIdent)
  {}

  //---------------------------------------------------------------------------
  GenericToken::GenericToken(ECmdCode a_iCode)
    :IToken(a_iCode, _T(""))
  {}

  //---------------------------------------------------------------------------
  GenericToken::~GenericToken()
  {}

  //---------------------------------------------------------------------------
  GenericToken::GenericToken(const GenericToken &a_Tok)
    :IToken(a_Tok)
  {}

  //---------------------------------------------------------------------------
  IToken* GenericToken::Clone() const
  {
    return new GenericToken(*this);
  }
  
  //------------------------------------------------------------------------------
  string_type GenericToken::AsciiDump() const
  {
    stringstream_type ss;

    ss << g_sCmdCode[ GetCode() ];
    ss << _T(" [addr=0x") << std::hex << this << _T("]");

    return ss.str();
  }

MUP_NAMESPACE_END