From bbec683e42879134ddb5653762d0f4c0c9c83f5b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 27 Sep 2016 12:40:17 +0200 Subject: [PATCH 1/7] ENH: provide list of enums for NamedEnum - can be used to loop over all enumerations in the order of definition. --- applications/test/NamedEnum/Make/files | 2 +- .../{Test-namedEnum.C => Test-NamedEnum.C} | 58 +++++++++++++++---- src/OpenFOAM/containers/NamedEnum/NamedEnum.C | 19 ++++++ src/OpenFOAM/containers/NamedEnum/NamedEnum.H | 10 +++- 4 files changed, 76 insertions(+), 13 deletions(-) rename applications/test/NamedEnum/{Test-namedEnum.C => Test-NamedEnum.C} (52%) diff --git a/applications/test/NamedEnum/Make/files b/applications/test/NamedEnum/Make/files index 755b72ba5e..a1a7086041 100644 --- a/applications/test/NamedEnum/Make/files +++ b/applications/test/NamedEnum/Make/files @@ -1,3 +1,3 @@ -Test-namedEnum.C +Test-NamedEnum.C EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum diff --git a/applications/test/NamedEnum/Test-namedEnum.C b/applications/test/NamedEnum/Test-NamedEnum.C similarity index 52% rename from applications/test/NamedEnum/Test-namedEnum.C rename to applications/test/NamedEnum/Test-NamedEnum.C index 22f3596932..63386ed79e 100644 --- a/applications/test/NamedEnum/Test-namedEnum.C +++ b/applications/test/NamedEnum/Test-NamedEnum.C @@ -34,26 +34,28 @@ class namedEnumTest { public: - enum options + enum option { a, b, - c + c, + d }; - static const Foam::NamedEnum namedEnum; + static const Foam::NamedEnum namedEnum; }; template<> -const char* Foam::NamedEnum::names[] = +const char* Foam::NamedEnum::names[] = { "a", "b", - "c" + "c", + "d" }; -const Foam::NamedEnum namedEnumTest::namedEnum; +const Foam::NamedEnum namedEnumTest::namedEnum; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -61,11 +63,47 @@ const Foam::NamedEnum namedEnumTest::namedEnum; int main(int argc, char *argv[]) { - Info<< namedEnumTest::namedEnum["a"] << endl; - Info<< namedEnumTest::namedEnum[namedEnumTest::a] << endl; + const List options + = namedEnumTest::namedEnum.enums(); - namedEnumTest::options hmm(namedEnumTest::namedEnum.read(Sin)); - Info<< namedEnumTest::namedEnum[hmm] << endl; + Info<< "enums: " << options << nl; + + Info<< "loop over enums (as list):" << nl; + forAll(options, i) + { + const namedEnumTest::option& opt = options[i]; + + Info<< "option[" << opt + << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; + } + +#if __cplusplus > 201100L + // C++11 + Info<< "loop over enums (C++11 for range):" << nl; + for (auto const& opt : options) + { + Info<< "option[" << opt + << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; + } +#else + Info<< "loop over enums (via iterator):" << nl; + forAllConstIter(List, options, iter) + { + const namedEnumTest::option& opt = *iter; + + Info<< "option[" << opt + << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; + } +#endif + + Info<< nl + << namedEnumTest::namedEnum["a"] << nl + << namedEnumTest::namedEnum[namedEnumTest::a] << nl; + + Info<< "--- test read construction ---" << endl; + + namedEnumTest::option dummy(namedEnumTest::namedEnum.read(Sin)); + Info<< namedEnumTest::namedEnum[dummy] << endl; Info<< "End\n" << endl; diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C index ba5bd4999d..99f723d987 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C @@ -120,4 +120,23 @@ Foam::wordList Foam::NamedEnum::words() } +template +Foam::List Foam::NamedEnum::enums() +{ + List lst(nEnum); + + label nElem = 0; + for (int enumI = 0; enumI < nEnum; ++enumI) + { + if (names[enumI] && names[enumI][0]) + { + lst[nElem++] = Enum(enumI); + } + } + + lst.setSize(nElem); + return lst; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H index f4af06792c..6005f86a60 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H @@ -45,6 +45,9 @@ SourceFiles namespace Foam { +// Forward declaration +template class NamedEnum; + /*---------------------------------------------------------------------------*\ Class NamedEnum Declaration \*---------------------------------------------------------------------------*/ @@ -60,10 +63,10 @@ class NamedEnum // Private Member Functions //- Disallow default bitwise copy construct - NamedEnum(const NamedEnum&); + NamedEnum(const NamedEnum&) = delete; //- Disallow default bitwise assignment - void operator=(const NamedEnum&); + void operator=(const NamedEnum&) = delete; public: @@ -95,6 +98,9 @@ public: //- The set of names as a list of words static wordList words(); + //- List of enumerations + static List enums(); + // Member Operators From 6d7ff59fc87933c1be10b36c9732a16c02e22fd2 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 28 Sep 2016 11:26:42 +0200 Subject: [PATCH 2/7] ENH: provide refOrNull method for autoPtr. - Normally use '()' to deference. This has extra safety and issues a fatal error if the underlying pointer is not valid. However, in some cases we are happy with getting a null reference. The refOrNull() method returns the reference without any checking. Usage example: autoPtr osPtr; if (Pstream::master()) { osPtr.reset(new OFstream(...)); } writeViaMaster(osPtr.refOrNull()); - The writeViaMaster() call takes an OFstream reference, but this is only used directly on the master. The slaves will pass things through to the master. --- src/OpenFOAM/memory/autoPtr/autoPtr.H | 11 ++++++++++- src/OpenFOAM/memory/autoPtr/autoPtrI.H | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/OpenFOAM/memory/autoPtr/autoPtr.H b/src/OpenFOAM/memory/autoPtr/autoPtr.H index 748975b5cf..a8802f23a9 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtr.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtr.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -104,6 +104,15 @@ public: inline void clear(); + // Access + + //- Return reference, without checking pointer validity. + inline T& refOrNull(); + + //- Return const reference, without checking pointer validity. + inline const T& refOrNull() const; + + // Member operators //- Return reference to the object data diff --git a/src/OpenFOAM/memory/autoPtr/autoPtrI.H b/src/OpenFOAM/memory/autoPtr/autoPtrI.H index ef199ca20c..7440bdce69 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtrI.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtrI.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -129,6 +129,20 @@ inline void Foam::autoPtr::clear() } +template +inline T& Foam::autoPtr::refOrNull() +{ + return *ptr_; +} + + +template +inline const T& Foam::autoPtr::refOrNull() const +{ + return *ptr_; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template From 1c2aadb8d825c3e55cf35f66c6c3a91e46499f25 Mon Sep 17 00:00:00 2001 From: mattijs Date: Wed, 28 Sep 2016 12:26:23 +0100 Subject: [PATCH 3/7] ENH: distributedTriSurfaceMesh: bail out if getting stuck due to precision errors --- .../distributedTriSurfaceMesh.C | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C index 9db0d72d4a..de5557bcb5 100644 --- a/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C +++ b/src/parallel/distributed/distributedTriSurfaceMesh/distributedTriSurfaceMesh.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -1766,6 +1766,8 @@ void Foam::distributedTriSurfaceMesh::findLineAll e1.setSize(compactI); pointMap.setSize(compactI); + + label iter = 0; while (returnReduce(e0.size(), sumOp