ListIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "List.H"
27 #include "Istream.H"
28 #include "token.H"
29 #include "SLList.H"
30 #include "contiguous.H"
31 
32 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
33 
34 template<class T>
36 :
37  UList<T>(nullptr, 0)
38 {
39  operator>>(is, *this);
40 }
41 
42 
43 template<class T>
45 {
46  // Anull list
47  L.setSize(0);
48 
49  is.fatalCheck("operator>>(Istream&, List<T>&)");
50 
51  token firstToken(is);
52 
53  is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
54 
55  if (firstToken.isCompound())
56  {
57  L.transfer
58  (
60  (
61  firstToken.transferCompoundToken(is)
62  )
63  );
64  }
65  else if (firstToken.isLabel())
66  {
67  label s = firstToken.labelToken();
68 
69  // Set list length to that read
70  L.setSize(s);
71 
72  // Read list contents depending on data format
73 
74  if (is.format() == IOstream::ASCII || !contiguous<T>())
75  {
76  // Read beginning of contents
77  char delimiter = is.readBeginList("List");
78 
79  if (s)
80  {
81  if (delimiter == token::BEGIN_LIST)
82  {
83  for (label i=0; i<s; i++)
84  {
85  is >> L[i];
86 
87  is.fatalCheck
88  (
89  "operator>>(Istream&, List<T>&) : reading entry"
90  );
91  }
92  }
93  else
94  {
95  T element;
96  is >> element;
97 
98  is.fatalCheck
99  (
100  "operator>>(Istream&, List<T>&) : "
101  "reading the single entry"
102  );
103 
104  for (label i=0; i<s; i++)
105  {
106  L[i] = element;
107  }
108  }
109  }
110 
111  // Read end of contents
112  is.readEndList("List");
113  }
114  else
115  {
116  if (s)
117  {
118  is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
119 
120  is.fatalCheck
121  (
122  "operator>>(Istream&, List<T>&) : reading the binary block"
123  );
124  }
125  }
126  }
127  else if (firstToken.isPunctuation())
128  {
129  if (firstToken.pToken() != token::BEGIN_LIST)
130  {
132  << "incorrect first token, expected '(', found "
133  << firstToken.info()
134  << exit(FatalIOError);
135  }
136 
137  // Putback the opening bracket
138  is.putBack(firstToken);
139 
140  // Now read as a singly-linked list
141  SLList<T> sll(is);
142 
143  // Convert the singly-linked list to this list
144  L = sll;
145  }
146  else
147  {
149  << "incorrect first token, expected <int> or '(', found "
150  << firstToken.info()
151  << exit(FatalIOError);
152  }
153 
154  return is;
155 }
156 
157 
158 template<class T>
160 {
161  List<T> L;
162  token firstToken(is);
163  is.putBack(firstToken);
164 
165  if (firstToken.isPunctuation())
166  {
167  if (firstToken.pToken() != token::BEGIN_LIST)
168  {
170  << "incorrect first token, expected '(', found "
171  << firstToken.info()
172  << exit(FatalIOError);
173  }
174 
175  // Read via a singly-linked list
176  L = SLList<T>(is);
177  }
178  else
179  {
180  // Create list with a single item
181  L.setSize(1);
182 
183  is >> L[0];
184  }
185 
186  return L;
187 }
188 
189 
190 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:392
punctuationToken pToken() const
Definition: tokenI.H:248
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
compound & transferCompoundToken(const Istream &is)
Definition: token.C:93
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:85
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
char readEndList(const char *funcName)
Definition: Istream.C:148
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:391
Template class for non-intrusive linked lists.
Definition: LList.H:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:72
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Template function to specify if the data of a type are contiguous.
List< T > readList(Istream &)
Read a bracket-delimited list, or handle a single value as list of size 1.
bool isCompound() const
Definition: tokenI.H:520
A templated class for holding compound tokens.
Definition: token.H:209
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
virtual Istream & read(token &)=0
Return next token from stream.
Istream & operator>>(Istream &, directionInfo &)
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
char readBeginList(const char *funcName)
Definition: Istream.C:127
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
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:60
const volScalarField & T
List()
Null constructor.
Definition: ListI.H:104
void setSize(const label)
Reset size of List.
Definition: List.C:281
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:149
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
label labelToken() const
Definition: tokenI.H:397
Non-intrusive singly-linked list.
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
bool isPunctuation() const
Definition: tokenI.H:243
IOerror FatalIOError