From 9cd72b2bb861fa0d8a8a108a544fbeba021035f6 Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 4 Apr 2019 09:24:42 +0100 Subject: [PATCH 01/19] BUG: uniformFixedValue: fix tutorials. See #1046. --- .../overLaplacianDyMFoam/heatTransfer/system/controlDict | 2 +- .../cylinder/cylinderAndBackground/system/controlDict | 2 +- .../twoSimpleRotors/system/controlDict | 2 +- .../hotCylinder/cylinderAndBackground/system/controlDict | 2 +- .../cylinder/cylinderAndBackground/system/controlDict | 2 +- .../overPimpleDyMFoam/simpleRotor/system/controlDict | 2 +- .../overPimpleDyMFoam/twoSimpleRotors/system/controlDict | 2 +- .../aeroFoil/background_overset/system/controlDict | 2 +- .../overInterDyMFoam/boatAndPropeller/system/controlDict | 7 ++++++- .../floatingBody/background/system/controlDict | 2 +- .../overInterDyMFoam/twoSimpleRotors/system/controlDict | 2 +- 11 files changed, 16 insertions(+), 11 deletions(-) diff --git a/tutorials/basic/overLaplacianDyMFoam/heatTransfer/system/controlDict b/tutorials/basic/overLaplacianDyMFoam/heatTransfer/system/controlDict index 2d9534c1a7..17c38062ff 100644 --- a/tutorials/basic/overLaplacianDyMFoam/heatTransfer/system/controlDict +++ b/tutorials/basic/overLaplacianDyMFoam/heatTransfer/system/controlDict @@ -15,7 +15,7 @@ FoamFile // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Make sure all preprocessing tools know about the 'overset' bc -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); application overLaplacianDyMFoam; diff --git a/tutorials/basic/overPotentialFoam/cylinder/cylinderAndBackground/system/controlDict b/tutorials/basic/overPotentialFoam/cylinder/cylinderAndBackground/system/controlDict index 616dc8689c..289864eb29 100644 --- a/tutorials/basic/overPotentialFoam/cylinder/cylinderAndBackground/system/controlDict +++ b/tutorials/basic/overPotentialFoam/cylinder/cylinderAndBackground/system/controlDict @@ -16,7 +16,7 @@ FoamFile // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Make sure all preprocessing tools know about the 'overset' bc -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); application overPotentialFoam; diff --git a/tutorials/compressible/overRhoPimpleDyMFoam/twoSimpleRotors/system/controlDict b/tutorials/compressible/overRhoPimpleDyMFoam/twoSimpleRotors/system/controlDict index e620aa0882..505268dcae 100644 --- a/tutorials/compressible/overRhoPimpleDyMFoam/twoSimpleRotors/system/controlDict +++ b/tutorials/compressible/overRhoPimpleDyMFoam/twoSimpleRotors/system/controlDict @@ -14,7 +14,7 @@ FoamFile } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); DebugSwitches { diff --git a/tutorials/compressible/overRhoSimpleFoam/hotCylinder/cylinderAndBackground/system/controlDict b/tutorials/compressible/overRhoSimpleFoam/hotCylinder/cylinderAndBackground/system/controlDict index aa6fb00c88..914f5d9678 100644 --- a/tutorials/compressible/overRhoSimpleFoam/hotCylinder/cylinderAndBackground/system/controlDict +++ b/tutorials/compressible/overRhoSimpleFoam/hotCylinder/cylinderAndBackground/system/controlDict @@ -16,7 +16,7 @@ FoamFile // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Make sure all preprocessing tools know about the 'overset' bc -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); application overRhoSimpleFoam; diff --git a/tutorials/incompressible/overPimpleDyMFoam/cylinder/cylinderAndBackground/system/controlDict b/tutorials/incompressible/overPimpleDyMFoam/cylinder/cylinderAndBackground/system/controlDict index e7ce36f33b..9617cc01b8 100644 --- a/tutorials/incompressible/overPimpleDyMFoam/cylinder/cylinderAndBackground/system/controlDict +++ b/tutorials/incompressible/overPimpleDyMFoam/cylinder/cylinderAndBackground/system/controlDict @@ -16,7 +16,7 @@ FoamFile // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); application overPimpleDyMFoam; diff --git a/tutorials/incompressible/overPimpleDyMFoam/simpleRotor/system/controlDict b/tutorials/incompressible/overPimpleDyMFoam/simpleRotor/system/controlDict index a9099f27a7..97e9382d04 100644 --- a/tutorials/incompressible/overPimpleDyMFoam/simpleRotor/system/controlDict +++ b/tutorials/incompressible/overPimpleDyMFoam/simpleRotor/system/controlDict @@ -14,7 +14,7 @@ FoamFile } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); application overPimpleDyMFoam; diff --git a/tutorials/incompressible/overPimpleDyMFoam/twoSimpleRotors/system/controlDict b/tutorials/incompressible/overPimpleDyMFoam/twoSimpleRotors/system/controlDict index 51a0871876..0d7803055c 100644 --- a/tutorials/incompressible/overPimpleDyMFoam/twoSimpleRotors/system/controlDict +++ b/tutorials/incompressible/overPimpleDyMFoam/twoSimpleRotors/system/controlDict @@ -14,7 +14,7 @@ FoamFile } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); DebugSwitches { diff --git a/tutorials/incompressible/overSimpleFoam/aeroFoil/background_overset/system/controlDict b/tutorials/incompressible/overSimpleFoam/aeroFoil/background_overset/system/controlDict index a4aa61f505..0a0adcbdd8 100644 --- a/tutorials/incompressible/overSimpleFoam/aeroFoil/background_overset/system/controlDict +++ b/tutorials/incompressible/overSimpleFoam/aeroFoil/background_overset/system/controlDict @@ -14,7 +14,7 @@ FoamFile } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); application overSimpleFoam; diff --git a/tutorials/multiphase/overInterDyMFoam/boatAndPropeller/system/controlDict b/tutorials/multiphase/overInterDyMFoam/boatAndPropeller/system/controlDict index 20511197e0..425b7a91c2 100644 --- a/tutorials/multiphase/overInterDyMFoam/boatAndPropeller/system/controlDict +++ b/tutorials/multiphase/overInterDyMFoam/boatAndPropeller/system/controlDict @@ -15,7 +15,12 @@ FoamFile // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so" "librigidBodyDynamics.so"); +libs +( + "liboverset.so" + "librigidBodyDynamics.so" + "libfvMotionSolvers.so" +); application overInterDyMFoam; diff --git a/tutorials/multiphase/overInterDyMFoam/floatingBody/background/system/controlDict b/tutorials/multiphase/overInterDyMFoam/floatingBody/background/system/controlDict index 26ce8daf71..7b6ac034bf 100644 --- a/tutorials/multiphase/overInterDyMFoam/floatingBody/background/system/controlDict +++ b/tutorials/multiphase/overInterDyMFoam/floatingBody/background/system/controlDict @@ -14,7 +14,7 @@ FoamFile } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); application overInterDyMFoam ; diff --git a/tutorials/multiphase/overInterDyMFoam/twoSimpleRotors/system/controlDict b/tutorials/multiphase/overInterDyMFoam/twoSimpleRotors/system/controlDict index 68a9ed9a5a..5d9e79b064 100644 --- a/tutorials/multiphase/overInterDyMFoam/twoSimpleRotors/system/controlDict +++ b/tutorials/multiphase/overInterDyMFoam/twoSimpleRotors/system/controlDict @@ -14,7 +14,7 @@ FoamFile } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -libs ("liboverset.so"); +libs ("liboverset.so" "libfvMotionSolvers.so"); DebugSwitches { From 54c10e651dc6bddaebd24dac860d34b635b4b64c Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 4 Apr 2019 14:52:04 +0100 Subject: [PATCH 02/19] STYLE: gravity: whitespace --- .../cfdTools/general/meshObjects/gravity/gravityMeshObject.H | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H b/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H index 7f0a0cade5..2083e6719e 100644 --- a/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H +++ b/src/finiteVolume/cfdTools/general/meshObjects/gravity/gravityMeshObject.H @@ -96,7 +96,7 @@ public: ) ); } - + //- Destructor virtual ~gravity() = default; From 89245fa79618cd8c88ba95585c334dcd177cb27d Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 1 Apr 2019 16:40:22 +0200 Subject: [PATCH 03/19] ENH: allow space char in fileName is now configurable (#1008) - having whitespace in fileName can be somewhat fragile since it means that the fileName components do not necessarily correspond to a 'Foam::word'. But in many cases it will work provided that spaces are not present in the final portion of the simulation directory itself. InfoSwitches { // Allow space character in fileName (use with caution) allowSpaceInFileName 0; } - now use doClean=true as default for fileName::validate(). Was false. Unlike fileName::clean() this requires no internal string rewrite since the characters are being copied. Also handle any path separator transformations (ie, backslash => forward slash) at the same time. This makes it resemble the std::filesystem a bit more. --- applications/test/fileNameOS/Make/files | 3 + applications/test/fileNameOS/Make/options | 2 + .../test/fileNameOS/Test-fileNameOS.C | 106 ++++++++++++++++++ etc/controlDict | 3 + src/OpenFOAM/global/argList/argList.C | 2 +- src/OpenFOAM/primitives/chars/char/char.H | 17 ++- src/OpenFOAM/primitives/chars/char/charIO.C | 10 +- .../primitives/strings/fileName/fileName.C | 23 +++- .../primitives/strings/fileName/fileName.H | 15 ++- .../primitives/strings/fileName/fileNameI.H | 11 +- 10 files changed, 166 insertions(+), 26 deletions(-) create mode 100644 applications/test/fileNameOS/Make/files create mode 100644 applications/test/fileNameOS/Make/options create mode 100644 applications/test/fileNameOS/Test-fileNameOS.C diff --git a/applications/test/fileNameOS/Make/files b/applications/test/fileNameOS/Make/files new file mode 100644 index 0000000000..0e1be7b394 --- /dev/null +++ b/applications/test/fileNameOS/Make/files @@ -0,0 +1,3 @@ +Test-fileNameOS.C + +EXE = $(FOAM_USER_APPBIN)/Test-fileNameOS diff --git a/applications/test/fileNameOS/Make/options b/applications/test/fileNameOS/Make/options new file mode 100644 index 0000000000..18e6fe47af --- /dev/null +++ b/applications/test/fileNameOS/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/fileNameOS/Test-fileNameOS.C b/applications/test/fileNameOS/Test-fileNameOS.C new file mode 100644 index 0000000000..b8c7be1ac9 --- /dev/null +++ b/applications/test/fileNameOS/Test-fileNameOS.C @@ -0,0 +1,106 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Application + Test-fileNameOS + +Description + Test fileName behaviour, potential OS capabilities etc. + + In the distant future could possibly replace parts with C++ filesystem + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "fileName.H" +#include "OSspecific.H" +#include "Switch.H" + +#include +#include +#include + + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +void testDirname(const std::string& rawInput) +{ + fileName input(fileName::validate(rawInput)); + + Info<< nl + << "input: " << rawInput << nl + << "fileName:" << input << nl + << " path:" << input.path() + << " name:\"" << input.name() << '"' + << " ext:\"" << input.ext() << '"' + << " components: " << flatOutput(input.components()) << nl; + + if (rawInput.size() != input.size()) + { + Info<< " This would be Fatal with debug > 1" << nl; + } +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noParallel(); + argList::addBoolOption("no-space", "allowSpaceInFileName = false"); + argList::addBoolOption("with-space", "set allowSpaceInFileName = true"); + + #include "setRootCase.H" + + if (args.found("with-space")) + { + fileName::allowSpaceInFileName = true; + } + + if (args.found("no-space")) + { + fileName::allowSpaceInFileName = false; + + } + + + Info<<"fileName with spaces? : " + << Switch(bool(fileName::allowSpaceInFileName)) << nl << nl; + + + { + testDirname("/abc"); + testDirname("/abc/with space/name"); + testDirname("/abc/with space/more space"); + } + + + Info<< "\nEnd\n" << endl; + return 0; +} + + +// ************************************************************************* // diff --git a/etc/controlDict b/etc/controlDict index 3d6c94dd80..bcc4096734 100644 --- a/etc/controlDict +++ b/etc/controlDict @@ -64,6 +64,9 @@ InfoSwitches // Allow case-supplied C++ code (#codeStream, codedFixedValue) allowSystemOperations 1; + + // Allow space character in fileName (use with caution) + allowSpaceInFileName 0; } diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index 1e4abbb172..8a100b33cb 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -648,7 +648,7 @@ void Foam::argList::setCasePaths() if (optIter.found()) { - caseDir = optIter.val(); + caseDir = fileName::validate(optIter.val()); caseDir.clean(); if (caseDir.empty() || caseDir == ".") diff --git a/src/OpenFOAM/primitives/chars/char/char.H b/src/OpenFOAM/primitives/chars/char/char.H index e42043ad72..9c5cb5f315 100644 --- a/src/OpenFOAM/primitives/chars/char/char.H +++ b/src/OpenFOAM/primitives/chars/char/char.H @@ -47,11 +47,19 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -char readChar(Istream&); -Istream& operator>>(Istream&, char&); -Ostream& operator<<(Ostream&, const char); -Ostream& operator<<(Ostream&, const char*); +//- Read single character +char readChar(Istream& is); +//- Read single character +Istream& operator>>(Istream& is, char& c); + +//- Write single character +Ostream& operator<<(Ostream& os, const char c); + +//- Write a nul-terminated C-string +Ostream& operator<<(Ostream& os, const char* str); + +//- Test for \em horizontal whitespace inline bool isspace(char c) { return @@ -63,6 +71,7 @@ inline bool isspace(char c) ); } + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/primitives/chars/char/charIO.C b/src/OpenFOAM/primitives/chars/char/charIO.C index c15a71b002..24b02ecb90 100644 --- a/src/OpenFOAM/primitives/chars/char/charIO.C +++ b/src/OpenFOAM/primitives/chars/char/charIO.C @@ -32,9 +32,9 @@ License char Foam::readChar(Istream& is) { - char c; - is.read(c); - return c; + char c; + is.read(c); + return c; } @@ -54,9 +54,9 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const char c) } -Foam::Ostream& Foam::operator<<(Ostream& os, const char* s) +Foam::Ostream& Foam::operator<<(Ostream& os, const char* str) { - os.write(s); + os.write(str); os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 7002ebd7a2..c06a1d3d05 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -36,7 +36,11 @@ License // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // const char* const Foam::fileName::typeName = "fileName"; -int Foam::fileName::debug(debug::debugSwitch(fileName::typeName, 0)); +int Foam::fileName::debug(Foam::debug::debugSwitch(fileName::typeName, 0)); +int Foam::fileName::allowSpaceInFileName +( + Foam::debug::infoSwitch("allowSpaceInFileName", 0) +); const Foam::fileName Foam::fileName::null; @@ -48,16 +52,27 @@ Foam::fileName Foam::fileName::validate const bool doClean ) { + // The logic is very similar to stripInvalid, + // but silently removes bad characters + fileName out; out.resize(s.size()); - char prev = 0; std::string::size_type len = 0; - // Largely as per stripInvalid + char prev = 0; for (auto iter = s.cbegin(); iter != s.cend(); ++iter) { - const char c = *iter; + char c = *iter; + + // Treat raw backslash like a path separator. There is no "normal" + // way for these to be there (except for an OS that uses them), but + // could also cause issues when writing strings, shell commands etc. + + if (c == '\\') + { + c = '/'; + } if (fileName::valid(c)) { diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index 824345ad09..b87d6a3b80 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -90,6 +90,9 @@ public: //- Debugging static int debug; + //- Allow space character in fileName. To be used with caution. + static int allowSpaceInFileName; + //- An empty fileName static const fileName null; @@ -141,14 +144,10 @@ public: //- Is this character valid for a fileName? inline static bool valid(char c); - //- Construct validated fileName (no invalid characters). - // Optionally perform some additional cleanup such as removing - // duplicate or trailing slashes. - static fileName validate - ( - const std::string& s, - const bool doClean=false - ); + //- Construct fileName with no invalid characters, possibly applying + //- other transformations such as changing the path separator, + //- removing duplicate or trailing slashes, etc. + static fileName validate(const std::string& s, const bool doClean=true); //- This is a specialized (possibly slower) version of compare() //- that ignores duplicate or trailing slashes. diff --git a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H index fb23263c8c..2eefa95f72 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H @@ -102,9 +102,9 @@ inline bool Foam::fileName::valid(char c) { return ( - !isspace(c) - && c != '"' // string quote - && c != '\'' // string quote + c != '"' // string quote + && c != '\'' // string quote + && (!isspace(c) || (allowSpaceInFileName && c == ' ')) ); } @@ -134,7 +134,10 @@ inline void Foam::fileName::stripInvalid() inline bool Foam::fileName::isAbsolute(const std::string& str) { - return !str.empty() && str[0] == '/'; + return + ( + !str.empty() && str[0] == '/' + ); } From baaf8d6bc9e0a776c4a66879cfaa0a0255de2fdf Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 5 Apr 2019 09:42:25 +0200 Subject: [PATCH 04/19] ENH: add fileName::concat static method - similar to the global '/' operator, but taking raw strings and not performing any stripping. Was previously a local function within POSIX.C, but it is useful enough to be in fileName itself. --- src/OSspecific/POSIX/POSIX.C | 40 +------------ .../primitives/strings/fileName/fileName.C | 57 ++++++++++++++----- .../primitives/strings/fileName/fileName.H | 10 +++- 3 files changed, 51 insertions(+), 56 deletions(-) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 55fa68cbe9..a63d3d746e 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -87,44 +87,6 @@ static bool cwdPreference_(Foam::debug::optimisationSwitch("cwd", 0)); // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // -// Like fileName "/" global operator, but retain any invalid characters -static inline Foam::fileName fileNameConcat -( - const std::string& a, - const std::string& b -) -{ - if (a.size()) - { - if (b.size()) - { - // Two non-empty strings: can concatenate - - if (a.back() == '/' || b.front() == '/') - { - return Foam::fileName(a + b, false); - } - else - { - return Foam::fileName(a + '/' + b, false); - } - } - - // The second string was empty - return Foam::fileName(a, false); - } - - if (b.size()) - { - // The first string is empty - return Foam::fileName(b, false); - } - - // Both strings are empty - return Foam::fileName(); -} - - // After a fork in system(), before the exec() do the following // - close stdin when executing in background (daemon-like) // - redirect stdout to stderr when infoDetailLevel == 0 @@ -1273,7 +1235,7 @@ bool Foam::rmDir(const fileName& directory, const bool silent) // otherwise we cannot subdirs with these types of names. // -> const fileName path = directory/name; <- - const fileName path(fileNameConcat(directory, item)); + const fileName path(fileName::concat(directory, item)); if (path.type(false) == fileName::DIRECTORY) { diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index c06a1d3d05..f74c6877a6 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -56,7 +56,7 @@ Foam::fileName Foam::fileName::validate // but silently removes bad characters fileName out; - out.resize(s.size()); + out.resize(s.length()); std::string::size_type len = 0; @@ -99,6 +99,33 @@ Foam::fileName Foam::fileName::validate } +Foam::fileName Foam::fileName::concat +( + const std::string& s1, + const std::string& s2 +) +{ + const auto n1 = s1.length(); + const auto n2 = s2.length(); + + fileName out; + out.reserve(n1 + n2 + 1); + + out += s1; + + if (n1 && n2 && s1.back() != '/' && s2.front() != '/') + { + // Add separator + out += '/'; + } + + out += s2; + + // Could also remove trailing '/', if desired. + return out; +} + + bool Foam::fileName::equals(const std::string& s1, const std::string& s2) { // Do not use (s1 == s2) or s1.compare(s2) first since this would @@ -107,8 +134,8 @@ bool Foam::fileName::equals(const std::string& s1, const std::string& s2) std::string::size_type i1 = 0; std::string::size_type i2 = 0; - const auto n1 = s1.size(); - const auto n2 = s2.size(); + const auto n1 = s1.length(); + const auto n2 = s2.length(); //Info<< "compare " << s1 << " == " << s2 << endl; while (i1 < n1 && i2 < n2) @@ -264,7 +291,7 @@ bool Foam::fileName::clean(std::string& str) // Number of output characters auto nChar = top+1; - const auto maxLen = str.size(); + const auto maxLen = str.length(); for (auto src = nChar; src < maxLen; /*nil*/) { @@ -477,7 +504,7 @@ Foam::fileName& Foam::fileName::operator/=(const string& other) s += '/'; } - s.append(other); + s += other; } } else if (other.size()) @@ -492,32 +519,32 @@ Foam::fileName& Foam::fileName::operator/=(const string& other) // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // -Foam::fileName Foam::operator/(const string& a, const string& b) +Foam::fileName Foam::operator/(const string& s1, const string& s2) { - if (a.size()) + if (s1.length()) { - if (b.size()) + if (s2.length()) { // Two non-empty strings: can concatenate - if (a.back() == '/' || b.front() == '/') + if (s1.back() == '/' || s2.front() == '/') { - return fileName(a + b); + return fileName(s1 + s2); } else { - return fileName(a + '/' + b); + return fileName(s1 + '/' + s2); } } // The second string was empty - return a; + return s1; } - if (b.size()) + if (s2.length()) { // The first string is empty - return b; + return s2; } // Both strings are empty diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index b87d6a3b80..a0c1570e75 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -82,7 +82,7 @@ public: }; - // Static data members + // Static Data Members //- The typeName static const char* const typeName; @@ -149,6 +149,12 @@ public: //- removing duplicate or trailing slashes, etc. static fileName validate(const std::string& s, const bool doClean=true); + //- Join two strings with '/' as a path separator. + // No '/' separator is added if either argument is an empty string or + // if the arguments already had the path separator at the junction. + // Invalid characters are \em not stripped (ie, retained). + static fileName concat(const std::string& s1, const std::string& s2); + //- This is a specialized (possibly slower) version of compare() //- that ignores duplicate or trailing slashes. static bool equals(const std::string& s1, const std::string& s2); @@ -402,7 +408,7 @@ Ostream& operator<<(Ostream& os, const fileName& val); //- Assemble words and fileNames as pathnames by adding a '/' separator. // No '/' separator is added if either argument is an empty string. -fileName operator/(const string& a, const string& b); +fileName operator/(const string& s1, const string& s2); //- Recursively search the given directory for the file From 939144233c48edb596344f92926fa1aaa3e78610 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 3 Apr 2019 08:57:01 +0200 Subject: [PATCH 05/19] STYLE: adjusted wording and messages for wmake - add an additional test for wmake pre-processing --- applications/test/wmake1/Make/files | 10 +++++ applications/test/wmake1/Make/options | 2 + applications/test/wmake1/Test-wmake1.C | 56 ++++++++++++++++++++++++++ applications/test/wmake1/newStub.C | 13 ++++++ applications/test/wmake1/oldStub.C | 17 ++++++++ wmake/makefiles/general | 2 +- wmake/makefiles/info | 5 +-- wmake/wmake | 18 ++++----- 8 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 applications/test/wmake1/Make/files create mode 100644 applications/test/wmake1/Make/options create mode 100644 applications/test/wmake1/Test-wmake1.C create mode 100644 applications/test/wmake1/newStub.C create mode 100644 applications/test/wmake1/oldStub.C diff --git a/applications/test/wmake1/Make/files b/applications/test/wmake1/Make/files new file mode 100644 index 0000000000..3ef1ed76a9 --- /dev/null +++ b/applications/test/wmake1/Make/files @@ -0,0 +1,10 @@ +Test-wmake1.C + +/* #if OPENFOAM == 1812 */ +#if OPENFOAM > 1812 +newStub.C +#else +oldStub.C +#endif + +EXE = $(FOAM_APPBIN)/Test-wmake1 diff --git a/applications/test/wmake1/Make/options b/applications/test/wmake1/Make/options new file mode 100644 index 0000000000..75c7356f1c --- /dev/null +++ b/applications/test/wmake1/Make/options @@ -0,0 +1,2 @@ +EXE_INC = +EXE_LIBS = diff --git a/applications/test/wmake1/Test-wmake1.C b/applications/test/wmake1/Test-wmake1.C new file mode 100644 index 0000000000..614d9189fb --- /dev/null +++ b/applications/test/wmake1/Test-wmake1.C @@ -0,0 +1,56 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Application + Test-wmake1 + +Description + Some tests for wmake features. + For example, testing how robust or fragile version-dependent conditional + compilation works. + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" + +namespace Foam +{ + void printTest(); +} + + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +int main(int argc, char *argv[]) +{ + printTest(); + + Info<< "\nEnd\n" << nl; + + return 0; +} + + +// ************************************************************************* // diff --git a/applications/test/wmake1/newStub.C b/applications/test/wmake1/newStub.C new file mode 100644 index 0000000000..153ccd4173 --- /dev/null +++ b/applications/test/wmake1/newStub.C @@ -0,0 +1,13 @@ +// Some test code + +#include "foamVersion.H" +#include "IOstreams.H" + +namespace Foam +{ + void printTest() + { + Info<< nl; + foamVersion::printBuildInfo(); + } +} diff --git a/applications/test/wmake1/oldStub.C b/applications/test/wmake1/oldStub.C new file mode 100644 index 0000000000..136b7d8086 --- /dev/null +++ b/applications/test/wmake1/oldStub.C @@ -0,0 +1,17 @@ +// Some test code + +#include "IOstreams.H" + +namespace Foam +{ + void printTest() + { + Info<< nl << "Using old stub" << nl; + + #if OPENFOAM + Info<< "OPENFOAM=" << OPENFOAM << nl; + #else + Info<< "OPENFOAM is undefined" << nl; + #endif + } +} diff --git a/wmake/makefiles/general b/wmake/makefiles/general index 198e5c1e59..2177e89a87 100644 --- a/wmake/makefiles/general +++ b/wmake/makefiles/general @@ -175,7 +175,7 @@ lib: $(LIB).a | silent $(LIB).a: $(OBJECTS) @$(WM_SCRIPTS)/makeTargetDir $(LIB) @rm -f $(LIB).a - $(call QUIET_MESSAGE,ar,$(LIB)) + $(call QUIET_MESSAGE,ar,$(LIB).a) $E $(AR) $(ARFLAGS) $(LIB).a $(OBJECTS) $(call QUIET_MESSAGE,ranlib,$(notdir $(LIB))) $E $(RANLIB) $(LIB).a diff --git a/wmake/makefiles/info b/wmake/makefiles/info index 90fa2b550d..600399a173 100644 --- a/wmake/makefiles/info +++ b/wmake/makefiles/info @@ -43,16 +43,13 @@ include $(GENERAL_RULES)/general # Display information #------------------------------------------------------------------------------ -export WM_VERSION - - .PHONY: compile compile: @echo "$(strip $(CC) $(c++FLAGS))" .PHONY: api api: - @echo "$${WM_VERSION#*=}" + @echo "$(WM_VERSION)" | sed -e 's/^.*=//' .PHONY: c c: diff --git a/wmake/wmake b/wmake/wmake index 7591fc9451..97f135a737 100755 --- a/wmake/wmake +++ b/wmake/wmake @@ -70,18 +70,16 @@ options: -s | -silent Quiet mode (does not echo commands) -a | -all wmake all sub-directories, running Allwmake if present -q | -queue wmakeCollect all sub-directories, running Allwmake if present - -k | -keep-going Keep going without stopping when errors occur (-non-stop) + -k | -keep-going Keep going even when errors occur (-non-stop) -j Compile using all local cores/hyperthreads - -jN or -j N Compile using N cores/hyperthreads + -jN | -j N Compile using N cores/hyperthreads -no-scheduler Disable scheduled parallel compilation - -pwd Print root directory containing a Make/ directory and exit - -update Update lnInclude directories, dep files, remove deprecated - files and directories - -show Identical to -show-compile - -show-api Print api value and exit - -show-compile Print C++ compiler value/flags and exit - -show-cxx Print C++ compiler value and exit - -show-cxxflags Print C++ compiler flags and exit + -pwd Print root directory containing a Make/ directory + -update Update lnInclude dirs, dep files, remove deprecated files/dirs + -show-api Print api value + -show-compile Print C++ compiler value and flags (shortcut: -show) + -show-cxx Print C++ compiler value + -show-cxxflags Print C++ compiler flags -h | -help Print the usage From c53459d4a2c1525acb8c49635858994ef83b68ed Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 4 Apr 2019 12:41:09 +0200 Subject: [PATCH 06/19] STYLE: simplify if nesting in generic patch fields (#1269) - use git show/diff -w when viewing --- .../genericFaPatchField/genericFaPatchField.C | 710 +++++++++-------- .../genericFvPatchField/genericFvPatchField.C | 712 +++++++++--------- .../genericFvsPatchField.C | 710 +++++++++-------- .../genericPointPatchField.C | 505 ++++++------- 4 files changed, 1297 insertions(+), 1340 deletions(-) diff --git a/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C b/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C index 1a7d060a67..16fe9f6d65 100644 --- a/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C +++ b/src/genericPatchFields/genericFaPatchField/genericFaPatchField.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -59,19 +59,20 @@ Foam::genericFaPatchField::genericFaPatchField actualTypeName_(dict.get("type")), dict_(dict) { + const label patchSize = this->size(); + if (!dict.found("value")) { FatalIOErrorInFunction(dict) - << "\n Cannot find 'value' entry" + << nl << " Cannot find 'value' entry" << " on patch " << this->patch().name() << " of field " << this->internalField().name() - << " in file " << this->internalField().objectPath() - << nl + << " in file " << this->internalField().objectPath() << nl << " which is required to set the" " values of the generic patch field." << nl - << " (Actual type " << actualTypeName_ << ")" << nl - << "\n Please add the 'value' entry to the write function " - "of the user-defined boundary-condition\n" + << " (Actual type " << actualTypeName_ << ')' << nl << nl + << " Please add the 'value' entry to the write function" + " of the user-defined boundary-condition" << nl << exit(FatalIOError); } @@ -79,338 +80,329 @@ Foam::genericFaPatchField::genericFaPatchField { const keyType& key = dEntry.keyword(); - if (key != "type" && key != "value") + if + ( + key == "type" + || key == "value" + || !dEntry.isStream() || dEntry.stream().empty() + ) { - if - ( - dEntry.isStream() - && dEntry.stream().size() - ) + continue; + } + + + ITstream& is = dEntry.stream(); + + // Read first token + token firstToken(is); + + if + ( + firstToken.isWord() + && firstToken.wordToken() == "nonuniform" + ) + { + token fieldToken(is); + + if (!fieldToken.isCompound()) { - ITstream& is = dEntry.stream(); - - // Read first token - token firstToken(is); - if ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" + fieldToken.isLabel() + && fieldToken.labelToken() == 0 ) { - token fieldToken(is); - - if (!fieldToken.isCompound()) - { - if - ( - fieldToken.isLabel() - && fieldToken.labelToken() == 0 - ) - { - scalarFields_.insert - ( - key, - autoPtr::New() - ); - } - else - { - FatalIOErrorInFunction(dict) - << "\n token following 'nonuniform' " - "is not a compound" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - scalarFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - vectorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - sphTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - symmTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - tensorFields_.insert(key, fPtr); - } - else - { - FatalIOErrorInFunction(dict) - << "\n compound " << fieldToken.compoundToken() - << " not supported" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } + scalarFields_.insert(key, autoPtr::New()); } - else if - ( - firstToken.isWord() - && firstToken.wordToken() == "uniform" - ) + else { - token fieldToken(is); + FatalIOErrorInFunction(dict) + << "\n token following 'nonuniform' " + "is not a compound" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); - if (!fieldToken.isPunctuation()) - { - scalarFields_.insert + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + scalarFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + vectorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + sphTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + symmTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + tensorFields_.insert(key, fPtr); + } + else + { + FatalIOErrorInFunction(dict) + << "\n compound " << fieldToken.compoundToken() + << " not supported" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + } + else if + ( + firstToken.isWord() + && firstToken.wordToken() == "uniform" + ) + { + token fieldToken(is); + + if (!fieldToken.isPunctuation()) + { + scalarFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + fieldToken.number() + ) + ); + } + else + { + // Read as scalarList. + is.putBack(fieldToken); + + scalarList l(is); + + if (l.size() == vector::nComponents) + { + vector vs(l[0], l[1], l[2]); + + vectorFields_.insert + ( + key, + autoPtr::New ( - key, - autoPtr::New - ( - this->size(), - fieldToken.number() - ) - ); - } - else - { - // Read as scalarList. - is.putBack(fieldToken); + patchSize, + vs + ) + ); + } + else if (l.size() == sphericalTensor::nComponents) + { + sphericalTensor vs(l[0]); - scalarList l(is); + sphTensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else if (l.size() == symmTensor::nComponents) + { + symmTensor vs(l[0], l[1], l[2], l[3], l[4], l[5]); - if (l.size() == vector::nComponents) - { - vector vs(l[0], l[1], l[2]); + symmTensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else if (l.size() == tensor::nComponents) + { + tensor vs + ( + l[0], l[1], l[2], + l[3], l[4], l[5], + l[6], l[7], l[8] + ); - vectorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == sphericalTensor::nComponents) - { - sphericalTensor vs(l[0]); - - sphTensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == symmTensor::nComponents) - { - symmTensor vs(l[0], l[1], l[2], l[3], l[4], l[5]); - - symmTensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == tensor::nComponents) - { - tensor vs - ( - l[0], l[1], l[2], - l[3], l[4], l[5], - l[6], l[7], l[8] - ); - - tensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else - { - FatalIOErrorInFunction(dict) - << "\n unrecognised native type " << l - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - } + tensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else + { + FatalIOErrorInFunction(dict) + << "\n unrecognised native type " << l + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); } } } @@ -707,46 +699,42 @@ void Foam::genericFaPatchField::write(Ostream& os) const { const keyType& key = dEntry.keyword(); - if (key != "type" && key != "value") + if (key == "type" || key == "value") { - if - ( - dEntry.isStream() - && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" - ) + continue; + } + else if + ( + dEntry.isStream() + && dEntry.stream().size() + && dEntry.stream()[0].isWord() + && dEntry.stream()[0].wordToken() == "nonuniform" + ) + { + if (scalarFields_.found(key)) { - if (scalarFields_.found(key)) - { - scalarFields_.find(key)() - ->writeEntry(key, os); - } - else if (vectorFields_.found(key)) - { - vectorFields_.find(key)() - ->writeEntry(key, os); - } - else if (sphTensorFields_.found(key)) - { - sphTensorFields_.find(key)() - ->writeEntry(key, os); - } - else if (symmTensorFields_.found(key)) - { - symmTensorFields_.find(key)() - ->writeEntry(key, os); - } - else if (tensorFields_.found(key)) - { - tensorFields_.find(key)() - ->writeEntry(key, os); - } + scalarFields_.cfind(key)()->writeEntry(key, os); } - else + else if (vectorFields_.found(key)) { - dEntry.write(os); + vectorFields_.cfind(key)()->writeEntry(key, os); } + else if (sphTensorFields_.found(key)) + { + sphTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (symmTensorFields_.found(key)) + { + symmTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (tensorFields_.found(key)) + { + tensorFields_.cfind(key)()->writeEntry(key, os); + } + } + else + { + dEntry.write(os); } } diff --git a/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C b/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C index bdf7e0351a..cecaaac172 100644 --- a/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C +++ b/src/genericPatchFields/genericFvPatchField/genericFvPatchField.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -56,22 +56,23 @@ Foam::genericFvPatchField::genericFvPatchField ) : calculatedFvPatchField(p, iF, dict), - actualTypeName_(dict.lookup("type")), + actualTypeName_(dict.get("type")), dict_(dict) { + const label patchSize = this->size(); + if (!dict.found("value")) { FatalIOErrorInFunction(dict) - << "\n Cannot find 'value' entry" + << nl << " Cannot find 'value' entry" << " on patch " << this->patch().name() << " of field " << this->internalField().name() - << " in file " << this->internalField().objectPath() - << nl + << " in file " << this->internalField().objectPath() << nl << " which is required to set the" " values of the generic patch field." << nl - << " (Actual type " << actualTypeName_ << ")" << nl - << "\n Please add the 'value' entry to the write function " - "of the user-defined boundary-condition\n" + << " (Actual type " << actualTypeName_ << ')' << nl << nl + << " Please add the 'value' entry to the write function" + " of the user-defined boundary-condition" << nl << exit(FatalIOError); } @@ -79,338 +80,329 @@ Foam::genericFvPatchField::genericFvPatchField { const keyType& key = dEntry.keyword(); - if (key != "type" && key != "value") + if + ( + key == "type" + || key == "value" + || !dEntry.isStream() || dEntry.stream().empty() + ) { - if - ( - dEntry.isStream() - && dEntry.stream().size() - ) + continue; + } + + + ITstream& is = dEntry.stream(); + + // Read first token + token firstToken(is); + + if + ( + firstToken.isWord() + && firstToken.wordToken() == "nonuniform" + ) + { + token fieldToken(is); + + if (!fieldToken.isCompound()) { - ITstream& is = dEntry.stream(); - - // Read first token - token firstToken(is); - if ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" + fieldToken.isLabel() + && fieldToken.labelToken() == 0 ) { - token fieldToken(is); - - if (!fieldToken.isCompound()) - { - if - ( - fieldToken.isLabel() - && fieldToken.labelToken() == 0 - ) - { - scalarFields_.insert - ( - dEntry.keyword(), - autoPtr::New() - ); - } - else - { - FatalIOErrorInFunction(dict) - << "\n token following 'nonuniform' " - "is not a compound" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - scalarFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - vectorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - sphTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - symmTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - tensorFields_.insert(key, fPtr); - } - else - { - FatalIOErrorInFunction(dict) - << "\n compound " << fieldToken.compoundToken() - << " not supported" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } + scalarFields_.insert(key, autoPtr::New()); } - else if - ( - firstToken.isWord() - && firstToken.wordToken() == "uniform" - ) + else { - token fieldToken(is); + FatalIOErrorInFunction(dict) + << "\n token following 'nonuniform' " + "is not a compound" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); - if (!fieldToken.isPunctuation()) - { - scalarFields_.insert + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + scalarFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + vectorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + sphTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + symmTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + tensorFields_.insert(key, fPtr); + } + else + { + FatalIOErrorInFunction(dict) + << "\n compound " << fieldToken.compoundToken() + << " not supported" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + } + else if + ( + firstToken.isWord() + && firstToken.wordToken() == "uniform" + ) + { + token fieldToken(is); + + if (!fieldToken.isPunctuation()) + { + scalarFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + fieldToken.number() + ) + ); + } + else + { + // Read as scalarList. + is.putBack(fieldToken); + + scalarList l(is); + + if (l.size() == vector::nComponents) + { + vector vs(l[0], l[1], l[2]); + + vectorFields_.insert + ( + key, + autoPtr::New ( - key, - autoPtr::New - ( - this->size(), - fieldToken.number() - ) - ); - } - else - { - // Read as scalarList. - is.putBack(fieldToken); + patchSize, + vs + ) + ); + } + else if (l.size() == sphericalTensor::nComponents) + { + sphericalTensor vs(l[0]); - scalarList l(is); + sphTensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else if (l.size() == symmTensor::nComponents) + { + symmTensor vs(l[0], l[1], l[2], l[3], l[4], l[5]); - if (l.size() == vector::nComponents) - { - vector vs(l[0], l[1], l[2]); + symmTensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else if (l.size() == tensor::nComponents) + { + tensor vs + ( + l[0], l[1], l[2], + l[3], l[4], l[5], + l[6], l[7], l[8] + ); - vectorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == sphericalTensor::nComponents) - { - sphericalTensor vs(l[0]); - - sphTensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == symmTensor::nComponents) - { - symmTensor vs(l[0], l[1], l[2], l[3], l[4], l[5]); - - symmTensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == tensor::nComponents) - { - tensor vs - ( - l[0], l[1], l[2], - l[3], l[4], l[5], - l[6], l[7], l[8] - ); - - tensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else - { - FatalIOErrorInFunction(dict) - << "\n unrecognised native type " << l - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - } + tensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else + { + FatalIOErrorInFunction(dict) + << "\n unrecognised native type " << l + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); } } } @@ -707,46 +699,42 @@ void Foam::genericFvPatchField::write(Ostream& os) const { const keyType& key = dEntry.keyword(); - if (key != "type" && key != "value") + if (key == "type" || key == "value") { - if - ( - dEntry.isStream() - && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" - ) + continue; + } + else if + ( + dEntry.isStream() + && dEntry.stream().size() + && dEntry.stream()[0].isWord() + && dEntry.stream()[0].wordToken() == "nonuniform" + ) + { + if (scalarFields_.found(key)) { - if (scalarFields_.found(key)) - { - scalarFields_.find(key)() - ->writeEntry(key, os); - } - else if (vectorFields_.found(key)) - { - vectorFields_.find(key)() - ->writeEntry(key, os); - } - else if (sphTensorFields_.found(key)) - { - sphTensorFields_.find(key)() - ->writeEntry(key, os); - } - else if (symmTensorFields_.found(key)) - { - symmTensorFields_.find(key)() - ->writeEntry(key, os); - } - else if (tensorFields_.found(key)) - { - tensorFields_.find(key)() - ->writeEntry(key, os); - } + scalarFields_.cfind(key)()->writeEntry(key, os); } - else + else if (vectorFields_.found(key)) { - dEntry.write(os); + vectorFields_.cfind(key)()->writeEntry(key, os); } + else if (sphTensorFields_.found(key)) + { + sphTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (symmTensorFields_.found(key)) + { + symmTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (tensorFields_.found(key)) + { + tensorFields_.cfind(key)()->writeEntry(key, os); + } + } + else + { + dEntry.write(os); } } diff --git a/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C b/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C index a30745fa41..dbfaf0c591 100644 --- a/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C +++ b/src/genericPatchFields/genericFvsPatchField/genericFvsPatchField.C @@ -54,22 +54,23 @@ Foam::genericFvsPatchField::genericFvsPatchField ) : calculatedFvsPatchField(p, iF, dict), - actualTypeName_(dict.lookup("type")), + actualTypeName_(dict.get("type")), dict_(dict) { + const label patchSize = this->size(); + if (!dict.found("value")) { FatalIOErrorInFunction(dict) - << "\n Cannot find 'value' entry" + << nl << " Cannot find 'value' entry" << " on patch " << this->patch().name() << " of field " << this->internalField().name() - << " in file " << this->internalField().objectPath() - << nl + << " in file " << this->internalField().objectPath() << nl << " which is required to set the" " values of the generic patch field." << nl - << " (Actual type " << actualTypeName_ << ")" << nl - << "\n Please add the 'value' entry to the write function " - "of the user-defined boundary-condition\n" + << " (Actual type " << actualTypeName_ << ')' << nl << nl + << " Please add the 'value' entry to the write function" + " of the user-defined boundary-condition" << nl << exit(FatalIOError); } @@ -77,338 +78,329 @@ Foam::genericFvsPatchField::genericFvsPatchField { const keyType& key = dEntry.keyword(); - if (key != "type" && key != "value") + if + ( + key == "type" + || key == "value" + || !dEntry.isStream() || dEntry.stream().empty() + ) { - if - ( - dEntry.isStream() - && dEntry.stream().size() - ) + continue; + } + + + ITstream& is = dEntry.stream(); + + // Read first token + token firstToken(is); + + if + ( + firstToken.isWord() + && firstToken.wordToken() == "nonuniform" + ) + { + token fieldToken(is); + + if (!fieldToken.isCompound()) { - ITstream& is = dEntry.stream(); - - // Read first token - token firstToken(is); - if ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" + fieldToken.isLabel() + && fieldToken.labelToken() == 0 ) { - token fieldToken(is); - - if (!fieldToken.isCompound()) - { - if - ( - fieldToken.isLabel() - && fieldToken.labelToken() == 0 - ) - { - scalarFields_.insert - ( - dEntry.keyword(), - autoPtr::New() - ); - } - else - { - FatalIOErrorInFunction(dict) - << "\n token following 'nonuniform' " - "is not a compound" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - scalarFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - vectorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - sphTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - symmTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - tensorFields_.insert(key, fPtr); - } - else - { - FatalIOErrorInFunction(dict) - << "\n compound " << fieldToken.compoundToken() - << " not supported" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } + scalarFields_.insert(key, autoPtr::New()); } - else if - ( - firstToken.isWord() - && firstToken.wordToken() == "uniform" - ) + else { - token fieldToken(is); + FatalIOErrorInFunction(dict) + << "\n token following 'nonuniform' " + "is not a compound" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); - if (!fieldToken.isPunctuation()) - { - scalarFields_.insert + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + scalarFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + vectorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + sphTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + symmTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + tensorFields_.insert(key, fPtr); + } + else + { + FatalIOErrorInFunction(dict) + << "\n compound " << fieldToken.compoundToken() + << " not supported" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + } + else if + ( + firstToken.isWord() + && firstToken.wordToken() == "uniform" + ) + { + token fieldToken(is); + + if (!fieldToken.isPunctuation()) + { + scalarFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + fieldToken.number() + ) + ); + } + else + { + // Read as scalarList. + is.putBack(fieldToken); + + scalarList l(is); + + if (l.size() == vector::nComponents) + { + vector vs(l[0], l[1], l[2]); + + vectorFields_.insert + ( + key, + autoPtr::New ( - key, - autoPtr::New - ( - this->size(), - fieldToken.number() - ) - ); - } - else - { - // Read as scalarList. - is.putBack(fieldToken); + patchSize, + vs + ) + ); + } + else if (l.size() == sphericalTensor::nComponents) + { + sphericalTensor vs(l[0]); - scalarList l(is); + sphTensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else if (l.size() == symmTensor::nComponents) + { + symmTensor vs(l[0], l[1], l[2], l[3], l[4], l[5]); - if (l.size() == vector::nComponents) - { - vector vs(l[0], l[1], l[2]); + symmTensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else if (l.size() == tensor::nComponents) + { + tensor vs + ( + l[0], l[1], l[2], + l[3], l[4], l[5], + l[6], l[7], l[8] + ); - vectorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == sphericalTensor::nComponents) - { - sphericalTensor vs(l[0]); - - sphTensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == symmTensor::nComponents) - { - symmTensor vs(l[0], l[1], l[2], l[3], l[4], l[5]); - - symmTensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else if (l.size() == tensor::nComponents) - { - tensor vs - ( - l[0], l[1], l[2], - l[3], l[4], l[5], - l[6], l[7], l[8] - ); - - tensorFields_.insert - ( - key, - autoPtr::New - ( - this->size(), - vs - ) - ); - } - else - { - FatalIOErrorInFunction(dict) - << "\n unrecognised native type " << l - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - } + tensorFields_.insert + ( + key, + autoPtr::New + ( + patchSize, + vs + ) + ); + } + else + { + FatalIOErrorInFunction(dict) + << "\n unrecognised native type " << l + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); } } } @@ -705,46 +697,42 @@ void Foam::genericFvsPatchField::write(Ostream& os) const { const keyType& key = dEntry.keyword(); - if (key != "type" && key != "value") + if (key == "type" || key == "value") { - if - ( - dEntry.isStream() - && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" - ) + continue; + } + else if + ( + dEntry.isStream() + && dEntry.stream().size() + && dEntry.stream()[0].isWord() + && dEntry.stream()[0].wordToken() == "nonuniform" + ) + { + if (scalarFields_.found(key)) { - if (scalarFields_.found(key)) - { - scalarFields_.find(key)() - ->writeEntry(key, os); - } - else if (vectorFields_.found(key)) - { - vectorFields_.find(key)() - ->writeEntry(key, os); - } - else if (sphTensorFields_.found(key)) - { - sphTensorFields_.find(key)() - ->writeEntry(key, os); - } - else if (symmTensorFields_.found(key)) - { - symmTensorFields_.find(key)() - ->writeEntry(key, os); - } - else if (tensorFields_.found(key)) - { - tensorFields_.find(key)() - ->writeEntry(key, os); - } + scalarFields_.cfind(key)()->writeEntry(key, os); } - else + else if (vectorFields_.found(key)) { - dEntry.write(os); + vectorFields_.cfind(key)()->writeEntry(key, os); } + else if (sphTensorFields_.found(key)) + { + sphTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (symmTensorFields_.found(key)) + { + symmTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (tensorFields_.found(key)) + { + tensorFields_.cfind(key)()->writeEntry(key, os); + } + } + else + { + dEntry.write(os); } } diff --git a/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C b/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C index 264bf18728..aee5b2337a 100644 --- a/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C +++ b/src/genericPatchFields/genericPointPatchField/genericPointPatchField.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -55,242 +55,234 @@ Foam::genericPointPatchField::genericPointPatchField actualTypeName_(dict.get("type")), dict_(dict) { + const label patchSize = this->size(); + for (const entry& dEntry : dict_) { const keyType& key = dEntry.keyword(); - if (key != "type") + if + ( + key == "type" + || !dEntry.isStream() || dEntry.stream().empty() + ) { - if - ( - dEntry.isStream() - && dEntry.stream().size() - ) + continue; + } + + + ITstream& is = dEntry.stream(); + + // Read first token + token firstToken(is); + + if + ( + firstToken.isWord() + && firstToken.wordToken() == "nonuniform" + ) + { + token fieldToken(is); + + if (!fieldToken.isCompound()) { - ITstream& is = dEntry.stream(); - - // Read first token - token firstToken(is); - if ( - firstToken.isWord() - && firstToken.wordToken() == "nonuniform" + fieldToken.isLabel() + && fieldToken.labelToken() == 0 ) { - token fieldToken(is); - - if (!fieldToken.isCompound()) - { - if - ( - fieldToken.isLabel() - && fieldToken.labelToken() == 0 - ) - { - scalarFields_.insert - ( - key, - autoPtr::New() - ); - } - else - { - FatalIOErrorInFunction(dict) - << "\n token following 'nonuniform' " - "is not a compound" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - scalarFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - vectorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - sphTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast - < - token::Compound> - > - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - symmTensorFields_.insert(key, fPtr); - } - else if - ( - fieldToken.compoundToken().type() - == token::Compound>::typeName - ) - { - auto fPtr = autoPtr::New(); - - fPtr->transfer - ( - dynamicCast>> - ( - fieldToken.transferCompoundToken(is) - ) - ); - - if (fPtr->size() != this->size()) - { - FatalIOErrorInFunction(dict) - << "\n size of field " << key - << " (" << fPtr->size() << ')' - << " is not the same size as the patch (" - << this->size() << ')' - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } - - tensorFields_.insert(key, fPtr); - } - else - { - FatalIOErrorInFunction(dict) - << "\n compound " << fieldToken.compoundToken() - << " not supported" - << "\n on patch " << this->patch().name() - << " of field " - << this->internalField().name() - << " in file " - << this->internalField().objectPath() - << exit(FatalIOError); - } + scalarFields_.insert(key, autoPtr::New()); } + else + { + FatalIOErrorInFunction(dict) + << "\n token following 'nonuniform' " + "is not a compound" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + scalarFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + vectorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + sphTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + symmTensorFields_.insert(key, fPtr); + } + else if + ( + fieldToken.compoundToken().type() + == token::Compound>::typeName + ) + { + auto fPtr = autoPtr::New(); + + fPtr->transfer + ( + dynamicCast>> + ( + fieldToken.transferCompoundToken(is) + ) + ); + + if (fPtr->size() != patchSize) + { + FatalIOErrorInFunction(dict) + << "\n size of field " << key + << " (" << fPtr->size() << ')' + << " is not the same size as the patch (" + << patchSize << ')' + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); + } + + tensorFields_.insert(key, fPtr); + } + else + { + FatalIOErrorInFunction(dict) + << "\n compound " << fieldToken.compoundToken() + << " not supported" + << "\n on patch " << this->patch().name() + << " of field " + << this->internalField().name() + << " in file " + << this->internalField().objectPath() << nl + << exit(FatalIOError); } } } @@ -488,41 +480,42 @@ void Foam::genericPointPatchField::write(Ostream& os) const { const keyType& key = dEntry.keyword(); - if (key != "type") + if (key == "type" || key == "value") { - if - ( - dEntry.isStream() - && dEntry.stream().size() - && dEntry.stream()[0].isWord() - && dEntry.stream()[0].wordToken() == "nonuniform" - ) + continue; + } + else if + ( + dEntry.isStream() + && dEntry.stream().size() + && dEntry.stream()[0].isWord() + && dEntry.stream()[0].wordToken() == "nonuniform" + ) + { + if (scalarFields_.found(key)) { - if (scalarFields_.found(key)) - { - scalarFields_.find(key)()->writeEntry(key, os); - } - else if (vectorFields_.found(key)) - { - vectorFields_.find(key)()->writeEntry(key, os); - } - else if (sphTensorFields_.found(key)) - { - sphTensorFields_.find(key)()->writeEntry(key, os); - } - else if (symmTensorFields_.found(key)) - { - symmTensorFields_.find(key)()->writeEntry(key, os); - } - else if (tensorFields_.found(key)) - { - tensorFields_.find(key)()->writeEntry(key, os); - } + scalarFields_.cfind(key)()->writeEntry(key, os); } - else + else if (vectorFields_.found(key)) { - dEntry.write(os); + vectorFields_.cfind(key)()->writeEntry(key, os); } + else if (sphTensorFields_.found(key)) + { + sphTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (symmTensorFields_.found(key)) + { + symmTensorFields_.cfind(key)()->writeEntry(key, os); + } + else if (tensorFields_.found(key)) + { + tensorFields_.cfind(key)()->writeEntry(key, os); + } + } + else + { + dEntry.write(os); } } } From 332cf3345ac284e1dd42d587c17be7df227ee1a1 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 4 Apr 2019 16:23:41 +0200 Subject: [PATCH 07/19] ENH: emit "constant" for uniform constant field (#1269) - this improves overall consistency and makes re-reading as a generic patch field behave properly when the underlying patch has zero size. --- .../PatchFunction1/ConstantField/ConstantField.C | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/meshTools/PatchFunction1/ConstantField/ConstantField.C b/src/meshTools/PatchFunction1/ConstantField/ConstantField.C index 5ef4d1d974..0650943323 100644 --- a/src/meshTools/PatchFunction1/ConstantField/ConstantField.C +++ b/src/meshTools/PatchFunction1/ConstantField/ConstantField.C @@ -253,8 +253,17 @@ void Foam::PatchFunction1Types::ConstantField::writeData ) const { PatchFunction1::writeData(os); - //os << token::SPACE << value_ << token::END_STATEMENT << nl; - value_.writeEntry(this->name_, os); + + if (isUniform_) + { + os.writeKeyword(this->name_) + << "constant " << uniformValue_ + << token::END_STATEMENT << nl; + } + else + { + value_.writeEntry(this->name_, os); + } } From dd9b7d3d7e0f0c73c6df2d8d7d119c80dd474228 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 2 Apr 2019 15:36:16 +0200 Subject: [PATCH 08/19] STYLE: make some OSspecific constructors explicit --- src/OSspecific/POSIX/Make/files | 10 +++++----- src/OSspecific/POSIX/POSIX.H | 4 ++-- src/OSspecific/POSIX/fileMonitor.H | 2 +- src/OSspecific/POSIX/fileStat.H | 2 +- src/OSspecific/POSIX/timer.H | 5 ++--- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/OSspecific/POSIX/Make/files b/src/OSspecific/POSIX/Make/files index 26e735cb88..06df47630f 100644 --- a/src/OSspecific/POSIX/Make/files +++ b/src/OSspecific/POSIX/Make/files @@ -1,3 +1,8 @@ +clockTime/clockTime.C +clockValue/clockValue.C +cpuInfo/cpuInfo.C +cpuTime/cpuTime.C +memInfo/memInfo.C signals/sigFpe.C signals/sigSegv.C signals/sigInt.C @@ -8,11 +13,6 @@ regExpPosix.C timer.C fileStat.C POSIX.C -cpuTime/cpuTime.C -clockTime/clockTime.C -clockValue/clockValue.C -cpuInfo/cpuInfo.C -memInfo/memInfo.C /* * Note: fileMonitor assumes inotify by default. Compile with -DFOAM_USE_STAT diff --git a/src/OSspecific/POSIX/POSIX.H b/src/OSspecific/POSIX/POSIX.H index 855ccffc01..9f8927936a 100644 --- a/src/OSspecific/POSIX/POSIX.H +++ b/src/OSspecific/POSIX/POSIX.H @@ -51,8 +51,8 @@ namespace POSIX //- Declare namespace and its debug switch NamespaceName("POSIX"); - const label pathLengthChunk = 256; - const label pathLengthMax = 4096; + constexpr label pathLengthChunk = 256; + constexpr label pathLengthMax = 4096; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OSspecific/POSIX/fileMonitor.H b/src/OSspecific/POSIX/fileMonitor.H index ed7af01365..be7f8f25ea 100644 --- a/src/OSspecific/POSIX/fileMonitor.H +++ b/src/OSspecific/POSIX/fileMonitor.H @@ -120,7 +120,7 @@ public: // Constructors //- Construct null - fileMonitor(const bool useInotify); + explicit fileMonitor(const bool useInotify); //- Destructor diff --git a/src/OSspecific/POSIX/fileStat.H b/src/OSspecific/POSIX/fileStat.H index ca0acc21ed..d390834243 100644 --- a/src/OSspecific/POSIX/fileStat.H +++ b/src/OSspecific/POSIX/fileStat.H @@ -111,7 +111,7 @@ public: ); //- Construct from Istream - fileStat(Istream& is); + explicit fileStat(Istream& is); // Member Functions diff --git a/src/OSspecific/POSIX/timer.H b/src/OSspecific/POSIX/timer.H index 390eeb0d56..50ea74d8ac 100644 --- a/src/OSspecific/POSIX/timer.H +++ b/src/OSspecific/POSIX/timer.H @@ -114,9 +114,8 @@ public: // Constructors - //- Construct from components. - // newTimeOut=0 makes it do nothing. - timer(const unsigned int newTimeOut); + //- Construct with specified time-out, a value of 0 makes it a no-op. + explicit timer(unsigned int seconds); //- Destructor From f3670521cda549c83271e79abb1d1548d1878166 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 3 Apr 2019 20:24:30 +0200 Subject: [PATCH 09/19] STYLE: use std:: qualifier on ::abort, ::exit - consistent with use of and makes for a stronger distinct from Foam::abort etc. --- applications/test/alloc/Test-malloc.C | 2 +- applications/test/alloc/Test.C | 2 +- applications/test/maxMem/Test-maxMem.C | 3 ++- .../miscellaneous/OSspecific/addr2line/addr2line.C | 4 ++-- src/OpenFOAM/db/dictionary/dictionary.C | 4 ++-- src/OpenFOAM/db/dictionary/entry/entry.C | 4 ++-- src/OpenFOAM/db/error/IOerror.C | 8 ++++---- src/OpenFOAM/db/error/error.C | 6 +++--- .../functionObjectList/functionObjectList.C | 2 +- src/OpenFOAM/global/argList/argList.C | 2 +- src/OpenFOAM/global/debug/debug.C | 2 +- src/OpenFOAM/global/etcFiles/etcFiles.C | 2 +- src/OpenFOAM/global/new.C | 2 +- src/Pstream/dummy/UPstream.C | 4 ++-- src/Pstream/mpi/UPstream.C | 6 +++--- src/conversion/ccm/common/ccmInternal.C | 2 +- .../snappyHexMesh/snappyHexMeshDriver/snappyLayerDriver.C | 2 +- 17 files changed, 29 insertions(+), 28 deletions(-) diff --git a/applications/test/alloc/Test-malloc.C b/applications/test/alloc/Test-malloc.C index 1614987e3c..4bfc18a20e 100644 --- a/applications/test/alloc/Test-malloc.C +++ b/applications/test/alloc/Test-malloc.C @@ -1,6 +1,6 @@ #include "stream.h" #include -#include +#include main() { diff --git a/applications/test/alloc/Test.C b/applications/test/alloc/Test.C index 67f77ab05c..a2de8b24df 100644 --- a/applications/test/alloc/Test.C +++ b/applications/test/alloc/Test.C @@ -1,4 +1,4 @@ -#include +#include class Int { diff --git a/applications/test/maxMem/Test-maxMem.C b/applications/test/maxMem/Test-maxMem.C index 590c542ee9..3a6c5c5c6e 100644 --- a/applications/test/maxMem/Test-maxMem.C +++ b/applications/test/maxMem/Test-maxMem.C @@ -1,5 +1,6 @@ #include -#include +#include + using namespace std; int main(int argc, char *argv[]) diff --git a/applications/utilities/miscellaneous/OSspecific/addr2line/addr2line.C b/applications/utilities/miscellaneous/OSspecific/addr2line/addr2line.C index 928c17216a..399f81b507 100644 --- a/applications/utilities/miscellaneous/OSspecific/addr2line/addr2line.C +++ b/applications/utilities/miscellaneous/OSspecific/addr2line/addr2line.C @@ -121,14 +121,14 @@ void usage() std::cout << "usage: addr2line [-e filename|--exe=filename]" " address [address...]\n" << std::endl; - ::exit(1); + std::exit(1); } void version() { std::cout<< "OpenFOAM addr2line emulator\n" << std::endl; - ::exit(0); + std::exit(0); } diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index 37ba5fe667..775d90ea59 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -272,7 +272,7 @@ void Foam::dictionary::checkITstream << " at line " << is.lineNumber() << '.' << nl << std::endl; - ::exit(1); + std::exit(1); } } else if (!is.size()) @@ -305,7 +305,7 @@ void Foam::dictionary::checkITstream << " at line " << is.lineNumber() << '.' << nl << std::endl; - ::exit(1); + std::exit(1); } } } diff --git a/src/OpenFOAM/db/dictionary/entry/entry.C b/src/OpenFOAM/db/dictionary/entry/entry.C index eccfa396c7..9bd55a3e05 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.C +++ b/src/OpenFOAM/db/dictionary/entry/entry.C @@ -116,7 +116,7 @@ void Foam::entry::checkITstream(const ITstream& is) const << " at line " << is.lineNumber() << '.' << nl << std::endl; - ::exit(1); + std::exit(1); } } else if (!is.size()) @@ -149,7 +149,7 @@ void Foam::entry::checkITstream(const ITstream& is) const << " at line " << is.lineNumber() << '.' << nl << std::endl; - ::exit(1); + std::exit(1); } } } diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index 51b007342b..d3b51b3e71 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -153,7 +153,7 @@ void Foam::IOerror::SafeFatalIOError << " in file " << sourceFileName << " at line " << sourceFileLineNumber << '.' << std::endl; - ::exit(1); + std::exit(1); } } @@ -210,7 +210,7 @@ void Foam::IOerror::exit(const int) { Perr<< endl << *this << endl << "\nFOAM exiting\n" << endl; - ::exit(1); + std::exit(1); } } } @@ -229,7 +229,7 @@ void Foam::IOerror::abort() Perr<< endl << *this << endl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; printStack(Perr); - ::abort(); + std::abort(); } if (Pstream::parRun()) @@ -256,7 +256,7 @@ void Foam::IOerror::abort() Perr<< endl << *this << endl << "\nFOAM aborting\n" << endl; printStack(Perr); - ::abort(); + std::abort(); } } } diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index 8140ed6f9c..c143fcf5f1 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -244,7 +244,7 @@ void Foam::error::exit(const int errNo) { Perr<< endl << *this << endl << "\nFOAM exiting\n" << endl; - ::exit(errNo); + std::exit(errNo); } } @@ -262,7 +262,7 @@ void Foam::error::abort() Perr<< endl << *this << endl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; printStack(Perr); - ::abort(); + std::abort(); } if (throwExceptions_) @@ -287,7 +287,7 @@ void Foam::error::abort() Perr<< endl << *this << endl << "\nFOAM aborting\n" << endl; printStack(Perr); - ::abort(); + std::abort(); } } diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index a95b8d1b2b..bdb0128ceb 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -887,7 +887,7 @@ bool Foam::functionObjectList::read() catch (const Foam::IOerror& ioErr) { Info<< ioErr << nl << endl; - ::exit(1); + std::exit(1); } catch (const Foam::error& err) { diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index 8a100b33cb..f9daf29dc8 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -906,7 +906,7 @@ void Foam::argList::parse if (quickExit) { - ::exit(0); + std::exit(0); } } diff --git a/src/OpenFOAM/global/debug/debug.C b/src/OpenFOAM/global/debug/debug.C index a22251d9ce..bdbc0ce8c4 100644 --- a/src/OpenFOAM/global/debug/debug.C +++ b/src/OpenFOAM/global/debug/debug.C @@ -147,7 +147,7 @@ Foam::dictionary& Foam::debug::switchSet << controlDict().name().c_str() << std::endl << std::endl; - ::exit(1); + std::exit(1); } subDictPtr = &(eptr->dict()); diff --git a/src/OpenFOAM/global/etcFiles/etcFiles.C b/src/OpenFOAM/global/etcFiles/etcFiles.C index de3c6f83c7..3e04dbe70f 100644 --- a/src/OpenFOAM/global/etcFiles/etcFiles.C +++ b/src/OpenFOAM/global/etcFiles/etcFiles.C @@ -82,7 +82,7 @@ static inline void errorMandatoryNotFound << locationToString(location) << ")\n '" << name << "'\n" << std::endl; - ::exit(1); + std::exit(1); } diff --git a/src/OpenFOAM/global/new.C b/src/OpenFOAM/global/new.C index ac7f38f077..d98ec3d2fd 100644 --- a/src/OpenFOAM/global/new.C +++ b/src/OpenFOAM/global/new.C @@ -46,7 +46,7 @@ void newError() "by e.g. bad use of pointers or an out of date shared library" << std::endl; - ::abort(); + std::abort(); } void (*old_new_handler)() = std::set_new_handler(newError); diff --git a/src/Pstream/dummy/UPstream.C b/src/Pstream/dummy/UPstream.C index 661e5a2cc5..9dbaf0f574 100644 --- a/src/Pstream/dummy/UPstream.C +++ b/src/Pstream/dummy/UPstream.C @@ -58,14 +58,14 @@ bool Foam::UPstream::init(int& argc, char**& argv, const bool needsThread) void Foam::UPstream::exit(int errnum) { // No MPI - just exit - ::exit(errnum); + std::exit(errnum); } void Foam::UPstream::abort() { // No MPI - just abort - ::abort(); + std::abort(); } diff --git a/src/Pstream/mpi/UPstream.C b/src/Pstream/mpi/UPstream.C index 5a2ec78973..0fec38de2c 100644 --- a/src/Pstream/mpi/UPstream.C +++ b/src/Pstream/mpi/UPstream.C @@ -213,7 +213,7 @@ void Foam::UPstream::exit(int errnum) if (!flag) { // Not initialized - just exit - ::exit(errnum); + std::exit(errnum); return; } @@ -224,7 +224,7 @@ void Foam::UPstream::exit(int errnum) WarningInFunction << "MPI was already finalized (perhaps by a connected program)" << endl; - ::exit(1); + std::exit(1); return; } @@ -271,7 +271,7 @@ void Foam::UPstream::exit(int errnum) if (errnum == 0) { MPI_Finalize(); - ::exit(errnum); + std::exit(errnum); } else { diff --git a/src/conversion/ccm/common/ccmInternal.C b/src/conversion/ccm/common/ccmInternal.C index 47dd0c3933..42698b4924 100644 --- a/src/conversion/ccm/common/ccmInternal.C +++ b/src/conversion/ccm/common/ccmInternal.C @@ -118,7 +118,7 @@ bool Foam::ccm::ccmGlobalState::assertNoError << "\n libccmio reports -> " << errorMsg(err) << " <-\n" << endl; - ::exit(1); + std::exit(1); } return (err == kCCMIONoErr); diff --git a/src/mesh/snappyHexMesh/snappyHexMeshDriver/snappyLayerDriver.C b/src/mesh/snappyHexMesh/snappyHexMeshDriver/snappyLayerDriver.C index 6518115a13..c8269b51d8 100644 --- a/src/mesh/snappyHexMesh/snappyHexMeshDriver/snappyLayerDriver.C +++ b/src/mesh/snappyHexMesh/snappyHexMeshDriver/snappyLayerDriver.C @@ -3916,7 +3916,7 @@ void Foam::snappyLayerDriver::addLayers else { Perr<< "\nFOAM exiting\n" << endl; - ::exit(0); + std::exit(0); } } } From 1c4e32fb6afcc6f36e1375d4ace6f1eb50db87f9 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 4 Apr 2019 17:14:27 +0200 Subject: [PATCH 10/19] ENH: add iterators to VectorSpace (#1265) - this adds support for various STL operations including * sorting, filling, find min/max element etc. * for-range iteration STYLE: use constexpr for VectorSpace rank --- applications/test/vector/Test-vector.C | 28 +++- .../Barycentric/BarycentricTensor.H | 2 +- .../primitives/DiagTensor/DiagTensor.H | 2 +- .../primitives/MatrixSpace/MatrixSpace.H | 10 +- .../primitives/MatrixSpace/MatrixSpaceI.H | 4 +- .../SphericalTensor/SphericalTensor.H | 2 +- .../SphericalTensor2D/SphericalTensor2D.H | 2 +- .../primitives/SymmTensor/SymmTensor.H | 2 +- .../primitives/SymmTensor2D/SymmTensor2D.H | 2 +- src/OpenFOAM/primitives/Tensor/Tensor.H | 7 +- src/OpenFOAM/primitives/Tensor2D/Tensor2D.H | 2 +- src/OpenFOAM/primitives/Vector/Vector.H | 2 +- src/OpenFOAM/primitives/Vector2D/Vector2D.H | 2 +- .../primitives/VectorSpace/VectorSpace.C | 18 +- .../primitives/VectorSpace/VectorSpace.H | 63 +++++-- .../primitives/VectorSpace/VectorSpaceI.H | 157 +++++++++++------- .../SpatialTensor/SpatialTensor.H | 2 +- 17 files changed, 201 insertions(+), 106 deletions(-) diff --git a/applications/test/vector/Test-vector.C b/applications/test/vector/Test-vector.C index 497dea43d1..fc3e430882 100644 --- a/applications/test/vector/Test-vector.C +++ b/applications/test/vector/Test-vector.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -31,6 +31,7 @@ Description #include "vector.H" #include "IOstreams.H" +#include using namespace Foam; @@ -74,6 +75,18 @@ void doTest(vector& vec1, vector& vec2) } +template +void testIterator(const VecSpace& vs) +{ + Info<< "size: " << vs.size() << " for:"; + for (const auto& val : vs) + { + Info<< " " << val; + } + Info<< nl; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -89,8 +102,21 @@ int main(int argc, char *argv[]) vector vec2(0.5, 0.51, -0.5); doTest(vec1, vec2); + + testIterator(vec1); + testIterator(vec2); + + // Use STL algorithm(s) + + std::sort(vec2.begin(), vec2.end()); + Info<< "sorted: " << vec2 << nl; + + std::random_shuffle(vec2.begin(), vec2.end()); + Info<< "shuffled: " << vec2 << nl; } + Info<< "\nEnd\n" << nl; + return 0; } diff --git a/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H b/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H index 83d5ac064a..e603dc5304 100644 --- a/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H +++ b/src/OpenFOAM/primitives/Barycentric/BarycentricTensor.H @@ -67,7 +67,7 @@ public: // Member constants //- Rank of BarycentricTensor is 2 - static const direction rank = 2; + static constexpr direction rank = 2; //- Component labeling enumeration diff --git a/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H b/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H index 3792d35e06..797b7c68eb 100644 --- a/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H +++ b/src/OpenFOAM/primitives/DiagTensor/DiagTensor.H @@ -67,7 +67,7 @@ public: // Member constants //- Rank of DiagTensor is 2 - static const direction rank = 2; + static constexpr direction rank = 2; //- Component labeling enumeration diff --git a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H index 8c45d5669c..f83297a056 100644 --- a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H +++ b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpace.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2016 OpenFOAM Foundation @@ -68,20 +68,20 @@ public: // Member constants - static const direction mRows = Mrows; - static const direction nCols = Ncols; + static constexpr direction mRows = Mrows; + static constexpr direction nCols = Ncols; // Static member functions //- Return the number of rows - static direction m() + static direction m() noexcept { return Mrows; } //- Return the number of columns - static direction n() + static direction n() noexcept { return Ncols; } diff --git a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H index d6ff7d796c..5e696035ae 100644 --- a/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H +++ b/src/OpenFOAM/primitives/MatrixSpace/MatrixSpaceI.H @@ -345,7 +345,7 @@ inline const Cmpt& Foam::MatrixSpace::operator() ) const { #ifdef FULLDEBUG - if (i > Mrows-1 || j > Ncols-1) + if (i >= Mrows || j >= Ncols) { FatalErrorInFunction << "indices out of range" @@ -365,7 +365,7 @@ inline Cmpt& Foam::MatrixSpace::operator() ) { #ifdef FULLDEBUG - if (i > Mrows-1 || j > Ncols-1) + if (i >= Mrows || j >= Ncols) { FatalErrorInFunction << "indices out of range" diff --git a/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H b/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H index 722a81f23d..ff9f8ec959 100644 --- a/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H +++ b/src/OpenFOAM/primitives/SphericalTensor/SphericalTensor.H @@ -65,7 +65,7 @@ public: // Member constants //- Rank of SphericalTensor is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members diff --git a/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H b/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H index c801540998..7318623108 100644 --- a/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H +++ b/src/OpenFOAM/primitives/SphericalTensor2D/SphericalTensor2D.H @@ -61,7 +61,7 @@ public: // Member constants //- Rank of SphericalTensor2D is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members diff --git a/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H b/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H index 71a8fadb78..2fb50c93b9 100644 --- a/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H +++ b/src/OpenFOAM/primitives/SymmTensor/SymmTensor.H @@ -67,7 +67,7 @@ public: // Member constants //- Rank of SymmTensor is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members diff --git a/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H b/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H index 7b026dc82f..c961f6203a 100644 --- a/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H +++ b/src/OpenFOAM/primitives/SymmTensor2D/SymmTensor2D.H @@ -67,7 +67,7 @@ public: // Member constants //- Rank of SymmTensor2D is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members diff --git a/src/OpenFOAM/primitives/Tensor/Tensor.H b/src/OpenFOAM/primitives/Tensor/Tensor.H index c373a266ce..21fc9176d0 100644 --- a/src/OpenFOAM/primitives/Tensor/Tensor.H +++ b/src/OpenFOAM/primitives/Tensor/Tensor.H @@ -53,8 +53,9 @@ See also namespace Foam { -template -class SymmTensor; +// Forward Declarations +template class SymmTensor; + /*---------------------------------------------------------------------------*\ Class Tensor Declaration @@ -75,7 +76,7 @@ public: // Member constants //- Rank of Tensor is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members diff --git a/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H b/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H index 1606215fa6..332d74e960 100644 --- a/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H +++ b/src/OpenFOAM/primitives/Tensor2D/Tensor2D.H @@ -70,7 +70,7 @@ public: // Member constants //- Rank of Tensor2D is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members diff --git a/src/OpenFOAM/primitives/Vector/Vector.H b/src/OpenFOAM/primitives/Vector/Vector.H index d46e29416f..bccc1bd25f 100644 --- a/src/OpenFOAM/primitives/Vector/Vector.H +++ b/src/OpenFOAM/primitives/Vector/Vector.H @@ -72,7 +72,7 @@ public: // Member constants //- Rank of Vector is 1 - static const direction rank = 1; + static constexpr direction rank = 1; //- Component labeling enumeration diff --git a/src/OpenFOAM/primitives/Vector2D/Vector2D.H b/src/OpenFOAM/primitives/Vector2D/Vector2D.H index d2d25c5f9f..ed69c95ed7 100644 --- a/src/OpenFOAM/primitives/Vector2D/Vector2D.H +++ b/src/OpenFOAM/primitives/Vector2D/Vector2D.H @@ -65,7 +65,7 @@ public: // Member constants //- Rank of Vector2D is 1 - static const direction rank = 1; + static constexpr direction rank = 1; //- Component labeling enumeration diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C b/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C index c88bb1f8fb..f40e9caacb 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpace.C @@ -38,18 +38,15 @@ Foam::VectorSpace::VectorSpace Istream& is ) { - // Read beginning of VectorSpace - is.readBegin("VectorSpace"); + is.readBegin("VectorSpace"); for (direction i=0; i> v_[i]; } - // Read end of VectorSpace - is.readEnd("VectorSpace"); + is.readEnd("VectorSpace"); - // Check state of Istream is.check(FUNCTION_NAME); } @@ -64,7 +61,7 @@ Foam::word Foam::name buf << '(' << vs.v_[0]; - for (direction i=1; i> VectorSpace& vs ) { - // Read beginning of VectorSpace - is.readBegin("VectorSpace"); + is.readBegin("VectorSpace"); for (direction i=0; i> vs.v_[i]; } - // Read end of VectorSpace - is.readEnd("VectorSpace"); + is.readEnd("VectorSpace"); - // Check state of Istream is.check(FUNCTION_NAME); return is; @@ -111,7 +105,7 @@ Foam::Ostream& Foam::operator<< { os << token::BEGIN_LIST << vs.v_[0]; - for (direction i=1; i + template class ConstBlock { const vsType& vs_; @@ -158,12 +154,12 @@ public: inline VectorSpace(const Foam::zero); //- Construct from Istream - VectorSpace(Istream&); + VectorSpace(Istream& is); - //- Construct as copy - inline VectorSpace(const VectorSpace&); + //- Copy construct + inline VectorSpace(const VectorSpace& vs); - //- Construct as copy of a VectorSpace with the same size + //- Copy construct of a VectorSpace with the same size template inline explicit VectorSpace(const VectorSpace&); @@ -171,7 +167,7 @@ public: // Member Functions //- Return the number of elements in the VectorSpace = Ncmpts. - inline static direction size(); + inline static constexpr direction size(); inline const Cmpt& component(const direction) const; inline Cmpt& component(const direction); @@ -200,6 +196,39 @@ public: inline void operator/=(const scalar); + // Iterators + + //- Random access iterator for traversing VectorSpace + typedef Cmpt* iterator; + + //- Random access iterator for traversing VectorSpace + typedef const Cmpt* const_iterator; + + + // Random access iterator (non-const) + + //- Return an iterator to begin of VectorSpace + inline iterator begin(); + + //- Return an iterator to end of UListVectorSpace + inline iterator end(); + + + // Random access iterator (const) + + //- Return const_iterator to begin of VectorSpace + inline const_iterator cbegin() const; + + //- Return const_iterator to end of VectorSpace + inline const_iterator cend() const; + + //- Return const_iterator to begin of VectorSpace + inline const_iterator begin() const; + + //- Return const_iterator to end of VectorSpace + inline const_iterator end() const; + + // IOstream Operators friend Istream& operator>> diff --git a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H index 89af43b3ab..bba3f77740 100644 --- a/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H +++ b/src/OpenFOAM/primitives/VectorSpace/VectorSpaceI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -31,27 +31,23 @@ License #include "ops.H" #include -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -template -inline VectorSpace::VectorSpace() +template +inline Foam::VectorSpace::VectorSpace() {} -template -inline VectorSpace::VectorSpace(const Foam::zero) +template +inline Foam::VectorSpace::VectorSpace(const Foam::zero) { VectorSpaceOps::eqOpS(*this, Zero, eqOp()); } -template -inline VectorSpace::VectorSpace +template +inline Foam::VectorSpace::VectorSpace ( const VectorSpace& vs ) @@ -60,9 +56,9 @@ inline VectorSpace::VectorSpace } -template +template template -inline VectorSpace::VectorSpace +inline Foam::VectorSpace::VectorSpace ( const VectorSpace& vs ) @@ -71,10 +67,10 @@ inline VectorSpace::VectorSpace } -template -template -inline -VectorSpace::ConstBlock::ConstBlock +template +template +inline Foam::VectorSpace::ConstBlock + ::ConstBlock ( const vsType& vs ) @@ -91,15 +87,15 @@ VectorSpace::ConstBlock::ConstBlock // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -inline direction VectorSpace::size() +template +inline constexpr Foam::direction Foam::VectorSpace::size() { return Ncmpts; } -template -inline const Cmpt& VectorSpace::component +template +inline const Cmpt& Foam::VectorSpace::component ( const direction d ) const @@ -117,8 +113,8 @@ inline const Cmpt& VectorSpace::component } -template -inline Cmpt& VectorSpace::component +template +inline Cmpt& Foam::VectorSpace::component ( const direction d ) @@ -136,8 +132,8 @@ inline Cmpt& VectorSpace::component } -template -inline void VectorSpace::component +template +inline void Foam::VectorSpace::component ( Cmpt& c, const direction d @@ -156,8 +152,8 @@ inline void VectorSpace::component } -template -inline void VectorSpace::replace +template +inline void Foam::VectorSpace::replace ( const direction d, const Cmpt& c @@ -176,8 +172,8 @@ inline void VectorSpace::replace } -template -inline Form VectorSpace::uniform(const Cmpt& s) +template +inline Form Foam::VectorSpace::uniform(const Cmpt& s) { Form v; VectorSpaceOps::eqOpS(v, s, eqOp()); @@ -185,20 +181,64 @@ inline Form VectorSpace::uniform(const Cmpt& s) } -template -template -inline const typename VectorSpace::template +template +template +inline const typename Foam::VectorSpace::template ConstBlock -VectorSpace::block() const +Foam::VectorSpace::block() const { return *this; } +// * * * * * * * * * * * * * * * * Iterator * * * * * * * * * * * * * * * * // + +template +inline Cmpt* Foam::VectorSpace::begin() +{ + return v_; +} + + +template +inline Cmpt* Foam::VectorSpace::end() +{ + return (v_ + Ncmpts); +} + + +template +inline const Cmpt* Foam::VectorSpace::cbegin() const +{ + return v_; +} + + +template +inline const Cmpt* Foam::VectorSpace::cend() const +{ + return (v_ + Ncmpts); +} + + +template +inline const Cmpt* Foam::VectorSpace::begin() const +{ + return v_; +} + + +template +inline const Cmpt* Foam::VectorSpace::end() const +{ + return (v_ + Ncmpts); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -template -inline const Cmpt& VectorSpace::operator[] +template +inline const Cmpt& Foam::VectorSpace::operator[] ( const direction d ) const @@ -216,8 +256,8 @@ inline const Cmpt& VectorSpace::operator[] } -template -inline Cmpt& VectorSpace::operator[] +template +inline Cmpt& Foam::VectorSpace::operator[] ( const direction d ) @@ -235,10 +275,10 @@ inline Cmpt& VectorSpace::operator[] } -template -template +template +template inline const Cmpt& -VectorSpace:: +Foam::VectorSpace:: ConstBlock::operator[] ( const direction d @@ -257,10 +297,10 @@ ConstBlock::operator[] } -template -template +template +template inline const Cmpt& -VectorSpace:: +Foam::VectorSpace:: ConstBlock::operator() ( const direction i, @@ -275,7 +315,7 @@ ConstBlock::operator() << abort(FatalError); } - if (j != 0) + if (j) { FatalErrorInFunction << "index " << j << " != 0" @@ -287,8 +327,8 @@ ConstBlock::operator() } -template -inline void VectorSpace::operator= +template +inline void Foam::VectorSpace::operator= ( const VectorSpace& vs ) @@ -297,8 +337,8 @@ inline void VectorSpace::operator= } -template -inline void VectorSpace::operator+= +template +inline void Foam::VectorSpace::operator+= ( const VectorSpace& vs ) @@ -307,8 +347,8 @@ inline void VectorSpace::operator+= } -template -inline void VectorSpace::operator-= +template +inline void Foam::VectorSpace::operator-= ( const VectorSpace& vs ) @@ -317,15 +357,15 @@ inline void VectorSpace::operator-= } -template -inline void VectorSpace::operator=(const Foam::zero) +template +inline void Foam::VectorSpace::operator=(const Foam::zero) { VectorSpaceOps::eqOpS(*this, 0, eqOp()); } -template -inline void VectorSpace::operator*= +template +inline void Foam::VectorSpace::operator*= ( const scalar s ) @@ -334,8 +374,8 @@ inline void VectorSpace::operator*= } -template -inline void VectorSpace::operator/= +template +inline void Foam::VectorSpace::operator/= ( const scalar s ) @@ -344,6 +384,11 @@ inline void VectorSpace::operator/= } +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H b/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H index 5bd51f6f3f..923a05bd61 100644 --- a/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H +++ b/src/OpenFOAM/primitives/spatialVectorAlgebra/SpatialTensor/SpatialTensor.H @@ -74,7 +74,7 @@ public: // Member constants //- Rank of Tensor is 2 - static const direction rank = 2; + static constexpr direction rank = 2; // Static data members From fe71bbd013fcf3c639f266ea6f6ea96c2cdeb1ac Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 4 Apr 2019 17:40:06 +0200 Subject: [PATCH 11/19] STYLE: use constexpr for pTraits rank --- src/OpenFOAM/primitives/Barycentric/Barycentric.H | 2 +- src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H | 2 +- src/OpenFOAM/primitives/Scalar/Scalar.H | 6 +++--- src/OpenFOAM/primitives/bools/bool/bool.H | 6 +++--- src/OpenFOAM/primitives/ints/int32/int32.H | 6 +++--- src/OpenFOAM/primitives/ints/int64/int64.H | 6 +++--- src/OpenFOAM/primitives/ints/uint32/uint32.H | 6 +++--- src/OpenFOAM/primitives/ints/uint64/uint64.H | 6 +++--- src/OpenFOAM/primitives/quaternion/quaternion.H | 2 +- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/OpenFOAM/primitives/Barycentric/Barycentric.H b/src/OpenFOAM/primitives/Barycentric/Barycentric.H index 529d975d13..ae4fe2fb42 100644 --- a/src/OpenFOAM/primitives/Barycentric/Barycentric.H +++ b/src/OpenFOAM/primitives/Barycentric/Barycentric.H @@ -63,7 +63,7 @@ public: // Member constants //- Rank of Barycentric is 1 - static const direction rank = 1; + static constexpr direction rank = 1; //- Component labeling enumeration diff --git a/src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H b/src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H index 49d650da82..b5c46093c9 100644 --- a/src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H +++ b/src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H @@ -63,7 +63,7 @@ public: // Member constants //- Rank of Barycentric2D is 1 - static const direction rank = 1; + static constexpr direction rank = 1; //- Component labeling enumeration diff --git a/src/OpenFOAM/primitives/Scalar/Scalar.H b/src/OpenFOAM/primitives/Scalar/Scalar.H index 833f73fe93..9039dbdcb9 100644 --- a/src/OpenFOAM/primitives/Scalar/Scalar.H +++ b/src/OpenFOAM/primitives/Scalar/Scalar.H @@ -59,13 +59,13 @@ public: // Member constants //- Dimensionality of space - static const direction dim = 3; + static constexpr direction dim = 3; //- Rank of Scalar is 0 - static const direction rank = 0; + static constexpr direction rank = 0; //- Number of components in Scalar is 1 - static const direction nComponents = 1; + static constexpr direction nComponents = 1; // Static data members diff --git a/src/OpenFOAM/primitives/bools/bool/bool.H b/src/OpenFOAM/primitives/bools/bool/bool.H index 6b6863b096..6874ca3be5 100644 --- a/src/OpenFOAM/primitives/bools/bool/bool.H +++ b/src/OpenFOAM/primitives/bools/bool/bool.H @@ -77,13 +77,13 @@ public: // Member constants //- Dimensionality of space - static const direction dim = 3; + static constexpr direction dim = 3; //- Rank of bool is 0 - static const direction rank = 0; + static constexpr direction rank = 0; //- Number of components in bool is 1 - static const direction nComponents = 1; + static constexpr direction nComponents = 1; // Static data members diff --git a/src/OpenFOAM/primitives/ints/int32/int32.H b/src/OpenFOAM/primitives/ints/int32/int32.H index 1840b66b32..5c4c22f020 100644 --- a/src/OpenFOAM/primitives/ints/int32/int32.H +++ b/src/OpenFOAM/primitives/ints/int32/int32.H @@ -142,13 +142,13 @@ public: // Member constants //- Dimensionality of space - static const direction dim = 3; + static constexpr direction dim = 3; //- Rank of int32_t is 0 - static const direction rank = 0; + static constexpr direction rank = 0; //- Number of components in int32_t is 1 - static const direction nComponents = 1; + static constexpr direction nComponents = 1; // Static data members diff --git a/src/OpenFOAM/primitives/ints/int64/int64.H b/src/OpenFOAM/primitives/ints/int64/int64.H index e8c5ece2fc..4027f86328 100644 --- a/src/OpenFOAM/primitives/ints/int64/int64.H +++ b/src/OpenFOAM/primitives/ints/int64/int64.H @@ -143,13 +143,13 @@ public: // Member constants //- Dimensionality of space - static const direction dim = 3; + static constexpr direction dim = 3; //- Rank of int64_t is 0 - static const direction rank = 0; + static constexpr direction rank = 0; //- Number of components in int64_t is 1 - static const direction nComponents = 1; + static constexpr direction nComponents = 1; // Static data members diff --git a/src/OpenFOAM/primitives/ints/uint32/uint32.H b/src/OpenFOAM/primitives/ints/uint32/uint32.H index 81a97323ca..629c14c3f0 100644 --- a/src/OpenFOAM/primitives/ints/uint32/uint32.H +++ b/src/OpenFOAM/primitives/ints/uint32/uint32.H @@ -133,13 +133,13 @@ public: // Member constants //- Dimensionality of space - static const direction dim = 3; + static constexpr direction dim = 3; //- Rank of uint32_t is 0 - static const direction rank = 0; + static constexpr direction rank = 0; //- Number of components in uint32_t is 1 - static const direction nComponents = 1; + static constexpr direction nComponents = 1; // Static data members diff --git a/src/OpenFOAM/primitives/ints/uint64/uint64.H b/src/OpenFOAM/primitives/ints/uint64/uint64.H index dee834635e..694e9f1158 100644 --- a/src/OpenFOAM/primitives/ints/uint64/uint64.H +++ b/src/OpenFOAM/primitives/ints/uint64/uint64.H @@ -141,13 +141,13 @@ public: // Member constants //- Dimensionality of space - static const direction dim = 3; + static constexpr direction dim = 3; //- Rank of uint64_t is 0 - static const direction rank = 0; + static constexpr direction rank = 0; //- Number of components in uint64_t is 1 - static const direction nComponents = 1; + static constexpr direction nComponents = 1; // Static data members diff --git a/src/OpenFOAM/primitives/quaternion/quaternion.H b/src/OpenFOAM/primitives/quaternion/quaternion.H index ab1e12111c..95878643ed 100644 --- a/src/OpenFOAM/primitives/quaternion/quaternion.H +++ b/src/OpenFOAM/primitives/quaternion/quaternion.H @@ -110,7 +110,7 @@ public: // Member constants //- Rank of quaternion is 1 - static const direction rank = 1; + static constexpr direction rank = 1; // Static data members From 567fced30b157f8470fe227398654a4d248e4c26 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 4 Apr 2019 19:00:29 +0200 Subject: [PATCH 12/19] ENH: add pTraits (#1247) --- applications/test/complex/Test-complex.C | 35 +++++++++++- applications/test/minMax1/Test-minMax1.C | 23 ++++++++ src/OpenFOAM/primitives/complex/complex.C | 35 ++++++++++++ src/OpenFOAM/primitives/complex/complex.H | 64 ++++++++++++++++++++++ src/OpenFOAM/primitives/complex/complexI.H | 1 + 5 files changed, 155 insertions(+), 3 deletions(-) diff --git a/applications/test/complex/Test-complex.C b/applications/test/complex/Test-complex.C index bc4d3d05dc..938f63fb8e 100644 --- a/applications/test/complex/Test-complex.C +++ b/applications/test/complex/Test-complex.C @@ -30,6 +30,8 @@ Description #include "argList.H" #include "complexFields.H" +#include "ops.H" +#include "ListOps.H" using namespace Foam; @@ -97,9 +99,6 @@ int main(int argc, char *argv[]) } - Info<< "sum = " << sum(fld1) << nl; - // Not yet Info<< "min = " << min(fld1) << nl; - fld1 *= 10; Info<< "scalar multiply: " << flatOutput(fld1) << nl; @@ -120,6 +119,36 @@ int main(int argc, char *argv[]) // Info<< "pow(2) : " << pow(fld1, 2) << nl; + // Make some changes + { + label i = 1; + for (complex& c : fld1) + { + c.Re() += i; + c.Im() -= 10 - i; + ++i; + } + } + + Info<< nl + << "field = " << fld1 << nl; + + Info<< "magSqr = " + << ListOps::create + ( + fld1, + [](const complex& c) { return magSqr(c); } + ) + << nl; + + Info + << "sum = " << sum(fld1) << nl + << "min = " << min(fld1) << nl + << "max = " << max(fld1) << nl; + + // MinMax fails since there is no less comparison operator + // Info<< "min/max = " << MinMax(fld1) << nl; + Info<< "\nEnd\n" << endl; return 0; } diff --git a/applications/test/minMax1/Test-minMax1.C b/applications/test/minMax1/Test-minMax1.C index 26d242a804..a33b3a3134 100644 --- a/applications/test/minMax1/Test-minMax1.C +++ b/applications/test/minMax1/Test-minMax1.C @@ -32,6 +32,7 @@ Description #include "HashOps.H" #include "ListOps.H" #include "scalarField.H" +#include "complexField.H" #include "MinMax.H" #include "dimensionedScalar.H" @@ -47,6 +48,19 @@ Ostream& printInfo(const MinMax& range) } +template +void testUniformField(const T& val) +{ + constexpr label N = 10; + + // Field fld(N, val); + List fld(N, val); + + Info<< "field: " << fld << nl + << "min/max: " << minMaxMag(fld) << nl; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Main program: @@ -225,6 +239,15 @@ int main(int argc, char *argv[]) Info<< "filtered: " << hashed << nl; } + + // Min/max of uniform fields + { + testUniformField(100); + // testUniformField(complex(100, 0)); + } + + Info<< "\nEnd\n" << nl; + return 0; } diff --git a/src/OpenFOAM/primitives/complex/complex.C b/src/OpenFOAM/primitives/complex/complex.C index 1368df2ae4..277864c487 100644 --- a/src/OpenFOAM/primitives/complex/complex.C +++ b/src/OpenFOAM/primitives/complex/complex.C @@ -35,6 +35,41 @@ const Foam::complex Foam::complex::zero(0, 0); const Foam::complex Foam::complex::one(1, 0); +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +const char* const Foam::pTraits::typeName = "complex"; +const char* const Foam::pTraits::componentNames[] = {"re", "im"}; + +const Foam::complex Foam::pTraits::zero(0, 0); +const Foam::complex Foam::pTraits::one(1, 0); + +const Foam::complex Foam::pTraits::min(-VGREAT, -VGREAT); +const Foam::complex Foam::pTraits::max(VGREAT, VGREAT); + +const Foam::complex Foam::pTraits::rootMin +( + -ROOTVGREAT, -ROOTVGREAT +); + +const Foam::complex Foam::pTraits::rootMax +( + ROOTVGREAT, ROOTVGREAT +); + + +Foam::pTraits::pTraits(const complex& val) +: + p_(val) +{} + + +Foam::pTraits::pTraits(Istream& is) +{ + is >> p_; +} + + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::complex::complex(Istream& is) diff --git a/src/OpenFOAM/primitives/complex/complex.H b/src/OpenFOAM/primitives/complex/complex.H index 48e3505d19..06f4eded4e 100644 --- a/src/OpenFOAM/primitives/complex/complex.H +++ b/src/OpenFOAM/primitives/complex/complex.H @@ -232,6 +232,70 @@ public: }; +// Template specialisation for pTraits +template<> +class pTraits +{ + complex p_; + +public: + + //- Component type + typedef complex cmptType; + + //- Equivalent type of labels used for valid component indexing + typedef label labelType; + + + // Member constants + + //- Dimensionality of space + static constexpr direction dim = 3; + + //- Rank of complex is 0 + static constexpr direction rank = 0; + + //- Number of components in complex is 2 + static constexpr direction nComponents = 2; + + + // Static Data Members + + static const char* const typeName; + static const char* const componentNames[]; + static const complex zero; + static const complex one; + static const complex max; + static const complex min; + static const complex rootMax; + static const complex rootMin; + + + // Constructors + + //- Construct from primitive + explicit pTraits(const complex& val); + + //- Construct from Istream + pTraits(Istream& is); + + + // Member Functions + + //- Access to the value + operator complex() const + { + return p_; + } + + //- Access to the value + operator complex&() + { + return p_; + } +}; + + // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * // Istream& operator>>(Istream& is, complex& c); diff --git a/src/OpenFOAM/primitives/complex/complexI.H b/src/OpenFOAM/primitives/complex/complexI.H index 25b6cc5a49..16e8594fb4 100644 --- a/src/OpenFOAM/primitives/complex/complexI.H +++ b/src/OpenFOAM/primitives/complex/complexI.H @@ -350,6 +350,7 @@ inline complex operator/(const scalar s, const complex& c) return complex(s/c.re, s/c.im); } + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam From 36fae9fd984da19cce5f945758f61388f8effee7 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 2 Apr 2019 22:05:13 +0200 Subject: [PATCH 13/19] ENH: open standard file streams with ios_base::binary - this improves overall consistency and is independent of higher level tagging as ASCII or BINARY for IFstream, OFstream etc. --- src/OSspecific/POSIX/POSIX.C | 13 +++++-------- src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C | 8 +++++--- src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index a63d3d746e..e09b2055a5 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -34,14 +34,11 @@ Description #include "OSspecific.H" #include "POSIX.H" -#include "foamVersion.H" #include "fileName.H" #include "fileStat.H" #include "timer.H" -#include "IFstream.H" #include "DynamicList.H" #include "CStringList.H" -#include "SubList.H" #include "IOstreams.H" #include "Pstream.H" @@ -49,7 +46,7 @@ Description #include #include -#include +#include #include #include #include @@ -896,14 +893,14 @@ bool Foam::cp(const fileName& src, const fileName& dest, const bool followLink) return false; } - // Open and check streams. - std::ifstream srcStream(src); + // Open and check streams. Enforce binary for extra safety + std::ifstream srcStream(src, ios_base::in | ios_base::binary); if (!srcStream) { return false; } - std::ofstream destStream(destFile); + std::ofstream destStream(destFile, ios_base::out | ios_base::binary); if (!destStream) { return false; diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C index a4908d9c7a..00562483a9 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C +++ b/src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -52,7 +52,9 @@ Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) } } - allocatedPtr_ = new std::ifstream(pathname); + const std::ios_base::openmode mode(std::ios_base::in|std::ios_base::binary); + + allocatedPtr_ = new std::ifstream(pathname, mode); // If the file is compressed, decompress it before reading. if (!allocatedPtr_->good() && isFile(pathname + ".gz", false)) @@ -63,7 +65,7 @@ Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) } delete allocatedPtr_; - allocatedPtr_ = new igzstream((pathname + ".gz").c_str()); + allocatedPtr_ = new igzstream((pathname + ".gz").c_str(), mode); if (allocatedPtr_->good()) { diff --git a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C index 709b0f07d3..027b7c82e2 100644 --- a/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C +++ b/src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -55,7 +55,7 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator } } - std::ios_base::openmode mode(std::ios_base::out); + std::ios_base::openmode mode(std::ios_base::out|std::ios_base::binary); if (append) { mode |= std::ios_base::app; From c24fbfd7e3955d11bcf4849d7f30cd1eda1c2573 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 2 Apr 2019 11:38:09 +0200 Subject: [PATCH 14/19] ENH: improve code isolation for signal-handling - file-local static for saving the old action, which moves system dependencies out of the header files. - set/reset of signals as file-local functions STYLE: use csignal header instead of signal.h --- src/OSspecific/POSIX/Make/files | 4 +- src/OSspecific/POSIX/cpuTime/cpuTime.H | 2 +- src/OSspecific/POSIX/fileMonitor.C | 2 +- src/OSspecific/POSIX/fileMonitor.H | 5 +- src/OSspecific/POSIX/fileStat.C | 1 - src/OSspecific/POSIX/signals/sigFpe.C | 39 ++--- src/OSspecific/POSIX/signals/sigFpe.H | 15 +- src/OSspecific/POSIX/signals/sigInt.C | 48 +++---- src/OSspecific/POSIX/signals/sigInt.H | 9 +- src/OSspecific/POSIX/signals/sigQuit.C | 48 +++---- src/OSspecific/POSIX/signals/sigQuit.H | 13 +- src/OSspecific/POSIX/signals/sigSegv.C | 48 +++---- src/OSspecific/POSIX/signals/sigSegv.H | 11 +- .../POSIX/signals/sigStopAtWriteNow.C | 113 +++++++-------- .../POSIX/signals/sigStopAtWriteNow.H | 36 ++--- src/OSspecific/POSIX/signals/sigWriteNow.C | 80 +++++------ src/OSspecific/POSIX/signals/sigWriteNow.H | 31 ++-- src/OSspecific/POSIX/signals/signalMacros.C | 74 ++++++++++ src/OSspecific/POSIX/signals/timer.C | 114 +++++++++++++++ src/OSspecific/POSIX/{ => signals}/timer.H | 32 ++--- src/OSspecific/POSIX/timer.C | 133 ------------------ 21 files changed, 402 insertions(+), 456 deletions(-) create mode 100644 src/OSspecific/POSIX/signals/signalMacros.C create mode 100644 src/OSspecific/POSIX/signals/timer.C rename src/OSspecific/POSIX/{ => signals}/timer.H (88%) delete mode 100644 src/OSspecific/POSIX/timer.C diff --git a/src/OSspecific/POSIX/Make/files b/src/OSspecific/POSIX/Make/files index 06df47630f..1f91738212 100644 --- a/src/OSspecific/POSIX/Make/files +++ b/src/OSspecific/POSIX/Make/files @@ -3,14 +3,16 @@ clockValue/clockValue.C cpuInfo/cpuInfo.C cpuTime/cpuTime.C memInfo/memInfo.C + signals/sigFpe.C signals/sigSegv.C signals/sigInt.C signals/sigQuit.C signals/sigStopAtWriteNow.C signals/sigWriteNow.C +signals/timer.C + regExpPosix.C -timer.C fileStat.C POSIX.C diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.H b/src/OSspecific/POSIX/cpuTime/cpuTime.H index 3643d5332e..d4be85e7f0 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.H +++ b/src/OSspecific/POSIX/cpuTime/cpuTime.H @@ -41,7 +41,7 @@ SourceFiles #define cpuTime_H #include -#include +#include #include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OSspecific/POSIX/fileMonitor.C b/src/OSspecific/POSIX/fileMonitor.C index c750e16339..f8f9a46af7 100644 --- a/src/OSspecific/POSIX/fileMonitor.C +++ b/src/OSspecific/POSIX/fileMonitor.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation diff --git a/src/OSspecific/POSIX/fileMonitor.H b/src/OSspecific/POSIX/fileMonitor.H index be7f8f25ea..9ccde7f074 100644 --- a/src/OSspecific/POSIX/fileMonitor.H +++ b/src/OSspecific/POSIX/fileMonitor.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -54,6 +54,7 @@ SourceFiles namespace Foam { +// Forward Declarations class fileMonitor; class fileMonitorWatcher; @@ -63,7 +64,6 @@ class fileMonitorWatcher; class fileMonitor { - public: // Public data types @@ -79,6 +79,7 @@ public: static const Enum fileStateNames_; private: + // Private data //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above) diff --git a/src/OSspecific/POSIX/fileStat.C b/src/OSspecific/POSIX/fileStat.C index 23f97cbe9c..df5c8ae95f 100644 --- a/src/OSspecific/POSIX/fileStat.C +++ b/src/OSspecific/POSIX/fileStat.C @@ -29,7 +29,6 @@ License #include "IOstreams.H" #include "timer.H" -#include #include #ifndef darwin #include diff --git a/src/OSspecific/POSIX/signals/sigFpe.C b/src/OSspecific/POSIX/signals/sigFpe.C index cf8b5b161e..abc3ae4f50 100644 --- a/src/OSspecific/POSIX/signals/sigFpe.C +++ b/src/OSspecific/POSIX/signals/sigFpe.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -33,6 +33,8 @@ License #include "Switch.H" #include "UList.H" +#include + #if defined(__linux__) && defined(__GNUC__) #ifndef __USE_GNU #define __USE_GNU // To use feenableexcept() @@ -47,7 +49,9 @@ License #include "feexceptErsatz.H" #endif -#include +// File-local functions +#include "signalMacros.C" + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -57,8 +61,6 @@ bool Foam::sigFpe::switchNan_(Foam::debug::optimisationSwitch("setNaN", 0)); bool Foam::sigFpe::sigActive_ = false; bool Foam::sigFpe::nanActive_ = false; -struct sigaction Foam::sigFpe::oldAction_; - // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // @@ -123,17 +125,11 @@ void Foam::sigFpe::sigHandler(int) { #if (defined(__linux__) && defined(__GNUC__)) || defined(darwin) - // Reset old handling - if (sigaction(SIGFPE, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGFPE trapping" - << abort(FatalError); - } + resetHandler("SIGFPE", SIGFPE); jobInfo.signalEnd(); // Update jobInfo file error::printStack(Perr); - raise(SIGFPE); // Throw signal (to old handler) + ::raise(SIGFPE); // Throw signal (to old handler) #endif // (__linux__ && __GNUC__) || darwin } @@ -203,17 +199,7 @@ void Foam::sigFpe::set(bool verbose) | FE_OVERFLOW ); - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - - if (sigaction(SIGFPE, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot set SIGFPE trapping" - << abort(FatalError); - } + setHandler("SIGFPE", SIGFPE, sigHandler); sigActive_ = true; @@ -293,12 +279,7 @@ void Foam::sigFpe::unset(bool verbose) << endl; } - if (sigaction(SIGFPE, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGFPE trapping" - << abort(FatalError); - } + resetHandler("SIGFPE", SIGFPE); // Reset exception raising const int oldExcept = fedisableexcept diff --git a/src/OSspecific/POSIX/signals/sigFpe.H b/src/OSspecific/POSIX/signals/sigFpe.H index a98541d720..38dd45bf35 100644 --- a/src/OSspecific/POSIX/signals/sigFpe.H +++ b/src/OSspecific/POSIX/signals/sigFpe.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -56,7 +56,6 @@ SourceFiles #ifndef sigFpe_H #define sigFpe_H -#include #include "scalar.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -64,7 +63,7 @@ SourceFiles namespace Foam { -// Forward declarations +// Forward Declarations template class UList; /*---------------------------------------------------------------------------*\ @@ -73,7 +72,7 @@ template class UList; class sigFpe { - // Private data + // Private Data //- Flag that floating point trapping should be used. // Can override with FOAM_SIGFPE env variable @@ -83,20 +82,16 @@ class sigFpe // Can override with FOAM_SETNAN env variable static bool switchNan_; - //- Flag to indicate floating point trapping is currently active + //- Floating point trapping currently active? static bool sigActive_; //- Flag to indicate mallocNan is currently active static bool nanActive_; - //- Saved old signal trapping setting - static struct sigaction oldAction_; - // Private Member Functions - //- Handler for caught signals. - // Ends job and prints stack + //- Handler for caught signals - ends job and prints stack static void sigHandler(int); diff --git a/src/OSspecific/POSIX/signals/sigInt.C b/src/OSspecific/POSIX/signals/sigInt.C index f0a997a010..c66a91408b 100644 --- a/src/OSspecific/POSIX/signals/sigInt.C +++ b/src/OSspecific/POSIX/signals/sigInt.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -30,9 +30,11 @@ License #include "JobInfo.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// File-local functions +#include "signalMacros.C" -struct sigaction Foam::sigInt::oldAction_; + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // bool Foam::sigInt::sigActive_ = false; @@ -41,16 +43,10 @@ bool Foam::sigInt::sigActive_ = false; void Foam::sigInt::sigHandler(int) { - // Reset old handling - if (sigaction(SIGINT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGINT trapping" - << abort(FatalError); - } + resetHandler("SIGINT", SIGINT); jobInfo.signalEnd(); // Update jobInfo file - raise(SIGINT); // Throw signal (to old handler) + ::raise(SIGINT); // Throw signal (to old handler) } @@ -74,35 +70,25 @@ Foam::sigInt::~sigInt() void Foam::sigInt::set(bool) { - if (!sigActive_) + if (sigActive_) { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(SIGINT, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot call sigInt::set() more than once" - << abort(FatalError); - } - sigActive_ = true; + return; } + sigActive_ = true; + + setHandler("SIGINT", SIGINT, sigHandler); } void Foam::sigInt::unset(bool) { - if (sigActive_) + if (!sigActive_) { - if (sigaction(SIGINT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot set SIGINT trapping" - << abort(FatalError); - } - sigActive_ = false; + return; } + sigActive_ = false; + + resetHandler("SIGINT", SIGINT); } diff --git a/src/OSspecific/POSIX/signals/sigInt.H b/src/OSspecific/POSIX/signals/sigInt.H index b62f52754d..a834362980 100644 --- a/src/OSspecific/POSIX/signals/sigInt.H +++ b/src/OSspecific/POSIX/signals/sigInt.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -47,8 +47,6 @@ SourceFiles #ifndef sigInt_H #define sigInt_H -#include - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -62,10 +60,7 @@ class sigInt { // Private data - //- Saved old signal trapping setting - static struct sigaction oldAction_; - - //- Flag to indicate signal trapping is enabled + //- Signal trapping enabled? static bool sigActive_; diff --git a/src/OSspecific/POSIX/signals/sigQuit.C b/src/OSspecific/POSIX/signals/sigQuit.C index 0e18a1c3ec..ba4b0c6064 100644 --- a/src/OSspecific/POSIX/signals/sigQuit.C +++ b/src/OSspecific/POSIX/signals/sigQuit.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -30,9 +30,11 @@ License #include "JobInfo.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// File-local functions +#include "signalMacros.C" -struct sigaction Foam::sigQuit::oldAction_; + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // bool Foam::sigQuit::sigActive_ = false; @@ -41,17 +43,11 @@ bool Foam::sigQuit::sigActive_ = false; void Foam::sigQuit::sigHandler(int) { - // Reset old handling - if (sigaction(SIGQUIT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGQUIT trapping" - << abort(FatalError); - } + resetHandler("SIGQUIT", SIGQUIT); jobInfo.signalEnd(); // Update jobInfo file error::printStack(Perr); - raise(SIGQUIT); // Throw signal (to old handler) + ::raise(SIGQUIT); // Throw signal (to old handler) } @@ -75,35 +71,25 @@ Foam::sigQuit::~sigQuit() void Foam::sigQuit::set(bool) { - if (!sigActive_) + if (sigActive_) { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(SIGQUIT, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot call more than once" - << abort(FatalError); - } - sigActive_ = true; + return; } + sigActive_ = true; + + setHandler("SIGQUIT", SIGQUIT, sigHandler); } void Foam::sigQuit::unset(bool) { - if (sigActive_) + if (!sigActive_) { - if (sigaction(SIGQUIT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot unset SIGQUIT trapping" - << abort(FatalError); - } - sigActive_ = false; + return; } + sigActive_ = false; + + resetHandler("SIGQUIT", SIGQUIT); } diff --git a/src/OSspecific/POSIX/signals/sigQuit.H b/src/OSspecific/POSIX/signals/sigQuit.H index 1847867891..a12940c500 100644 --- a/src/OSspecific/POSIX/signals/sigQuit.H +++ b/src/OSspecific/POSIX/signals/sigQuit.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -46,8 +46,6 @@ SourceFiles #ifndef sigQuit_H #define sigQuit_H -#include - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -59,12 +57,9 @@ namespace Foam class sigQuit { - // Private data + // Private Data - //- Saved old signal trapping setting - static struct sigaction oldAction_; - - //- Flag to indicate signal trapping is enabled + //- Signal trapping enabled? static bool sigActive_; @@ -86,7 +81,7 @@ public: ~sigQuit(); - // Member functions + // Member Functions //- Activate SIGQUIT signal handler static void set(bool verbose=false); diff --git a/src/OSspecific/POSIX/signals/sigSegv.C b/src/OSspecific/POSIX/signals/sigSegv.C index 06ca476bc6..d3e5593df0 100644 --- a/src/OSspecific/POSIX/signals/sigSegv.C +++ b/src/OSspecific/POSIX/signals/sigSegv.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -30,9 +30,11 @@ License #include "JobInfo.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// File-local functions +#include "signalMacros.C" -struct sigaction Foam::sigSegv::oldAction_; + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // bool Foam::sigSegv::sigActive_ = false; @@ -41,17 +43,11 @@ bool Foam::sigSegv::sigActive_ = false; void Foam::sigSegv::sigHandler(int) { - // Reset old handling - if (sigaction(SIGSEGV, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGSEGV trapping" - << abort(FatalError); - } + resetHandler("SIGSEGV", SIGSEGV); jobInfo.signalEnd(); // Update jobInfo file error::printStack(Perr); - raise(SIGSEGV); // Throw signal (to old handler) + ::raise(SIGSEGV); // Throw signal (to old handler) } @@ -75,35 +71,25 @@ Foam::sigSegv::~sigSegv() void Foam::sigSegv::set(bool) { - if (!sigActive_) + if (sigActive_) { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(SIGSEGV, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot call more than once" - << abort(FatalError); - } - sigActive_ = true; + return; } + sigActive_ = true; + + setHandler("SIGSEGV", SIGSEGV, sigHandler); } void Foam::sigSegv::unset(bool) { - if (sigActive_) + if (!sigActive_) { - if (sigaction(SIGSEGV, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot unset SIGSEGV trapping" - << abort(FatalError); - } - sigActive_ = false; + return; } + sigActive_ = false; + + resetHandler("SIGSEGV", SIGSEGV); } diff --git a/src/OSspecific/POSIX/signals/sigSegv.H b/src/OSspecific/POSIX/signals/sigSegv.H index a5f0ae7e85..bbed12192d 100644 --- a/src/OSspecific/POSIX/signals/sigSegv.H +++ b/src/OSspecific/POSIX/signals/sigSegv.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -46,8 +46,6 @@ SourceFiles #ifndef sigSegv_H #define sigSegv_H -#include - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -59,12 +57,9 @@ namespace Foam class sigSegv { - // Private data + // Private Data - //- Saved old signal trapping setting - static struct sigaction oldAction_; - - //- Flag to indicate signal trapping is enabled + //- Signal trapping enabled? static bool sigActive_; diff --git a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C index b11ba2cf45..6d55136a2d 100644 --- a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C +++ b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -25,12 +25,17 @@ License \*---------------------------------------------------------------------------*/ +#include "sigWriteNow.H" #include "sigStopAtWriteNow.H" #include "error.H" #include "JobInfo.H" #include "IOstreams.H" #include "Time.H" +// File-local functions +#include "signalMacros.C" + + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // Signal number to catch @@ -39,21 +44,19 @@ int Foam::sigStopAtWriteNow::signal_ Foam::debug::optimisationSwitch("stopAtWriteNowSignal", -1) ); -Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr; +// Pointer to Time (file-local variable) +static Foam::Time const* runTimePtr_ = nullptr; -struct sigaction Foam::sigStopAtWriteNow::oldAction_; +// * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * // namespace Foam { // Register re-reader -class addstopAtWriteNowSignalToOpt +struct addstopAtWriteNowSignalToOpt : public ::Foam::simpleRegIOobject { - -public: - addstopAtWriteNowSignalToOpt(const char* name) : ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name) @@ -85,15 +88,9 @@ addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_ void Foam::sigStopAtWriteNow::sigHandler(int) { - // Reset old handling - if (sigaction(signal_, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset " << signal_ << " trapping" - << abort(FatalError); - } + resetHandler("stopAtWriteNow", signal_); - jobInfo.signalEnd(); // Update jobInfo file + jobInfo.signalEnd(); // Update jobInfo file if (runTimePtr_) { @@ -113,7 +110,7 @@ Foam::sigStopAtWriteNow::sigStopAtWriteNow() Foam::sigStopAtWriteNow::sigStopAtWriteNow(const Time& runTime, bool verbose) { - runTimePtr_ = &runTime; // Store runTime + runTimePtr_ = &runTime; // Store runTime set(verbose); } @@ -122,61 +119,55 @@ Foam::sigStopAtWriteNow::sigStopAtWriteNow(const Time& runTime, bool verbose) Foam::sigStopAtWriteNow::~sigStopAtWriteNow() { - // Reset old handling - if (signal_ > 0) + if (!active()) { - if (sigaction(signal_, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset " << signal_ << " trapping" - << abort(FatalError); - } + return; } + + resetHandler("stopAtWriteNow", signal_); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::sigStopAtWriteNow::set(bool verbose) -{ - if (signal_ > 0) - { - // Check that the signal is different from the writeNowSignal - if (sigWriteNow::signal_ == signal_) - { - FatalErrorInFunction - << "stopAtWriteNowSignal : " << signal_ - << " cannot be the same as the writeNowSignal." - << " Please change this in the etc/controlDict." - << exit(FatalError); - } - - - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(signal_, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot set " << signal_ << " trapping" - << abort(FatalError); - } - - if (verbose) - { - Info<< "sigStopAtWriteNow :" - << " Enabling writing and stopping upon signal " << signal_ - << endl; - } - } -} - - -bool Foam::sigStopAtWriteNow::active() const +bool Foam::sigStopAtWriteNow::active() { return signal_ > 0; } +int Foam::sigStopAtWriteNow::signalNumber() +{ + return signal_; +} + + +void Foam::sigStopAtWriteNow::set(bool verbose) +{ + if (!active()) + { + return; + } + + // Check that the signal is different from the writeNowSignal + if (sigWriteNow::signalNumber() == signal_) + { + FatalErrorInFunction + << "stopAtWriteNowSignal : " << signal_ + << " cannot be the same as the writeNowSignal." + << " Please change this in the etc/controlDict." + << exit(FatalError); + } + + if (verbose) + { + Info<< "sigStopAtWriteNow :" + << " Enabling writing and stopping upon signal " << signal_ + << endl; + } + + setHandler("stopAtWriteNow", signal_, sigHandler); +} + + // ************************************************************************* // diff --git a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H index fb08a219fd..0464a6d52a 100644 --- a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H +++ b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -27,10 +27,8 @@ Class Foam::sigStopAtWriteNow Description - Signal handler for interupt defined by - OptimisationSwitches::stopAtWriteNowSignal - - Write and stop the job. + Signal handler to write and stop the job. + The interrupt is defined by OptimisationSwitches::stopAtWriteNowSignal SourceFiles sigStopAtWriteNow.C @@ -40,32 +38,25 @@ SourceFiles #ifndef sigStopAtWriteNow_H #define sigStopAtWriteNow_H -#include - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +// Forward Declarations class Time; /*---------------------------------------------------------------------------*\ - Class sigStopAtWriteNow Declaration + Class sigStopAtWriteNow Declaration \*---------------------------------------------------------------------------*/ class sigStopAtWriteNow { - // Private data + // Private Data - //- Number of signal to use + //- Signal number to use static int signal_; - //- Pointer to Time - static Time const* runTimePtr_; - - //- Saved old signal trapping setting - static struct sigaction oldAction_; - // Private Member Functions @@ -92,13 +83,16 @@ public: ~sigStopAtWriteNow(); - // Member functions - - //- (re)set signal catcher - static void set(bool verbose=false); + // Member Functions //- Is active? - bool active() const; + static bool active(); + + //- Signal number being used + static int signalNumber(); + + //- Set/reset signal handler + static void set(bool verbose=false); }; diff --git a/src/OSspecific/POSIX/signals/sigWriteNow.C b/src/OSspecific/POSIX/signals/sigWriteNow.C index b47fb07c80..71d309adc5 100644 --- a/src/OSspecific/POSIX/signals/sigWriteNow.C +++ b/src/OSspecific/POSIX/signals/sigWriteNow.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -31,6 +31,10 @@ License #include "IOstreams.H" #include "Time.H" +// File-local functions +#include "signalMacros.C" + + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // Signal number to catch @@ -39,22 +43,20 @@ int Foam::sigWriteNow::signal_ Foam::debug::optimisationSwitch("writeNowSignal", -1) ); -Foam::Time* Foam::sigWriteNow::runTimePtr_ = nullptr; +// Pointer to Time (file-local variable) +static Foam::Time* runTimePtr_ = nullptr; -struct sigaction Foam::sigWriteNow::oldAction_; +// * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * // namespace Foam { // Register re-reader -class addwriteNowSignalToOpt +struct addwriteNowSignalToOpt : public ::Foam::simpleRegIOobject { - -public: - addwriteNowSignalToOpt(const char* name) : ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name) @@ -100,7 +102,7 @@ Foam::sigWriteNow::sigWriteNow() Foam::sigWriteNow::sigWriteNow(Time& runTime, bool verbose) { - runTimePtr_ = &runTime; // Store runTime + runTimePtr_ = &runTime; // Store runTime set(verbose); } @@ -109,50 +111,44 @@ Foam::sigWriteNow::sigWriteNow(Time& runTime, bool verbose) Foam::sigWriteNow::~sigWriteNow() { - // Reset old handling - if (signal_ > 0) + if (!active()) { - if (sigaction(signal_, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset " << signal_ << " trapping" - << abort(FatalError); - } + return; } + + resetHandler("writeNow", signal_); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::sigWriteNow::set(bool verbose) -{ - if (signal_ >= 0) - { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(signal_, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot set " << signal_ << " trapping" - << abort(FatalError); - } - - if (verbose) - { - Info<< "sigWriteNow :" - << " Enabling writing upon signal " << signal_ - << endl; - } - } -} - - -bool Foam::sigWriteNow::active() const +bool Foam::sigWriteNow::active() { return signal_ > 0; } +int Foam::sigWriteNow::signalNumber() +{ + return signal_; +} + + +void Foam::sigWriteNow::set(bool verbose) +{ + if (!active()) + { + return; + } + + if (verbose) + { + Info<< "sigWriteNow :" + << " Enabling writing upon signal " << signal_ << nl; + } + + setHandler("writeNow", signal_, sigHandler); +} + + // ************************************************************************* // diff --git a/src/OSspecific/POSIX/signals/sigWriteNow.H b/src/OSspecific/POSIX/signals/sigWriteNow.H index eec6bc5cc7..8ba2cedc89 100644 --- a/src/OSspecific/POSIX/signals/sigWriteNow.H +++ b/src/OSspecific/POSIX/signals/sigWriteNow.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -27,9 +27,8 @@ Class Foam::sigWriteNow Description - Signal handler for interupt defined by OptimisationSwitches::writeNowSignal - - Write once and continue. + Signal handler to write once and continue. + The interrupt is defined by OptimisationSwitches::writeNowSignal SourceFiles sigWriteNow.C @@ -39,32 +38,25 @@ SourceFiles #ifndef sigWriteNow_H #define sigWriteNow_H -#include - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +// Forward Declarations class Time; /*---------------------------------------------------------------------------*\ - Class sigWriteNow Declaration + Class sigWriteNow Declaration \*---------------------------------------------------------------------------*/ class sigWriteNow { - // Private data + // Private Data - //- Number of signal to use + //- Signal number to use static int signal_; - //- Pointer to Time - static Time* runTimePtr_; - - //- Saved old signal trapping setting - static struct sigaction oldAction_; - // Private Member Functions @@ -74,8 +66,6 @@ class sigWriteNow public: - friend class sigStopAtWriteNow; - //- Allow setter access to signal_ friend class addwriteNowSignalToOpt; @@ -96,9 +86,12 @@ public: // Member Functions //- Is active? - bool active() const; + static bool active(); - //- (re)set signal catcher + //- The signal number being used + static int signalNumber(); + + //- Set/reset signal handler static void set(bool verbose=false); }; diff --git a/src/OSspecific/POSIX/signals/signalMacros.C b/src/OSspecific/POSIX/signals/signalMacros.C new file mode 100644 index 0000000000..f1982073ba --- /dev/null +++ b/src/OSspecific/POSIX/signals/signalMacros.C @@ -0,0 +1,74 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + File-local code for setting/resetting signal handlers. + +SourceFiles + signalMacros.C + +\*---------------------------------------------------------------------------*/ + +#include "error.H" +#include + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Saved old signal trapping setting (file-local variable) +static struct sigaction oldAction_; + + +static void resetHandler(const char *what, int sigNum) +{ + if (sigaction(sigNum, &oldAction_, nullptr) < 0) + { + FatalError + << "Cannot unset " << what << " signal (" << sigNum + << ") trapping" << endl + << abort(FatalError); + } +} + + +static void setHandler(const char *what, int sigNum, void (*handler)(int)) +{ + struct sigaction newAction; + newAction.sa_handler = handler; + newAction.sa_flags = SA_NODEFER; + sigemptyset(&newAction.sa_mask); + if (sigaction(sigNum, &newAction, &oldAction_) < 0) + { + FatalError + << "Could not set " << what << " signal (" << sigNum + << ") trapping" << endl + << abort(FatalError); + } +} + +} // End namespace Foam + + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/signals/timer.C b/src/OSspecific/POSIX/signals/timer.C new file mode 100644 index 0000000000..182eaa2077 --- /dev/null +++ b/src/OSspecific/POSIX/signals/timer.C @@ -0,0 +1,114 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2004-2010, 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- + | Copyright (C) 2011-2016 OpenFOAM Foundation +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "error.H" +#include "timer.H" + +#include + +// File-local functions +#include "signalMacros.C" + + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineTypeNameAndDebug(timer, 0); +} + +jmp_buf Foam::timer::envAlarm; + +unsigned int Foam::timer::oldTimeOut_ = 0; + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +void Foam::timer::sigHandler(int) +{ + DebugInFunction<< "Timed out. Jumping." << endl; + + longjmp(envAlarm, 1); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::timer::timer(unsigned int seconds) +: + timeOut_(seconds) +{ + if (!timeOut_) + { + return; + } + + // Singleton since handler is static function + if (oldTimeOut_) + { + FatalErrorInFunction + << "timer already used." + << abort(FatalError); + } + + // Set alarm signal handler + // - do not block any signals while in it + // - clear list of signals to mask + + setHandler("SIGALRM", SIGALRM, sigHandler); + + // Set alarm timer + oldTimeOut_ = ::alarm(timeOut_); + + DebugInFunction + << "Installing timeout " << int(timeOut_) << " seconds" + << " (overriding old timeout " << int(oldTimeOut_) << ")." << endl; +} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::timer::~timer() +{ + if (!timeOut_) + { + return; + } + + DebugInFunction + << "timeOut=" << int(timeOut_) + << " : resetting timeOut to " << int(oldTimeOut_) << endl; + + // Reset alarm timer + ::alarm(oldTimeOut_); + oldTimeOut_ = 0; + + resetHandler("SIGALRM", SIGALRM); +} + + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/timer.H b/src/OSspecific/POSIX/signals/timer.H similarity index 88% rename from src/OSspecific/POSIX/timer.H rename to src/OSspecific/POSIX/signals/timer.H index 50ea74d8ac..4af28d6007 100644 --- a/src/OSspecific/POSIX/timer.H +++ b/src/OSspecific/POSIX/signals/timer.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -46,14 +46,15 @@ Description Constructor set signal handler on sigalarm and alarm(). Destructor clears these. - timedOut is macro because setjmp can't be in member function of timer. - ?something to do with stack frames. - Warning The setjmp restores complete register state so including local vars held in regs. So if in blocking part something gets calced in a stack based variable make sure it is declared 'volatile'. +Note + timedOut is macro because setjmp can't be in member function of timer. + ?something to do with stack frames. + SourceFiles timer.C @@ -63,16 +64,14 @@ SourceFiles #define timer_H #include "className.H" - -#include -#include +#include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Check it a timeout has occured +//- Check if timeout has occurred // keep setjmp in same stack frame so no function calls #define timedOut(x) \ - (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false) + ((x).timeOut_ ? setjmp(Foam::timer::envAlarm) : false) namespace Foam { @@ -83,10 +82,7 @@ namespace Foam class timer { - // Private data - - //- Old signal masks - static struct sigaction oldAction_; + // Private Data //- Old alarm() value static unsigned int oldTimeOut_; @@ -95,18 +91,18 @@ class timer // Private Member Functions //- Alarm handler - static void signalHandler(int); + static void sigHandler(int); public: - // Public data + // Public Data //- Declare name of the class and its debug switch ClassName("timer"); - //- Current time out value. Needed by macro timedOut - unsigned int newTimeOut_; + //- The time-out value (seconds). Needed by macro timedOut + unsigned int timeOut_; //- State for setjmp. Needed by macro timedOut static jmp_buf envAlarm; @@ -118,7 +114,7 @@ public: explicit timer(unsigned int seconds); - //- Destructor + //- Destructor. Restores the alarm and signal handler as required. ~timer(); }; diff --git a/src/OSspecific/POSIX/timer.C b/src/OSspecific/POSIX/timer.C deleted file mode 100644 index e43047abde..0000000000 --- a/src/OSspecific/POSIX/timer.C +++ /dev/null @@ -1,133 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | - \\/ M anipulation | -------------------------------------------------------------------------------- - | Copyright (C) 2011-2016 OpenFOAM Foundation -------------------------------------------------------------------------------- -License - This file is part of OpenFOAM. - - OpenFOAM is free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - OpenFOAM is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - for more details. - - You should have received a copy of the GNU General Public License - along with OpenFOAM. If not, see . - -\*---------------------------------------------------------------------------*/ - -#include - -#include "error.H" -#include "timer.H" - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - -namespace Foam -{ -defineTypeNameAndDebug(timer, 0); - -jmp_buf timer::envAlarm; - -struct sigaction timer::oldAction_; - -unsigned int timer::oldTimeOut_ = 0; -} - - -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -void Foam::timer::signalHandler(int) -{ - if (debug) - { - InfoInFunction<< "Timed out. Jumping." - << endl; - } - longjmp(envAlarm, 1); -} - -// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // - -Foam::timer::timer(const unsigned int newTimeOut) -: - newTimeOut_(newTimeOut) -{ - - if (newTimeOut > 0) - { - // Is singleton since handler is static function - if (oldTimeOut_ != 0) - { - FatalErrorInFunction - << "timer already used." - << abort(FatalError); - } - - // Install alarm signal handler: - // - do not block any signals while in it - // - clear list of signals to mask - struct sigaction newAction; - newAction.sa_handler = timer::signalHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - - if (sigaction(SIGALRM, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "sigaction(SIGALRM) error" - << abort(FatalError); - } - - oldTimeOut_ = ::alarm(newTimeOut); - - if (debug) - { - InfoInFunction - << "Installing timeout " << int(newTimeOut_) - << " seconds" - << " (overriding old timeout " << int(oldTimeOut_) - << ")." << endl; - } - } -} - - -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -Foam::timer::~timer() -{ - if (newTimeOut_ > 0) - { - if (debug) - { - InfoInFunction - << "timeOut=" << int(newTimeOut_) - << " : resetting timeOut to " << int(oldTimeOut_) << endl; - } - - // Reset timer - ::alarm(oldTimeOut_); - oldTimeOut_ = 0; - - // Restore signal handler - if (sigaction(SIGALRM, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "sigaction(SIGALRM) error" - << abort(FatalError); - } - } -} - - -// ************************************************************************* // From af0dc60fa15687346b7dc9ee84928ecebf5acfdf Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 2 Apr 2019 16:56:00 +0200 Subject: [PATCH 15/19] STYLE: reduce indentation in mkDir, minor logic simplication --- src/OSspecific/POSIX/POSIX.C | 245 ++++++++++++++++------------------- 1 file changed, 114 insertions(+), 131 deletions(-) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index e09b2055a5..652aa9e212 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -414,135 +414,122 @@ bool Foam::mkDir(const fileName& pathName, mode_t mode) // Directory made OK so return true return true; } - else + + switch (errno) { - switch (errno) + case EPERM: { - case EPERM: - { - FatalErrorInFunction - << "The filesystem containing " << pathName - << " does not support the creation of directories." - << exit(FatalError); + FatalErrorInFunction + << "The filesystem containing " << pathName + << " does not support the creation of directories." + << exit(FatalError); + break; + } - return false; + case EEXIST: + { + // Directory already exists so simply return true + return true; + } + + case EFAULT: + { + FatalErrorInFunction + << "" << pathName + << " points outside your accessible address space." + << exit(FatalError); + break; + } + + case EACCES: + { + FatalErrorInFunction + << "The parent directory does not allow write " + "permission to the process,"<< nl + << " or one of the directories in " << pathName + << " did not allow search (execute) permission." + << exit(FatalError); + break; + } + + case ENAMETOOLONG: + { + FatalErrorInFunction + << "" << pathName << " is too long." + << exit(FatalError); + break; + } + + case ENOENT: + { + // Part of the path does not exist so try to create it + if (pathName.path().size() && mkDir(pathName.path(), mode)) + { + return mkDir(pathName, mode); } - case EEXIST: - { - // Directory already exists so simply return true - return true; - } + FatalErrorInFunction + << "Couldn't create directory " << pathName + << exit(FatalError); + break; + } - case EFAULT: - { - FatalErrorInFunction - << "" << pathName - << " points outside your accessible address space." - << exit(FatalError); + case ENOTDIR: + { + FatalErrorInFunction + << "A component used as a directory in " << pathName + << " is not, in fact, a directory." + << exit(FatalError); + break; + } - return false; - } + case ENOMEM: + { + FatalErrorInFunction + << "Insufficient kernel memory was available to make directory " + << pathName << '.' + << exit(FatalError); + break; + } - case EACCES: - { - FatalErrorInFunction - << "The parent directory does not allow write " - "permission to the process,"<< nl - << "or one of the directories in " << pathName - << " did not allow search (execute) permission." - << exit(FatalError); + case EROFS: + { + FatalErrorInFunction + << "" << pathName + << " refers to a file on a read-only filesystem." + << exit(FatalError); + break; + } - return false; - } + case ELOOP: + { + FatalErrorInFunction + << "Too many symbolic links were encountered in resolving " + << pathName << '.' + << exit(FatalError); + break; + } - case ENAMETOOLONG: - { - FatalErrorInFunction - << "" << pathName << " is too long." - << exit(FatalError); + case ENOSPC: + { + FatalErrorInFunction + << "The device containing " << pathName + << " has no room for the new directory or " + << "the user's disk quota is exhausted." + << exit(FatalError); + break; + } - return false; - } - - case ENOENT: - { - // Part of the path does not exist so try to create it - if (pathName.path().size() && mkDir(pathName.path(), mode)) - { - return mkDir(pathName, mode); - } - else - { - FatalErrorInFunction - << "Couldn't create directory " << pathName - << exit(FatalError); - - return false; - } - } - - case ENOTDIR: - { - FatalErrorInFunction - << "A component used as a directory in " << pathName - << " is not, in fact, a directory." - << exit(FatalError); - - return false; - } - - case ENOMEM: - { - FatalErrorInFunction - << "Insufficient kernel memory was available to make " - "directory " << pathName << '.' - << exit(FatalError); - - return false; - } - - case EROFS: - { - FatalErrorInFunction - << "" << pathName - << " refers to a file on a read-only filesystem." - << exit(FatalError); - - return false; - } - - case ELOOP: - { - FatalErrorInFunction - << "Too many symbolic links were encountered in resolving " - << pathName << '.' - << exit(FatalError); - - return false; - } - - case ENOSPC: - { - FatalErrorInFunction - << "The device containing " << pathName - << " has no room for the new directory or " - << "the user's disk quota is exhausted." - << exit(FatalError); - - return false; - } - - default: - { - FatalErrorInFunction - << "Couldn't create directory " << pathName - << exit(FatalError); - - return false; - } + default: + { + FatalErrorInFunction + << "Couldn't create directory " << pathName + << exit(FatalError); + break; } } + + return false; } @@ -1100,12 +1087,10 @@ bool Foam::mv(const fileName& src, const fileName& dst, const bool followLink) { const fileName dstName(dst/src.name()); - return ::rename(src.c_str(), dstName.c_str()) == 0; - } - else - { - return ::rename(src.c_str(), dst.c_str()) == 0; + return (0 == ::rename(src.c_str(), dstName.c_str())); } + + return (0 == ::rename(src.c_str(), dst.c_str())); } @@ -1146,7 +1131,7 @@ bool Foam::mvBak(const fileName& src, const std::string& ext) // possible index where we have no choice if (!exists(dstName, false) || n == maxIndex) { - return ::rename(src.c_str(), dstName.c_str()) == 0; + return (0 == ::rename(src.c_str(), dstName.c_str())); } } } @@ -1174,15 +1159,13 @@ bool Foam::rm(const fileName& file) return false; } - // Try returning plain file name; if not there, try with .gz - if (::remove(file.c_str()) == 0) - { - return true; - } - else - { - return ::remove(string(file + ".gz").c_str()) == 0; - } + // If removal of plain file name fails, try with .gz + + return + ( + 0 == ::remove(file.c_str()) + || 0 == ::remove((file + ".gz").c_str()) + ); } From ae94509f421d8f26a8d5a9c4585353b89a792557 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 3 Apr 2019 17:34:56 +0200 Subject: [PATCH 16/19] ENH: wrap internal POSIX directory reading as an iterator class --- applications/test/readDir/Make/files | 3 + applications/test/readDir/Make/options | 2 + applications/test/readDir/Test-readDir.C | 74 +++++++++ src/OSspecific/POSIX/POSIX.C | 196 ++++++++++++++++++----- 4 files changed, 234 insertions(+), 41 deletions(-) create mode 100644 applications/test/readDir/Make/files create mode 100644 applications/test/readDir/Make/options create mode 100644 applications/test/readDir/Test-readDir.C diff --git a/applications/test/readDir/Make/files b/applications/test/readDir/Make/files new file mode 100644 index 0000000000..4ef8e0d2b1 --- /dev/null +++ b/applications/test/readDir/Make/files @@ -0,0 +1,3 @@ +Test-readDir.C + +EXE = $(FOAM_USER_APPBIN)/Test-readDir diff --git a/applications/test/readDir/Make/options b/applications/test/readDir/Make/options new file mode 100644 index 0000000000..4e772fdf9d --- /dev/null +++ b/applications/test/readDir/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/readDir/Test-readDir.C b/applications/test/readDir/Test-readDir.C new file mode 100644 index 0000000000..17827e8ae7 --- /dev/null +++ b/applications/test/readDir/Test-readDir.C @@ -0,0 +1,74 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Test functionality of Foam::readDir + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "OSspecific.H" +#include "fileNameList.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::addBoolOption("dir", "list directories instead of files"); + + #include "setRootCase.H" + + fileName::Type listType = fileName::FILE; + + if (args.found("dir")) + { + Info<< "Listing directories" << nl; + listType = fileName::DIRECTORY; + } + else + { + Info<< "Listing files" << nl; + } + + + { + Info<< nl; + for (const word& item : readDir(".", listType)) + { + Info<< " " << item << nl; + } + } + + + Info<< "\nEnd\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 652aa9e212..57ffb64476 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -104,6 +104,141 @@ static inline void redirects(const bool bg) } +// * * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace POSIX +{ + +//- A simple directory contents iterator +class directoryIterator +{ + DIR* dirptr_; + + bool exists_; + + bool hidden_; + + std::string item_; + + //- Accept file/dir name + inline bool accept() const + { + return + ( + item_.size() && item_ != "." && item_ != ".." + && (hidden_ || item_[0] != '.') + ); + } + + +public: + + // Constructors + + //- Construct for dirName, optionally allowing hidden files/dirs + directoryIterator(const fileName& dirName, bool allowHidden = false) + : + dirptr_(nullptr), + exists_(false), + hidden_(allowHidden), + item_() + { + if (!dirName.empty()) + { + dirptr_ = ::opendir(dirName.c_str()); + exists_ = (dirptr_ != nullptr); + next(); // Move to first element + } + } + + + //- Destructor + ~directoryIterator() + { + close(); + } + + + // Member Functions + + //- Directory open succeeded + bool exists() const + { + return exists_; + } + + //- Directory pointer is valid + bool good() const + { + return dirptr_; + } + + //- Close directory + void close() + { + if (dirptr_) + { + ::closedir(dirptr_); + dirptr_ = nullptr; + } + } + + //- The current item + const std::string& val() const + { + return item_; + } + + //- Read next item, always ignoring "." and ".." entries. + // Normally also ignore hidden files/dirs (beginning with '.') + // Automatically close when there are no more items + bool next() + { + struct dirent *list; + + while (dirptr_ && (list = ::readdir(dirptr_)) != nullptr) + { + item_ = list->d_name; + + if (accept()) + { + return true; + } + } + close(); // No more items + + return false; + } + + + // Member Operators + + //- Same as good() + operator bool() const + { + return good(); + } + + //- Same as val() + const std::string& operator*() const + { + return val(); + } + + //- Same as next() + directoryIterator& operator++() + { + next(); + return *this; + } +}; + +} // End namespace POSIX +} // End namespace Foam + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // pid_t Foam::pid() @@ -743,7 +878,7 @@ Foam::fileNameList Foam::readDir ) { // Initial filename list size and the increment when resizing the list - static const int maxNnames = 100; + constexpr int maxNnames = 100; // Basic sanity: cannot strip '.gz' from directory names const bool stripgz = filtergz && (type != fileName::DIRECTORY); @@ -751,14 +886,10 @@ Foam::fileNameList Foam::readDir fileNameList dirEntries; - // Open directory and set the structure pointer - // Do not attempt to open an empty directory name - DIR *source; - if - ( - directory.empty() - || (source = ::opendir(directory.c_str())) == nullptr - ) + // Iterate contents (ignores an empty directory name) + + POSIX::directoryIterator dirIter(directory); + if (!dirIter.exists()) { if (POSIX::debug) { @@ -781,19 +912,12 @@ Foam::fileNameList Foam::readDir label nFailed = 0; // Entries with invalid characters label nEntries = 0; // Number of selected entries - dirEntries.setSize(maxNnames); + dirEntries.resize(maxNnames); - // Read and parse all the entries in the directory - for (struct dirent *list; (list = ::readdir(source)) != nullptr; /*nil*/) + // Process the directory entries + for (/*nil*/; dirIter; ++dirIter) { - const std::string item(list->d_name); - - // Ignore files/directories beginning with "." - // These are the ".", ".." directories and any hidden files/dirs - if (item.empty() || item[0] == '.') - { - continue; - } + const std::string& item = *dirIter; // Validate filename without spaces, quotes, etc in the name. // No duplicate slashes to strip - dirent will not have them anyhow. @@ -813,7 +937,7 @@ Foam::fileNameList Foam::readDir { if (nEntries >= dirEntries.size()) { - dirEntries.setSize(dirEntries.size() + maxNnames); + dirEntries.resize(dirEntries.size() + maxNnames); } if (stripgz && name.hasExt(extgz)) @@ -827,10 +951,9 @@ Foam::fileNameList Foam::readDir } } } - ::closedir(source); // Finalize the length of the entries list - dirEntries.setSize(nEntries); + dirEntries.resize(nEntries); if (nFailed && POSIX::debug) { @@ -1171,14 +1294,11 @@ bool Foam::rm(const fileName& file) bool Foam::rmDir(const fileName& directory, const bool silent) { - // Open directory and set the structure pointer - // Do not attempt to open an empty directory name - DIR *source; - if - ( - directory.empty() - || (source = ::opendir(directory.c_str())) == nullptr - ) + // Iterate contents (ignores an empty directory name) + // Also retain hidden files/dirs for removal + + POSIX::directoryIterator dirIter(directory, true); + if (!dirIter.exists()) { if (!silent) { @@ -1201,18 +1321,13 @@ bool Foam::rmDir(const fileName& directory, const bool silent) // Process each directory entry, counting any errors encountered label nErrors = 0; - for (struct dirent *list; (list = ::readdir(source)) != nullptr; /*nil*/) - { - const std::string item(list->d_name); - // Ignore "." and ".." directories - if (item.empty() || item == "." || item == "..") - { - continue; - } + for (/*nil*/; dirIter; ++dirIter) + { + const std::string& item = *dirIter; // Allow invalid characters (spaces, quotes, etc), - // otherwise we cannot subdirs with these types of names. + // otherwise we cannot remove subdirs with these types of names. // -> const fileName path = directory/name; <- const fileName path(fileName::concat(directory, item)); @@ -1256,7 +1371,6 @@ bool Foam::rmDir(const fileName& directory, const bool silent) } // clean up - ::closedir(source); return !nErrors; } From 693afb540820aadab8ff56d7aaf38691a2d79fe5 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 5 Apr 2019 12:03:41 +0200 Subject: [PATCH 17/19] BUG: surfaceWriter::expire() should not affect the expected nFields() count - the writer nFields() is a separate type of accounting (currently only needed by VTK legacy) and is independent of the geometry. --- src/surfMesh/writers/surfaceWriter.C | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/surfMesh/writers/surfaceWriter.C b/src/surfMesh/writers/surfaceWriter.C index d910897c9d..2198ff1386 100644 --- a/src/surfMesh/writers/surfaceWriter.C +++ b/src/surfMesh/writers/surfaceWriter.C @@ -384,9 +384,11 @@ bool Foam::surfaceWriter::expire() upToDate_ = false; wroteGeom_ = false; - nFields_ = 0; merged_.clear(); + // Field count (nFields_) is a different type of accounting + // and is unaffected by geometry changes + return changed; } From cd91a252ac54d54206d42b4b500fc3e80824a6cf Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 5 Apr 2019 13:10:53 +0200 Subject: [PATCH 18/19] ENH: improve handling of pt-scotch headers/libraries - provide dedicated detection 'have_ptscotch' function that can be used after the 'have_scotch' function. It sets the PTSCOTCH_ARCH_PATH, PTSCOTCH_INC_DIR, PTSCOTCH_LIB_DIR and helps when the serial and parallel versions are located with different logic. --- src/parallel/decompose/Allwmake | 3 +- .../decompose/ptscotchDecomp/Make/options | 10 +- wmake/scripts/have_scotch | 100 +++++++++++++++++- wmake/scripts/sysFunctions | 12 ++- 4 files changed, 114 insertions(+), 11 deletions(-) diff --git a/src/parallel/decompose/Allwmake b/src/parallel/decompose/Allwmake index b745c6a503..ea5369a94c 100755 --- a/src/parallel/decompose/Allwmake +++ b/src/parallel/decompose/Allwmake @@ -41,7 +41,8 @@ wmakeLnInclude -u decompositionMethods if have_scotch then wmake $targetType scotchDecomp - if [ -d "$FOAM_LIBBIN/$FOAM_MPI" ] + + if have_ptscotch then wmakeMpiLib "$SCOTCH_VERSION" ptscotchDecomp fi diff --git a/src/parallel/decompose/ptscotchDecomp/Make/options b/src/parallel/decompose/ptscotchDecomp/Make/options index 8cd4aa7b58..42e41f8146 100644 --- a/src/parallel/decompose/ptscotchDecomp/Make/options +++ b/src/parallel/decompose/ptscotchDecomp/Make/options @@ -1,13 +1,12 @@ /* - * NB: mplib PINC must appear after the SCOTCH_ARCH_PATH/include/FOAM_MPI - * to ensure we do not accidentally get a ptscotch header from the - * mpi distribution. + * NB: mplib PINC must appear after PTSCOTCH_INC_DIR to ensure we + * do not accidentally get a ptscotch header from the MPI distribution. */ sinclude $(GENERAL_RULES)/mplib$(WM_MPLIB) sinclude $(DEFAULT_RULES)/mplib$(WM_MPLIB) EXE_INC = \ - -I$(SCOTCH_ARCH_PATH)/include/$(FOAM_MPI) \ + -I$(PTSCOTCH_INC_DIR) \ -I$(SCOTCH_INC_DIR) \ $(PFLAGS) $(PINC) \ -I../decompositionMethods/lnInclude @@ -17,9 +16,8 @@ EXE_INC = \ * ptscotch 6 requires scotch linked in, but does not declare the dependency */ LIB_LIBS = \ + -L$(PTSCOTCH_LIB_DIR) \ -L$(SCOTCH_LIB_DIR) \ - -L$(FOAM_EXT_LIBBIN)/$(FOAM_MPI) \ - -L$(FOAM_EXT_LIBBIN) \ -lptscotch -lptscotcherrexit \ -lscotch diff --git a/wmake/scripts/have_scotch b/wmake/scripts/have_scotch index e25525926a..8d17fa7f17 100644 --- a/wmake/scripts/have_scotch +++ b/wmake/scripts/have_scotch @@ -37,6 +37,7 @@ no_scotch() { unset HAVE_SCOTCH SCOTCH_ARCH_PATH SCOTCH_INC_DIR SCOTCH_LIB_DIR unset SCOTCH_VERSION + unset HAVE_PTSCOTCH PTSCOTCH_ARCH_PATH PTSCOTCH_INC_DIR PTSCOTCH_LIB_DIR return 0 } @@ -48,6 +49,11 @@ echo_scotch() echo "root=$SCOTCH_ARCH_PATH" echo "include=$SCOTCH_INC_DIR" echo "library=$SCOTCH_LIB_DIR" + echo + echo "ptscotch=${HAVE_PTSCOTCH:-false}" + echo "root=$PTSCOTCH_ARCH_PATH" + echo "include=$PTSCOTCH_INC_DIR" + echo "library=$PTSCOTCH_LIB_DIR" } @@ -177,13 +183,105 @@ have_scotch() } +# Must be called after have_scotch! +# +# On success, return 0 and export variables +# -> HAVE_PTSCOTCH, PTSCOTCH_ARCH_PATH, PTSCOTCH_INC_DIR, PTSCOTCH_LIB_DIR +have_ptscotch() +{ + local prefix header library static settings warn + warn="==> skip ptscotch" + + if [ "$HAVE_SCOTCH" != true ] + then + echo "$warn (no serial scotch available?)" + return 1 + fi + + # Reuse old settings + [ -n "$PTSCOTCH_ARCH_PATH" ] || PTSCOTCH_ARCH_PATH="$SCOTCH_ARCH_PATH" + + # Location + prefix="$PTSCOTCH_ARCH_PATH" + + # Header/library names + header="ptscotch.h" + library="libptscotch$extLibso" + static="libptscotch$extLiba" + + # ---------------------------------- + if isNone "$prefix" + then + [ -n "$warn" ] && echo "$warn (disabled)" + return 1 + elif hasAbsdir "$prefix" + then + header=$(findFirstFile \ + "$prefix/include/$FOAM_MPI/$header" \ + "$prefix/include/$header" + ) + + library=$(findFirstFile \ + "$(thirdExtLib $FOAM_MPI/$library)" \ + "$(thirdExtLib $library)" \ + "$prefix/lib/$static" \ + "$prefix/lib/$library" \ + "$prefix/lib$WM_COMPILER_LIB_ARCH/$static" \ + "$prefix/lib$WM_COMPILER_LIB_ARCH/$library" \ + ) + + elif isSystem "$prefix" + then + prefix=/usr + + header=$(findFirstFile \ + "/usr/local/include/ptscotch/$header" \ + "/usr/local/include/scotch/$header" \ + "/usr/local/include/$header" \ + "/usr/include/ptscotch/$header" \ + "/usr/include/scotch/$header" \ + "/usr/include/$header" \ + ) + + case "$header" in (/usr/local/*) prefix=/usr/local ;; esac + + library=$(findFirstFile \ + "$prefix/lib/$library" \ + "$prefix/lib$WM_COMPILER_LIB_ARCH/$library" \ + ) + else + unset prefix header library + fi + # ---------------------------------- + + # Header found? + [ -n "$header" ] || { + [ -n "$warn" ] && echo "$warn (no header)" + return 2 + } + + # Library found? + [ -n "$library" ] || { + [ -n "$warn" ] && echo "$warn (no library)" + return 2 + } + + # OK + echo "ptscotch - $prefix" + export HAVE_PTSCOTCH=true + export PTSCOTCH_ARCH_PATH="$prefix" + export PTSCOTCH_INC_DIR="${header%/*}" # Basename + export PTSCOTCH_LIB_DIR="${library%/*}" # Basename +} + + # Force reset of old variables no_scotch # Testing if [ "$1" = "-test" ] then - have_scotch + have_scotch && have_ptscotch echo_scotch fi diff --git a/wmake/scripts/sysFunctions b/wmake/scripts/sysFunctions index 30de032b90..a11f58c3c4 100644 --- a/wmake/scripts/sysFunctions +++ b/wmake/scripts/sysFunctions @@ -48,14 +48,20 @@ then esac - # True if OS is Darwin. - # Uses libso extension to cache the value - # (instead of calling 'uname -s' each time) + # True if target OS is Darwin. + # Uses cached value from libso extension isDarwin() { test "$extLibso" = ".dylib" } + # True if target OS is Windows + # Uses cached value from libso extension + isWindows() + { + test "$extLibso" = ".dll" + } + # True if '$1' begins with '/' isAbsdir() From 63a18262114d7ae6376f7f3c8bdf4b5d1f1f9c84 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 5 Apr 2019 15:30:30 +0200 Subject: [PATCH 19/19] ENH: provide 'sanitized' values for wmake -show compiler information (#1256) - with the wmake rules we may have some compiler options bound to the internal compiler variable. For example, CC = g++ -std=c++11 -m64 c++FLAGS = ... So shift any flags from CC to CXXFLAGS for the output of 'wmake -show-cxx', 'wmake -show-cxxflags', etc. This makes it much easier to handle the values correctly elsewhere. Eg, CXX="$(wmake -show-cxx)" CXXFLAGS="$(wmake -show-cxxflags)" \ ./configure --- wmake/makefiles/info | 37 +++++++++++++++++++++++++++---------- wmake/wmake | 14 ++++++++------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/wmake/makefiles/info b/wmake/makefiles/info index 600399a173..c43841dcf7 100644 --- a/wmake/makefiles/info +++ b/wmake/makefiles/info @@ -31,6 +31,13 @@ SHELL = /bin/sh .SUFFIXES: +#------------------------------------------------------------------------------ +# Some default values +#------------------------------------------------------------------------------ + +# Shared library extension (with '.' separator) +EXT_SO = .so + #------------------------------------------------------------------------------ # Compilation rules #------------------------------------------------------------------------------ @@ -38,34 +45,44 @@ SHELL = /bin/sh GENERAL_RULES = $(WM_DIR)/rules/General include $(GENERAL_RULES)/general +# Commands +COMPILE_C := $(strip $(cc) $(cFLAGS)) +COMPILE_CXX := $(strip $(CC) $(c++FLAGS)) #------------------------------------------------------------------------------ # Display information #------------------------------------------------------------------------------ -.PHONY: compile -compile: - @echo "$(strip $(CC) $(c++FLAGS))" - .PHONY: api api: - @echo "$(WM_VERSION)" | sed -e 's/^.*=//' + @echo "$(lastword $(subst =, ,$(WM_VERSION)))" + +.PHONY: ext-so +ext-so: + @echo "$(EXT_SO)" + +.PHONY: compile-c +compile-c: + @echo "$(COMP_C)" .PHONY: c c: - @echo "$(strip $(cc))" + @echo "$(firstword $(cc))" .PHONY: cflags cflags: - @echo "$(strip $(cFLAGS))" + @echo "$(wordlist 2,$(words $(COMPILE_C)), $(COMPILE_C))" + +.PHONY: compile-cxx +compile-cxx: + @echo "$(COMPILE_CXX)" .PHONY: cxx cxx: - @echo "$(strip $(CC))" + @echo "$(firstword $(CC))" .PHONY: cxxflags cxxflags: - @echo "$(strip $(c++FLAGS))" - + @echo "$(wordlist 2,$(words $(COMPILE_CXX)), $(COMPILE_CXX))" #----------------------------- vim: set ft=make: ------------------------------ diff --git a/wmake/wmake b/wmake/wmake index 97f135a737..71ecf5ccb6 100755 --- a/wmake/wmake +++ b/wmake/wmake @@ -77,7 +77,11 @@ options: -pwd Print root directory containing a Make/ directory -update Update lnInclude dirs, dep files, remove deprecated files/dirs -show-api Print api value - -show-compile Print C++ compiler value and flags (shortcut: -show) + -show-ext-so Print shared library extension (with '.' separator) + -show-compile-c Same as '-show-c -show-cflags' + -show-compile-cxx Same as '-show-cxx -show-cxxflags' + -show-c Print C compiler value + -show-cflags Print C compiler flags -show-cxx Print C++ compiler value -show-cxxflags Print C++ compiler flags -h | -help Print the usage @@ -136,11 +140,9 @@ do -s | -silent) export WM_QUIET=true ;; - -show | -show-compile) - $make -f $WM_DIR/makefiles/info compile - optShow=true - ;; - -show-api | -show-cxx | -show-cxxflags | -show-c | -show-cflags) + -show-api | -show-ext-so | \ + -show-compile-c | -show-c | -show-cflags | \ + -show-compile-cxx | -show-cxx | -show-cxxflags) $make -f $WM_DIR/makefiles/info "${1#-show-}" optShow=true ;;