Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
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());
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// ************************************************************************* //
Input/output streams with (internal or external) character storage.
Input/output from string buffers.
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
virtual void rewind() override
Rewind the stream, clearing any old errors.
void reset(const char *buffer, size_t nbytes)
Reset input area, position to buffer start and clear errors.
An input stream of tokens.
Definition ITstream.H:56
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An OSstream with internal List storage.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition one.H:57
void setValues(const dictionary &dict, bool verbose=false, bool dryrun=false)
Set values (invoke callbacks) from dictionary entries.
void setNamedValue(const std::string_view name, int val, bool verbose=false, bool dryrun=false)
Set named value, but also handle embedded 'name=value' syntax.
Abstract base class for registered object with I/O. Used in debug symbol registration.
A class for handling character strings derived from std::string.
Definition string.H:76
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
System signed integer.
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition ListOps.H:855
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
messageStream Info
Information stream (stdout output on master, null elsewhere).
int readInt(Istream &is)
Read int from stream.
Definition intIO.C:73
int infoDetailLevel
Global for selective suppression of Info output.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
List< token > tokenList
List of token, used for dictionary primitive entry (for example).
Definition tokenList.H:32
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict