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-2022 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() == IOstreamOption::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 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:132
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:439
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
A token holds an item read from Istream.
Definition: token.H:64
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:244
A templated class for holding compound tokens.
Definition: token.H:281
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
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:99
Istream & readList(Istream &is)
Read List from Istream, discarding contents of existing List.
Definition: ListIO.C:42
#define FUNCTION_NAME
Type & dynamicCast(U &obj)
A dynamic_cast (for references) that generates FatalError on failed casts.
Definition: typeInfo.H:108
const volScalarField & T
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
Non-intrusive singly-linked list.
constexpr List() noexcept
Default construct.
Definition: ListI.H:88
streamFormat format() const noexcept
Get the current stream format.
std::streamsize size_bytes() const noexcept
Number of contiguous bytes for the List data.
Definition: UListI.H:251
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...