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-2021 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 "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(name, 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 std::string strval(name.substr(eq+1));
111 name.erase(eq); // Truncate the name
112
113 float fvalue(val);
114
115 if (Foam::readInt(strval, val))
116 {
117 tok = static_cast<label>(val);
118 }
119 else if (Foam::readFloat(strval, fvalue))
120 {
121 tok = fvalue;
122 }
123 // Treat 'name=' like 'name' (ie, no value parameter)
124 // silently ignore 'name=junk', but could warn
125 }
126
127
128 simpleObjectRegistryEntry* objPtr = this->find(name.c_str());
129
130 if (objPtr)
131 {
132 // The generic interface requires an Istream.
133 ITstream is("", tokenList(Foam::one{}, tok));
134
135 Log << name.c_str() << '=' << tok << nl;
136
137 const List<simpleRegIOobject*>& objects = *objPtr;
138
139 for (simpleRegIOobject* obj : objects)
140 {
141 is.rewind();
142 obj->readData(is);
143 }
144 }
145 else
146 {
147 Log << name.c_str() << " (unregistered)" << nl;
148 }
149}
150
151
152// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
Input/output from string buffers.
T * find(const word &keyword)
Find and return an entry, nullptr on failure.
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:1064
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:112
An input stream of tokens.
Definition: ITstream.H:56
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:561
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:231
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
void setValues(const dictionary &dict, bool report=false)
Set values (invoke callbacks) from dictionary entries.
void setNamedValue(std::string name, int val, bool report=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 token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
System signed integer.
dimensionedScalar log(const dimensionedScalar &ds)
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:80
int infoDetailLevel
Global for selective suppression of Info output.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:44
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict