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 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  }
87 
88  is.check(FUNCTION_NAME);
89  return is;
90 }
91 
92 
93 Foam::Ostream& Foam::operator<<(Ostream& os, const namedDictionary& obj)
94 {
95  // Three possible outputs:
96  // - key
97  // - key { ... }
98  // - { ... }
99  // No distinction between a missing and an empty dictionary
100 
101  if (obj.keyword().empty() || !obj.dict().empty())
102  {
103  // Never allow empty output.
104  // Otherwise cannot re-read for streaming
105  obj.dict().writeEntry(obj.keyword(), os);
106  }
107  else
108  {
109  os << obj.keyword();
110  }
111 
112  return os;
113 }
114 
115 
116 // ************************************************************************* //
Begin block [isseparator].
Definition: token.H:162
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:312
End entry [isseparator].
Definition: token.H:157
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
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:76
bool empty() const noexcept
Empty if both keyword and dictionary are empty.
void clear()
Clear keyword and dictionary.