From 51cd7ceecbdb3e11be5acb2e0f05b6b276e575ea Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Thu, 25 Feb 2021 10:14:47 +0100 Subject: [PATCH] ENH: improvements for token methods - direct check of punctuation. For example, while (!tok.isPunctuation(token::BEGIN_LIST)) .. instead of while (!(tok.isPunctuation() && tok.pToken() == token::BEGIN_LIST)) .. Using direct comparison (tok != token::BEGIN_LIST) can be fragile when comparing int values: int c = readChar(is); while (tok != c) .. // Danger, uses LABEL comparison! - direct check of word. For example, if (tok.isWord("uniform")) .. instead of if (tok.isWord() && tok.wordToken() == "uniform") .. - make token lineNumber() a setter method ENH: adjust internal compound method empty() -> moved() - support named compound tokens STYLE: setter method for stream indentation --- .../test/OListStream/Test-OListStream.C | 4 +- .../cv2DControls/cv2DControls.C | 6 +- .../dataConversion/smapToFoam/smapToFoam.C | 3 +- .../containers/Bits/PackedList/PackedListIO.C | 62 ++++------- .../HashTables/HashPtrTable/HashPtrTableIO.C | 19 +--- .../HashTables/HashTable/HashTableIO.C | 19 +--- .../LinkedLists/accessTypes/ILList/ILListIO.C | 19 +--- .../LinkedLists/accessTypes/LList/LListIO.C | 19 +--- .../accessTypes/LPtrList/LPtrListIO.C | 19 +--- src/OpenFOAM/containers/Lists/List/ListIO.C | 45 +++----- src/OpenFOAM/containers/Lists/UList/UListIO.C | 45 +++----- .../containers/PtrLists/PtrList/PtrListIO.C | 46 +++----- src/OpenFOAM/db/IOobject/IOobjectReadHeader.C | 9 +- .../decomposedBlockData/decomposedBlockData.C | 7 +- src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H | 43 ++++++-- .../db/IOstreams/Pstreams/UIPstream.C | 4 +- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C | 4 +- src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C | 6 +- src/OpenFOAM/db/IOstreams/token/token.C | 10 +- src/OpenFOAM/db/IOstreams/token/token.H | 103 ++++++++++++------ src/OpenFOAM/db/IOstreams/token/tokenI.H | 60 ++++++---- src/OpenFOAM/db/IOstreams/token/tokenIO.C | 4 +- .../dictionaryListEntryIO.C | 31 ++---- src/OpenFOAM/db/dictionary/entry/entryIO.C | 8 +- .../expressions/exprTools/exprTools.C | 18 +-- src/OpenFOAM/fields/Fields/Field/Field.C | 64 +++++------ .../vtk/file/foamVtkSeriesWriter.C | 10 +- .../genericFaPatchField/genericFaPatchField.C | 17 +-- .../genericFvPatchField/genericFvPatchField.C | 17 +-- .../genericFvsPatchField.C | 17 +-- .../genericPointPatchField.C | 11 +- src/lagrangian/basic/IOPosition/IOPosition.C | 36 ++---- .../blockMesh/blockMesh/blockMeshTopology.C | 10 +- .../blockMeshTools/blockMeshToolsTemplates.C | 53 ++++----- .../blockVertices/blockVertex/blockVertex.C | 6 +- .../gradingDescriptor/gradingDescriptor.C | 4 +- .../PatchFunction1/MappedFile/rawIOField.C | 7 +- .../chemkinReader/chemkinLexer.L | 4 +- 38 files changed, 353 insertions(+), 516 deletions(-) diff --git a/applications/test/OListStream/Test-OListStream.C b/applications/test/OListStream/Test-OListStream.C index e24a9e55ee8..c0e19de3cde 100644 --- a/applications/test/OListStream/Test-OListStream.C +++ b/applications/test/OListStream/Test-OListStream.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -214,7 +214,7 @@ int main(int argc, char *argv[]) { OListStream os2; - os2.indentSize() = 0; + os2.indentSize(0); outputDict(os2); diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoi2DMesh/cv2DControls/cv2DControls.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoi2DMesh/cv2DControls/cv2DControls.C index c63ddc5fe41..96dfbe8fb73 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoi2DMesh/cv2DControls/cv2DControls.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoi2DMesh/cv2DControls/cv2DControls.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2013-2015 OpenFOAM Foundation - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -134,7 +134,7 @@ Foam::cv2DControls::cv2DControls void Foam::cv2DControls::write(Ostream& os) const { - os.indentLevel() = 1; + const auto oldLevel = os.indentLevel(1); os.precision(2); os.flags(ios_base::scientific); @@ -148,6 +148,8 @@ void Foam::cv2DControls::write(Ostream& os) const << indent << "ppDist_ : " << ppDist_ << nl << indent << "minEdgeLen2_ : " << minEdgeLen2_ << nl << token::END_BLOCK << endl; + + os.indentLevel(oldLevel); } diff --git a/applications/utilities/postProcessing/dataConversion/smapToFoam/smapToFoam.C b/applications/utilities/postProcessing/dataConversion/smapToFoam/smapToFoam.C index 32a81d37942..af91270a9e5 100644 --- a/applications/utilities/postProcessing/dataConversion/smapToFoam/smapToFoam.C +++ b/applications/utilities/postProcessing/dataConversion/smapToFoam/smapToFoam.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation + Copyright (C) 2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -96,7 +97,7 @@ int main(int argc, char *argv[]) break; } - if (!fieldName.isWord() || fieldName.wordToken() != "CELL") + if (!fieldName.isWord("CELL")) { FatalErrorInFunction << "Expected first CELL, found " diff --git a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C index 19cac26bff9..45bb1c1fea7 100644 --- a/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C +++ b/src/OpenFOAM/containers/Bits/PackedList/PackedListIO.C @@ -139,59 +139,39 @@ Foam::Istream& Foam::PackedList<Width>::read(Istream& is) } } } - else if (firstTok.isPunctuation()) + else if (firstTok.isPunctuation(token::BEGIN_LIST)) { - if (firstTok.pToken() == token::BEGIN_LIST) - { - token nextTok(is); - is.fatalCheck(FUNCTION_NAME); - - while - ( - !( nextTok.isPunctuation() - && nextTok.pToken() == token::END_LIST - ) - ) - { - is.putBack(nextTok); - list.append(list.readValue(is)); + token nextTok(is); + is.fatalCheck(FUNCTION_NAME); - is >> nextTok; - is.fatalCheck(FUNCTION_NAME); - } - } - else if (firstTok.pToken() == token::BEGIN_BLOCK) + while (!nextTok.isPunctuation(token::END_LIST)) { - token nextTok(is); - is.fatalCheck(FUNCTION_NAME); + is.putBack(nextTok); + list.append(list.readValue(is)); - while - ( - !( nextTok.isPunctuation() - && nextTok.pToken() == token::END_BLOCK - ) - ) - { - is.putBack(nextTok); - list.setPair(is); - - is >> nextTok; - is.fatalCheck(FUNCTION_NAME); - } + is >> nextTok; + is.fatalCheck(FUNCTION_NAME); } - else + } + else if (firstTok.isPunctuation(token::BEGIN_BLOCK)) + { + token nextTok(is); + is.fatalCheck(FUNCTION_NAME); + + while (!nextTok.isPunctuation(token::END_BLOCK)) { - FatalIOErrorInFunction(is) - << "incorrect first token, expected '(', found " - << firstTok.info() - << exit(FatalIOError); + is.putBack(nextTok); + list.setPair(is); + + is >> nextTok; + is.fatalCheck(FUNCTION_NAME); } } else { FatalIOErrorInFunction(is) << "incorrect first token, expected <int>, '(' or '{', found " - << firstTok.info() + << firstTok.info() << nl << exit(FatalIOError); } diff --git a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C index 7adb7c00614..c1692542a97 100644 --- a/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashPtrTable/HashPtrTableIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -92,23 +92,10 @@ void Foam::HashPtrTable<T, Key, Hash>::readIstream // Read end of contents is.readEndList("HashPtrTable"); } - else if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() - << exit(FatalIOError); - } - token lastToken(is); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) + while (!lastToken.isPunctuation(token::END_LIST)) { is.putBack(lastToken); Key key; diff --git a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C index ae0ca036e46..888201f1c37 100644 --- a/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C +++ b/src/OpenFOAM/containers/HashTables/HashTable/HashTableIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -203,23 +203,10 @@ Foam::Istream& Foam::operator>> // Read end of contents is.readEndList("HashTable"); } - else if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() - << exit(FatalIOError); - } - token lastToken(is); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) + while (!lastToken.isPunctuation(token::END_LIST)) { is.putBack(lastToken); diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C index c10293b901c..48fcff12d07 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/ILList/ILListIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2018 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -90,25 +90,12 @@ void Foam::ILList<LListBase, T>::readIstream(Istream& is, const INew& inew) // Read end of contents is.readEndList("ILList"); } - else if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() - << exit(FatalIOError); - } - token lastToken(is); is.fatalCheck(FUNCTION_NAME); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) + while (!lastToken.isPunctuation(token::END_LIST)) { is.putBack(lastToken); diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C index 1994232d762..9941055c426 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LList/LListIO.C @@ -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. @@ -86,25 +86,12 @@ Foam::Istream& Foam::operator>>(Istream& is, LList<LListBase, T>& lst) // Read end of contents is.readEndList("LList"); } - else if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() - << exit(FatalIOError); - } - token lastToken(is); is.fatalCheck(FUNCTION_NAME); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) + while (!lastToken.isPunctuation(token::END_LIST)) { is.putBack(lastToken); diff --git a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C index a7fe35090b0..78f0038f34b 100644 --- a/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C +++ b/src/OpenFOAM/containers/LinkedLists/accessTypes/LPtrList/LPtrListIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2018 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -91,25 +91,12 @@ void Foam::LPtrList<LListBase, T>::readIstream(Istream& is, const INew& inew) // Read end of contents is.readEndList("LPtrList"); } - else if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() - << exit(FatalIOError); - } - token lastToken(is); is.fatalCheck(FUNCTION_NAME); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) + while (!lastToken.isPunctuation(token::END_LIST)) { is.putBack(lastToken); this->append(inew(is).ptr()); diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index 534a1817596..8458c9cc2f3 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -55,9 +55,10 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& list) is.fatalCheck(FUNCTION_NAME); - // Compound: simply transfer contents if (firstToken.isCompound()) { + // Compound: simply transfer contents + list.transfer ( dynamicCast<token::Compound<List<T>>> @@ -65,14 +66,11 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& list) firstToken.transferCompoundToken(is) ) ); - - return is; } - - - // Label: could be int(..), int{...} or just a plain '0' - if (firstToken.isLabel()) + else if (firstToken.isLabel()) { + // Label: could be int(..), int{...} or just a plain '0' + const label len = firstToken.labelToken(); // Resize to length read @@ -140,37 +138,24 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& list) "reading the binary block" ); } - - return is; } - - - // "(...)" : read as SLList and transfer contents - if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, expected '(', found " - << firstToken.info() - << exit(FatalIOError); - } + // "(...)" : read as SLList and transfer contents is.putBack(firstToken); // Putback the opening bracket - SLList<T> sll(is); // Read as singly-linked list // Reallocate and move assign list elements list = std::move(sll); - - return is; } - - - FatalIOErrorInFunction(is) - << "incorrect first token, expected <int> or '(', found " - << firstToken.info() - << exit(FatalIOError); + else + { + FatalIOErrorInFunction(is) + << "incorrect first token, expected <int> or '(', found " + << firstToken.info() << nl + << exit(FatalIOError); + } return is; } diff --git a/src/OpenFOAM/containers/Lists/UList/UListIO.C b/src/OpenFOAM/containers/Lists/UList/UListIO.C index b5369cdb9fe..c77d98c0cfa 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListIO.C +++ b/src/OpenFOAM/containers/Lists/UList/UListIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -170,9 +170,10 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list) is.fatalCheck("operator>>(Istream&, UList<T>&) : reading first token"); - // Compound: simply transfer contents if (firstToken.isCompound()) { + // Compound: simply transfer contents + List<T> elems; elems.transfer ( @@ -197,14 +198,11 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list) { list[i] = std::move(elems[i]); } - - return is; } - - - // Label: could be int(..), int{...} or just a plain '0' - if (firstToken.isLabel()) + else if (firstToken.isLabel()) { + // Label: could be int(..), int{...} or just a plain '0' + const label inputLen = firstToken.labelToken(); // List lengths must match @@ -276,24 +274,12 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list) "operator>>(Istream&, UList<T>&) : reading the binary block" ); } - - return is; } - - - // "(...)" : read as SLList and transfer contents - if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, expected '(', found " - << firstToken.info() - << exit(FatalIOError); - } + // "(...)" : read as SLList and transfer contents is.putBack(firstToken); // Putback the opening bracket - SLList<T> sll(is); // Read as singly-linked list // List lengths must match @@ -310,15 +296,14 @@ Foam::Istream& Foam::operator>>(Istream& is, UList<T>& list) { list[i] = std::move(sll.removeHead()); } - - return is; } - - - FatalIOErrorInFunction(is) - << "incorrect first token, expected <int> or '(', found " - << firstToken.info() - << exit(FatalIOError); + else + { + FatalIOErrorInFunction(is) + << "incorrect first token, expected <int> or '(', found " + << firstToken.info() << nl + << exit(FatalIOError); + } return is; } diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListIO.C b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListIO.C index 67017d0c165..d89b403bd02 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListIO.C +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -49,10 +49,10 @@ void Foam::PtrList<T>::readIstream(Istream& is, const INew& inew) "reading first token" ); - - // Label: could be int(..), int{...} or just a plain '0' if (firstToken.isLabel()) { + // Label: could be int(..), int{...} or just a plain '0' + // Read size of list const label len = firstToken.labelToken(); @@ -98,33 +98,17 @@ void Foam::PtrList<T>::readIstream(Istream& is, const INew& inew) // Read end of contents is.readEndList("PtrList"); - - return; } - - - // "(...)" : read as SLList and transfer contents - // This would be more efficient (fewer allocations, lower overhead) - // using a DynamicList, but then we have circular dependencies - if (firstToken.isPunctuation()) + else if (firstToken.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " << firstToken.info() - << exit(FatalIOError); - } + // "(...)" : read as SLList and transfer contents + // This would be more efficient (fewer allocations, lower overhead) + // using a DynamicList, but then we have circular dependencies SLList<T*> slList; token lastToken(is); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) + while (!lastToken.isPunctuation(token::END_LIST)) { is.putBack(lastToken); @@ -147,14 +131,14 @@ void Foam::PtrList<T>::readIstream(Istream& is, const INew& inew) { set(i++, ptr); } - - return; } - - FatalIOErrorInFunction(is) - << "incorrect first token, expected <int> or '(', found " - << firstToken.info() - << exit(FatalIOError); + else + { + FatalIOErrorInFunction(is) + << "incorrect first token, expected <int> or '(', found " + << firstToken.info() << nl + << exit(FatalIOError); + } } diff --git a/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C b/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C index fe6aed04bbd..7726a6d76a6 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C +++ b/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -62,12 +62,7 @@ bool Foam::IOobject::readHeader(Istream& is) token firstToken(is); - if - ( - is.good() - && firstToken.isWord() - && firstToken.wordToken() == "FoamFile" - ) + if (is.good() && firstToken.isWord("FoamFile")) { const dictionary headerDict(is); diff --git a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C index d95db4561cb..41234f75097 100644 --- a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C +++ b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockData.C @@ -1084,12 +1084,7 @@ Foam::label Foam::decomposedBlockData::numBlocks(const fileName& fName) // FoamFile header token firstToken(is); - if - ( - is.good() - && firstToken.isWord() - && firstToken.wordToken() == "FoamFile" - ) + if (is.good() && firstToken.isWord("FoamFile")) { dictionary headerDict(is); is.version(headerDict.get<token>("version")); diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H index d6f4eab9c5c..e27dcc0c6ad 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -171,32 +171,36 @@ public: //- Add indentation characters virtual void indent() = 0; - //- Return indent level - unsigned short indentSize() const + //- Return indent size (spaces per level) + unsigned short indentSize() const noexcept { return indentSize_; } - //- Access to indent size - unsigned short& indentSize() + //- Change indent size (spaces per level), return old value + unsigned short indentSize(unsigned short val) noexcept { - return indentSize_; + auto old(indentSize_); + indentSize_ = val; + return old; } - //- Return indent level - unsigned short indentLevel() const + //- Return the indent level + unsigned short indentLevel() const noexcept { return indentLevel_; } - //- Access to indent level - unsigned short& indentLevel() + //- Change the indent level, return old value + unsigned short indentLevel(unsigned short val) noexcept { - return indentLevel_; + auto old(indentLevel_); + indentLevel_ = val; + return old; } //- Increment the indent level - void incrIndent() + void incrIndent() noexcept { ++indentLevel_; } @@ -293,6 +297,21 @@ public: { return const_cast<Ostream&>(*this); } + + + // Housekeeping + + //- Access to indent level + unsigned short& indentLevel() noexcept + { + return indentLevel_; + } + + //- Access to indent size + unsigned short& indentSize() noexcept + { + return indentSize_; + } }; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C index 7bd2b3764d8..eed1e261cbc 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -202,7 +202,7 @@ Foam::Istream& Foam::UIPstream::read(token& t) // Set the line number of this token to the current stream line number - t.lineNumber() = lineNumber(); + t.lineNumber(this->lineNumber()); // Analyse input starting with this character. switch (c) diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index cfc56d7e9ad..d1c4adc93d1 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.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. @@ -175,7 +175,7 @@ Foam::Istream& Foam::ISstream::read(token& t) char c = nextValid(); // Set the line number of this token to the current stream line number - t.lineNumber() = lineNumber(); + t.lineNumber(this->lineNumber()); // Return on error if (!c) diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C index 8fca84428d8..4736a1eb463 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2020 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -285,11 +285,11 @@ Foam::Istream& Foam::ITstream::read(token& tok) if (size()) { - tok.lineNumber() = tokenList::last().lineNumber(); + tok.lineNumber(tokenList::last().lineNumber()); } else { - tok.lineNumber() = lineNumber(); + tok.lineNumber(this->lineNumber()); } } diff --git a/src/OpenFOAM/db/IOstreams/token/token.C b/src/OpenFOAM/db/IOstreams/token/token.C index 6eac51cc1f7..313ca75fd18 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.C +++ b/src/OpenFOAM/db/IOstreams/token/token.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. @@ -94,7 +94,7 @@ Foam::token::compound& Foam::token::transferCompoundToken() parseError("compound"); } - if (data_.compoundPtr->empty()) + if (data_.compoundPtr->moved()) { FatalErrorInFunction << "compound has already been transferred from token\n " @@ -102,7 +102,7 @@ Foam::token::compound& Foam::token::transferCompoundToken() } else { - data_.compoundPtr->empty() = true; + data_.compoundPtr->moved(true); } return *data_.compoundPtr; @@ -116,7 +116,7 @@ Foam::token::compound& Foam::token::transferCompoundToken(const Istream& is) parseError("compound"); } - if (data_.compoundPtr->empty()) + if (data_.compoundPtr->moved()) { FatalIOErrorInFunction(is) << "compound has already been transferred from token\n " @@ -124,7 +124,7 @@ Foam::token::compound& Foam::token::transferCompoundToken(const Istream& is) } else { - data_.compoundPtr->empty() = true; + data_.compoundPtr->moved(true); } return *data_.compoundPtr; diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index 407a1c74c2d..4f26bc3ef01 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.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. @@ -115,22 +115,17 @@ public: enum punctuationToken : char { NULL_TOKEN = '\0', //!< Nul character - SPACE = ' ', //!< Space [isspace] TAB = '\t', //!< Tab [isspace] NL = '\n', //!< Newline [isspace] + SPACE = ' ', //!< Space [isspace] - END_STATEMENT = ';', //!< End entry [#isseparator] - BEGIN_LIST = '(', //!< Begin list [#isseparator] - END_LIST = ')', //!< End list [#isseparator] - BEGIN_SQR = '[', //!< Begin dimensions [#isseparator] - END_SQR = ']', //!< End dimensions [#isseparator] - BEGIN_BLOCK = '{', //!< Begin block [#isseparator] - END_BLOCK = '}', //!< End block [#isseparator] COLON = ':', //!< Colon [#isseparator] + SEMICOLON = ';', //!< Semicolon [#isseparator] COMMA = ',', //!< Comma [#isseparator] HASH = '#', //!< Hash - directive or verbatim string DOLLAR = '$', //!< Dollar - start variable - ATSYM = '@', //!< At + QUESTION = '?', //!< Question mark (eg, ternary) + ATSYM = '@', //!< The 'at' symbol SQUOTE = '\'', //!< Single quote DQUOTE = '"', //!< Double quote @@ -140,8 +135,25 @@ public: MULTIPLY = '*', //!< Multiply [#isseparator] DIVIDE = '/', //!< Divide [#isseparator] - BEGIN_STRING = DQUOTE, //!< Begin string with double quote - END_STRING = DQUOTE //!< End string with double quote + LPAREN = '(', //!< Left parenthesis [#isseparator] + RPAREN = ')', //!< Right parenthesis [#isseparator] + LSQUARE = '[', //!< Left square bracket [#isseparator] + RSQUARE = ']', //!< Right square bracket [#isseparator] + LBRACE = '{', //!< Left brace [#isseparator] + RBRACE = '}', //!< Right brace [#isseparator] + + // With semantically meaning + + END_STATEMENT = SEMICOLON, //!< End entry [#isseparator] + BEGIN_LIST = LPAREN, //!< Begin list [#isseparator] + END_LIST = RPAREN, //!< End list [#isseparator] + BEGIN_SQR = LSQUARE, //!< Begin dimensions [#isseparator] + END_SQR = RSQUARE, //!< End dimensions [#isseparator] + BEGIN_BLOCK = LBRACE, //!< Begin block [#isseparator] + END_BLOCK = RBRACE, //!< End block [#isseparator] + + BEGIN_STRING = DQUOTE, //!< Begin string with double quote + END_STRING = DQUOTE //!< End string with double quote }; @@ -150,7 +162,8 @@ public: : public refCount { - bool empty_; + //- Has compound token already been transferred + bool moved_; //- No copy construct compound(const compound&) = delete; @@ -179,7 +192,7 @@ public: //- Default construct constexpr compound() noexcept : - empty_(false) + moved_(false) {} //- Construct compound from Istream @@ -195,16 +208,16 @@ public: //- Test if name is a known (registered) compound type static bool isCompound(const word& name); - //- Has compound been transferred? - bool empty() const noexcept + //- Get compound transferred status + bool moved() const noexcept { - return empty_; + return moved_; } - //- Access to empty - bool& empty() noexcept + //- Set compound transferred status + void moved(bool b) noexcept { - return empty_; + moved_ = b; } //- The size of the underlying content @@ -256,9 +269,6 @@ public: // Member Functions - //- Using empty from compound refCount - using token::compound::empty; - //- The size of the underlying content virtual label size() const { @@ -416,8 +426,8 @@ public: //- The line number for the token inline label lineNumber() const noexcept; - //- The line number for the token - inline label& lineNumber() noexcept; + //- Change token line number, return old value + inline label lineNumber(const label lineNum) noexcept; //- True if token is not UNDEFINED or ERROR inline bool good() const noexcept; @@ -437,6 +447,9 @@ public: //- Token is PUNCTUATION inline bool isPunctuation() const noexcept; + //- True if token is PUNCTUATION and equal to parameter + inline bool isPunctuation(const punctuationToken p) const noexcept; + //- Token is PUNCTUATION and isseparator inline bool isSeparator() const noexcept; @@ -458,6 +471,9 @@ public: //- Token is WORD or DIRECTIVE word inline bool isWord() const noexcept; + //- Token is WORD or DIRECTIVE word and equal to parameter + inline bool isWord(const std::string& s) const; + //- Token is DIRECTIVE (word variant) inline bool isDirective() const noexcept; @@ -599,20 +615,20 @@ public: // Equality inline bool operator==(const token& tok) const; - inline bool operator==(const punctuationToken p) const; - inline bool operator==(const label val) const; - inline bool operator==(const floatScalar val) const; - inline bool operator==(const doubleScalar val) const; + inline bool operator==(const punctuationToken p) const noexcept; + inline bool operator==(const label val) const noexcept; + inline bool operator==(const floatScalar val) const noexcept; + inline bool operator==(const doubleScalar val) const noexcept; inline bool operator==(const std::string& s) const; // Inequality inline bool operator!=(const token& tok) const; - inline bool operator!=(const punctuationToken p) const; - inline bool operator!=(const label val) const; - inline bool operator!=(const floatScalar val) const; - inline bool operator!=(const doubleScalar val) const; + inline bool operator!=(const punctuationToken p) const noexcept; + inline bool operator!=(const label val) const noexcept; + inline bool operator!=(const floatScalar val) const noexcept; + inline bool operator!=(const doubleScalar val) const noexcept; inline bool operator!=(const std::string& s) const; @@ -628,6 +644,10 @@ public: // Housekeeping + //- Write access for the token line number + // \deprecated(2021-03) - use lineNumber(label) + label& lineNumber() noexcept { return line_; } + //- Token is FLOAT // \deprecated(2020-01) - isFloat() bool isFloatScalar() const { return isFloat(); }; @@ -670,12 +690,23 @@ Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip); // Handling of compound types -#define defineCompoundTypeName(Type, Name) \ +//- Define compound using \a Type for its name +#define defineCompoundTypeName(Type, UnusedTag) \ defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0); -#define addCompoundToRunTimeSelectionTable(Type, Name) \ +//- Define compound using \a Name for its name +#define defineNamedCompoundTypeName(Type, Name) \ + defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Name, 0); + +//- Add compound to selection table, lookup using typeName +#define addCompoundToRunTimeSelectionTable(Type, Tag) \ + token::compound::addIstreamConstructorToTable<token::Compound<Type>> \ + add##Tag##IstreamConstructorToTable_; + +//- Add compound to selection table, lookup as \a Name +#define addNamedCompoundToRunTimeSelectionTable(Type, Tag, Name) \ token::compound::addIstreamConstructorToTable<token::Compound<Type>> \ - add##Name##IstreamConstructorToTable_; + add##Tag##IstreamConstructorToTable_(#Name); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/db/IOstreams/token/tokenI.H b/src/OpenFOAM/db/IOstreams/token/tokenI.H index eda92d59680..fdd5916274d 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenI.H +++ b/src/OpenFOAM/db/IOstreams/token/tokenI.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. @@ -390,9 +390,11 @@ inline Foam::label Foam::token::lineNumber() const noexcept } -inline Foam::label& Foam::token::lineNumber() noexcept +inline Foam::label Foam::token::lineNumber(const label lineNum) noexcept { - return line_; + label old(line_); + line_ = lineNum; + return old; } @@ -456,15 +458,13 @@ inline bool Foam::token::isPunctuation() const noexcept } -inline Foam::token::punctuationToken Foam::token::pToken() const +inline bool Foam::token::isPunctuation(const punctuationToken p) const noexcept { - if (type_ == tokenType::PUNCTUATION) - { - return data_.punctuationVal; - } - - parseError("punctuation character"); - return punctuationToken::NULL_TOKEN; + return + ( + type_ == tokenType::PUNCTUATION + && data_.punctuationVal == p + ); } @@ -478,6 +478,18 @@ inline bool Foam::token::isSeparator() const noexcept } +inline Foam::token::punctuationToken Foam::token::pToken() const +{ + if (type_ == tokenType::PUNCTUATION) + { + return data_.punctuationVal; + } + + parseError("punctuation character"); + return punctuationToken::NULL_TOKEN; +} + + inline bool Foam::token::isLabel() const noexcept { return (type_ == tokenType::LABEL); @@ -590,6 +602,12 @@ inline bool Foam::token::isWord() const noexcept } +inline bool Foam::token::isWord(const std::string& s) const +{ + return (isWord() && s == *data_.wordPtr); +} + + inline bool Foam::token::isDirective() const noexcept { return (type_ == tokenType::DIRECTIVE); @@ -884,9 +902,9 @@ inline bool Foam::token::operator==(const token& tok) const } -inline bool Foam::token::operator==(const punctuationToken p) const +inline bool Foam::token::operator==(const punctuationToken p) const noexcept { - return (type_ == tokenType::PUNCTUATION && data_.punctuationVal == p); + return isPunctuation(p); } @@ -901,7 +919,7 @@ inline bool Foam::token::operator==(const std::string& s) const } -inline bool Foam::token::operator==(const label val) const +inline bool Foam::token::operator==(const label val) const noexcept { return ( @@ -911,7 +929,7 @@ inline bool Foam::token::operator==(const label val) const } -inline bool Foam::token::operator==(const floatScalar val) const +inline bool Foam::token::operator==(const floatScalar val) const noexcept { return ( @@ -921,7 +939,7 @@ inline bool Foam::token::operator==(const floatScalar val) const } -inline bool Foam::token::operator==(const doubleScalar val) const +inline bool Foam::token::operator==(const doubleScalar val) const noexcept { return ( @@ -937,25 +955,25 @@ inline bool Foam::token::operator!=(const token& tok) const } -inline bool Foam::token::operator!=(const punctuationToken p) const +inline bool Foam::token::operator!=(const punctuationToken p) const noexcept { - return !operator==(p); + return !isPunctuation(p); } -inline bool Foam::token::operator!=(const label val) const +inline bool Foam::token::operator!=(const label val) const noexcept { return !operator==(val); } -inline bool Foam::token::operator!=(const floatScalar val) const +inline bool Foam::token::operator!=(const floatScalar val) const noexcept { return !operator==(val); } -inline bool Foam::token::operator!=(const doubleScalar val) const +inline bool Foam::token::operator!=(const doubleScalar val) const noexcept { return !operator==(val); } diff --git a/src/OpenFOAM/db/IOstreams/token/tokenIO.C b/src/OpenFOAM/db/IOstreams/token/tokenIO.C index 2cca7d1f296..da1a39a3cad 100644 --- a/src/OpenFOAM/db/IOstreams/token/tokenIO.C +++ b/src/OpenFOAM/db/IOstreams/token/tokenIO.C @@ -92,9 +92,9 @@ static OS& printTokenInfo(OS& os, const token& tok) case token::tokenType::COMPOUND: { - if (tok.compoundToken().empty()) + if (tok.compoundToken().moved()) { - os << "empty "; + os << "moved "; } os << "compound of type " << tok.compoundToken().type(); diff --git a/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C b/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C index db76bd1b312..72a06b2bb68 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryListEntry/dictionaryListEntryIO.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation + Copyright (C) 2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -69,43 +70,35 @@ Foam::dictionaryListEntry::dictionaryListEntry dictionary::null ) { - token firstToken(is); - if (firstToken.isLabel()) + token tok(is); + if (tok.isLabel()) { - const label sz = firstToken.labelToken(); + const label len = tok.labelToken(); is.readBeginList("List"); - for (label i=0; i<sz; ++i) + for (label i=0; i<len; ++i) { entry::New(*this, is); } is.readEndList("List"); } - else if - ( - firstToken.isPunctuation() - && firstToken.pToken() == token::BEGIN_LIST - ) + else if (tok.isPunctuation(token::BEGIN_LIST)) { while (true) { - token nextToken(is); - if (nextToken.error()) + is >> tok; + if (tok.error()) { FatalIOErrorInFunction(is) - << "parsing error " << nextToken.info() + << "parsing error " << tok.info() << nl << exit(FatalIOError); } - else if - ( - nextToken.isPunctuation() - && nextToken.pToken() == token::END_LIST - ) + else if (tok.isPunctuation(token::END_LIST)) { break; } - is.putBack(nextToken); + is.putBack(tok); entry::New(*this, is); } } @@ -113,7 +106,7 @@ Foam::dictionaryListEntry::dictionaryListEntry { FatalIOErrorInFunction(is) << "incorrect first token, expected <int> or '(', found " - << firstToken.info() + << tok.info() << nl << exit(FatalIOError); } } diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 47670683a16..48a0d9d83a6 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2019 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -133,11 +133,7 @@ bool Foam::entry::New const bool valid = getKeyword(keyword, keyToken, is); // Can accept a list of entries too - if - ( - keyToken.isLabel() - || (keyToken.isPunctuation() && keyToken.pToken() == token::BEGIN_LIST) - ) + if (keyToken.isLabel() || keyToken.isPunctuation(token::BEGIN_LIST)) { is.putBack(keyToken); return parentDict.add diff --git a/src/OpenFOAM/expressions/exprTools/exprTools.C b/src/OpenFOAM/expressions/exprTools/exprTools.C index 02a5ff3a078..7ce4f220029 100644 --- a/src/OpenFOAM/expressions/exprTools/exprTools.C +++ b/src/OpenFOAM/expressions/exprTools/exprTools.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -157,29 +157,21 @@ Foam::exprTools::getList ITstream& is = eptr->stream(); - token firstToken(is); + token tok(is); List<string> list; - if - ( - firstToken.isLabel() - || - ( - firstToken.type() == token::PUNCTUATION - && firstToken.pToken() == token::BEGIN_LIST - ) - ) + if (tok.isLabel() || tok.isPunctuation(token::BEGIN_LIST)) { // A list of strings is.rewind(); is >> list; } - else if (firstToken.isString()) + else if (tok.isString()) { // A single string list.resize(1); - list[0] = firstToken.stringToken(); + list[0] = tok.stringToken(); } else { diff --git a/src/OpenFOAM/fields/Fields/Field/Field.C b/src/OpenFOAM/fields/Fields/Field/Field.C index 45bc05f2a8d..ba91d0b82be 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.C +++ b/src/OpenFOAM/fields/Fields/Field/Field.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2015-2020 OpenCFD Ltd. + Copyright (C) 2015-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -191,47 +191,35 @@ Foam::Field<Type>::Field // Read first token token firstToken(is); - if (firstToken.isWord()) + if (firstToken.isWord("uniform")) { - if (firstToken.wordToken() == "uniform") - { - this->setSize(len); - operator=(pTraits<Type>(is)); - } - else if (firstToken.wordToken() == "nonuniform") + this->resize(len); + operator=(pTraits<Type>(is)); + } + else if (firstToken.isWord("nonuniform")) + { + is >> static_cast<List<Type>&>(*this); + const label lenRead = this->size(); + if (len != lenRead) { - is >> static_cast<List<Type>&>(*this); - label currentSize = this->size(); - if (currentSize != len) + if (len < lenRead && allowConstructFromLargerSize) { - if (len < currentSize && allowConstructFromLargerSize) - { - #ifdef FULLDEBUG - IOWarningInFunction(dict) - << "Sizes do not match. " - << "Re-sizing " << currentSize - << " entries to " << len - << endl; - #endif - - // Resize the data - this->setSize(len); - } - else - { - FatalIOErrorInFunction(dict) - << "size " << this->size() - << " is not equal to the given value of " << len - << exit(FatalIOError); - } + #ifdef FULLDEBUG + IOWarningInFunction(dict) + << "Sizes do not match. Truncating " << lenRead + << " entries to " << len << endl; + #endif + + // Truncate the data + this->resize(len); + } + else + { + FatalIOErrorInFunction(dict) + << "size " << lenRead + << " is not equal to the expected length " << len + << exit(FatalIOError); } - } - else - { - FatalIOErrorInFunction(dict) - << "Expected keyword 'uniform' or 'nonuniform', found " - << firstToken.wordToken() - << exit(FatalIOError); } } else diff --git a/src/fileFormats/vtk/file/foamVtkSeriesWriter.C b/src/fileFormats/vtk/file/foamVtkSeriesWriter.C index 104127b0623..6185cad622d 100644 --- a/src/fileFormats/vtk/file/foamVtkSeriesWriter.C +++ b/src/fileFormats/vtk/file/foamVtkSeriesWriter.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,10 +51,7 @@ namespace Foam return ( // Token 1 = ':' separator - ( - getToken(is, tok) - && tok.isPunctuation() && tok.pToken() == token::COLON - ) + (getToken(is, tok) && tok.isPunctuation(token::COLON)) // Token 2 is the value && getToken(is, tok) @@ -441,8 +438,7 @@ Foam::label Foam::vtk::seriesWriter::load if ( getValueToken(is, tok) - && tok.isPunctuation() - && tok.pToken() == token::BEGIN_SQR + && tok.isPunctuation(token::BEGIN_SQR) ) { state = parse::FILES_ARRAY; diff --git a/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C b/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C index 67ee6843944..fc9f319ccc1 100644 --- a/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C +++ b/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -97,11 +97,7 @@ Foam::genericFaPatchField<Type>::genericFaPatchField // Read first token token firstToken(is); - if - ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" - ) + if (firstToken.isWord("nonuniform")) { token fieldToken(is); @@ -306,11 +302,7 @@ Foam::genericFaPatchField<Type>::genericFaPatchField << exit(FatalIOError); } } - else if - ( - firstToken.isWord() - && firstToken.wordToken() == "uniform" - ) + else if (firstToken.isWord("uniform")) { token fieldToken(is); @@ -709,8 +701,7 @@ void Foam::genericFaPatchField<Type>::write(Ostream& os) const ( dEntry.isStream() && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" + && dEntry.stream()[0].isWord("nonuniform") ) { if (scalarFields_.found(key)) diff --git a/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C b/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C index 1aac7745bbd..3ce915e5ec6 100644 --- a/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C +++ b/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -97,11 +97,7 @@ Foam::genericFvPatchField<Type>::genericFvPatchField // Read first token token firstToken(is); - if - ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" - ) + if (firstToken.isWord("nonuniform")) { token fieldToken(is); @@ -306,11 +302,7 @@ Foam::genericFvPatchField<Type>::genericFvPatchField << exit(FatalIOError); } } - else if - ( - firstToken.isWord() - && firstToken.wordToken() == "uniform" - ) + else if (firstToken.isWord("uniform")) { token fieldToken(is); @@ -708,8 +700,7 @@ void Foam::genericFvPatchField<Type>::write(Ostream& os) const ( dEntry.isStream() && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" + && dEntry.stream()[0].isWord("nonuniform") ) { if (scalarFields_.found(key)) diff --git a/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C b/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C index bdcfd637bfb..8b3af677ff6 100644 --- a/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C +++ b/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -96,11 +96,7 @@ Foam::genericFvsPatchField<Type>::genericFvsPatchField // Read first token token firstToken(is); - if - ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" - ) + if (firstToken.isWord("nonuniform")) { token fieldToken(is); @@ -305,11 +301,7 @@ Foam::genericFvsPatchField<Type>::genericFvsPatchField << exit(FatalIOError); } } - else if - ( - firstToken.isWord() - && firstToken.wordToken() == "uniform" - ) + else if (firstToken.isWord("uniform")) { token fieldToken(is); @@ -708,8 +700,7 @@ void Foam::genericFvsPatchField<Type>::write(Ostream& os) const ( dEntry.isStream() && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" + && dEntry.stream()[0].isWord("nonuniform") ) { if (scalarFields_.found(key)) diff --git a/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C b/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C index 8a70223bd1b..a4ccaecb189 100644 --- a/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C +++ b/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -77,11 +77,7 @@ Foam::genericPointPatchField<Type>::genericPointPatchField // Read first token token firstToken(is); - if - ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" - ) + if (firstToken.isWord("nonuniform")) { token fieldToken(is); @@ -490,8 +486,7 @@ void Foam::genericPointPatchField<Type>::write(Ostream& os) const ( dEntry.isStream() && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" + && dEntry.stream()[0].isWord("nonuniform") ) { if (scalarFields_.found(key)) diff --git a/src/lagrangian/basic/IOPosition/IOPosition.C b/src/lagrangian/basic/IOPosition/IOPosition.C index 1af75c19a25..27014dab3a0 100644 --- a/src/lagrangian/basic/IOPosition/IOPosition.C +++ b/src/lagrangian/basic/IOPosition/IOPosition.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2017-2018 OpenCFD Ltd. + Copyright (C) 2017-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -98,18 +98,18 @@ void Foam::IOPosition<CloudType>::readData(Istream& is, CloudType& c) { const polyMesh& mesh = c.pMesh(); - token firstToken(is); + token tok(is); const bool newFormat = (geometryType_ == cloud::geometryType::COORDINATES); - if (firstToken.isLabel()) + if (tok.isLabel()) { - label s = firstToken.labelToken(); + const label len = tok.labelToken(); // Read beginning of contents is.readBeginList(FUNCTION_NAME); - for (label i=0; i<s; i++) + for (label i=0; i<len; ++i) { // Read position only c.append @@ -127,39 +127,27 @@ void Foam::IOPosition<CloudType>::readData(Istream& is, CloudType& c) // Read end of contents is.readEndList(FUNCTION_NAME); } - else if (firstToken.isPunctuation()) + else if (tok.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) + is >> tok; + while (!tok.isPunctuation(token::END_LIST)) { - FatalIOErrorInFunction(is) - << "incorrect first token, '(', found " - << firstToken.info() << exit(FatalIOError); - } - - token lastToken(is); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) - { - is.putBack(lastToken); + is.putBack(tok); // Read position only c.append ( new typename CloudType::particleType(mesh, is, false, newFormat) ); - is >> lastToken; + is >> tok; } } else { FatalIOErrorInFunction(is) << "incorrect first token, expected <int> or '(', found " - << firstToken.info() << exit(FatalIOError); + << tok.info() << nl + << exit(FatalIOError); } is.check(FUNCTION_NAME); diff --git a/src/mesh/blockMesh/blockMesh/blockMeshTopology.C b/src/mesh/blockMesh/blockMesh/blockMeshTopology.C index 994149d53e9..5e3a01fa33c 100644 --- a/src/mesh/blockMesh/blockMesh/blockMeshTopology.C +++ b/src/mesh/blockMesh/blockMesh/blockMeshTopology.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -139,13 +139,7 @@ void Foam::blockMesh::readPatches nPatches = 0; token lastToken(patchStream); - while - ( - !( - lastToken.isPunctuation() - && lastToken.pToken() == token::END_LIST - ) - ) + while (!lastToken.isPunctuation(token::END_LIST)) { if (tmpBlocksPatches.size() <= nPatches) { diff --git a/src/mesh/blockMesh/blockMeshTools/blockMeshToolsTemplates.C b/src/mesh/blockMesh/blockMeshTools/blockMeshToolsTemplates.C index 4118b61b870..e09b31fd0be 100644 --- a/src/mesh/blockMesh/blockMeshTools/blockMeshToolsTemplates.C +++ b/src/mesh/blockMesh/blockMeshTools/blockMeshToolsTemplates.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation + Copyright (C) 2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -31,29 +32,29 @@ template<class T> void Foam::blockMeshTools::read ( Istream& is, - List<T>& L, + List<T>& list, const dictionary& dict ) { - token firstToken(is); + token tok(is); - if (firstToken.isLabel()) + if (tok.isLabel()) { - const label s = firstToken.labelToken(); + const label len = tok.labelToken(); // Set list length to that read - L.setSize(s); + list.resize(len); // Read beginning of contents const char delimiter = is.readBeginList("List"); - if (s) + if (len) { if (delimiter == token::BEGIN_LIST) { - for (label i=0; i<s; ++i) + for (label i=0; i<len; ++i) { - read(is, L[i], dict); + read(is, list[i], dict); } } } @@ -61,39 +62,33 @@ void Foam::blockMeshTools::read // Read end of contents is.readEndList("List"); } - else if (firstToken.isPunctuation()) + else if (tok.isPunctuation(token::BEGIN_LIST)) { - if (firstToken.pToken() != token::BEGIN_LIST) - { - FatalIOErrorInFunction(is) - << "incorrect first token, expected '(', found " - << firstToken.info() - << exit(FatalIOError); - } - SLList<T> sll; - while (true) + is >> tok; + is.fatalCheck(FUNCTION_NAME); + + while (!tok.isPunctuation(token::END_LIST)) { - token t(is); - if (t.isPunctuation() && t.pToken() == token::END_LIST) - { - break; - } - is.putBack(t); + is.putBack(tok); + T elem; read(is, elem, dict); sll.append(elem); + + is >> tok; + is.fatalCheck(FUNCTION_NAME); } // Convert the singly-linked list to this list - L = sll; + list = std::move(sll); } else { FatalIOErrorInFunction(is) << "incorrect first token, expected <int> or '(', found " - << firstToken.info() + << tok.info() << nl << exit(FatalIOError); } } @@ -106,9 +101,9 @@ Foam::List<T> Foam::blockMeshTools::read const dictionary& dict ) { - List<T> L; - read(is, L, dict); - return L; + List<T> list; + read(is, list, dict); + return list; } diff --git a/src/mesh/blockMesh/blockVertices/blockVertex/blockVertex.C b/src/mesh/blockMesh/blockVertices/blockVertex/blockVertex.C index 05c2809bd3e..7f1e99575ab 100644 --- a/src/mesh/blockMesh/blockVertices/blockVertex/blockVertex.C +++ b/src/mesh/blockMesh/blockVertices/blockVertex/blockVertex.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -60,7 +60,7 @@ Foam::autoPtr<Foam::blockVertex> Foam::blockVertex::New token firstToken(is); - if (firstToken.isPunctuation() && firstToken.pToken() == token::BEGIN_LIST) + if (firstToken.isPunctuation(token::BEGIN_LIST)) { // Putback the opening bracket is.putBack(firstToken); @@ -92,7 +92,7 @@ Foam::autoPtr<Foam::blockVertex> Foam::blockVertex::New FatalIOErrorInFunction(is) << "incorrect first token, expected <word> or '(', found " - << firstToken.info() + << firstToken.info() << nl << exit(FatalIOError); return nullptr; diff --git a/src/mesh/blockMesh/gradingDescriptor/gradingDescriptor.C b/src/mesh/blockMesh/gradingDescriptor/gradingDescriptor.C index f3753a85345..e15dcfc2acc 100644 --- a/src/mesh/blockMesh/gradingDescriptor/gradingDescriptor.C +++ b/src/mesh/blockMesh/gradingDescriptor/gradingDescriptor.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2015 OpenFOAM Foundation - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -126,7 +126,7 @@ Foam::Istream& Foam::operator>>(Istream& is, gradingDescriptor& gd) gd.nDivFraction_ = 1.0; gd.expansionRatio_ = t.number(); } - else if (t.isPunctuation() && t.pToken() == token::BEGIN_LIST) + else if (t.isPunctuation(token::BEGIN_LIST)) { is >> gd.blockFraction_ >> gd.nDivFraction_ >> gd.expansionRatio_; is.readEnd("gradingDescriptor"); diff --git a/src/meshTools/PatchFunction1/MappedFile/rawIOField.C b/src/meshTools/PatchFunction1/MappedFile/rawIOField.C index 15e6fdd85e3..efa47470890 100644 --- a/src/meshTools/PatchFunction1/MappedFile/rawIOField.C +++ b/src/meshTools/PatchFunction1/MappedFile/rawIOField.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -65,10 +65,7 @@ Foam::rawIOField<Type>::rawIOField(const IOobject& io, const bool readAverage) const token firstToken(is); - headerOk = - is.good() - && firstToken.isWord() - && firstToken.wordToken() == "FoamFile"; + headerOk = is.good() && firstToken.isWord("FoamFile"); } isPtr.clear(); diff --git a/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L b/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L index b6015483f39..a117a58a85f 100644 --- a/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L +++ b/src/thermophysicalModels/reactionThermo/chemistryReaders/chemkinReader/chemkinLexer.L @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -1216,7 +1216,7 @@ bool finishReaction = false; reactionCoeffsString.replaceAll("d", "e"); reactionCoeffsString.replaceAll("D", "e"); IStringStream reactionCoeffsStream(reactionCoeffsString); - reactionCoeffsStream.lineNumber() = lineNo_; + reactionCoeffsStream.lineNumber(lineNo_); reactionCoeffsStream >> ArrheniusCoeffs[0] -- GitLab