tokenI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include <iostream>
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 inline void Foam::token::clear()
31 {
32  if (type_ == WORD)
33  {
34  delete wordTokenPtr_;
35  }
36  else if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
37  {
38  delete stringTokenPtr_;
39  }
40  else if (type_ == COMPOUND)
41  {
43  {
44  delete compoundTokenPtr_;
45  }
46  else
47  {
48  compoundTokenPtr_->refCount::operator--();
49  }
50  }
51 
52  type_ = UNDEFINED;
53 }
54 
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
59 :
60  type_(UNDEFINED),
61  lineNumber_(0)
62 {}
63 
64 
65 inline Foam::token::token(const token& t)
66 :
67  type_(t.type_),
68  lineNumber_(t.lineNumber_)
69 {
70  switch (type_)
71  {
72  case token::UNDEFINED:
73  break;
74 
75  case PUNCTUATION:
77  break;
78 
79  case WORD:
81  break;
82 
83  case STRING:
84  case VARIABLE:
85  case VERBATIMSTRING:
87  break;
88 
89  case LABEL:
91  break;
92 
93  case FLOAT_SCALAR:
95  break;
96 
97  case DOUBLE_SCALAR:
99  break;
100 
101  case COMPOUND:
103  compoundTokenPtr_->refCount::operator++();
104  break;
105 
106  case token::ERROR:
107  break;
108  }
109 }
110 
111 
113 :
114  type_(PUNCTUATION),
116  lineNumber_(lineNumber)
117 {}
118 
119 
121 :
122  type_(WORD),
123  wordTokenPtr_(new word(w)),
124  lineNumber_(lineNumber)
125 {}
126 
127 
128 inline Foam::token::token(const string& s, label lineNumber)
129 :
130  type_(STRING),
131  stringTokenPtr_(new string(s)),
132  lineNumber_(lineNumber)
133 {}
134 
135 
137 :
138  type_(LABEL),
139  labelToken_(l),
140  lineNumber_(lineNumber)
141 {}
142 
143 
145 :
146  type_(FLOAT_SCALAR),
148  lineNumber_(lineNumber)
149 {}
150 
151 
153 :
154  type_(DOUBLE_SCALAR),
156  lineNumber_(lineNumber)
157 {}
158 
159 
160 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
161 
163 {
164  clear();
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
169 
171 {
172  return type_;
173 }
174 
176 {
177  return type_;
178 }
179 
180 inline bool Foam::token::good() const
181 {
182  return (type_ != ERROR && type_ != UNDEFINED);
183 }
184 
185 inline bool Foam::token::undefined() const
186 {
187  return (type_ == UNDEFINED);
188 }
189 
190 inline bool Foam::token::error() const
191 {
192  return (type_ == ERROR);
193 }
194 
195 inline bool Foam::token::isPunctuation() const
196 {
197  return (type_ == PUNCTUATION);
198 }
199 
201 {
202  if (type_ == PUNCTUATION)
203  {
204  return punctuationToken_;
205  }
206  else
207  {
208  parseError("punctuation character");
209  return NULL_TOKEN;
210  }
211 }
212 
213 inline bool Foam::token::isWord() const
214 {
215  return (type_ == WORD);
216 }
217 
218 inline const Foam::word& Foam::token::wordToken() const
219 {
220  if (type_ == WORD)
221  {
222  return *wordTokenPtr_;
223  }
224  else
225  {
226  parseError(word::typeName);
227  return word::null;
228  }
229 }
230 
231 inline bool Foam::token::isVariable() const
232 {
233  return (type_ == VARIABLE);
234 }
235 
236 inline bool Foam::token::isString() const
237 {
238  return (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING);
239 }
240 
242 {
243  if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
244  {
245  return *stringTokenPtr_;
246  }
247  else
248  {
249  parseError(string::typeName);
250  return string::null;
251  }
252 }
253 
254 inline bool Foam::token::isLabel() const
255 {
256  return (type_ == LABEL);
257 }
258 
260 {
261  if (type_ == LABEL)
262  {
263  return labelToken_;
264  }
265  else
266  {
267  parseError(pTraits<label>::typeName);
268  return 0;
269  }
270 }
271 
272 inline bool Foam::token::isFloatScalar() const
273 {
274  return (type_ == FLOAT_SCALAR);
275 }
276 
278 {
279  if (type_ == FLOAT_SCALAR)
280  {
281  return floatScalarToken_;
282  }
283  else
284  {
285  parseError("floatScalar");
286  return 0.0;
287  }
288 }
289 
290 
291 inline bool Foam::token::isDoubleScalar() const
292 {
293  return (type_ == DOUBLE_SCALAR);
294 }
295 
297 {
298  if (type_ == DOUBLE_SCALAR)
299  {
300  return doubleScalarToken_;
301  }
302  else
303  {
304  parseError("doubleScalar");
305  return 0.0;
306  }
307 }
308 
309 
310 inline bool Foam::token::isScalar() const
311 {
312  return (type_ == FLOAT_SCALAR || type_ == DOUBLE_SCALAR);
313 }
314 
315 inline Foam::scalar Foam::token::scalarToken() const
316 {
317  if (type_ == FLOAT_SCALAR)
318  {
319  return floatScalarToken_;
320  }
321  else if (type_ == DOUBLE_SCALAR)
322  {
323  return doubleScalarToken_;
324  }
325  else
326  {
327  parseError(pTraits<scalar>::typeName);
328  return 0.0;
329  }
330 }
331 
332 inline bool Foam::token::isNumber() const
333 {
334  return (type_ == LABEL || isScalar());
335 }
336 
337 inline Foam::scalar Foam::token::number() const
338 {
339  if (type_ == LABEL)
340  {
341  return labelToken_;
342  }
343  else if (isScalar())
344  {
345  return scalarToken();
346  }
347  else
348  {
349  parseError("number (label or scalar)");
350  return 0.0;
351  }
352 }
353 
354 inline bool Foam::token::isCompound() const
355 {
356  return (type_ == COMPOUND);
357 }
358 
360 {
361  if (type_ == COMPOUND)
362  {
363  return *compoundTokenPtr_;
364  }
365  else
366  {
367  parseError("compound");
368  return *compoundTokenPtr_;
369  }
370 }
371 
372 
374 {
375  return lineNumber_;
376 }
377 
379 {
380  return lineNumber_;
381 }
382 
383 
384 inline void Foam::token::setBad()
385 {
386  clear();
387  type_ = ERROR;
388 }
389 
390 
391 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
392 
393 inline void Foam::token::operator=(const token& t)
394 {
395  clear();
396  type_ = t.type_;
397 
398  switch (type_)
399  {
400  case token::UNDEFINED:
401  break;
402 
403  case PUNCTUATION:
405  break;
406 
407  case WORD:
408  wordTokenPtr_ = new word(*t.wordTokenPtr_);
409  break;
410 
411  case STRING:
412  case VARIABLE:
413  case VERBATIMSTRING:
415  break;
416 
417  case LABEL:
419  break;
420 
421  case FLOAT_SCALAR:
423  break;
424 
425  case DOUBLE_SCALAR:
427  break;
428 
429  case COMPOUND:
431  compoundTokenPtr_->refCount::operator++();
432  break;
433 
434  case token::ERROR:
435  break;
436  }
437 
438  lineNumber_ = t.lineNumber_;
439 }
440 
442 {
443  clear();
444  type_ = PUNCTUATION;
446 }
447 
448 inline void Foam::token::operator=(word* wPtr)
449 {
450  clear();
451  type_ = WORD;
452  wordTokenPtr_ = wPtr;
453 }
454 
455 inline void Foam::token::operator=(const word& w)
456 {
457  operator=(new word(w));
458 }
459 
460 inline void Foam::token::operator=(string* sPtr)
461 {
462  clear();
463  type_ = STRING;
464  stringTokenPtr_ = sPtr;
465 }
466 
467 inline void Foam::token::operator=(const string& s)
468 {
469  operator=(new string(s));
470 }
471 
472 inline void Foam::token::operator=(const label l)
473 {
474  clear();
475  type_ = LABEL;
476  labelToken_ = l;
477 }
478 
480 {
481  clear();
482  type_ = FLOAT_SCALAR;
484 }
485 
487 {
488  clear();
489  type_ = DOUBLE_SCALAR;
491 }
492 
494 {
495  clear();
496  type_ = COMPOUND;
497  compoundTokenPtr_ = cPtr;
498 }
499 
500 
501 inline bool Foam::token::operator==(const token& t) const
502 {
503  if (type_ != t.type_)
504  {
505  return false;
506  }
507 
508  switch (type_)
509  {
510  case token::UNDEFINED:
511  return true;
512 
513  case PUNCTUATION:
515 
516  case WORD:
517  return *wordTokenPtr_ == *t.wordTokenPtr_;
518 
519  case STRING:
520  case VARIABLE:
521  case VERBATIMSTRING:
522  return *stringTokenPtr_ == *t.stringTokenPtr_;
523 
524  case LABEL:
525  return labelToken_ == t.labelToken_;
526 
527  case FLOAT_SCALAR:
529 
530  case DOUBLE_SCALAR:
532 
533  case COMPOUND:
535 
536  case token::ERROR:
537  return true;
538  }
539 
540  return false;
541 }
542 
544 {
545  return (type_ == PUNCTUATION && punctuationToken_ == p);
546 }
547 
548 inline bool Foam::token::operator==(const word& w) const
549 {
550  return (type_ == WORD && wordToken() == w);
551 }
552 
553 inline bool Foam::token::operator==(const string& s) const
554 {
555  return
556  (
557  (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
558  && stringToken() == s
559  );
560 }
561 
562 inline bool Foam::token::operator==(const label l) const
563 {
564  return (type_ == LABEL && labelToken_ == l);
565 }
566 
567 inline bool Foam::token::operator==(const floatScalar s) const
568 {
569  return (type_ == FLOAT_SCALAR && equal(floatScalarToken_, s));
570 }
571 
572 inline bool Foam::token::operator==(const doubleScalar s) const
573 {
574  return (type_ == DOUBLE_SCALAR && equal(doubleScalarToken_, s));
575 }
576 
577 inline bool Foam::token::operator!=(const token& t) const
578 {
579  return !operator==(t);
580 }
581 
583 {
584  return !operator==(p);
585 }
586 
587 inline bool Foam::token::operator!=(const word& w) const
588 {
589  return !operator==(w);
590 }
591 
592 inline bool Foam::token::operator!=(const string& s) const
593 {
594  return !operator==(s);
595 }
596 
597 inline bool Foam::token::operator!=(const floatScalar s) const
598 {
599  return !operator==(s);
600 }
601 
602 inline bool Foam::token::operator!=(const doubleScalar s) const
603 {
604  return !operator==(s);
605 }
606 
607 inline bool Foam::token::operator!=(const label l) const
608 {
609  return !operator==(l);
610 }
611 
612 
613 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:254
doubleScalar doubleScalarToken_
Definition: token.H:261
~token()
Destructor.
Definition: tokenI.H:162
bool isWord() const
Definition: tokenI.H:213
punctuationToken pToken() const
Definition: tokenI.H:200
bool operator!=(const token &) const
Definition: tokenI.H:577
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
compound * compoundTokenPtr_
Definition: token.H:262
scalar number() const
Definition: tokenI.H:337
label labelToken_
Definition: token.H:259
const word & wordToken() const
Definition: tokenI.H:218
A token holds items read from Istream.
Definition: token.H:69
string * stringTokenPtr_
Definition: token.H:258
Traits class for primitives.
Definition: pTraits.H:50
tokenType
Enumeration defining the types of token.
Definition: token.H:75
bool operator==(const token &) const
Definition: tokenI.H:501
scalar scalarToken() const
Definition: tokenI.H:315
bool unique() const
Return true if the reference count is zero.
Definition: refCount.H:87
word * wordTokenPtr_
Definition: token.H:257
bool isNumber() const
Definition: tokenI.H:332
bool isCompound() const
Definition: tokenI.H:354
Abstract base class for complex tokens.
Definition: token.H:124
static const char *const typeName
Definition: string.H:82
void operator=(const token &)
Definition: tokenI.H:393
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
floatScalar floatScalarToken() const
Definition: tokenI.H:277
A class for handling words, derived from string.
Definition: word.H:59
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
void setBad()
Set bad.
Definition: tokenI.H:384
static const word null
An empty word.
Definition: word.H:77
bool good() const
Definition: tokenI.H:180
bool isScalar() const
Definition: tokenI.H:310
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
label lineNumber() const
Definition: tokenI.H:373
const compound & compoundToken() const
Definition: tokenI.H:359
bool isVariable() const
Definition: tokenI.H:231
bool undefined() const
Definition: tokenI.H:185
static const string null
An empty string.
Definition: string.H:86
bool isFloatScalar() const
Definition: tokenI.H:272
bool isDoubleScalar() const
Definition: tokenI.H:291
const string & stringToken() const
Definition: tokenI.H:241
bool isString() const
Definition: tokenI.H:236
bool error() const
Definition: tokenI.H:190
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
floatScalar floatScalarToken_
Definition: token.H:260
tokenType type() const
Definition: tokenI.H:170
punctuationToken punctuationToken_
Definition: token.H:256
label labelToken() const
Definition: tokenI.H:259
token()
Construct null.
Definition: tokenI.H:58
punctuationToken
Standard punctuation tokens.
Definition: token.H:94
volScalarField & p
static const char *const typeName
Definition: word.H:73
doubleScalar doubleScalarToken() const
Definition: tokenI.H:296
A class for handling character strings derived from std::string.
Definition: string.H:74
bool isPunctuation() const
Definition: tokenI.H:195