Commit bf3b4fab authored by Mark Olesen's avatar Mark Olesen

ENH: UniformList to wrap a single value into a list-like container

- refactor UniformField accordingly
parent 0d08942b
#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<class T>
void printInfo(const UniformList<T>& list, const label i=0)
{
UniformField<scalar> uf1(13.1);
UniformField<vector> uf2(vector(1, 2, 3));
Info<< nl
<< "value: " << list.value() << nl
<< "cast: " << static_cast<const T&>(list) << nl
<< "[" << i << "] = " << list[i] << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
{
UniformField<scalar> fld(3.14159);
printInfo(fld, -100);
Info<< "uf1 = " << uf1[22] << "; uf2 = " << uf2[1002] << endl;
// Change value
fld.value() *= 0.5;
Info<< nl << "/= 2 " << nl;
printInfo(fld, -100);
}
{
UniformField<vector> 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;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
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 <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class UniformList Declaration
\*---------------------------------------------------------------------------*/
template<class T>
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
// ************************************************************************* //
......@@ -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 Type>
class UniformField
:
public UniformList<Type>
{
// Private data
Type value_;
public:
// Constructors
//- Construct given value
inline UniformField(const Type& value);
// Member Operators
//- Inherit constructors from UniformList
using UniformList<Type>::UniformList;
inline operator Type() const;
inline Type operator[](const label) const;
// Member Functions
inline UniformField field() const;
UniformField field() const
{
return UniformField(UniformList<Type>::value());
}
};
......
......@@ -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<class Type>
inline Foam::UniformField<Type>::UniformField(const Type& value)
:
value_(value)
{}
template<class Type>
inline Foam::UniformField<Type>::operator Type() const
{
return value_;
}
template<class Type>
inline Type Foam::UniformField<Type>::operator[](const label) const
{
return value_;
}
template<class Type>
inline Foam::UniformField<Type> Foam::UniformField<Type>::field() const
{
return UniformField(value_);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
......@@ -67,50 +38,50 @@ namespace Foam
template<class Type>
inline UniformField<Type> min
(
const UniformField<Type>& u1,
const UniformField<Type>& u2
const UniformField<Type>& a,
const UniformField<Type>& b
)
{
return UniformField<Type>(min(u1.operator Type(), u2.operator Type()));
return UniformField<Type>(min(a.value(), b.value()));
}
template<class Type, class OtherType>
inline OtherType min(const UniformField<Type>& u, const OtherType& o)
{
return min(u.operator Type(), o);
return min(u.value(), o);
}
template<class Type, class OtherType>
inline OtherType min(const OtherType& o, const UniformField<Type>& u)
{
return min(o, u.operator Type());
return min(o, u.value());
}
template<class Type>
inline UniformField<Type> max
(
const UniformField<Type>& u1,
const UniformField<Type>& u2
const UniformField<Type>& a,
const UniformField<Type>& b
)
{
return UniformField<Type>(max(u1.operator Type(), u2.operator Type()));
return UniformField<Type>(max(a.value(), b.value()));
}
template<class Type, class OtherType>
inline OtherType max(const UniformField<Type>& u, const OtherType& o)
{
return max(u.operator Type(), o);
return max(u.value(), o);
}
template<class Type, class OtherType>
inline OtherType max(const OtherType& o, const UniformField<Type>& u)
{
return max(o, u.operator Type());
return max(o, u.value());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment