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-2020 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 "StringStream.H"
31 #include "int.H"
32 
33 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
34 
36 (
37  const dictionary& dict,
38  bool report
39 )
40 {
41  // Report enables output, but respect DetailInfo state as well.
42  // The local log variable captures this logic.
43 
44  const bool log = (report && Foam::infoDetailLevel > 0);
45 
46  for (const entry& dEntry : dict)
47  {
48  const word& name = dEntry.keyword();
49 
50  simpleObjectRegistryEntry* objPtr = this->find(name);
51 
52  if (objPtr)
53  {
54  Log << " " << dEntry << nl;
55 
56  const List<simpleRegIOobject*>& objects = *objPtr;
57 
58  if (dEntry.isDict())
59  {
60  OStringStream os(IOstream::ASCII);
61  os << dEntry.dict();
62  IStringStream is(os.str());
63 
64  // Or alternatively?
65  // ITstream is(name, dEntry.dict().tokens());
66 
67  for (simpleRegIOobject* obj : objects)
68  {
69  is.rewind();
70  obj->readData(is);
71  }
72  }
73  else
74  {
75  for (simpleRegIOobject* obj : objects)
76  {
77  obj->readData(dEntry.stream());
78  }
79  }
80  }
81  else
82  {
83  Log << " " << name << " (unregistered)" << nl;
84  }
85  }
86 }
87 
88 
90 (
91  std::string name,
92  int val,
93  bool report
94 )
95 {
96  // Report enables output, but respect DetailInfo state as well.
97  // The local log variable captures this logic.
98 
99  const bool log = (report && Foam::infoDetailLevel > 0);
100 
101 
102  // Handle name=value
103  const auto eq = name.find('=');
104 
105  if (eq != std::string::npos)
106  {
107  int intval = 0;
108 
109  if (readInt(name.substr(eq+1), intval))
110  {
111  val = intval;
112  }
113  // Could warn about bad entry
114 
115  name.resize(eq); // Truncate the name
116  }
117 
118 
119  simpleObjectRegistryEntry* objPtr = this->find(name.c_str());
120 
121  if (objPtr)
122  {
123  // The generic interface requires an Istream.
124  IStringStream is(std::to_string(val));
125 
126  // Or alternatively?
127  // ITstream is("input", tokenList(1, token(label(val))));
128 
129  Log << name.c_str() << '=' << val << nl;
130 
131  const List<simpleRegIOobject*>& objects = *objPtr;
132 
133  for (simpleRegIOobject* obj : objects)
134  {
135  is.rewind();
136  obj->readData(is);
137  }
138  }
139  else
140  {
141  Log << name.c_str() << " (unregistered)" << nl;
142  }
143 }
144 
145 
146 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
int.H
System signed integer.
Log
#define Log
Definition: PDRblock.C:35
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::simpleObjectRegistry::setValues
void setValues(const dictionary &dict, bool report=false)
Set values (invoke callbacks) from dictionary entries.
Definition: simpleObjectRegistry.C:36
simpleObjectRegistry.H
StringStream.H
Input/output from string buffers.
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:874
Foam::simpleObjectRegistry::setNamedInt
void setNamedInt(std::string name, int val, bool report=false)
Set named value, but also handle embedded name=value syntax.
Definition: simpleObjectRegistry.C:90
Foam::simpleObjectRegistryEntry
Definition: simpleObjectRegistry.H:56
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:121
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:262
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.
Definition: StringStream.H:111
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
Foam::Detail::StringStreamAllocator::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:91
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::List< simpleRegIOobject * >
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:196
dictionary.H
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