namedDictionary.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) 2020 OpenFOAM Foundation
9  Copyright (C) 2021-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "namedDictionary.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 {
35  is >> *this;
36 }
37 
38 
39 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 
42 {
43  first().clear();
44  second().clear();
45 }
46 
47 
49 {
50  return (first().empty() && second().empty());
51 }
52 
53 
54 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
55 
56 Foam::Istream& Foam::operator>>(Istream& is, namedDictionary& obj)
57 {
58  obj.clear();
59 
60  // Three possible inputs:
61  // - key
62  // - key { ... }
63  // - { ... }
64 
65  // Minor consistency with primitiveEntry, also accept the following:
66  // - key ;
67 
68  token tok(is);
69  is.putBack(tok);
70 
71  if (!tok.isPunctuation(token::BEGIN_BLOCK))
72  {
73  is >> obj.keyword();
74  is >> tok;
75 
76  // Discards possible trailing ';'
77  if (!tok.isPunctuation(token::END_STATEMENT))
78  {
79  is.putBack(tok);
80  }
81  }
82 
83  if (tok.isPunctuation(token::BEGIN_BLOCK))
84  {
85  obj.dict().read(is);
86  // Provide a name as context for error messages etc.
87  obj.dict().name() = is.relativeName();
88  }
89 
90  is.check(FUNCTION_NAME);
91  return is;
92 }
93 
94 
95 Foam::Ostream& Foam::operator<<(Ostream& os, const namedDictionary& obj)
96 {
97  // Three possible outputs:
98  // - key
99  // - key { ... }
100  // - { ... }
101  // No distinction between a missing and an empty dictionary
102 
103  if (obj.keyword().empty() || !obj.dict().empty())
104  {
105  // Never allow empty output.
106  // Otherwise cannot re-read for streaming
107  obj.dict().writeEntry(obj.keyword(), os);
108  }
109  else
110  {
111  os << obj.keyword();
112  }
113 
114  return os;
115 }
116 
117 
118 // ************************************************************************* //
Begin block [isseparator].
Definition: token.H:165
namedDictionary()=default
Default construct.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:321
End entry [isseparator].
Definition: token.H:160
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
bool empty() const noexcept
Empty if both keyword and dictionary are empty.
void clear()
Clear keyword and dictionary.