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 -------------------------------------------------------------------------------
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 "List.H"
30 #include "Istream.H"
31 #include "token.H"
32 #include "SLList.H"
33 #include "contiguous.H"
34 
35 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 
37 template<class T>
39 :
40  UList<T>(nullptr, 0)
41 {
42  this->readList(is);
43 }
44 
45 
46 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
47 
48 template<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  (
70  tok.transferCompoundToken(is)
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 
83  if (is.format() == IOstream::BINARY && is_contiguous<T>::value)
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 // ************************************************************************* //
token.H
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
List.H
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::token::Compound
A templated class for holding compound tokens.
Definition: token.H:246
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::List::readList
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:49
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::LList
Template class for non-intrusive linked lists.
Definition: LList.H:54
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
Istream.H
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::List::List
constexpr List() noexcept
Default construct.
Definition: ListI.H:95
Foam::token::transferCompoundToken
compound & transferCompoundToken()
Return reference to compound and mark internally as released.
Definition: token.C:90
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::nl
constexpr char nl
Definition: Ostream.H:404
SLList.H
Non-intrusive singly-linked list.
contiguous.H
Foam::dynamicCast
To & dynamicCast(From &r)
Definition: typeInfo.H:88
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::token::isCompound
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:716
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75