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 "SpanStream.H"
32 #include "StringStream.H"
33 #include "int.H"
34 #include "floatScalar.H"
35 
36 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
37 
39 (
40  const dictionary& dict,
41  bool report
42 )
43 {
44  // Report enables output, but respect DetailInfo state as well.
45  // The local log variable captures this logic.
46 
47  const bool log = (report && Foam::infoDetailLevel > 0);
48 
49  for (const entry& dEntry : dict)
50  {
51  const word& name = dEntry.keyword();
52 
53  simpleObjectRegistryEntry* objPtr = this->find(name);
54 
55  if (objPtr)
56  {
57  Log << " " << dEntry << nl;
58 
59  const List<simpleRegIOobject*>& objects = *objPtr;
60 
62  ISpanStream is;
63 
64  if (dEntry.isDict())
65  {
66  os.rewind();
67  os << dEntry.dict();
68 
69  is.reset(os.view());
70 
71  // Or alternatively?
72  // ITstream is(dEntry.dict().tokens());
73 
74  for (simpleRegIOobject* obj : objects)
75  {
76  is.rewind();
77  obj->readData(is);
78  }
79  }
80  else // dEntry.isStream()
81  {
82  for (simpleRegIOobject* obj : objects)
83  {
84  obj->readData(dEntry.stream());
85  }
86  }
87  }
88  else
89  {
90  Log << " " << name << " (unregistered)" << nl;
91  }
92  }
93 }
94 
95 
97 (
98  std::string name,
99  int val,
100  bool report
101 )
102 {
103  // Report enables output, but respect DetailInfo state as well.
104  // The local log variable captures this logic.
105 
106  const bool log = (report && Foam::infoDetailLevel > 0);
107 
108  token tok(static_cast<label>(val));
109 
110  // Handle name=value
111  const auto eq = name.find('=');
112 
113  if (eq != std::string::npos)
114  {
115  string strval(name.substr(eq+1));
116  name.erase(eq); // Truncate the name
117 
118  // Treat 'name=' like 'name' (ie, default value)
119  if (strval.length())
120  {
121  float fvalue(0);
122 
123  if (Foam::readInt(strval, val))
124  {
125  // Parses as int
126  tok = static_cast<label>(val);
127  }
128  else if (Foam::readFloat(strval, fvalue))
129  {
130  // Parses as float
131  tok = fvalue;
132  }
133  else
134  {
135  // Accept 'name=string' for named enums,
136  tok = std::move(strval);
137  }
138  }
139  }
140 
141 
142  simpleObjectRegistryEntry* objPtr = this->find(name.c_str());
143 
144  if (objPtr)
145  {
146  Log << name.c_str() << '=' << tok << nl;
147 
148  // The generic interface requires an Istream.
149  ITstream is(tokenList(Foam::one{}, std::move(tok)));
150 
151  const List<simpleRegIOobject*>& objects = *objPtr;
152 
153  for (simpleRegIOobject* obj : objects)
154  {
155  is.rewind();
156  obj->readData(is);
157  }
158  }
159  else
160  {
161  Log << name.c_str() << " (unregistered)" << nl;
162  }
163 }
164 
165 
166 // ************************************************************************* //
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:827
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.
Input/output streams with (internal or external) character storage.
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
void reset(const char *buffer, size_t nbytes)
Reset input area, position to buffer start and clear errors.
Definition: ISpanStream.H:412
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
An OSstream with internal List storage.
Definition: OCharStream.H:231
virtual void rewind() override
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:459
OBJstream os(runTime.globalPath()/outputName)
#define Log
Definition: PDRblock.C:28
simpleObjectRegistryEntry * find(const word &keyword)
Find and return an entry, nullptr on failure.
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: ISpanStream.H:251
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