token.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Note
28  Included by global/globals.C
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "token.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
41 }
42 
44 
45 
46 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
47 
48 void Foam::token::parseError(const char* expected) const
49 {
51  << "Parse error, expected a " << expected
52  << ", found \n " << info() << endl;
53 }
54 
55 
56 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
57 
59 (
60  const word& compoundType
61 )
62 {
63  auto* ctorPtr = emptyConstructorTable(compoundType);
64 
65  if (!ctorPtr)
66  {
68  (
69  "compound",
70  compoundType,
71  *emptyConstructorTablePtr_
72  ) << abort(FatalError);
73  }
74 
75  return autoPtr<token::compound>(ctorPtr());
76 }
77 
78 
80 (
81  const word& compoundType,
82  Istream& is,
83  const bool readContent
84 )
85 {
86  auto* ctorPtr = emptyConstructorTable(compoundType);
87 
88  if (!ctorPtr)
89  {
91  (
92  is,
93  "compound",
94  compoundType,
95  *emptyConstructorTablePtr_
96  ) << abort(FatalIOError);
97  }
98 
99  autoPtr<token::compound> aptr(ctorPtr());
100 
101  if (readContent)
102  {
103  aptr->read(is);
104  }
105  return aptr;
106 }
107 
108 
109 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
110 
111 bool Foam::token::compound::isCompound(const word& compoundType)
112 {
113  // Could also return the constructor pointer directly
114  // and test as bool or use for construction
115  //
116  // token::compound::emptyConstructorPtr ctorPtr = nullptr;
117  // if (emptyConstructorTablePtr_)
118  // {
119  // ctorPtr = emptyConstructorTablePtr_->cfind(compoundType);
120  // if (iter.good()) ctorPtr = iter.val();
121  // }
122  // return ctorPtr;
123 
124  return
125  (
126  emptyConstructorTablePtr_
127  && emptyConstructorTablePtr_->contains(compoundType)
128  );
129 }
130 
131 
133 (
134  const word& compoundType,
135  Istream& is,
136  const bool readContent
137 )
138 {
139  // Like compound::New() but more failure tolerant.
140  // - a no-op if the compoundType is unknown
141 
142  auto* ctorPtr = token::compound::emptyConstructorTable(compoundType);
143 
144  if (!ctorPtr)
145  {
146  return false;
147  }
148 
149  autoPtr<token::compound> aptr(ctorPtr());
150 
151  if (readContent)
152  {
153  aptr->read(is);
154  }
155  // Could also set pending(false) for !readContent,
156  // but prefer to leave that to the caller.
158  (*this) = std::move(aptr);
159 
160  return true;
161 }
162 
163 
165 {
166  if (type_ != tokenType::COMPOUND)
167  {
168  parseError("compound");
169  }
170 
171  if (data_.compoundPtr->moved())
172  {
173  if (is)
174  {
176  << "compound has already been transferred from token\n "
177  << info() << abort(FatalIOError);
178  }
179  else
180  {
182  << "compound has already been transferred from token\n "
183  << info() << abort(FatalError);
184  }
185  }
186  // // TDB
187  // else if (data_.compoundPtr->pending())
188  // {
189  // if (is)
190  // {
191  // FatalIOErrorInFunction(*is)
192  // << "compound is pending (not yet read?)\n "
193  // << info() << abort(FatalIOError);
194  // }
195  // else
196  // {
197  // FatalErrorInFunction
198  // << "compound is pending (not yet read?)\n "
199  // << info() << abort(FatalError);
200  // }
201  // }
202  else
203  {
204  // TBD: reset pending?
205  data_.compoundPtr->moved(true);
206  }
207 
208  return *data_.compoundPtr;
209 }
210 
211 
212 // ************************************************************************* //
static bool isCompound(const word &compoundType)
True if a known (registered) compound type.
Definition: token.C:104
static const token undefinedToken
An undefined token.
Definition: token.H:542
static autoPtr< compound > New(const word &compoundType)
Default construct specified compound type.
Definition: token.C:52
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds an item read from Istream.
Definition: token.H:65
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool readCompoundToken(const word &compoundType, Istream &is, const bool readContent=true)
Default construct the specified compound type and read from stream.
Definition: token.C:126
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:605
token::compound tokenCompound
Definition: token.C:31
Abstract base class for complex tokens.
Definition: token.H:176
defineTypeName(manifoldCellsMeshObject)
compound & transferCompoundToken(const Istream *is=nullptr)
Return reference to compound and mark internally as released.
Definition: token.C:157
A class for handling words, derived from Foam::string.
Definition: word.H:63
errorManip< error > abort(error &err)
Definition: errorManip.H:139
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:1078
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:635
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...