diff --git a/applications/test/bitSet1/Test-bitSet1.C b/applications/test/bitSet1/Test-bitSet1.C index 1f5bb1acc3..a6dd8e1418 100644 --- a/applications/test/bitSet1/Test-bitSet1.C +++ b/applications/test/bitSet1/Test-bitSet1.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -37,6 +37,8 @@ Description #include "HashSet.H" #include "ListOps.H" #include "cpuTime.H" +#include "StringStream.H" +#include "FlatOutput.H" #include #include @@ -65,6 +67,28 @@ inline Ostream& report } +// Create equivalent to flat output +inline void undecorated +( + Ostream& os, + const bitSet& list +) +{ + const label len = list.size(); + + os << token::BEGIN_LIST; + + // Contents + for (label i=0; i < len; ++i) + { + if (i) os << token::SPACE; + os << label(list.get(i)); + } + + os << token::END_LIST; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // int main(int argc, char *argv[]) @@ -97,6 +121,28 @@ int main(int argc, char *argv[]) Info<<"bits used: " << flatOutput(set3b.toc()) << nl; Info<<"inverted: " << flatOutput(invert(set3b)) << nl; + + Info<< "Test read/write (ASCII)" << nl; + + OStringStream ostr; + + undecorated(ostr, set3a); // like flatOutput + ostr << bitSet(); + set3a.flip(); + undecorated(ostr, set3a); // like flatOutput + + { + IStringStream istr(ostr.str()); + Info<< "parse: " << istr.str() << nl; + + bitSet bset1(istr); + bitSet bset2(istr); + bitSet bset3(istr); + + Info<< "got: " << bset1 << nl + << "and: " << bset2 << nl + << "and: " << bset3 << nl; + } } Info<< "End\n" << endl; diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H index c27a442ece..09f3668cc1 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedList.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedList.H @@ -416,7 +416,7 @@ public: Ostream& printBits(Ostream& os, bool debugOutput=false) const; //- Clear list and read from stream - Istream& read(Istream& is); + Istream& readList(Istream& is); //- Write List, with line-breaks in ASCII when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H index 98e179b9cd..95aa8e32d7 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListI.H @@ -212,7 +212,7 @@ inline Foam::PackedList::PackedList(Istream& is) blocks_(), size_(0) { - read(is); + readList(is); } diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C index 9bcf609c53..c2b0ca7e63 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C @@ -61,70 +61,33 @@ Foam::Ostream& Foam::PackedList::printBits template -Foam::Istream& Foam::PackedList::read(Istream& is) +Foam::Istream& Foam::PackedList::readList(Istream& is) { PackedList& list = *this; + // Anull list list.clear(); + is.fatalCheck(FUNCTION_NAME); - token firstTok(is); - is.fatalCheck - ( - "PackedList::read(Istream&) : " - "reading first token" - ); + token tok(is); - if (firstTok.isLabel()) + is.fatalCheck("PackedList::readList(Istream&) : reading first token"); + + if (tok.isLabel()) { - const label len = firstTok.labelToken(); + const label len = tok.labelToken(); // Set list length to that read list.resize(len); - // Read list contents depending on data format - if (is.format() == IOstream::ASCII) + if (is.format() == IOstream::BINARY) { - // Read beginning of contents - const char delimiter = is.readBeginList("PackedList"); - - if (len) - { - if (delimiter == token::BEGIN_LIST) - { - for (label i=0; i(list.data()), @@ -133,37 +96,73 @@ Foam::Istream& Foam::PackedList::read(Istream& is) is.fatalCheck ( - "PackedList::read(Istream&) : " + "PackedList::readList(Istream&) : " "reading the binary block" ); } } + else + { + // Begin of contents marker + const char delimiter = is.readBeginList("PackedList"); + + if (len) + { + if (delimiter == token::BEGIN_LIST) + { + for (label i=0; i> tok; is.fatalCheck(FUNCTION_NAME); - while (!nextTok.isPunctuation(token::END_LIST)) + while (!tok.isPunctuation(token::END_LIST)) { - is.putBack(nextTok); + is.putBack(tok); list.append(list.readValue(is)); - is >> nextTok; + is >> tok; is.fatalCheck(FUNCTION_NAME); } } - else if (firstTok.isPunctuation(token::BEGIN_BLOCK)) + else if (tok.isPunctuation(token::BEGIN_BLOCK)) { - token nextTok(is); + is >> tok; is.fatalCheck(FUNCTION_NAME); - while (!nextTok.isPunctuation(token::END_BLOCK)) + while (!tok.isPunctuation(token::END_BLOCK)) { - is.putBack(nextTok); + is.putBack(tok); list.setPair(is); - is >> nextTok; + is >> tok; is.fatalCheck(FUNCTION_NAME); } } @@ -171,7 +170,7 @@ Foam::Istream& Foam::PackedList::read(Istream& is) { FatalIOErrorInFunction(is) << "incorrect first token, expected , '(' or '{', found " - << firstTok.info() << nl + << tok.info() << nl << exit(FatalIOError); } @@ -189,40 +188,11 @@ Foam::Ostream& Foam::PackedList::writeList const PackedList& list = *this; const label len = list.size(); - // Write list contents depending on data format - if (os.format() == IOstream::ASCII) + if (os.format() == IOstream::BINARY) { - if (len > 1 && list.uniform()) - { - // Two or more entries, and all have identical values. - os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; - } - else if (!shortLen || len <= shortLen) - { - // Shorter list, or line-breaks suppressed - os << len << token::BEGIN_LIST; - for (label i=0; i < len; ++i) - { - if (i) os << token::SPACE; - os << list[i]; - } - os << token::END_LIST; - } - else - { - // Longer list - os << nl << len << nl << token::BEGIN_LIST << nl; - for (label i=0; i < len; ++i) - { - os << list[i] << nl; - } - os << token::END_LIST << nl; - } - } - else - { - // Contents are binary and contiguous - os << nl << len << nl; + // Binary (always contiguous) + + os << nl << len << nl; if (len) { @@ -234,6 +204,44 @@ Foam::Ostream& Foam::PackedList::writeList ); } } + else if (len > 1 && list.uniform()) + { + // Two or more entries, and all entries have identical values. + os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; + } + else if (!shortLen || len <= shortLen) + { + // Single-line output + + // Size and start delimiter + os << len << token::BEGIN_LIST; + + // Contents + for (label i=0; i < len; ++i) + { + if (i) os << token::SPACE; + os << label(list.get(i)); + } + + // End delimiter + os << token::END_LIST; + } + else + { + // Multi-line output + + // Size and start delimiter + os << nl << len << nl << token::BEGIN_LIST << nl; + + // Contents + for (label i=0; i < len; ++i) + { + os << label(list.get(i)) << nl; + } + + // End delimiter + os << token::END_LIST << nl; + } return os; } @@ -260,7 +268,7 @@ void Foam::PackedList::writeEntry template Foam::Istream& Foam::operator>>(Istream& is, PackedList& list) { - return list.read(is); + return list.readList(is); } diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H index ee82696182..5730f29c93 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSet.H +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSet.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -74,11 +74,6 @@ class bitSet protected: - // Protected Member Functions - - //- Write as a dictionary entry - void writeEntry(Ostream& os) const; - // Logic/Set Operations //- The set difference @@ -582,16 +577,6 @@ public: inline bitSet& operator-=(const bitSet& other); - // IO - - //- Write bitSet, with line-breaks (ASCII) when length exceeds shortLen. - // Using '0' suppresses line-breaks entirely. - Ostream& writeList(Ostream& os, const label shortLen=0) const; - - //- Write as a dictionary entry with keyword - void writeEntry(const word& keyword, Ostream& os) const; - - // IOstream Operators //- Return info proxy @@ -611,8 +596,7 @@ public: // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // -//- Write bitset to Ostream, as per bitSet::writeList() with default length -//- of 40 items. +//- Write bitset to Ostream with 40 items per line. Ostream& operator<<(Ostream& os, const bitSet& bitset); //- Output bitset information diff --git a/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C b/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C index 64032740d1..a8a59d263f 100644 --- a/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C +++ b/src/OpenFOAM/containers/Bits/bitSet/bitSetIO.C @@ -28,90 +28,6 @@ License #include "bitSet.H" #include "IOstreams.H" -// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // - -void Foam::bitSet::writeEntry(Ostream& os) const -{ - os << *this; -} - - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -Foam::Ostream& Foam::bitSet::writeList -( - Ostream& os, - const label shortLen -) const -{ - const bitSet& list = *this; - const label len = list.size(); - - // Write list contents depending on data format - if (os.format() == IOstream::ASCII) - { - if (len > 1 && list.uniform()) - { - // Two or more entries, and all entries have identical values. - os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; - } - else if (!shortLen || len <= shortLen) - { - // Shorter list, or line-breaks suppressed - os << len << token::BEGIN_LIST; - for (label i=0; i < len; ++i) - { - if (i) os << token::SPACE; - os << list[i]; - } - os << token::END_LIST; - } - else - { - // Longer list - os << nl << len << nl << token::BEGIN_LIST << nl; - for (label i=0; i < len; ++i) - { - os << list[i] << nl; - } - os << token::END_LIST << nl; - } - } - else - { - // Contents are binary and contiguous - os << nl << len << nl; - - if (len) - { - // write(...) includes surrounding start/end delimiters - os.write - ( - reinterpret_cast(list.cdata()), - list.size_bytes() - ); - } - } - - return os; -} - - -void Foam::bitSet::writeEntry -( - const word& keyword, - Ostream& os -) const -{ - if (keyword.size()) - { - os.writeKeyword(keyword); - } - writeEntry(os); - os << token::END_STATEMENT << endl; -} - - // * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * * // Foam::Ostream& Foam::operator<<(Ostream& os, const bitSet& bitset) diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H index d691cca8d9..173de687e3 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTable.H @@ -168,7 +168,10 @@ public: //- Clear all entries from table and delete any allocated pointers void clear(); - //- Write + + // Reading/writing + + //- Invoke write() on each non-null entry void write(Ostream& os) const; @@ -183,6 +186,7 @@ public: // IOstream Operators + //- Clear table and read from Istream friend Istream& operator>> ( Istream& is, diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C index c1692542a9..b69b7e3c6b 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C @@ -44,7 +44,7 @@ void Foam::HashPtrTable::readIstream { is.fatalCheck(FUNCTION_NAME); - token firstToken(is); + token tok(is); is.fatalCheck ( @@ -52,9 +52,9 @@ void Foam::HashPtrTable::readIstream "reading first token" ); - if (firstToken.isLabel()) + if (tok.isLabel()) { - const label len = firstToken.labelToken(); + const label len = tok.labelToken(); // Read beginning of contents const char delimiter = is.readBeginList("HashPtrTable"); @@ -84,7 +84,8 @@ void Foam::HashPtrTable::readIstream else { FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() + << "incorrect first token, '(', found " + << tok.info() << nl << exit(FatalIOError); } } @@ -92,12 +93,13 @@ void Foam::HashPtrTable::readIstream // Read end of contents is.readEndList("HashPtrTable"); } - else if (firstToken.isPunctuation(token::BEGIN_LIST)) + else if (tok.isPunctuation(token::BEGIN_LIST)) { - token lastToken(is); - while (!lastToken.isPunctuation(token::END_LIST)) + is >> tok; + + while (!tok.isPunctuation(token::END_LIST)) { - is.putBack(lastToken); + is.putBack(tok); Key key; is >> key; this->set(key, inew(key, is).ptr()); @@ -108,14 +110,14 @@ void Foam::HashPtrTable::readIstream "reading entry" ); - is >> lastToken; + is >> tok; } } else { FatalIOErrorInFunction(is) << "incorrect first token, expected or '(', found " - << firstToken.info() + << tok.info() << nl << exit(FatalIOError); } diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H index 8e2a2e4561..ab1c2302d9 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTable.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -105,6 +105,12 @@ template class UList; template class FixedList; template class HashTable; +template +Istream& operator>>(Istream&, HashTable&); + +template +Ostream& operator<<(Ostream&, const HashTable&); + /*---------------------------------------------------------------------------*\ Class HashTable Declaration \*---------------------------------------------------------------------------*/ @@ -148,6 +154,12 @@ class HashTable template bool setEntry(const bool overwrite, const Key& key, Args&&... args); + //- Read hash table + Istream& readTable(Istream& is); + + //- Write hash table + Ostream& writeTable(Ostream& os) const; + public: @@ -893,7 +905,7 @@ public: inline constexpr const_iterator cend() const noexcept; - // Writing + // Reading/writing //- Print information Ostream& printInfo(Ostream& os) const; @@ -902,18 +914,24 @@ public: //- when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. Ostream& writeKeys(Ostream& os, const label shortLen=0) const; + + + // IOstream Operators + + friend Istream& operator>> + ( + Istream&, + HashTable& tbl + ); + + friend Ostream& operator<< + ( + Ostream&, + const HashTable& tbl + ); }; -// IOstream Operators - -template -Istream& operator>>(Istream& is, HashTable& tbl); - -template -Ostream& operator<<(Ostream& os, const HashTable& tbl); - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C index 888201f1c3..35c7b90d2a 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C @@ -141,21 +141,20 @@ Foam::Ostream& Foam::HashTable::writeKeys } -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - template -Foam::Istream& Foam::operator>> +Foam::Istream& Foam::HashTable::readTable ( - Istream& is, - HashTable& tbl + Istream& is ) { + HashTable& tbl = *this; + // Anull existing table tbl.clear(); is.fatalCheck(FUNCTION_NAME); - token firstToken(is); + token tok(is); is.fatalCheck ( @@ -163,9 +162,9 @@ Foam::Istream& Foam::operator>> "reading first token" ); - if (firstToken.isLabel()) + if (tok.isLabel()) { - const label len = firstToken.labelToken(); + const label len = tok.labelToken(); // Read beginning of contents const char delimiter = is.readBeginList("HashTable"); @@ -175,7 +174,8 @@ Foam::Istream& Foam::operator>> if (delimiter != token::BEGIN_LIST) { FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() + << "incorrect first token, '(', found " + << tok.info() << nl << exit(FatalIOError); } @@ -203,12 +203,12 @@ Foam::Istream& Foam::operator>> // Read end of contents is.readEndList("HashTable"); } - else if (firstToken.isPunctuation(token::BEGIN_LIST)) + else if (tok.isPunctuation(token::BEGIN_LIST)) { - token lastToken(is); - while (!lastToken.isPunctuation(token::END_LIST)) + is >> tok; + while (!tok.isPunctuation(token::END_LIST)) { - is.putBack(lastToken); + is.putBack(tok); Key key; @@ -222,14 +222,14 @@ Foam::Istream& Foam::operator>> "reading entry" ); - is >> lastToken; + is >> tok; } } else { FatalIOErrorInFunction(is) << "incorrect first token, expected or '(', found " - << firstToken.info() + << tok.info() << nl << exit(FatalIOError); } @@ -239,12 +239,13 @@ Foam::Istream& Foam::operator>> template -Foam::Ostream& Foam::operator<< +Foam::Ostream& Foam::HashTable::writeTable ( - Ostream& os, - const HashTable& tbl -) + Ostream& os +) const { + const HashTable& tbl = *this; + const label len = tbl.size(); if (len) @@ -271,4 +272,28 @@ Foam::Ostream& Foam::operator<< } +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +template +Foam::Istream& Foam::operator>> +( + Istream& is, + HashTable& tbl +) +{ + return tbl.readTable(is); +} + + +template +Foam::Ostream& Foam::operator<< +( + Ostream& os, + const HashTable& tbl +) +{ + return tbl.writeTable(os); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H index 95fa0d3fe6..cef22166d9 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBase.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. diff --git a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseIO.C b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseIO.C index 07ca98094c..727533b11b 100644 --- a/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseIO.C +++ b/src/OpenFOAM/containers/IndirectLists/IndirectListBase/IndirectListBaseIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2014 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -44,59 +44,9 @@ Foam::Ostream& Foam::IndirectListBase::writeList const label len = list.size(); - // Write list contents depending on data format - if (os.format() == IOstream::ASCII || !is_contiguous::value) + if (os.format() == IOstream::BINARY && is_contiguous::value) { - if (len > 1 && is_contiguous::value && list.uniform()) - { - // Two or more entries, and all entries have identical values. - os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; - } - else if - ( - (len <= 1 || !shortLen) - || - ( - (len <= shortLen) - && - ( - Detail::ListPolicy::no_linebreak::value - || is_contiguous::value - ) - ) - ) - { - // Size and start delimiter - os << len << token::BEGIN_LIST; - - // Contents - for (label i=0; i < len; ++i) - { - if (i) os << token::SPACE; - os << list[i]; - } - - // End delimiter - os << token::END_LIST; - } - else - { - // Size and start delimiter - os << nl << len << nl << token::BEGIN_LIST << nl; - - // Contents - for (label i=0; i < len; ++i) - { - os << list[i] << nl; - } - - // End delimiter - os << token::END_LIST << nl; - } - } - else - { - // Contents are binary and contiguous + // Binary and contiguous os << nl << len << nl; if (len) @@ -119,6 +69,56 @@ Foam::Ostream& Foam::IndirectListBase::writeList os.endRawWrite(); } } + else if (len > 1 && is_contiguous::value && list.uniform()) + { + // Two or more entries, and all entries have identical values. + os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK; + } + else if + ( + (len <= 1 || !shortLen) + || + ( + (len <= shortLen) + && + ( + is_contiguous::value + || Detail::ListPolicy::no_linebreak::value + ) + ) + ) + { + // Single-line output + + // Size and start delimiter + os << len << token::BEGIN_LIST; + + // Contents + for (label i=0; i < len; ++i) + { + if (i) os << token::SPACE; + os << list[i]; + } + + // End delimiter + os << token::END_LIST; + } + else + { + // Multi-line output + + // Size and start delimiter + os << nl << len << nl << token::BEGIN_LIST << nl; + + // Contents + for (label i=0; i < len; ++i) + { + os << list[i] << nl; + } + + // End delimiter + os << token::END_LIST << nl; + } os.check(FUNCTION_NAME); return os; diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H index f30fc3f387..9ab4510e47 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2018 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -46,7 +46,7 @@ SourceFiles namespace Foam { -// Forward declarations +// Forward Declarations class Istream; class Ostream; @@ -79,7 +79,7 @@ public: // Constructors - //- Null construct + //- Default construct ILList() = default; //- Construct and insert the initial T item pointer @@ -126,7 +126,7 @@ public: void transfer(ILList& lst); - // Member operators + // Member Operators //- Copy assignment using the 'clone()' method for each element void operator=(const ILList& lst); @@ -135,7 +135,7 @@ public: void operator=(ILList&& lst); - // Istream operator + // Istream Operator //- Read from Istream, discarding existing contents. friend Istream& operator>> diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C index 48fcff12d0..8dad8a9923 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C @@ -38,7 +38,7 @@ void Foam::ILList::readIstream(Istream& is, const INew& inew) { is.fatalCheck(FUNCTION_NAME); - token firstToken(is); + token tok(is); is.fatalCheck ( @@ -46,9 +46,9 @@ void Foam::ILList::readIstream(Istream& is, const INew& inew) "reading first token" ); - if (firstToken.isLabel()) + if (tok.isLabel()) { - const label len = firstToken.labelToken(); + const label len = tok.labelToken(); // Read beginning of contents const char delimiter = is.readBeginList("ILList"); @@ -69,7 +69,7 @@ void Foam::ILList::readIstream(Istream& is, const INew& inew) ); } } - else + else // BEGIN_BLOCK { T* p = inew(is).ptr(); this->append(p); @@ -90,19 +90,19 @@ void Foam::ILList::readIstream(Istream& is, const INew& inew) // Read end of contents is.readEndList("ILList"); } - else if (firstToken.isPunctuation(token::BEGIN_LIST)) + else if (tok.isPunctuation(token::BEGIN_LIST)) { - token lastToken(is); + is >> tok; is.fatalCheck(FUNCTION_NAME); - while (!lastToken.isPunctuation(token::END_LIST)) + while (!tok.isPunctuation(token::END_LIST)) { - is.putBack(lastToken); + is.putBack(tok); T* p = inew(is).ptr(); this->append(p); - is >> lastToken; + is >> tok; is.fatalCheck(FUNCTION_NAME); } } @@ -110,7 +110,7 @@ void Foam::ILList::readIstream(Istream& is, const INew& inew) { FatalIOErrorInFunction(is) << "incorrect first token, expected or '(', found " - << firstToken.info() + << tok.info() << nl << exit(FatalIOError); } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H index 9a8d0318b3..8b9a60811f 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -47,7 +47,7 @@ SourceFiles namespace Foam { -// Forward declarations +// Forward Declarations class Istream; class Ostream; @@ -162,7 +162,7 @@ public: // Constructors - //- Null construct + //- Default construct LList() = default; //- Construct and copy insert the initial T item @@ -283,7 +283,7 @@ public: void transfer(LList& lst); - // Member operators + // Member Operators //- Copy assignment void operator=(const LList& lst); @@ -295,7 +295,10 @@ public: void operator=(std::initializer_list lst); - // IOstream operators + // IOstream Operators + + //- Read list from Istream + Istream& readList(Istream& is); //- Write LList with line-breaks when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C index 9941055c42..efc97814bd 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C @@ -42,22 +42,24 @@ Foam::LList::LList(Istream& is) // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template -Foam::Istream& Foam::operator>>(Istream& is, LList& lst) +Foam::Istream& Foam::LList::readList(Istream& is) { + LList& list = *this; + // Anull list - lst.clear(); + list.clear(); is.fatalCheck(FUNCTION_NAME); - token firstToken(is); + token tok(is); is.fatalCheck("LList::readList : reading first token"); - if (firstToken.isLabel()) + if (tok.isLabel()) { - const label len = firstToken.labelToken(); + const label len = tok.labelToken(); - // Read beginning of contents + // Begin of contents marker const char delimiter = is.readBeginList("LList"); if (len) @@ -68,7 +70,7 @@ Foam::Istream& Foam::operator>>(Istream& is, LList& lst) { T element; is >> element; - lst.append(element); + list.append(element); } } else @@ -78,28 +80,28 @@ Foam::Istream& Foam::operator>>(Istream& is, LList& lst) for (label i=0; i> tok; is.fatalCheck(FUNCTION_NAME); - while (!lastToken.isPunctuation(token::END_LIST)) + while (!tok.isPunctuation(token::END_LIST)) { - is.putBack(lastToken); + is.putBack(tok); T element; is >> element; - lst.append(element); + list.append(element); - is >> lastToken; + is >> tok; is.fatalCheck(FUNCTION_NAME); } } @@ -107,7 +109,7 @@ Foam::Istream& Foam::operator>>(Istream& is, LList& lst) { FatalIOErrorInFunction(is) << "incorrect first token, expected or '(', found " - << firstToken.info() + << tok.info() << exit(FatalIOError); } @@ -123,6 +125,8 @@ Foam::Ostream& Foam::LList::writeList const label shortLen ) const { + // NB: no binary, contiguous output + const label len = this->size(); if @@ -167,9 +171,16 @@ Foam::Ostream& Foam::LList::writeList template -Foam::Ostream& Foam::operator<<(Ostream& os, const LList& lst) +Foam::Istream& Foam::operator>>(Istream& is, LList& list) { - return lst.writeList(os, -1); // Always with line breaks + return list.readList(is); +} + + +template +Foam::Ostream& Foam::operator<<(Ostream& os, const LList& list) +{ + return list.writeList(os, -1); // Always with line breaks } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C index 78f0038f34..a689bda501 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C @@ -39,7 +39,7 @@ void Foam::LPtrList::readIstream(Istream& is, const INew& inew) { is.fatalCheck(FUNCTION_NAME); - token firstToken(is); + token tok(is); is.fatalCheck ( @@ -47,9 +47,9 @@ void Foam::LPtrList::readIstream(Istream& is, const INew& inew) "reading first token" ); - if (firstToken.isLabel()) + if (tok.isLabel()) { - const label len = firstToken.labelToken(); + const label len = tok.labelToken(); // Read beginning of contents const char delimiter = is.readBeginList("LPtrList"); @@ -70,7 +70,7 @@ void Foam::LPtrList::readIstream(Istream& is, const INew& inew) ); } } - else + else // Assumed to be token::BEGIN_BLOCK { T* p = inew(is).ptr(); this->append(p); @@ -91,17 +91,17 @@ void Foam::LPtrList::readIstream(Istream& is, const INew& inew) // Read end of contents is.readEndList("LPtrList"); } - else if (firstToken.isPunctuation(token::BEGIN_LIST)) + else if (tok.isPunctuation(token::BEGIN_LIST)) { - token lastToken(is); + is >> tok; is.fatalCheck(FUNCTION_NAME); - while (!lastToken.isPunctuation(token::END_LIST)) + while (!tok.isPunctuation(token::END_LIST)) { - is.putBack(lastToken); + is.putBack(tok); this->append(inew(is).ptr()); - is >> lastToken; + is >> tok; is.fatalCheck(FUNCTION_NAME); } } @@ -109,7 +109,7 @@ void Foam::LPtrList::readIstream(Istream& is, const INew& inew) { FatalIOErrorInFunction(is) << "incorrect first token, expected or '(', found " - << firstToken.info() + << tok.info() << nl << exit(FatalIOError); } diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H index 83af52262f..a194b42103 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILList.H @@ -47,8 +47,7 @@ SourceFiles namespace Foam { -// Forward declarations - +// Forward Declarations class Ostream; template class UILList; @@ -107,7 +106,7 @@ public: // Constructors - //- Null construct + //- Default construct UILList() = default; //- Construct and insert the initial T item @@ -179,7 +178,7 @@ public: bool operator!=(const UILList& lst) const; - // IOstream operators + // IOstream Operators //- Write UILList with line-breaks when length exceeds shortLen. // Using '0' suppresses line-breaks entirely. diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C index 7d1bdb2c94..f7cac7f181 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/UILList/UILListIO.C @@ -39,6 +39,8 @@ Foam::Ostream& Foam::UILList::writeList const label shortLen ) const { + // NB: no binary, contiguous output + const label len = this->size(); if diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index 5060385a01..76670941fe 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -334,20 +334,26 @@ public: inline void operator=(SortableList&& lst); + // Reading/writing + + //- Read from Istream, discarding existing contents + inline Istream& readList(Istream& is); + + // IOstream Operators //- Read from Istream, discarding existing contents friend Istream& operator>> ( Istream& is, - DynamicList& rhs + DynamicList& list ); //- Write to Ostream friend Ostream& operator<< ( Ostream& os, - const DynamicList& rhs + const DynamicList& list ); }; diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 6263b781c7..fdab0b30d2 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -223,9 +223,11 @@ inline Foam::DynamicList::DynamicList template inline Foam::DynamicList::DynamicList(Istream& is) : - List(is), - capacity_(List::size()) -{} + List(), + capacity_(0) +{ + this->readList(is); +} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -865,20 +867,32 @@ inline void Foam::DynamicList::operator= // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // +template +inline Foam::Istream& Foam::DynamicList::readList +( + Istream& is +) +{ + DynamicList& list = *this; + + // Use entire storage - ie, resize(capacity()) + (void) list.expandStorage(); + + static_cast&>(list).readList(is); + list.capacity_ = list.size(); + + return is; +} + + template inline Foam::Istream& Foam::operator>> ( Istream& is, - DynamicList& rhs + DynamicList& list ) { - // Use entire storage - ie, resize(capacity()) - (void) rhs.expandStorage(); - - is >> static_cast&>(rhs); - rhs.capacity_ = rhs.List::size(); - - return is; + return list.readList(is); } @@ -886,10 +900,10 @@ template inline Foam::Ostream& Foam::operator<< ( Ostream& os, - const DynamicList& rhs + const DynamicList& list ) { - os << static_cast&>(rhs); + os << static_cast&>(list); return os; } diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index a4406b5861..cc4d642f41 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -430,7 +430,10 @@ public: bool operator>=(const FixedList& list) const; - // Writing + // Reading/writing + + //- Read from Istream, discarding contents of existing List + Istream& readList(Istream& is); //- Write the list as a dictionary entry with keyword void writeEntry(const word& keyword, Ostream& os) const; diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C index 198383ce40..437b608f9b 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -72,62 +72,65 @@ Foam::Ostream& Foam::FixedList::writeList { const FixedList& list = *this; - // Write list contents depending on data format + // Unlike UList, no compact ascii output since a FixedList is generally + // small and we prefer a consistent appearance. + // Eg, FixedList or Pair as "(-1 -1)", never as "2{-1}" - // Unlike UList, no compact output form since a FixedList is generally - // small and we desire a consistent appearance. - // Eg, FixedList or Pair as "(-1 -1)", not as "2{-1}" - - if (os.format() == IOstream::ASCII || !is_contiguous::value) + if (os.format() == IOstream::BINARY && is_contiguous::value) { - if + // Binary and contiguous. Size is always non-zero + + // write(...) includes surrounding start/end delimiters + os.write ( - (N <= 1 || !shortLen) - || + reinterpret_cast(list.cdata()), + list.size_bytes() + ); + } + else if + ( + (N <= 1 || !shortLen) + || + ( + (N <= unsigned(shortLen)) + && ( - (N <= unsigned(shortLen)) - && - ( - Detail::ListPolicy::no_linebreak::value - || is_contiguous::value - ) + is_contiguous::value + || Detail::ListPolicy::no_linebreak::value ) ) + ) + { + // Single-line output + + // Start delimiter + os << token::BEGIN_LIST; + + // Contents + for (unsigned i=0; i(list.cdata()), N*sizeof(T)); + // Start delimiter + os << nl << token::BEGIN_LIST << nl; + + // Contents + for (unsigned i=0; i::writeList } -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - template -Foam::FixedList::FixedList(Istream& is) +Foam::Istream& Foam::FixedList::readList +( + Istream& is +) { - operator>>(is, *this); -} + FixedList& list = *this; - -template -Foam::Istream& Foam::operator>>(Foam::Istream& is, FixedList& list) -{ is.fatalCheck(FUNCTION_NAME); - if (is.format() == IOstream::ASCII || !is_contiguous::value) + if (is.format() == IOstream::BINARY && is_contiguous::value) { - token firstToken(is); + // Binary and contiguous + + Detail::readContiguous + ( + is, + reinterpret_cast(list.data()), + list.size_bytes() + ); is.fatalCheck ( - "operator>>(Istream&, FixedList&) : " + "FixedList::readList(Istream&) : " + "reading the binary block" + ); + } + else + { + token tok(is); + + is.fatalCheck + ( + "FixedList::readList(Istream&) : " "reading first token" ); - if (firstToken.isCompound()) + if (tok.isCompound()) { + // Compound: transfer contents list = dynamicCast>> ( - firstToken.transferCompoundToken(is) + tok.transferCompoundToken(is) ); } - else if (firstToken.isLabel()) + else if (tok.isLabel()) { - const label len = firstToken.labelToken(); + const label len = tok.labelToken(); // List lengths must match list.checkSize(len); } - else if (!firstToken.isPunctuation()) + else if (!tok.isPunctuation()) { FatalIOErrorInFunction(is) << "incorrect first token, expected