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-2015 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 // Clear any allocated storage (word or string)
36 inline void token::clear()
37 {
38  if (type_ == WORD)
39  {
40  delete wordTokenPtr_;
41  }
42  else if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
43  {
44  delete stringTokenPtr_;
45  }
46  else if (type_ == COMPOUND)
47  {
49  {
50  delete compoundTokenPtr_;
51  }
52  else
53  {
54  compoundTokenPtr_->refCount::operator--();
55  }
56  }
57 
58  type_ = UNDEFINED;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
64 // Construct null
65 inline token::token()
66 :
67  type_(UNDEFINED),
68  lineNumber_(0)
69 {}
70 
71 // Construct as copy
72 inline token::token(const token& t)
73 :
74  type_(t.type_),
75  lineNumber_(t.lineNumber_)
76 {
77  switch (type_)
78  {
79  case token::UNDEFINED:
80  break;
81 
82  case PUNCTUATION:
84  break;
85 
86  case WORD:
88  break;
89 
90  case STRING:
91  case VARIABLE:
92  case VERBATIMSTRING:
94  break;
95 
96  case LABEL:
98  break;
99 
100  case FLOAT_SCALAR:
102  break;
103 
104  case DOUBLE_SCALAR:
106  break;
107 
108  case COMPOUND:
110  compoundTokenPtr_->refCount::operator++();
111  break;
112 
113  case token::ERROR:
114  break;
115  }
116 }
117 
118 // Construct punctuation character token
120 :
121  type_(PUNCTUATION),
123  lineNumber_(lineNumber)
124 {}
125 
126 // Construct word token
128 :
129  type_(WORD),
130  wordTokenPtr_(new word(w)),
131  lineNumber_(lineNumber)
132 {}
133 
134 // Construct string token
135 inline token::token(const string& s, label lineNumber)
136 :
137  type_(STRING),
138  stringTokenPtr_(new string(s)),
139  lineNumber_(lineNumber)
140 {}
141 
142 // Construct label token
144 :
145  type_(LABEL),
146  labelToken_(l),
147  lineNumber_(lineNumber)
148 {}
149 
150 // Construct floatScalar token
152 :
153  type_(FLOAT_SCALAR),
155  lineNumber_(lineNumber)
156 {}
157 
158 // Construct doubleScalar token
160 :
161  type_(DOUBLE_SCALAR),
163  lineNumber_(lineNumber)
164 {}
165 
166 
167 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
168 
169 // Delete token clearing the storage used by word or string
171 {
172  clear();
173 }
174 
175 
176 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177 
179 {
180  return type_;
181 }
182 
184 {
185  return type_;
186 }
187 
188 inline bool token::good() const
189 {
190  return (type_ != ERROR && type_ != UNDEFINED);
191 }
192 
193 inline bool token::undefined() const
194 {
195  return (type_ == UNDEFINED);
196 }
197 
198 inline bool token::error() const
199 {
200  return (type_ == ERROR);
201 }
202 
203 inline bool token::isPunctuation() const
204 {
205  return (type_ == PUNCTUATION);
206 }
207 
209 {
210  if (type_ == PUNCTUATION)
211  {
212  return punctuationToken_;
213  }
214  else
215  {
216  parseError("punctuation character");
217  return NULL_TOKEN;
218  }
219 }
220 
221 inline bool token::isWord() const
222 {
223  return (type_ == WORD);
224 }
225 
226 inline const word& token::wordToken() const
227 {
228  if (type_ == WORD)
229  {
230  return *wordTokenPtr_;
231  }
232  else
233  {
234  parseError("word");
235  return word::null;
236  }
237 }
238 
239 inline bool token::isVariable() const
240 {
241  return (type_ == VARIABLE);
242 }
243 
244 inline bool token::isString() const
245 {
246  return (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING);
247 }
248 
249 inline const string& token::stringToken() const
250 {
251  if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
252  {
253  return *stringTokenPtr_;
254  }
255  else
256  {
257  parseError("string");
258  return string::null;
259  }
260 }
261 
262 inline bool token::isLabel() const
263 {
264  return (type_ == LABEL);
265 }
266 
267 inline label token::labelToken() const
268 {
269  if (type_ == LABEL)
270  {
271  return labelToken_;
272  }
273  else
274  {
275  parseError("label");
276  return 0;
277  }
278 }
279 
280 inline bool token::isFloatScalar() const
281 {
282  return (type_ == FLOAT_SCALAR);
283 }
284 
286 {
287  if (type_ == FLOAT_SCALAR)
288  {
289  return floatScalarToken_;
290  }
291  else
292  {
293  parseError("floatScalar");
294  return 0.0;
295  }
296 }
297 
298 
299 inline bool token::isDoubleScalar() const
300 {
301  return (type_ == DOUBLE_SCALAR);
302 }
303 
305 {
306  if (type_ == DOUBLE_SCALAR)
307  {
308  return doubleScalarToken_;
309  }
310  else
311  {
312  parseError("doubleScalar");
313  return 0.0;
314  }
315 }
316 
317 
318 inline bool token::isScalar() const
319 {
320  return (type_ == FLOAT_SCALAR || type_ == DOUBLE_SCALAR);
321 }
322 
323 inline scalar token::scalarToken() const
324 {
325  if (type_ == FLOAT_SCALAR)
326  {
327  return floatScalarToken_;
328  }
329  else if (type_ == DOUBLE_SCALAR)
330  {
331  return doubleScalarToken_;
332  }
333  else
334  {
335  parseError("scalar");
336  return 0.0;
337  }
338 }
339 
340 inline bool token::isNumber() const
341 {
342  return (type_ == LABEL || isScalar());
343 }
344 
345 inline scalar token::number() const
346 {
347  if (type_ == LABEL)
348  {
349  return labelToken_;
350  }
351  else if (isScalar())
352  {
353  return scalarToken();
354  }
355  else
356  {
357  parseError("number (label or scalar)");
358  return 0.0;
359  }
360 }
361 
362 inline bool token::isCompound() const
363 {
364  return (type_ == COMPOUND);
365 }
366 
368 {
369  if (type_ == COMPOUND)
370  {
371  return *compoundTokenPtr_;
372  }
373  else
374  {
375  parseError("compound");
376  return *compoundTokenPtr_;
377  }
378 }
379 
380 
381 inline label token::lineNumber() const
382 {
383  return lineNumber_;
384 }
385 
387 {
388  return lineNumber_;
389 }
390 
391 
392 inline void token::setBad()
393 {
394  clear();
395  type_ = ERROR;
396 }
397 
398 
399 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
400 
401 inline void token::operator=(const token& t)
402 {
403  clear();
404  type_ = t.type_;
405 
406  switch (type_)
407  {
408  case token::UNDEFINED:
409  break;
410 
411  case PUNCTUATION:
413  break;
414 
415  case WORD:
416  wordTokenPtr_ = new word(*t.wordTokenPtr_);
417  break;
418 
419  case STRING:
420  case VARIABLE:
421  case VERBATIMSTRING:
423  break;
424 
425  case LABEL:
427  break;
428 
429  case FLOAT_SCALAR:
431  break;
432 
433  case DOUBLE_SCALAR:
435  break;
436 
437  case COMPOUND:
439  compoundTokenPtr_->refCount::operator++();
440  break;
441 
442  case token::ERROR:
443  break;
444  }
445 
446  lineNumber_ = t.lineNumber_;
447 }
448 
450 {
451  clear();
452  type_ = PUNCTUATION;
454 }
455 
456 inline void token::operator=(word* wPtr)
457 {
458  clear();
459  type_ = WORD;
460  wordTokenPtr_ = wPtr;
461 }
462 
463 inline void token::operator=(const word& w)
464 {
465  operator=(new word(w));
466 }
467 
468 inline void token::operator=(string* sPtr)
469 {
470  clear();
471  type_ = STRING;
472  stringTokenPtr_ = sPtr;
473 }
474 
475 inline void token::operator=(const string& s)
476 {
477  operator=(new string(s));
478 }
479 
480 inline void token::operator=(const label l)
481 {
482  clear();
483  type_ = LABEL;
484  labelToken_ = l;
485 }
486 
487 inline void token::operator=(const floatScalar s)
488 {
489  clear();
490  type_ = FLOAT_SCALAR;
492 }
493 
494 inline void token::operator=(const doubleScalar s)
495 {
496  clear();
497  type_ = DOUBLE_SCALAR;
499 }
500 
502 {
503  clear();
504  type_ = COMPOUND;
505  compoundTokenPtr_ = cPtr;
506 }
507 
508 
509 inline bool token::operator==(const token& t) const
510 {
511  if (type_ != t.type_)
512  {
513  return false;
514  }
515 
516  switch (type_)
517  {
518  case token::UNDEFINED:
519  return true;
520 
521  case PUNCTUATION:
523 
524  case WORD:
525  return *wordTokenPtr_ == *t.wordTokenPtr_;
526 
527  case STRING:
528  case VARIABLE:
529  case VERBATIMSTRING:
530  return *stringTokenPtr_ == *t.stringTokenPtr_;
531 
532  case LABEL:
533  return labelToken_ == t.labelToken_;
534 
535  case FLOAT_SCALAR:
537 
538  case DOUBLE_SCALAR:
540 
541  case COMPOUND:
543 
544  case token::ERROR:
545  return true;
546  }
547 
548  return false;
549 }
550 
551 inline bool token::operator==(const punctuationToken p) const
552 {
553  return (type_ == PUNCTUATION && punctuationToken_ == p);
554 }
555 
556 inline bool token::operator==(const word& w) const
557 {
558  return (type_ == WORD && wordToken() == w);
559 }
560 
561 inline bool token::operator==(const string& s) const
562 {
563  return
564  (
565  (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
566  && stringToken() == s
567  );
568 }
569 
570 inline bool token::operator==(const label l) const
571 {
572  return (type_ == LABEL && labelToken_ == l);
573 }
574 
575 inline bool token::operator==(const floatScalar s) const
576 {
577  return (type_ == FLOAT_SCALAR && equal(floatScalarToken_, s));
578 }
579 
580 inline bool token::operator==(const doubleScalar s) const
581 {
582  return (type_ == DOUBLE_SCALAR && equal(doubleScalarToken_, s));
583 }
584 
585 inline bool token::operator!=(const token& t) const
586 {
587  return !operator==(t);
588 }
589 
590 inline bool token::operator!=(const punctuationToken p) const
591 {
592  return !operator==(p);
593 }
594 
595 inline bool token::operator!=(const word& w) const
596 {
597  return !operator==(w);
598 }
599 
600 inline bool token::operator!=(const string& s) const
601 {
602  return !operator==(s);
603 }
604 
605 inline bool token::operator!=(const floatScalar s) const
606 {
607  return !operator==(s);
608 }
609 
610 inline bool token::operator!=(const doubleScalar s) const
611 {
612  return !operator==(s);
613 }
614 
615 inline bool token::operator!=(const label l) const
616 {
617  return !operator==(l);
618 }
619 
620 
621 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
622 
623 } // End namespace Foam
624 
625 // ************************************************************************* //
doubleScalar doubleScalarToken() const
Definition: tokenI.H:304
scalar number() const
Definition: tokenI.H:345
bool error() const
Definition: tokenI.H:198
bool isFloatScalar() const
Definition: tokenI.H:280
bool isWord() const
Definition: tokenI.H:221
bool isLabel() const
Definition: tokenI.H:262
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 ))
~token()
Destructor.
Definition: tokenI.H:170
punctuationToken punctuationToken_
Definition: token.H:254
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
bool isScalar() const
Definition: tokenI.H:318
floatScalar floatScalarToken() const
Definition: tokenI.H:285
doubleScalar doubleScalarToken_
Definition: token.H:259
A class for handling words, derived from string.
Definition: word.H:59
void operator=(const token &)
Definition: tokenI.H:401
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
A class for handling character strings derived from std::string.
Definition: string.H:74
bool isPunctuation() const
Definition: tokenI.H:203
const word & wordToken() const
Definition: tokenI.H:226
token()
Construct null.
Definition: tokenI.H:65
const compound & compoundToken() const
Definition: tokenI.H:367
Namespace for OpenFOAM.
string * stringTokenPtr_
Definition: token.H:256
bool isNumber() const
Definition: tokenI.H:340
static const string null
An empty string.
Definition: string.H:86
bool operator!=(const token &) const
Definition: tokenI.H:585
punctuationToken
Standard punctuation tokens.
Definition: token.H:92
bool isString() const
Definition: tokenI.H:244
bool isDoubleScalar() const
Definition: tokenI.H:299
tokenType type() const
Definition: tokenI.H:178
void setBad()
Set bad.
Definition: tokenI.H:392
label lineNumber() const
Definition: tokenI.H:381
volScalarField & p
Definition: createFields.H:51
floatScalar floatScalarToken_
Definition: token.H:258
bool operator==(const token &) const
Definition: tokenI.H:509
word * wordTokenPtr_
Definition: token.H:255
A token holds items read from Istream.
Definition: token.H:67
bool okToDelete() const
Return true if the reference count is zero.
Definition: refCount.H:81
const string & stringToken() const
Definition: tokenI.H:249
tokenType
Enumeration defining the types of token.
Definition: token.H:73
label labelToken() const
Definition: tokenI.H:267
bool isCompound() const
Definition: tokenI.H:362
label labelToken_
Definition: token.H:257
Abstract base class for complex tokens.
Definition: token.H:122
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
bool good() const
Definition: tokenI.H:188
compound * compoundTokenPtr_
Definition: token.H:260
bool undefined() const
Definition: tokenI.H:193
punctuationToken pToken() const
Definition: tokenI.H:208
bool isVariable() const
Definition: tokenI.H:239
scalar scalarToken() const
Definition: tokenI.H:323
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
static const word null
An empty word.
Definition: word.H:77