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-2025 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 verbose,
42  bool dryrun
43 )
44 {
45  // Report enables output, but respect DetailInfo state as well.
46  verbose = (verbose && Foam::infoDetailLevel > 0);
47 
48  if (dryrun && !verbose)
49  {
50  return;
51  }
52 
53  for (const entry& dEntry : dict)
54  {
55  const word& name = dEntry.keyword();
56 
57  simpleObjectRegistryEntry* objPtr = this->find(name);
58 
59  if (verbose)
60  {
61  if (objPtr)
62  {
63  Info<< " " << dEntry << nl;
64  }
65  else
66  {
67  Info<< " " << name << " (unregistered)" << nl;
68  }
69  }
70 
71  if (dryrun)
72  {
73  // Nothing else to do
74  }
75  else if (objPtr)
76  {
77  const List<simpleRegIOobject*>& objects = *objPtr;
78 
80  ISpanStream is;
81 
82  if (dEntry.isDict())
83  {
84  os.rewind();
85  os << dEntry.dict();
86 
87  is.reset(os.view());
88 
89  // Or alternatively?
90  // ITstream is(dEntry.dict().tokens());
91 
92  for (simpleRegIOobject* obj : objects)
93  {
94  is.rewind();
95  obj->readData(is);
96  }
97  }
98  else // dEntry.isStream()
99  {
100  for (simpleRegIOobject* obj : objects)
101  {
102  obj->readData(dEntry.stream());
103  }
104  }
105  }
106  }
107 }
108 
109 
111 (
112  const std::string_view name,
113  int val,
114  bool verbose,
115  bool dryrun
116 )
117 {
118  // Respect DetailInfo state
119  verbose = (verbose && Foam::infoDetailLevel > 0);
120 
121  if (dryrun && !verbose)
122  {
123  return;
124  }
125 
126  token tok(static_cast<label>(val));
127 
128  // Handle name=value,
129  // treating 'name=' like 'name' (ie, default value)
130 
131  const auto eq = name.find('=');
132  std::string_view objName = name;
133  std::string_view param;
134  std::string key;
135 
136  if (eq != std::string_view::npos)
137  {
138  key = name.substr(0, eq);
139  param = name.substr(eq+1);
140  objName = key;
141  }
142 
143  simpleObjectRegistryEntry* objPtr = this->find(objName.data());
144 
145  // Fail early
146  if (!objPtr)
147  {
148  if (verbose)
149  {
150  Info<< objName.data() << " (unregistered)" << nl;
151  }
152  return;
153  }
154 
155 
156  if (!param.empty())
157  {
158  float fvalue(0);
159 
160  if (Foam::readInt(param.data(), val))
161  {
162  // Parses as int
163  tok = static_cast<label>(val);
164  }
165  else if (Foam::readFloat(param.data(), fvalue))
166  {
167  // Parses as float
168  tok = fvalue;
169  }
170  else
171  {
172  // Accept 'name=string' for named enums
173  tok = Foam::string(param.data(), param.size());
174  }
175  }
176 
177  if (verbose)
178  {
179  Info<< objName.data() << '=' << tok << nl;
180  }
181 
182  if (dryrun)
183  {
184  // Nothing else to do
185  }
186  else if (objPtr)
187  {
188  // The generic interface requires an Istream.
189  ITstream is(tokenList(Foam::one{}, std::move(tok)));
190 
191  const List<simpleRegIOobject*>& objects = *objPtr;
192 
193  for (simpleRegIOobject* obj : objects)
194  {
195  is.rewind();
196  obj->readData(is);
197  }
198  }
199 }
200 
201 
202 // ************************************************************************* //
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:840
dictionary dict
System signed integer.
Input/output streams with (internal or external) character storage.
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:435
int infoDetailLevel
Global for selective suppression of Info output.
void setValues(const dictionary &dict, bool verbose=false, bool dryrun=false)
Set values (invoke callbacks) from dictionary entries.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
void setNamedValue(const std::string_view name, int val, bool verbose=false, bool dryrun=false)
Set named value, but also handle embedded &#39;name=value&#39; syntax.
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
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
auto key(const Type &t) -> std::enable_if_t< std::is_enum_v< Type >, std::underlying_type_t< Type > >
Definition: foamGltfBase.H:103
virtual void rewind() override
Rewind the stream, clearing any old errors.
Definition: ISpanStream.H:480
OBJstream os(runTime.globalPath()/outputName)
simpleObjectRegistryEntry * find(const word &keyword)
Find and return an entry, nullptr on failure.
messageStream Info
Information stream (stdout output on master, null elsewhere)
A class for handling character strings derived from std::string.
Definition: string.H:72
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: ISpanStream.H:266
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