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 -------------------------------------------------------------------------------
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(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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
int.H
System signed integer.
Foam::ITstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:561
Log
#define Log
Definition: PDRblock.C:35
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::simpleObjectRegistry::setValues
void setValues(const dictionary &dict, bool report=false)
Set values (invoke callbacks) from dictionary entries.
Definition: simpleObjectRegistry.C:38
ITstream.H
simpleObjectRegistry.H
Foam::one
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:61
StringStream.H
Input/output from string buffers.
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:1064
Foam::simpleObjectRegistryEntry
Definition: simpleObjectRegistry.H:56
Foam::simpleObjectRegistry::setNamedValue
void setNamedValue(std::string name, int val, bool report=false)
Set named value, but also handle embedded 'name=value' syntax.
Definition: simpleObjectRegistry.C:92
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:262
os
OBJstream os(runTime.globalPath()/outputName)
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::IStringStream
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:108
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
Foam::tokenList
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:44
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< simpleRegIOobject * >
Foam::OStringStream
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:227
dictionary.H
floatScalar.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::readInt
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:80
Foam::simpleRegIOobject
Abstract base class for registered object with I/O. Used in debug symbol registration.
Definition: simpleRegIOobject.H:52