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-2019 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 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
36 
37 template<class T>
39 :
40  UList<T>(nullptr, 0)
41 {
42  operator>>(is, *this);
43 }
44 
45 
46 template<class T>
48 {
49  // Anull list
50  list.resize(0);
51 
53 
54  token firstToken(is);
55 
57 
58  // Compound: simply transfer contents
59  if (firstToken.isCompound())
60  {
61  list.transfer
62  (
64  (
65  firstToken.transferCompoundToken(is)
66  )
67  );
68 
69  return is;
70  }
71 
72 
73  // Label: could be int(..), int{...} or just a plain '0'
74  if (firstToken.isLabel())
75  {
76  const label len = firstToken.labelToken();
77 
78  // Resize to length read
79  list.resize(len);
80 
81  // Read list contents depending on data format
82 
83  if (is.format() == IOstream::ASCII || !is_contiguous<T>::value)
84  {
85  // Read beginning of contents
86  const char delimiter = is.readBeginList("List");
87 
88  if (len)
89  {
90  if (delimiter == token::BEGIN_LIST)
91  {
92  for (label i=0; i<len; ++i)
93  {
94  is >> list[i];
95 
96  is.fatalCheck
97  (
98  "operator>>(Istream&, List<T>&) : "
99  "reading entry"
100  );
101  }
102  }
103  else
104  {
105  // Uniform content (delimiter == token::BEGIN_BLOCK)
106 
107  T element;
108  is >> element;
109 
110  is.fatalCheck
111  (
112  "operator>>(Istream&, List<T>&) : "
113  "reading the single entry"
114  );
115 
116  for (label i=0; i<len; ++i)
117  {
118  list[i] = element; // Copy the value
119  }
120  }
121  }
122 
123  // Read end of contents
124  is.readEndList("List");
125  }
126  else if (len)
127  {
128  // Non-empty, binary, contiguous
129 
130  Detail::readContiguous<T>
131  (
132  is,
133  reinterpret_cast<char*>(list.data()),
134  len*sizeof(T)
135  );
136 
137  is.fatalCheck
138  (
139  "operator>>(Istream&, List<T>&) : "
140  "reading the binary block"
141  );
142  }
143 
144  return is;
145  }
146 
147 
148  // "(...)" : read as SLList and transfer contents
149  if (firstToken.isPunctuation())
150  {
151  if (firstToken.pToken() != token::BEGIN_LIST)
152  {
154  << "incorrect first token, expected '(', found "
155  << firstToken.info()
156  << exit(FatalIOError);
157  }
158 
159  is.putBack(firstToken); // Putback the opening bracket
160 
161  SLList<T> sll(is); // Read as singly-linked list
162 
163  // Reallocate and move assign list elements
164  list = std::move(sll);
165 
166  return is;
167  }
168 
169 
171  << "incorrect first token, expected <int> or '(', found "
172  << firstToken.info()
173  << exit(FatalIOError);
174 
175  return is;
176 }
177 
178 
179 // ************************************************************************* //
token.H
List.H
Foam::token::Compound
A templated class for holding compound tokens.
Definition: token.H:225
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:57
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:167
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
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:459
Istream.H
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::List::List
constexpr List() noexcept
Null constructor.
Definition: ListI.H:94
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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 token.
Definition: Istream.C:53
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
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401