Pol  Revision:cb584c9
token.cpp
Go to the documentation of this file.
1 
7 #include "token.h"
8 
9 #include <cstdio>
10 #include <cstring>
11 
12 namespace Pol
13 {
14 namespace Bscript
15 {
16 unsigned int Token::_instances = 0;
17 
18 #if STORE_INSTANCELIST
19 set<Token*> Token::_instancelist;
20 #endif
21 
22 unsigned int Token::instances()
23 {
24  return _instances;
25 }
27 {
28 #if STORE_INSTANCELIST
29  for ( Instances::iterator itr = _instancelist.begin(), end = _instancelist.end(); itr != end;
30  ++itr )
31  {
32  Token* tkn = ( *itr );
33  cout << tkn << ": " << ( *tkn ) << endl;
34  }
35 #endif
36 }
37 
39 {
40  ++_instances;
41 #if STORE_INSTANCELIST
42  _instancelist.insert( this );
43 #endif
44 }
45 
47 {
48 #if defined( _DEBUG ) && defined( _DBG_TRYING_TO_FIND_WIN32_SHUTDOWN_ASSERTION )
49  if ( exit_signalled )
50  {
51  cout << "TOK/unreginst: " << ( token ? token : "<unknown>" ) << endl;
52  printOn( cout );
53  cout << "----" << endl;
54  }
55 #endif
56  --_instances;
57 #if STORE_INSTANCELIST
58  _instancelist.erase( this );
59 #endif
60 }
61 
66  : id( TOK_TERM ),
68  dval( 0.0 ),
69  precedence( -1 ),
70  dbg_filenum( 0 ),
71  dbg_linenum( 0 ),
72  lval( 0 ),
73  userfunc( NULL ),
74  deprecated( false ),
75  ownsStr( false ),
76  module( Mod_Basic ),
77  token( NULL )
78 {
80 }
81 
85 Token::Token( const Token& tok )
86  : id( tok.id ),
87  type( tok.type ),
88  dval( tok.dval ),
89  precedence( tok.precedence ),
90  dbg_filenum( tok.dbg_filenum ),
91  dbg_linenum( tok.dbg_linenum ),
92  lval( tok.lval ),
93  userfunc( tok.userfunc ),
94  deprecated( tok.deprecated ),
95  ownsStr( false ),
96  module( tok.module ),
97  token( NULL )
98 {
100  if ( tok.token )
101  {
102  if ( !tok.ownsStr )
103  setStr( tok.token );
104  else
105  copyStr( tok.token );
106  }
107 }
108 
113 {
114  module = tok.module;
115  id = tok.id;
116  type = tok.type;
117  precedence = tok.precedence;
118  deprecated = tok.deprecated;
119 
120  nulStr();
121  ownsStr = false;
122  if ( tok.token )
123  {
124  if ( !tok.ownsStr )
125  setStr( tok.token );
126  else
127  copyStr( tok.token );
128  }
129  dval = tok.dval;
130  lval = tok.lval;
131 
132  dbg_filenum = tok.dbg_filenum;
133  dbg_linenum = tok.dbg_linenum;
134 
135  userfunc = tok.userfunc;
136 
137  return *this;
138 }
139 
140 Token::Token( ModuleID i_module, BTokenId i_id, BTokenType i_type )
141  : id( i_id ),
142  type( i_type ),
143  dval( 0.0 ),
144  precedence( -1 ),
145  dbg_filenum( 0 ),
146  dbg_linenum( 0 ),
147  lval( 0 ),
148  userfunc( NULL ),
149  deprecated( false ),
150  ownsStr( false ),
151  module( static_cast<unsigned char>( i_module ) ),
152  token( NULL )
153 {
155 }
156 
158  : id( i_id ),
159  type( i_type ),
160  dval( 0.0 ),
161  precedence( -1 ),
162  dbg_filenum( 0 ),
163  dbg_linenum( 0 ),
164  lval( 0 ),
165  userfunc( NULL ),
166  deprecated( false ),
167  ownsStr( false ),
168  module( Mod_Basic ),
169  token( NULL )
170 {
172 }
173 
174 Token::Token( ModuleID i_module, BTokenId i_id, BTokenType i_type, UserFunction* i_userfunc )
175  : id( i_id ),
176  type( i_type ),
177  dval( 0.0 ),
178  precedence( -1 ),
179  dbg_filenum( 0 ),
180  dbg_linenum( 0 ),
181  lval( 0 ),
182  userfunc( i_userfunc ),
183  deprecated( false ),
184  ownsStr( false ),
185  module( static_cast<unsigned char>( i_module ) ),
186  token( NULL )
187 {
189 }
190 
195 {
196  if ( token && ownsStr )
197  {
198  char* tmp = (char*)token;
199  delete[] tmp;
200  }
201  token = NULL;
202 }
203 
204 void Token::setStr( const char* s )
205 {
206  nulStr();
207  ownsStr = false;
208  token = s;
209 }
210 
214 void Token::copyStr( const char* s )
215 {
216  nulStr();
217  ownsStr = true;
218  size_t len = strlen( s );
219  auto tmp = new char[len + 1];
220  if ( tmp )
221  {
222  memcpy( tmp, s, len + 1 );
223  token = tmp;
224  }
225  else
226  {
227  token = NULL;
228  }
229 }
230 
231 void Token::copyStr( const char* s, int len )
232 {
233  nulStr();
234  ownsStr = true;
235  auto tmp = new char[static_cast<size_t>( len + 1 )];
236  if ( tmp )
237  {
238  memcpy( tmp, s, len );
239  tmp[len] = '\0';
240  token = tmp;
241  }
242  else
243  {
244  token = NULL;
245  }
246 }
247 
248 
250 {
251  nulStr();
253 }
254 }
255 }
Token & operator=(const Token &tok)
Definition: token.cpp:112
BTokenType type
Definition: token.h:40
BTokenId id
Definition: token.h:39
void copyStr(const char *s)
Definition: token.cpp:214
static unsigned int instances()
Definition: token.cpp:22
static void show_instances()
Definition: token.cpp:26
void unregister_instance()
Definition: token.cpp:46
void register_instance()
Definition: token.cpp:38
unsigned char module
Definition: token.h:55
UserFunction * userfunc
Definition: token.h:52
void setStr(const char *s)
Definition: token.cpp:204
const char * token
Definition: token.h:61
void printOn(std::ostream &outputStream) const
Definition: tkn_strm.cpp:20
Definition: berror.cpp:12
static unsigned int _instances
Definition: token.h:66
std::atomic< bool > exit_signalled