ENH: component/element access for zero/one return themselves

STYLE: more consistent access for uniform list/fields
This commit is contained in:
Mark Olesen
2022-10-13 11:23:35 +02:00
parent 9433898941
commit 7a43cac55a
8 changed files with 53 additions and 42 deletions

View File

@ -36,8 +36,8 @@ Note
\*---------------------------------------------------------------------------*/
#ifndef UniformList_H
#define UniformList_H
#ifndef Foam_UniformList_H
#define Foam_UniformList_H
#include "labelFwd.H"
#include <utility>
@ -51,26 +51,26 @@ namespace Foam
Class UniformList Declaration
\*---------------------------------------------------------------------------*/
template<class T>
template<class Type>
class UniformList
{
// Private Data
//- The value to be returned.
T value_;
Type value_;
public:
// Constructors
//- Construct from given value
explicit UniformList(const T& val) noexcept
explicit UniformList(const Type& val) noexcept
:
value_(val)
{}
//- Move construct from given value
explicit UniformList(T&& val) noexcept
explicit UniformList(Type&& val) noexcept
:
value_(std::move(val))
{}
@ -79,13 +79,13 @@ public:
// Member Functions
//- Return the value
const T& value() const noexcept
const Type& value() const noexcept
{
return value_;
}
//- Non-const access to the value
T& value() noexcept
Type& value() noexcept
{
return value_;
}
@ -94,13 +94,13 @@ public:
// Member Operators
//- Implicit cast to the value
operator const T&() const noexcept
operator const Type&() const noexcept
{
return value_;
}
//- Return the value
const T& operator[](const label) const noexcept
const Type& operator[](const label) const noexcept
{
return value_;
}

View File

@ -33,10 +33,9 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef UniformField_H
#define UniformField_H
#ifndef Foam_UniformField_H
#define Foam_UniformField_H
#include "label.H"
#include "UniformList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -63,6 +62,7 @@ public:
// Member Functions
//- Method name compatibility with DimensionedField
UniformField field() const
{
return UniformField(UniformList<Type>::value());

View File

@ -70,18 +70,11 @@ public:
// Member Functions
//- Method name compatibility with DimensionedField
oneField field() const noexcept
{
return oneField{};
}
// Member Operators
one operator[](const label) const noexcept
{
return one{};
}
};

View File

@ -65,6 +65,7 @@ public:
// Member Functions
//- Method name compatibility with DimensionedField
zeroField field() const noexcept
{
return zeroField{};

View File

@ -73,19 +73,14 @@ Foam::UniformDimensionedField<Type>::UniformDimensionedField
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::UniformDimensionedField<Type>::~UniformDimensionedField()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
bool Foam::UniformDimensionedField<Type>::readData(Istream& is)
{
dictionary dict(is);
// The dimensions
scalar multiplier(1);
this->dimensions().read
(
@ -93,7 +88,8 @@ bool Foam::UniformDimensionedField<Type>::readData(Istream& is)
multiplier
);
dict.readEntry("value", this->value());
// The value
dict.readEntry("value", this->value(), keyType::LITERAL);
this->value() *= multiplier;
return is.good();
@ -103,9 +99,12 @@ bool Foam::UniformDimensionedField<Type>::readData(Istream& is)
template<class Type>
bool Foam::UniformDimensionedField<Type>::writeData(Ostream& os) const
{
// The dimensions
scalar multiplier(1);
os.writeKeyword("dimensions");
this->dimensions().write(os, multiplier) << token::END_STATEMENT << nl;
// The value
os.writeEntry("value", this->value()/multiplier) << nl;
return os.good();

View File

@ -38,8 +38,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef UniformDimensionedField_H
#define UniformDimensionedField_H
#ifndef Foam_UniformDimensionedField_H
#define Foam_UniformDimensionedField_H
#include "regIOobject.H"
#include "dimensionedType.H"
@ -59,7 +59,6 @@ class UniformDimensionedField
public regIOobject,
public dimensioned<Type>
{
public:
//- Runtime type information
@ -78,20 +77,20 @@ public:
);
//- Construct as copy
UniformDimensionedField(const UniformDimensionedField<Type>& rdt);
UniformDimensionedField(const UniformDimensionedField<Type>&);
//- Construct from Istream
UniformDimensionedField(const IOobject& io);
//- Destructor
virtual ~UniformDimensionedField();
virtual ~UniformDimensionedField() = default;
// Member Functions
//- Name function provided to resolve the ambiguity between the
// name functions in regIOobject and dimensioned<Type>
//- name functions in regIOobject and dimensioned<Type>
virtual const word& name() const
{
return dimensioned<Type>::name();
@ -125,9 +124,10 @@ public:
//- Assign name, dimensions and value.
void operator=(const dimensioned<Type>& rhs);
Type operator[](const label) const
//- Return value
const Type& operator[](const label) const noexcept
{
return this->value();
return dimensioned<Type>::value();
}
};

View File

@ -40,8 +40,8 @@ SeeAlso
\*---------------------------------------------------------------------------*/
#ifndef one_H
#define one_H
#ifndef Foam_one_H
#define Foam_one_H
#include "label.H"
@ -93,6 +93,12 @@ public:
{
return 1;
}
//- Component-wise or element-wise access returns one
one operator[](const label) const noexcept
{
return one{};
}
};
@ -133,6 +139,12 @@ public:
{
return -1;
}
//- Component-wise or element-wise access returns minus one
one::minus operator[](const label) const noexcept
{
return one::minus{};
}
};

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -41,8 +41,8 @@ SeeAlso
\*---------------------------------------------------------------------------*/
#ifndef zero_H
#define zero_H
#ifndef Foam_zero_H
#define Foam_zero_H
#include "labelFwd.H"
@ -99,6 +99,12 @@ public:
{
return 0;
}
//- Component-wise or element-wise access returns zero
zero operator[](const label) const noexcept
{
return zero{};
}
};