Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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/>.
27\*---------------------------------------------------------------------------*/
28
29#include "namedDictionary.H"
30
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32
35 is >> *this;
36}
37
38
39// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40
42{
43 first().clear();
44 second().clear();
45}
46
47
50 return (first().empty() && second().empty());
51}
52
53
54// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
55
56Foam::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
95Foam::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// ************************************************************************* //
bool empty() const noexcept
True if the list is empty.
Definition DLListBase.H:189
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition IOstream.C:39
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition Istream.C:71
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
const keyType & first() const noexcept
Definition Tuple2.H:132
const dictionary & second() const noexcept
Definition Tuple2.H:142
const fileName & name() const noexcept
The dictionary name.
Definition dictionaryI.H:41
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
bool read(Istream &is)
Read dictionary from Istream (discards the header). Reads entries until EOF or when the first token i...
A tuple of keyType and dictionary, which can be used when reading named or unnamed dictionary entries...
bool empty() const noexcept
Empty if both keyword and dictionary are empty.
namedDictionary()=default
Default construct.
const keyType & keyword() const noexcept
Return keyword.
void clear()
Clear keyword and dictionary.
const dictionary & dict() const noexcept
Read-access to the dictionay.
A token holds an item read from Istream.
Definition token.H:70
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition tokenI.H:650
@ BEGIN_BLOCK
Begin block [isseparator].
Definition token.H:178
@ END_STATEMENT
End entry [isseparator].
Definition token.H:173
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265