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 :
36 {}
37 
38 
40 {
41  is >> *this;
42 }
43 
44 
45 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
46 
48 {
49  first().clear();
50  second().clear();
51 }
52 
53 
54 bool Foam::namedDictionary::empty() const noexcept
55 {
56  return (first().empty() && second().empty());
57 }
58 
59 
60 // * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
61 
62 Foam::Istream& Foam::operator>>(Istream& is, namedDictionary& obj)
63 {
64  obj.clear();
65 
66  // Three possible inputs:
67  // - key
68  // - key { ... }
69  // - { ... }
70 
71  // Minor consistency with primitiveEntry, also accept the following:
72  // - key ;
73 
74  token tok(is);
75  is.putBack(tok);
76 
77  if (!tok.isPunctuation(token::BEGIN_BLOCK))
78  {
79  is >> obj.keyword();
80  is >> tok;
81 
82  // Discards possible trailing ';'
83  if (!tok.isPunctuation(token::END_STATEMENT))
84  {
85  is.putBack(tok);
86  }
87  }
88 
89  if (tok.isPunctuation(token::BEGIN_BLOCK))
90  {
91  obj.dict().read(is);
92  }
93 
94  is.check(FUNCTION_NAME);
95  return is;
96 }
97 
98 
99 Foam::Ostream& Foam::operator<<(Ostream& os, const namedDictionary& obj)
100 {
101  // Three possible outputs:
102  // - key
103  // - key { ... }
104  // - { ... }
105  // No distinction between a missing and an empty dictionary
106 
107  if (obj.keyword().empty() || !obj.dict().empty())
108  {
109  // Never allow empty output.
110  // Otherwise cannot re-read for streaming
111  obj.dict().writeEntry(obj.keyword(), os);
112  }
113  else
114  {
115  os << obj.keyword();
116  }
117 
118  return os;
119 }
120 
121 
122 // ************************************************************************* //
Foam::namedDictionary::dict
const dictionary & dict() const noexcept
Definition: namedDictionary.H:122
Foam::namedDictionary::namedDictionary
namedDictionary()
Definition: namedDictionary.C:26
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:223
Foam::token
A token holds an item read from Istream.
Definition: token.H:64
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Definition: boundaryPatch.C:76
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:66
Foam::dictionary::writeEntry
void writeEntry(Ostream &os) const
Definition: dictionaryIO.C:157
namedDictionary.H
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::namedDictionary
A tuple of keyType and dictionary, which can be used when reading named or unnamed dictionary entries...
Definition: namedDictionary.H:79
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Definition: tokenI.H:452
Foam::token::END_STATEMENT
@ END_STATEMENT
End entry [isseparator].
Definition: token.H:150
Foam::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.C:51
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:119
os
OBJstream os(runTime.globalPath()/outputName)
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:155
Foam::dictionary::read
bool read(Istream &is)
Definition: dictionaryIO.C:134
Foam::namedDictionary::empty
bool empty() const noexcept
Definition: namedDictionary.C:47
Foam::namedDictionary::keyword
const keyType & keyword() const noexcept
Definition: namedDictionary.H:110
Foam::Istream::putBack
void putBack(const token &tok)
Definition: Istream.C:63
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:302
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Definition: Ostream.H:232
Foam::namedDictionary::clear
void clear()
Definition: namedDictionary.C:40
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:56