From bf3b4fabb48752d6c37f735dd058f6a4fce793d7 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 11 Sep 2020 13:18:43 +0200 Subject: [PATCH] ENH: UniformList to wrap a single value into a list-like container - refactor UniformField accordingly --- .../test/UniformField/Test-UniformField.C | 68 +++++++++- .../Lists/UniformList/UniformList.H | 116 ++++++++++++++++++ .../UniformField.H | 24 ++-- .../UniformFieldI.H | 51 ++------ 4 files changed, 201 insertions(+), 58 deletions(-) create mode 100644 src/OpenFOAM/containers/Lists/UniformList/UniformList.H rename src/OpenFOAM/fields/Fields/{uniformField => UniformField}/UniformField.H (87%) rename src/OpenFOAM/fields/Fields/{uniformField => UniformField}/UniformFieldI.H (69%) diff --git a/applications/test/UniformField/Test-UniformField.C b/applications/test/UniformField/Test-UniformField.C index 88053afdba..60f846a106 100644 --- a/applications/test/UniformField/Test-UniformField.C +++ b/applications/test/UniformField/Test-UniformField.C @@ -1,15 +1,73 @@ -#include "UniformField.H" +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM, distributed under GPL-3.0-or-later. + +Application + Test-UniformField + +Description + Test uniform list/field +\*---------------------------------------------------------------------------*/ + +#include "argList.H" #include "vector.H" #include "IOstreams.H" +#include "UniformField.H" using namespace Foam; -int main() +template +void printInfo(const UniformList& list, const label i=0) { - UniformField uf1(13.1); - UniformField uf2(vector(1, 2, 3)); + Info<< nl + << "value: " << list.value() << nl + << "cast: " << static_cast(list) << nl + << "[" << i << "] = " << list[i] << nl; +} - Info<< "uf1 = " << uf1[22] << "; uf2 = " << uf2[1002] << endl; +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + { + UniformField fld(3.14159); + + printInfo(fld, -100); + + // Change value + fld.value() *= 0.5; + + Info<< nl << "/= 2 " << nl; + + printInfo(fld, -100); + } + + { + UniformField fld(vector(1, 2, -1)); + + printInfo(fld); + + // Change value + fld.value() *= 0.5; + + Info<< nl << "/= 2 " << nl; + + printInfo(fld); + } + + + Info<< "\nEnd\n" << endl; return 0; } + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/UniformList/UniformList.H b/src/OpenFOAM/containers/Lists/UniformList/UniformList.H new file mode 100644 index 0000000000..7dbafb85b4 --- /dev/null +++ b/src/OpenFOAM/containers/Lists/UniformList/UniformList.H @@ -0,0 +1,116 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +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 . + +Class + Foam::UniformList + +Description + A single value that is represented as a list with an + operator[] to access the value. + This can be useful for templated operations expecting a list accessor. + +Note + The list currently has no sizing associated with it. + +\*---------------------------------------------------------------------------*/ + +#ifndef UniformList_H +#define UniformList_H + +#include "labelFwd.H" +#include + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class UniformList Declaration +\*---------------------------------------------------------------------------*/ + +template +class UniformList +{ + // Private Data + + //- The value to be returned. + T value_; + +public: + + // Constructors + + //- Construct from given value + explicit UniformList(const T& val) noexcept + : + value_(val) + {} + + //- Move construct from given value + explicit UniformList(T&& val) noexcept + : + value_(std::move(val)) + {} + + + // Member Functions + + //- Return the value + const T& value() const noexcept + { + return value_; + } + + //- Non-const access to the value + T& value() noexcept + { + return value_; + } + + + // Member Operators + + //- Implicit cast to the value + operator const T&() const noexcept + { + return value_; + } + + //- Return the value + const T& operator[](const label) const noexcept + { + return value_; + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/fields/Fields/uniformField/UniformField.H b/src/OpenFOAM/fields/Fields/UniformField/UniformField.H similarity index 87% rename from src/OpenFOAM/fields/Fields/uniformField/UniformField.H rename to src/OpenFOAM/fields/Fields/UniformField/UniformField.H index 4974b20e88..7707408b42 100644 --- a/src/OpenFOAM/fields/Fields/uniformField/UniformField.H +++ b/src/OpenFOAM/fields/Fields/UniformField/UniformField.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2013 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,6 +37,7 @@ Description #define UniformField_H #include "label.H" +#include "UniformList.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -48,27 +50,23 @@ namespace Foam template class UniformField +: + public UniformList { - // Private data - - Type value_; - public: // Constructors - //- Construct given value - inline UniformField(const Type& value); + //- Inherit constructors from UniformList + using UniformList::UniformList; - // Member Operators + // Member Functions - - inline operator Type() const; - - inline Type operator[](const label) const; - - inline UniformField field() const; + UniformField field() const + { + return UniformField(UniformList::value()); + } }; diff --git a/src/OpenFOAM/fields/Fields/uniformField/UniformFieldI.H b/src/OpenFOAM/fields/Fields/UniformField/UniformFieldI.H similarity index 69% rename from src/OpenFOAM/fields/Fields/uniformField/UniformFieldI.H rename to src/OpenFOAM/fields/Fields/UniformField/UniformFieldI.H index 18707e0bb4..af940a0c0a 100644 --- a/src/OpenFOAM/fields/Fields/uniformField/UniformFieldI.H +++ b/src/OpenFOAM/fields/Fields/UniformField/UniformFieldI.H @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2013 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,36 +28,6 @@ License #include "UniformField.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -template -inline Foam::UniformField::UniformField(const Type& value) -: - value_(value) -{} - - -template -inline Foam::UniformField::operator Type() const -{ - return value_; -} - - -template -inline Type Foam::UniformField::operator[](const label) const -{ - return value_; -} - - -template -inline Foam::UniformField Foam::UniformField::field() const -{ - return UniformField(value_); -} - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -67,50 +38,50 @@ namespace Foam template inline UniformField min ( - const UniformField& u1, - const UniformField& u2 + const UniformField& a, + const UniformField& b ) { - return UniformField(min(u1.operator Type(), u2.operator Type())); + return UniformField(min(a.value(), b.value())); } template inline OtherType min(const UniformField& u, const OtherType& o) { - return min(u.operator Type(), o); + return min(u.value(), o); } template inline OtherType min(const OtherType& o, const UniformField& u) { - return min(o, u.operator Type()); + return min(o, u.value()); } template inline UniformField max ( - const UniformField& u1, - const UniformField& u2 + const UniformField& a, + const UniformField& b ) { - return UniformField(max(u1.operator Type(), u2.operator Type())); + return UniformField(max(a.value(), b.value())); } template inline OtherType max(const UniformField& u, const OtherType& o) { - return max(u.operator Type(), o); + return max(u.value(), o); } template inline OtherType max(const OtherType& o, const UniformField& u) { - return max(o, u.operator Type()); + return max(o, u.value()); }