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 | Copyright (C) 2011-2015 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 // Construct from Istream
35 template<class T>
37 :
38  UList<T>(NULL, 0)
39 {
40  operator>>(is, *this);
41 }
42 
43 
44 template<class T>
46 {
47  // Anull list
48  L.setSize(0);
49 
50  is.fatalCheck("operator>>(Istream&, List<T>&)");
51 
52  token firstToken(is);
53 
54  is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
55 
56  if (firstToken.isCompound())
57  {
58  L.transfer
59  (
61  (
62  firstToken.transferCompoundToken(is)
63  )
64  );
65  }
66  else if (firstToken.isLabel())
67  {
68  label s = firstToken.labelToken();
69 
70  // Set list length to that read
71  L.setSize(s);
72 
73  // Read list contents depending on data format
74 
75  if (is.format() == IOstream::ASCII || !contiguous<T>())
76  {
77  // Read beginning of contents
78  char delimiter = is.readBeginList("List");
79 
80  if (s)
81  {
82  if (delimiter == token::BEGIN_LIST)
83  {
84  for (label i=0; i<s; i++)
85  {
86  is >> L[i];
87 
88  is.fatalCheck
89  (
90  "operator>>(Istream&, List<T>&) : reading entry"
91  );
92  }
93  }
94  else
95  {
96  T element;
97  is >> element;
98 
99  is.fatalCheck
100  (
101  "operator>>(Istream&, List<T>&) : "
102  "reading the single entry"
103  );
104 
105  for (label i=0; i<s; i++)
106  {
107  L[i] = element;
108  }
109  }
110  }
111 
112  // Read end of contents
113  is.readEndList("List");
114  }
115  else
116  {
117  if (s)
118  {
119  is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
120 
121  is.fatalCheck
122  (
123  "operator>>(Istream&, List<T>&) : reading the binary block"
124  );
125  }
126  }
127  }
128  else if (firstToken.isPunctuation())
129  {
130  if (firstToken.pToken() != token::BEGIN_LIST)
131  {
132  FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
133  << "incorrect first token, expected '(', found "
134  << firstToken.info()
135  << exit(FatalIOError);
136  }
137 
138  // Putback the opening bracket
139  is.putBack(firstToken);
140 
141  // Now read as a singly-linked list
142  SLList<T> sll(is);
143 
144  // Convert the singly-linked list to this list
145  L = sll;
146  }
147  else
148  {
149  FatalIOErrorIn("operator>>(Istream&, List<T>&)", is)
150  << "incorrect first token, expected <int> or '(', found "
151  << firstToken.info()
152  << exit(FatalIOError);
153  }
154 
155  return is;
156 }
157 
158 
159 template<class T>
161 {
162  List<T> L;
163  token firstToken(is);
164  is.putBack(firstToken);
165 
166  if (firstToken.isPunctuation())
167  {
168  if (firstToken.pToken() != token::BEGIN_LIST)
169  {
170  FatalIOErrorIn("readList<T>(Istream&)", is)
171  << "incorrect first token, expected '(', found "
172  << firstToken.info()
173  << exit(FatalIOError);
174  }
175 
176  // read via a singly-linked list
177  L = SLList<T>(is);
178  }
179  else
180  {
181  // create list with a single item
182  L.setSize(1);
183 
184  is >> L[0];
185  }
186 
187  return L;
188 }
189 
190 
191 // ************************************************************************* //
Non-intrusive singly-linked list.
Definition: SLList.H:47
bool isLabel() const
Definition: tokenI.H:262
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 ))
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:390
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
char readBeginList(const char *funcName)
Definition: Istream.C:131
virtual Istream & read(token &)=0
Return next token from stream.
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
bool isPunctuation() const
Definition: tokenI.H:203
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const volScalarField & T
Definition: createFields.H:25
T * data()
Return a pointer to the first data element,.
Definition: UListI.H:152
List()
Null constructor.
Definition: ListI.H:29
friend Istream & operator>>(Istream &, List< T > &)
Read List from Istream, discarding contents of existing List.
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
void setSize(const label)
Reset size of List.
Definition: List.C:318
IOerror FatalIOError
void putBack(const token &)
Put back token.
Definition: Istream.C:30
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
char readEndList(const char *funcName)
Definition: Istream.C:152
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:107
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
A templated class for holding compound tokens.
Definition: token.H:213
A token holds items read from Istream.
Definition: token.H:67
Template function to specify if the data of a type are contiguous.
compound & transferCompoundToken(const Istream &is)
Definition: token.C:93
label labelToken() const
Definition: tokenI.H:267
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:362
To & dynamicCast(From &r)
Reference type cast template function,.
Definition: typeInfo.H:85
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
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
punctuationToken pToken() const
Definition: tokenI.H:208
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325