simpleObjectRegistry.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) 2019-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "simpleObjectRegistry.H"
29 #include "dictionary.H"
30 #include "ITstream.H"
31 #include "StringStream.H"
32 #include "int.H"
33 #include "floatScalar.H"
34 
35 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36 
38 (
39  const dictionary& dict,
40  bool report
41 )
42 {
43  // Report enables output, but respect DetailInfo state as well.
44  // The local log variable captures this logic.
45 
46  const bool log = (report && Foam::infoDetailLevel > 0);
47 
48  for (const entry& dEntry : dict)
49  {
50  const word& name = dEntry.keyword();
51 
52  simpleObjectRegistryEntry* objPtr = this->find(name);
53 
54  if (objPtr)
55  {
56  Log << " " << dEntry << nl;
57 
58  const List<simpleRegIOobject*>& objects = *objPtr;
59 
60  if (dEntry.isDict())
61  {
63  os << dEntry.dict();
64  IStringStream is(os.str());
65 
66  // Or alternatively?
67  // ITstream is(dEntry.dict().tokens());
68 
69  for (simpleRegIOobject* obj : objects)
70  {
71  is.rewind();
72  obj->readData(is);
73  }
74  }
75  else
76  {
77  for (simpleRegIOobject* obj : objects)
78  {
79  obj->readData(dEntry.stream());
80  }
81  }
82  }
83  else
84  {
85  Log << " " << name << " (unregistered)" << nl;
86  }
87  }
88 }
89 
90 
92 (
93  std::string name,
94  int val,
95  bool report
96 )
97 {
98  // Report enables output, but respect DetailInfo state as well.
99  // The local log variable captures this logic.
100 
101  const bool log = (report && Foam::infoDetailLevel > 0);
102 
103  token tok(static_cast<label>(val));
104 
105  // Handle name=value
106  const auto eq = name.find('=');
107 
108  if (eq != std::string::npos)
109  {
110  string strval(name.substr(eq+1));
111  name.erase(eq); // Truncate the name
112 
113  // Treat 'name=' like 'name' (ie, default value)
114  if (strval.length())
115  {
116  float fvalue(0);
117 
118  if (Foam::readInt(strval, val))
119  {
120  // Parses as int
121  tok = static_cast<label>(val);
122  }
123  else if (Foam::readFloat(strval, fvalue))
124  {
125  // Parses as float
126  tok = fvalue;
127  }
128  else
129  {
130  // Accept 'name=string' for named enums,
131  tok = std::move(strval);
132  }
133  }
134  }
135 
136 
137  simpleObjectRegistryEntry* objPtr = this->find(name.c_str());
138 
139  if (objPtr)
140  {
141  Log << name.c_str() << '=' << tok << nl;
142 
143  // The generic interface requires an Istream.
144  ITstream is(tokenList(Foam::one{}, std::move(tok)));
145 
146  const List<simpleRegIOobject*>& objects = *objPtr;
147 
148  for (simpleRegIOobject* obj : objects)
149  {
150  is.rewind();
151  obj->readData(is);
152  }
153  }
154  else
155  {
156  Log << name.c_str() << " (unregistered)" << nl;
157  }
158 }
159 
160 
161 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
dictionary dict
void setNamedValue(std::string name, int val, bool report=false)
Set named value, but also handle embedded &#39;name=value&#39; syntax.
System signed integer.
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: ISstream.C:1078
dimensionedScalar log(const dimensionedScalar &ds)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Input/output from string buffers.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
int infoDetailLevel
Global for selective suppression of Info output.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:73
List< token > tokenList
List of token, used for dictionary primitive entry (for example)
Definition: tokenList.H:32
void setValues(const dictionary &dict, bool report=false)
Set values (invoke callbacks) from dictionary entries.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
OBJstream os(runTime.globalPath()/outputName)
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:120
#define Log
Definition: PDRblock.C:28
simpleObjectRegistryEntry * find(const word &keyword)
Find and return an entry, nullptr on failure.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56