ListIO.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2018-2021 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/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "List.H"
30#include "Istream.H"
31#include "token.H"
32#include "SLList.H"
33#include "contiguous.H"
34
35// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36
37template<class T>
39:
40 UList<T>(nullptr, 0)
41{
42 this->readList(is);
43}
44
45
46// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
47
48template<class T>
50{
51 List<T>& list = *this;
52
53 // Anull list
54 list.clear();
55
57
58 token tok(is);
59
60 is.fatalCheck("List<T>::readList(Istream&) : reading first token");
61
62 if (tok.isCompound())
63 {
64 // Compound: simply transfer contents
65
66 list.transfer
67 (
69 (
71 )
72 );
73 }
74 else if (tok.isLabel())
75 {
76 // Label: could be int(..), int{...} or just a plain '0'
77
78 const label len = tok.labelToken();
79
80 // Resize to actual length read
81 list.resize(len);
82
84 {
85 // Binary and contiguous
86
87 if (len)
88 {
89 Detail::readContiguous<T>
90 (
91 is,
92 list.data_bytes(),
93 list.size_bytes()
94 );
95
96 is.fatalCheck
97 (
98 "List<T>::readList(Istream&) : "
99 "reading the binary block"
100 );
101 }
102 }
103 else
104 {
105 // Begin of contents marker
106 const char delimiter = is.readBeginList("List");
107
108 if (len)
109 {
110 if (delimiter == token::BEGIN_LIST)
111 {
112 for (label i=0; i<len; ++i)
113 {
114 is >> list[i];
115
116 is.fatalCheck
117 (
118 "List<T>::readList(Istream&) : "
119 "reading entry"
120 );
121 }
122 }
123 else
124 {
125 // Uniform content (delimiter == token::BEGIN_BLOCK)
126
127 T elem;
128 is >> elem;
129
130 is.fatalCheck
131 (
132 "List<T>::readList(Istream&) : "
133 "reading the single entry"
134 );
135
136 for (label i=0; i<len; ++i)
137 {
138 list[i] = elem; // Copy the value
139 }
140 }
141 }
142
143 // End of contents marker
144 is.readEndList("List");
145 }
146 }
147 else if (tok.isPunctuation(token::BEGIN_LIST))
148 {
149 // "(...)" : read as SLList and transfer contents
150
151 is.putBack(tok); // Putback the opening bracket
152 SLList<T> sll(is); // Read as singly-linked list
153
154 // Reallocate and move assign list elements
155 list = std::move(sll);
156 }
157 else
158 {
160 << "incorrect first token, expected <int> or '(', found "
161 << tok.info() << nl
162 << exit(FatalIOError);
163 }
164
165 return is;
166}
167
168
169// ************************************************************************* //
Non-intrusive singly-linked list.
streamFormat format() const noexcept
Get the current stream format.
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
Template class for non-intrusive linked lists.
Definition: LList.H:79
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void transfer(List< T > &list)
Definition: List.C:447
constexpr List() noexcept
Default construct.
Definition: ListI.H:95
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:49
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:251
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:258
A templated class for holding compound tokens.
Definition: token.H:250
A token holds an item read from Istream.
Definition: token.H:69
compound & transferCompoundToken()
Return reference to compound and mark internally as released.
Definition: token.C:90
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
label labelToken() const
Return label value.
Definition: tokenI.H:513
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:716
const volScalarField & T
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FUNCTION_NAME
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
To & dynamicCast(From &r)
Definition: typeInfo.H:88
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78