coordinateSystems.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) 2018-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29#include "coordinateSystems.H"
30#include "predicates.H"
31#include "PtrListOps.H"
32#include "Time.H"
33
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36namespace Foam
37{
39}
40
41// File-local
42
43//- Header name for 1806 and earlier
44static const char* headerTypeCompat = "IOPtrList<coordinateSystem>";
45
46
47// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48
49void Foam::coordinateSystems::readFromStream(const bool valid)
50{
51 Istream& is = readStream(word::null, valid);
52
53 if (valid)
54 {
55 if (headerClassName() == typeName)
56 {
57 this->readIstream(is, coordinateSystem::iNew());
58 close();
59 }
61 {
62 // Older (1806 and earlier) header name
63 if (error::master())
64 {
65 std::cerr
66 << "--> FOAM IOWarning :" << nl
67 << " Found header class name '" << headerTypeCompat
68 << "' instead of '" << typeName << "'" << nl;
69
70 error::warnAboutAge("header class", 1806);
71 }
72
73 this->readIstream(is, coordinateSystem::iNew());
74 close();
75 }
76 else
77 {
79 << "unexpected class name " << headerClassName()
80 << " expected " << typeName
81 << " or " << headerTypeCompat << nl
82 << " while reading object " << name()
84 }
85 }
86}
87
88
89bool Foam::coordinateSystems::readContents()
90{
91 if
92 (
93 (
94 readOpt() == IOobject::MUST_READ
96 )
97 || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
98 )
99 {
100 readFromStream();
101 return true;
102 }
103
104 return false;
105}
106
107
108// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
109
111:
114{
115 readContents();
116}
117
118
120:
122 (
124 (
125 typeName,
126 obr.time().constant(),
127 obr,
128 IOobject::READ_IF_PRESENT,
129 IOobject::NO_WRITE
130 )
131 )
132{}
133
134
136(
137 const IOobject& io,
138 const PtrList<coordinateSystem>& content
139)
140:
143{
144 if (!readContents())
145 {
146 static_cast<PtrList<coordinateSystem>&>(*this) = content;
147 }
148}
149
150
152(
153 const IOobject& io,
155)
156:
158 PtrList<coordinateSystem>(std::move(content))
159{
160 readContents();
161}
162
163
164// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
165
167(
168 const objectRegistry& obr
169)
170{
171 // Previously registered?
172
173 const coordinateSystems* ptr = obr.findObject<coordinateSystems>(typeName);
174
175 if (ptr)
176 {
177 return *ptr;
178 }
179
180 // Read construct from registry
181 return obr.store(new coordinateSystems(obr));
182}
183
184
185// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
186
188{
189 if (key.empty())
190 {
191 return labelList();
192 }
193 return PtrListOps::findMatching(*this, key);
194}
195
196
198{
199 if (matcher.empty())
200 {
201 return labelList();
202 }
203 return PtrListOps::findMatching(*this, matcher);
204}
205
206
207Foam::label Foam::coordinateSystems::findIndex(const wordRe& key) const
208{
209 if (key.empty())
210 {
211 return -1;
212 }
213 return PtrListOps::firstMatching(*this, key);
214}
215
216
217Foam::label Foam::coordinateSystems::findIndex(const wordRes& matcher) const
218{
219 if (matcher.empty())
220 {
221 return -1;
222 }
223 return PtrListOps::firstMatching(*this, matcher);
224}
225
226
228{
229 return findIndex(key) != -1;
230}
231
232
235{
236 const label index =
237 (
238 name.empty() ? -1 : PtrListOps::firstMatching(*this, name)
239 );
240
241 if (coordinateSystem::debug)
242 {
244 << "Global coordinate system: "
245 << name << '=' << index << endl;
246 }
247
248 if (index < 0)
249 {
250 return nullptr;
251 }
252
253 return this->operator()(index);
254}
255
256
259{
260 const coordinateSystem* ptr = this->cfind(name);
261
262 if (!ptr)
263 {
265 << "Could not find coordinate system: " << name << nl
266 << "available coordinate systems: "
267 << flatOutput(names()) << nl << nl
268 << exit(FatalError);
269 }
270
271 return *ptr;
272}
273
274
276{
277 return PtrListOps::names(*this); // match any/all
278}
279
280
282{
283 if (key.empty())
284 {
285 return wordList();
286 }
287 return PtrListOps::names(*this, key);
288}
289
290
292{
293 if (matcher.empty())
294 {
295 return wordList();
296 }
297 return PtrListOps::names(*this, matcher);
298}
299
300
302{
303 const PtrList<coordinateSystem>& list = *this;
304
305 os << nl << size() << nl << token::BEGIN_LIST;
306
307 for (const coordinateSystem& csys : list)
308 {
309 os << nl;
310 csys.writeEntry(csys.name(), os);
311 }
312
313 os << token::END_LIST << nl;
314
315 return os.good();
316}
317
318
320(
322 const bool valid
323) const
324{
325 // Force ASCII, uncompressed
327 (
329 valid
330 );
331}
332
333
334// ************************************************************************* //
Functions to operate on Pointer Lists.
bool found
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
const word & headerClassName() const noexcept
Return name of the class name read from header.
Definition: IOobjectI.H:83
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:180
The IOstreamOption is a simple container for options an IOstream can normally have.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:38
const labelUList & indices() const noexcept
Return the list of sorted indices (updated every sort).
Definition: SortListI.H:54
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
Base class for coordinate system specification, the default coordinate system type is cartesian .
A centralized collection of named coordinate systems.
const coordinateSystem * cfind(const word &name) const
Return pointer to named coordinateSystem or nullptr on error.
bool writeData(Ostream &os) const
Write data.
virtual bool writeObject(IOstreamOption streamOpt, const bool valid=true) const
Write using stream options.
wordList names() const
A list of the coordinate-system names.
label findIndex(const wordRe &key) const
Find and return index for the first match, return -1 if not found.
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:55
Registry of regIOobjects.
const Type * findObject(const word &name, const bool recursive=false) const
Return const pointer to the object of the given Type.
constant condensation/saturation model.
Lookup type of boundary radiation properties.
Definition: lookup.H:66
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
void close()
Close Istream.
virtual bool writeObject(IOstreamOption streamOpt, const bool valid) const
Write using stream options.
splitCell * master() const
Definition: splitCell.H:113
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
@ END_LIST
End list [isseparator].
Definition: token.H:156
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
A class for handling words, derived from Foam::string.
Definition: word.H:68
static const word null
An empty word.
Definition: word.H:80
#define defineTypeName(Type)
Define the typeName.
Definition: className.H:96
static const char * headerTypeCompat
Header name for 1806 and earlier.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
#define InfoInFunction
Report an information message using Foam::Info.
labelList findMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Extract list indices for all items with 'name()' that matches.
label firstMatching(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Find first list item with 'name()' that matches, -1 on failure.
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Namespace for OpenFOAM.
List< word > wordList
A List of words.
Definition: fileName.H:63
label findIndex(const ListType &input, typename ListType::const_reference val, const label start=0)
Deprecated(2017-10) search for first occurrence of the given element.
Definition: ListOps.H:398
List< label > labelList
A List of labels.
Definition: List.H:66
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
IOerror FatalIOError
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53