From cf2b305b4f62966bab6fb994b4357f3c2b091c55 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 27 Jan 2025 14:53:36 +0100 Subject: [PATCH] ENH: upgrade to use some C++17 constructs - 'if constexpr (...)' * instead of std::enable_if * terminate template recursion * compile-time elimination of code - use C++14 '_t', '_v' versions, eg, std::is_integral_v instead of std::is_integral::value - std::begin, std::end, std::void_t instead of prev stdFoam versions - provide is_contiguous_v<..> as short form of is_contiguous<..>::value with the additional benefit of removing any cv qualifiers. ENH: include is_rotational_vectorspace trait - tests for vector-space and nComponents > 1 (ie, not sphericalTensor) ENH: improve robustness of pTraits_.. tests by removing cv qualifiers --- .../test/DiagTensor/Test-DiagTensor.C | 101 ++++----- .../test/Dictionary/Test-Dictionary.C | 2 +- applications/test/FixedList/Test-FixedList.C | 7 +- applications/test/Hashing1/HashFunction.H | 115 ++++------- applications/test/Hashing2/Test-Hashing2.C | 18 +- applications/test/IOField/Test-IOField.cxx | 5 +- .../test/IOobject-type/Test-IOobject-type.cxx | 2 +- applications/test/List/Test-List.C | 4 +- .../test/MathFunctions/Test-MathFunctions.C | 21 +- .../SphericalTensor/Test-SphericalTensor.C | 103 ++++------ .../Test-SphericalTensor2D.C | 99 ++++----- .../test/SymmTensor/Test-SymmTensor.C | 107 ++++------ .../test/SymmTensor2D/Test-SymmTensor2D.C | 101 ++++----- applications/test/Tensor/Test-Tensor.C | 101 ++++----- applications/test/Tensor2D/Test-Tensor2D.C | 104 ++++------ applications/test/TestTools/TestTools.H | 183 +++++------------ .../test/boolVector/Test-boolVector.C | 4 +- .../test/contiguous/Test-contiguous.C | 12 +- .../test/exprValue1/Test-exprValue1.cxx | 4 +- .../test/extendedStencil/Make/options | 3 +- .../test/fieldDependency/Make/options | 3 +- .../test/fileOperation1/Test-fileOperation1.C | 2 +- applications/test/invTensor/Test-invTensor.C | 3 +- applications/test/mappedPatch/Make/options | 3 +- .../DiagonalMatrix/Test-DiagonalMatrix.C | 36 ++-- .../matrices/EigenMatrix/Test-EigenMatrix.C | 42 ++-- .../test/matrices/QRMatrix/Test-QRMatrix.C | 31 ++- .../Test-RectangularMatrix.C | 49 +++-- .../matrices/SquareMatrix/Test-SquareMatrix.C | 48 +++-- .../Test-SymmetricSquareMatrix.C | 48 +++-- applications/test/pTraits/Test-pTraits.C | 73 ++++--- .../Test-parallel-broadcast.C | 6 +- .../parallel-chunks/Test-parallel-chunks.C | 50 +---- .../test/parallelOverset/Make/options | 1 + applications/test/refPtr/Test-refPtr.C | 10 +- applications/test/regex1/Test-regex1.C | 6 +- applications/test/sizeof/Test-sizeof.C | 21 ++ applications/test/spline/Make/options | 2 +- applications/test/surfaceReading/Make/options | 1 + applications/test/tmp/Test-tmp.C | 10 +- applications/test/volField/Test-volField.C | 2 +- .../dataConversion/foamToVTK/foamToVTK.C | 4 +- doc/BuildIssues.md | 12 +- .../containers/Bits/PackedList/PackedList.C | 14 +- .../containers/Bits/PackedList/PackedListI.H | 4 +- .../containers/Buffers/CircularBufferIO.C | 10 +- .../containers/Circulators/Circulator.H | 10 +- .../CompactListList/CompactListList.C | 6 +- .../HashTables/HashTable/HashTable.H | 20 +- .../HashTables/HashTable/HashTableDetail.H | 49 ++--- .../IndirectListBase/IndirectListBaseIO.C | 12 +- .../Lists/DynamicList/DynamicListIO.C | 6 +- .../containers/Lists/FixedList/FixedList.C | 6 +- .../containers/Lists/FixedList/FixedList.H | 16 +- .../containers/Lists/FixedList/FixedListIO.C | 21 +- src/OpenFOAM/containers/Lists/List/List.H | 4 +- src/OpenFOAM/containers/Lists/List/ListIO.C | 6 +- src/OpenFOAM/containers/Lists/List/UList.C | 6 +- src/OpenFOAM/containers/Lists/List/UList.H | 22 +- src/OpenFOAM/containers/Lists/List/UListI.H | 4 +- src/OpenFOAM/containers/Lists/List/UListIO.C | 20 +- .../containers/Lists/policy/ListPolicy.H | 4 +- .../containers/PtrLists/UPtrList/UPtrList.H | 10 +- src/OpenFOAM/db/IOobject/IOobjectTemplates.C | 16 +- .../db/IOstreams/IOstreams/IOstream.H | 26 +-- src/OpenFOAM/db/IOstreams/IOstreams/Istream.H | 6 +- .../db/IOstreams/Pstreams/PstreamBroadcast.C | 6 +- .../IOstreams/Pstreams/PstreamCombineGather.C | 10 +- .../db/IOstreams/Pstreams/PstreamExchange.C | 6 +- .../Pstreams/PstreamExchangeConsensus.C | 12 +- .../db/IOstreams/Pstreams/PstreamGather.C | 194 ++++++++---------- .../db/IOstreams/Pstreams/PstreamGatherList.C | 25 +-- .../db/IOstreams/Pstreams/UPstreamTemplates.C | 86 ++++---- src/OpenFOAM/db/IOstreams/token/token.H | 26 +-- src/OpenFOAM/db/IOstreams/token/tokenI.H | 10 +- src/OpenFOAM/db/dictionary/dictionary.H | 18 +- .../objectRegistry/objectRegistryTemplates.C | 12 +- .../exprDriver/exprDriverTemplates.C | 8 +- .../expressions/exprResult/exprResultI.H | 6 +- src/OpenFOAM/fields/Fields/Field/Field.C | 4 +- .../global/constants/unitConversion.H | 10 +- src/OpenFOAM/include/openfoam_mpi.H | 46 ++--- src/OpenFOAM/include/stdFoam.H | 186 +++-------------- .../tableReaders/csv/csvTableReader.C | 4 +- .../matrices/EigenMatrix/EigenMatrix.H | 2 +- .../LUscalarMatrix/LUscalarMatrixTemplates.C | 10 +- src/OpenFOAM/matrices/Matrix/Matrix.C | 6 +- src/OpenFOAM/matrices/Matrix/Matrix.H | 2 +- src/OpenFOAM/matrices/Matrix/MatrixI.H | 4 +- src/OpenFOAM/matrices/Matrix/MatrixIO.C | 12 +- .../processorLduInterfaceTemplates.C | 110 +++++----- .../PrecisionAdaptor/PrecisionAdaptor.H | 18 +- .../mapDistributeBaseTemplates.C | 17 +- .../meshes/polyMesh/syncTools/syncTools.H | 4 +- .../polyMesh/syncTools/syncToolsTemplates.C | 4 +- .../globalIndex/globalIndexTemplates.C | 32 +-- .../primitives/VectorSpace/VectorSpaceOps.H | 51 +++-- src/OpenFOAM/primitives/complex/complex.H | 16 +- src/OpenFOAM/primitives/enums/Enum.H | 2 +- .../primitives/functions/Function1/CSV/CSV.C | 2 +- src/OpenFOAM/primitives/ints/int/int.H | 4 +- src/OpenFOAM/primitives/ints/uint/uint.H | 4 +- src/OpenFOAM/primitives/ops/ops.H | 22 +- .../primitives/ranges/IntRange/IntRange.H | 2 +- .../primitives/ranges/MinMax/MinMaxOps.H | 18 +- src/OpenFOAM/primitives/traits/contiguous.H | 11 +- src/OpenFOAM/primitives/traits/pTraits.H | 114 ++++++---- .../primitives/transform/symmTransform.H | 4 +- src/OpenFOAM/primitives/transform/transform.H | 6 +- .../ensight/output/ensightOutputTemplates.C | 20 +- src/fileFormats/gltf/foamGltfBase.H | 17 +- .../vtk/file/foamVtkFileWriterTemplates.C | 10 +- src/fileFormats/vtk/format/foamVtkFormatter.H | 5 +- .../vtk/format/foamVtkFormatterI.H | 13 +- .../vtk/output/foamVtkOutputTemplates.C | 14 +- .../processor/processorFaPatchField.C | 4 +- .../calculatedProcessorFvPatchField.C | 4 +- .../processor/processorFvPatchField.C | 12 +- .../IntegralScaleBox/IntegralScaleBox.H | 8 +- .../turbulentDigitalFilterInletFvPatchField.H | 8 +- .../field/DMD/DMDModels/derived/STDMD/STDMD.H | 29 +-- .../DMDModels/derived/STDMD/STDMDTemplates.C | 55 ++--- src/functionObjects/field/multiply/multiply.H | 24 +-- .../field/multiply/multiplyTemplates.C | 66 +++--- .../ensightCloud/ensightCloudWriteObject.cxx | 42 ++-- .../ensightCloudWriteObjectImpl.cxx | 8 +- .../lagrangian/vtkCloud/vtkCloudTemplates.C | 10 +- .../conversion/ensight/ensightOutputCloud.C | 4 +- .../meshRefinement/meshRefinementRefine.C | 4 +- .../cyclicAMIPolyPatchTemplates.C | 47 ++++- .../writers/common/coordSetWriterTemplates.C | 40 ++-- src/surfMesh/writers/common/surfaceWriter.C | 29 ++- .../nastran/nastranSurfaceWriterImpl.C | 14 +- 133 files changed, 1548 insertions(+), 1942 deletions(-) diff --git a/applications/test/DiagTensor/Test-DiagTensor.C b/applications/test/DiagTensor/Test-DiagTensor.C index fa5a652364..7db83d7e3e 100644 --- a/applications/test/DiagTensor/Test-DiagTensor.C +++ b/applications/test/DiagTensor/Test-DiagTensor.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2020-2022 OpenCFD Ltd. + Copyright (C) 2020-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -38,12 +38,11 @@ Description \*---------------------------------------------------------------------------*/ +#include "complex.H" #include "Tensor.H" #include "SymmTensor.H" #include "SphericalTensor.H" #include "DiagTensor.H" -#include "scalar.H" -#include "complex.H" using namespace Foam; @@ -57,45 +56,11 @@ unsigned nTest_ = 0; unsigned nFail_ = 0; -// Compare two floating point types, and print output. -// Do ++nFail_ if values of two objects are not equal within a given tolerance. -// The function is converted from PEP-485. -template -typename std::enable_if::rank == 0, void>::type -cmp -( - const word& msg, - const Type& x, - const Type& y, - const scalar relTol = 1e-8, // -typename std::enable_if::rank != 0, void>::type -cmp +void cmp ( const word& msg, const Type& x, @@ -104,18 +69,37 @@ cmp const scalar absTol = 0 ) { - Info<< msg << x << endl; + const auto notEqual = [=](const auto& a, const auto& b) -> bool + { + return + ( + Foam::max(absTol, relTol*Foam::max(Foam::mag(a), Foam::mag(b))) + < Foam::mag(a - b) + ); + }; unsigned nFail = 0; - for (direction i = 0; i < pTraits::nComponents; ++i) + if constexpr (is_vectorspace_v) { - if (max(absTol, relTol*max(mag(x[i]), mag(y[i]))) < mag(x[i] - y[i])) + for (direction i = 0; i < pTraits::nComponents; ++i) + { + if (notEqual(x[i], y[i])) + { + ++nFail; + } + } + } + else + { + if (notEqual(x, y)) { ++nFail; } } + Info<< msg << x << endl; + if (nFail) { Info<< nl @@ -368,27 +352,26 @@ void test_global_opers(Type) // Do compile-time recursion over the given types template -inline typename std::enable_if::type -run_tests(const std::tuple& types, const List& typeID){} - - -template -inline typename std::enable_if::type -run_tests(const std::tuple& types, const List& typeID) +void run_tests(const std::tuple& types, const List& names) { - Info<< nl << " ## Test constructors: "<< typeID[I] <<" ##" << nl; - test_constructors(std::get(types)); + if constexpr (I < sizeof...(Tp)) + { + const auto& name = names[I]; - Info<< nl << " ## Test member functions: "<< typeID[I] <<" ##" << nl; - test_member_funcs(std::get(types)); + Info<< nl << " ## Test constructors: " << name << " ##" << nl; + test_constructors(std::get(types)); - Info<< nl << " ## Test global functions: "<< typeID[I] << " ##" << nl; - test_global_funcs(std::get(types)); + Info<< nl << " ## Test member functions: " << name << " ##" << nl; + test_member_funcs(std::get(types)); - Info<< nl << " ## Test global operators: "<< typeID[I] <<" ##" << nl; - test_global_opers(std::get(types)); + Info<< nl << " ## Test global functions: " << name << " ##" << nl; + test_global_funcs(std::get(types)); - run_tests(types, typeID); + Info<< nl << " ## Test global operators: " << name << " ##" << nl; + test_global_opers(std::get(types)); + + run_tests(types, names); + } } @@ -403,8 +386,8 @@ int main() const List typeID ({ - "DiagTensor", - "DiagTensor", + "DiagTensor", + "DiagTensor", "DiagTensor" }); diff --git a/applications/test/Dictionary/Test-Dictionary.C b/applications/test/Dictionary/Test-Dictionary.C index f1e85eecce..fae3a14337 100644 --- a/applications/test/Dictionary/Test-Dictionary.C +++ b/applications/test/Dictionary/Test-Dictionary.C @@ -185,7 +185,7 @@ int main(int argc, char *argv[]) } std::cout<< "iter type: " - << typeid(stdFoam::begin(scalarDict2)).name() << '\n'; + << typeid(std::begin(scalarDict2)).name() << '\n'; scalarDict.transfer(scalarDict2); diff --git a/applications/test/FixedList/Test-FixedList.C b/applications/test/FixedList/Test-FixedList.C index ae4bbc995c..e77190a793 100644 --- a/applications/test/FixedList/Test-FixedList.C +++ b/applications/test/FixedList/Test-FixedList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2019-2023 OpenCFD Ltd. + Copyright (C) 2019-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -85,8 +85,7 @@ void compileInfo() template -typename std::enable_if -<(FixedListType::max_size() == 2), bool>::type +std::enable_if_t<(FixedListType::max_size() == 2), bool> is_pair() { return true; @@ -94,7 +93,7 @@ is_pair() template -typename std::enable_if<(FixedListType::max_size() != 2), std::string>::type +std::enable_if_t<(FixedListType::max_size() != 2), std::string> is_pair() { return "not really at all"; diff --git a/applications/test/Hashing1/HashFunction.H b/applications/test/Hashing1/HashFunction.H index 964b5b2e29..3b7544a9b8 100644 --- a/applications/test/Hashing1/HashFunction.H +++ b/applications/test/Hashing1/HashFunction.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021 OpenCFD Ltd. + Copyright (C) 2021-2025 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -42,9 +42,11 @@ Note #include "Hash.H" #ifdef FULLDEBUG -#define HashTypeInfo(Args) void info() { std::cerr<< "" Args << "\n"; } +#define HashTypeInfo(Name) \ + static constexpr const char* name() noexcept { return Name; } \ + void info() const { std::cerr<< name() << " hashing\n"; } #else -#define HashTypeInfo(Args) void info() {} +#define HashTypeInfo(Name) void info() const {} #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -53,20 +55,36 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class Hash Declaration + Class HashFunc Declaration \*---------------------------------------------------------------------------*/ -template +template struct HashFun { - #ifdef FULLDEBUG - static constexpr const char* name() noexcept { return "default"; } - #endif - HashTypeInfo("plain hash") + void info() const + { + #ifdef FULLDEBUG + if constexpr (std::is_base_of_v) + { + std::cerr<< "std::string hashing\n"; + } + else + { + std::cerr<< "default hashing\n"; + } + #endif + } unsigned operator()(const T& obj, unsigned seed=0) const { - return Foam::Hasher(&obj, sizeof(obj), seed); + if constexpr (std::is_base_of_v) + { + return Foam::Hasher(obj.data(), obj.size(), seed); + } + else + { + return Foam::Hasher(&obj, sizeof(obj), seed); + } } }; @@ -76,45 +94,17 @@ struct HashFun //- Hashing for label template<> struct HashFun : Hash