Updated List read

This commit is contained in:
Hrvoje Jasak 2020-03-20 14:00:19 +00:00
parent cb6aa365c3
commit 24584aa90c

View file

@ -41,57 +41,66 @@ Foam::List<T>::List(Istream& is)
template<class T> template<class T>
Foam::Istream& Foam::operator>>(Istream& is, List<T>& L) Foam::Istream& Foam::operator>>(Istream& is, List<T>& list)
{ {
// Anull list // Anull list
L.setSize(0); list.resize(0);
is.fatalCheck("operator>>(Istream&, List<T>&)"); is.fatalCheck(FUNCTION_NAME);
token firstToken(is); token firstToken(is);
is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token"); is.fatalCheck(FUNCTION_NAME);
// Compound: simply transfer contents
if (firstToken.isCompound()) if (firstToken.isCompound())
{ {
L.transfer list.transfer
( (
dynamicCast<token::Compound<List<T> > > dynamicCast<token::Compound<List<T> > >
( (
firstToken.transferCompoundToken(is) firstToken.transferCompoundToken(is)
) )
); );
}
else if (firstToken.isLabel())
{
label s = firstToken.labelToken();
// Set list length to that read return is;
L.setSize(s); }
// Label: could be int(..), int{...} or just a plain '0'
if (firstToken.isLabel())
{
const label len = firstToken.labelToken();
// Resize to length read
list.resize(len);
// Read list contents depending on data format // Read list contents depending on data format
if (is.format() == IOstream::ASCII || !contiguous<T>()) if (is.format() == IOstream::ASCII || !contiguous<T>())
{ {
// Read beginning of contents // Read beginning of contents
char delimiter = is.readBeginList("List"); const char delimiter = is.readBeginList("List");
if (s) if (len)
{ {
if (delimiter == token::BEGIN_LIST) if (delimiter == token::BEGIN_LIST)
{ {
for (label i=0; i<s; i++) for (label i=0; i<len; ++i)
{ {
is >> L[i]; is >> list[i];
is.fatalCheck is.fatalCheck
( (
"operator>>(Istream&, List<T>&) : reading entry" "operator>>(Istream&, List<T>&) : "
"reading entry"
); );
} }
} }
else else
{ {
// Uniform content (delimiter == token::BEGIN_BLOCK)
T element; T element;
is >> element; is >> element;
@ -101,9 +110,9 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
"reading the single entry" "reading the single entry"
); );
for (label i=0; i<s; i++) for (label i=0; i<len; ++i)
{ {
L[i] = element; list[i] = element; // Copy the value
} }
} }
} }
@ -111,20 +120,25 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
// Read end of contents // Read end of contents
is.readEndList("List"); is.readEndList("List");
} }
else else if (len)
{ {
if (s) // Non-empty, binary, contiguous
{
is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
is.fatalCheck is.read(reinterpret_cast<char*>(list.data()), len*sizeof(T));
(
"operator>>(Istream&, List<T>&) : reading the binary block" is.fatalCheck
); (
} "operator>>(Istream&, List<T>&) : "
"reading the binary block"
);
} }
return is;
} }
else if (firstToken.isPunctuation())
// "(...)" : read as SLList and transfer contents
if (firstToken.isPunctuation())
{ {
if (firstToken.pToken() != token::BEGIN_LIST) if (firstToken.pToken() != token::BEGIN_LIST)
{ {
@ -134,23 +148,22 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
<< exit(FatalIOError); << exit(FatalIOError);
} }
// Putback the opening bracket is.putBack(firstToken); // Putback the opening bracket
is.putBack(firstToken);
// Now read as a singly-linked list SLList<T> sll(is); // Read as singly-linked list
SLList<T> sll(is);
// Convert the singly-linked list to this list // Reallocate and move assign list elements
L = sll; list = std::move(sll);
}
else return is;
{
FatalIOErrorInFunction(is)
<< "incorrect first token, expected <int> or '(', found "
<< firstToken.info()
<< exit(FatalIOError);
} }
FatalIOErrorInFunction(is)
<< "incorrect first token, expected <int> or '(', found "
<< firstToken.info()
<< exit(FatalIOError);
return is; return is;
} }