From 24584aa90cc9587b9d6899b4a1a0e6d7a4147560 Mon Sep 17 00:00:00 2001 From: Hrvoje Jasak Date: Fri, 20 Mar 2020 14:00:19 +0000 Subject: [PATCH] Updated List read --- src/foam/containers/Lists/List/ListIO.C | 95 ++++++++++++++----------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/src/foam/containers/Lists/List/ListIO.C b/src/foam/containers/Lists/List/ListIO.C index 44a13c786..180a17403 100644 --- a/src/foam/containers/Lists/List/ListIO.C +++ b/src/foam/containers/Lists/List/ListIO.C @@ -41,57 +41,66 @@ Foam::List::List(Istream& is) template -Foam::Istream& Foam::operator>>(Istream& is, List& L) +Foam::Istream& Foam::operator>>(Istream& is, List& list) { // Anull list - L.setSize(0); + list.resize(0); - is.fatalCheck("operator>>(Istream&, List&)"); + is.fatalCheck(FUNCTION_NAME); token firstToken(is); - is.fatalCheck("operator>>(Istream&, List&) : reading first token"); + is.fatalCheck(FUNCTION_NAME); + // Compound: simply transfer contents if (firstToken.isCompound()) { - L.transfer + list.transfer ( dynamicCast > > ( firstToken.transferCompoundToken(is) ) ); - } - else if (firstToken.isLabel()) - { - label s = firstToken.labelToken(); - // Set list length to that read - L.setSize(s); + return is; + } + + + // 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 if (is.format() == IOstream::ASCII || !contiguous()) { // Read beginning of contents - char delimiter = is.readBeginList("List"); + const char delimiter = is.readBeginList("List"); - if (s) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i> L[i]; + is >> list[i]; is.fatalCheck ( - "operator>>(Istream&, List&) : reading entry" + "operator>>(Istream&, List&) : " + "reading entry" ); } } else { + // Uniform content (delimiter == token::BEGIN_BLOCK) + T element; is >> element; @@ -101,9 +110,9 @@ Foam::Istream& Foam::operator>>(Istream& is, List& L) "reading the single entry" ); - for (label i=0; i>(Istream& is, List& L) // Read end of contents is.readEndList("List"); } - else + else if (len) { - if (s) - { - is.read(reinterpret_cast(L.data()), s*sizeof(T)); + // Non-empty, binary, contiguous - is.fatalCheck - ( - "operator>>(Istream&, List&) : reading the binary block" - ); - } + is.read(reinterpret_cast(list.data()), len*sizeof(T)); + + is.fatalCheck + ( + "operator>>(Istream&, List&) : " + "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) { @@ -134,23 +148,22 @@ Foam::Istream& Foam::operator>>(Istream& is, List& L) << exit(FatalIOError); } - // Putback the opening bracket - is.putBack(firstToken); + is.putBack(firstToken); // Putback the opening bracket - // Now read as a singly-linked list - SLList sll(is); + SLList sll(is); // Read as singly-linked list - // Convert the singly-linked list to this list - L = sll; - } - else - { - FatalIOErrorInFunction(is) - << "incorrect first token, expected or '(', found " - << firstToken.info() - << exit(FatalIOError); + // Reallocate and move assign list elements + list = std::move(sll); + + return is; } + + FatalIOErrorInFunction(is) + << "incorrect first token, expected or '(', found " + << firstToken.info() + << exit(FatalIOError); + return is; }