ENH: additional dimensionedType constructors

- construct from dimensioned/value, defaulting name from value.
  Can be convenient for these type of operations:

      max(.., dimensionedScalar(somedims, 0.5))

- construct from dimensioned/one, forwarding to pTraits::one.
  Can be convenient for constructors:

    volScalarField( ..., dimensionedScalar(somedims, one{}))

ENH: minor updates to zero/one classes.

- add global 'One' constant for symmetry with 'Zero'.
This commit is contained in:
Mark Olesen
2020-04-27 12:28:29 +02:00
parent 79048eb68f
commit 64fcbd5e4f
4 changed files with 94 additions and 46 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -46,7 +46,7 @@ void Foam::dimensioned<Type>::initialize(Istream& is, const bool checkDims)
is.putBack(nextToken);
}
scalar mult(1.0);
scalar mult{1};
if (nextToken == token::BEGIN_SQR)
{
@ -128,7 +128,11 @@ Foam::dimensioned<Type>::dimensioned(const dimensionSet& dims)
template<class Type>
Foam::dimensioned<Type>::dimensioned(const dimensionSet& dims, const zero)
Foam::dimensioned<Type>::dimensioned
(
const dimensionSet& dims,
const Foam::zero
)
:
name_("0"),
dimensions_(dims),
@ -139,13 +143,26 @@ Foam::dimensioned<Type>::dimensioned(const dimensionSet& dims, const zero)
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dimensioned<Type>& dt
const dimensionSet& dims,
const Foam::one
)
:
name_(name),
dimensions_(dt.dimensions_),
value_(dt.value_)
name_("1"),
dimensions_(dims),
value_(pTraits<Type>::one)
{}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const dimensionSet& dims,
const Type& val
)
:
name_(::Foam::name(val)),
dimensions_(dims),
value_(val)
{}
@ -163,6 +180,19 @@ Foam::dimensioned<Type>::dimensioned
{}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
const word& name,
const dimensioned<Type>& dt
)
:
name_(name),
dimensions_(dt.dimensions_),
value_(dt.value_)
{}
template<class Type>
Foam::dimensioned<Type>::dimensioned
(
@ -490,7 +520,7 @@ Foam::Istream& Foam::dimensioned<Type>::read(Istream& is, const bool readName)
}
// Read dimensionSet + multiplier
scalar mult(1.0);
scalar mult{1};
dimensions_.read(is, mult);
// Read value
@ -510,7 +540,7 @@ Foam::dimensioned<Type>::read(Istream& is, const dictionary& readSet)
is >> name_;
// Read dimensionSet + multiplier
scalar mult(1.0);
scalar mult{1};
dimensions_.read(is, mult, readSet);
// Read value
@ -533,7 +563,7 @@ Foam::Istream& Foam::dimensioned<Type>::read
is >> name_;
// Read dimensionSet + multiplier
scalar mult(1.0);
scalar mult{1};
dimensions_.read(is, mult, readSet);
// Read value
@ -561,7 +591,7 @@ void Foam::dimensioned<Type>::writeEntry
}
// The dimensions
scalar mult(1.0);
scalar mult{1};
dimensions_.write(os, mult);
// The value
@ -777,7 +807,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const dimensioned<Type>& dt)
os << dt.name() << token::SPACE;
// The dimensions
scalar mult(1.0);
scalar mult{1};
dt.dimensions().write(os, mult);
// The value

View File

@ -48,7 +48,8 @@ SourceFiles
namespace Foam
{
// Forward declarations
// Forward Declarations
class one;
class zero;
class dictionary;
class primitiveEntry;
@ -58,7 +59,6 @@ template<class Type> class dimensioned;
template<class Type>
Istream& operator>>(Istream& is, dimensioned<Type>& dt);
/*---------------------------------------------------------------------------*\
Class dimensioned Declaration
\*---------------------------------------------------------------------------*/
@ -130,7 +130,10 @@ public:
explicit dimensioned(const dimensionSet& dims);
//- A dimensioned Zero, named "0"
explicit dimensioned(const dimensionSet& dims, const zero);
explicit dimensioned(const dimensionSet& dims, const Foam::zero);
//- A dimensioned pTraits::one, named "1"
explicit dimensioned(const dimensionSet& dims, const Foam::one);
//- Implicit construct dimensionless from given value.
dimensioned(const Type& val)
@ -140,8 +143,8 @@ public:
value_(val)
{}
//- Copy construct dimensioned Type with a new name
dimensioned(const word& name, const dimensioned<Type>& dt);
//- Construct dimensioned from given value
dimensioned(const dimensionSet& dims, const Type& val);
//- Construct from components (name, dimensions, value).
dimensioned
@ -151,6 +154,9 @@ public:
const Type& val
);
//- Copy construct dimensioned Type with a new name
dimensioned(const word& name, const dimensioned<Type>& dt);
//- Construct from primitive entry with given name.
// The entry may contain optional name and dimensions.
// \verbatim
@ -234,7 +240,7 @@ public:
// Static Member Functions
//- Construct dimensioned from dictionary, with default value.
//- FatalIOError if there are excess tokens.
// FatalIOError if there are excess tokens.
static dimensioned<Type> getOrDefault
(
const word& name,

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,8 +28,9 @@ Class
Foam::one
Description
A class representing the concept of 1 (one), which can be used to avoid
manipulating objects that are known to be \em one at compile-time.
A class representing the concept of 1 (one) that can be used to avoid
manipulating objects known to be \em one at compile-time.
It is also used for tagged dispatch.
SourceFiles
oneI.H
@ -49,7 +50,7 @@ SeeAlso
namespace Foam
{
// Forward declarations
// Forward Declarations
class one;
class Istream;
class Ostream;
@ -61,13 +62,14 @@ class Ostream;
class one
{
public:
typedef one value_type;
// Forward declarations
// Forward Declarations
class minus;
class null;
//- Null constructible
//- Default construct
constexpr one() noexcept {}
//- Construct from Istream consumes no content.
@ -75,19 +77,19 @@ public:
//- Return 1 for label
inline constexpr operator label() const noexcept
constexpr operator label() const noexcept
{
return 1;
}
//- Return 1 for float
inline constexpr operator float() const noexcept
constexpr operator float() const noexcept
{
return 1;
}
//- Return 1 for double
inline constexpr operator double() const noexcept
constexpr operator double() const noexcept
{
return 1;
}
@ -99,14 +101,15 @@ public:
\*---------------------------------------------------------------------------*/
//- A class representing the concept of -1.
// Note that this class must never be derived from 'one', since this could
// \note this class must never be derived from Foam::one, since this could
// potentially mask its behaviour.
class one::minus
{
public:
typedef minus value_type;
//- Null constructible
//- Default construct
constexpr minus() noexcept {}
//- Construct from Istream consumes no content.
@ -114,19 +117,19 @@ public:
//- Return -1 for label
inline constexpr operator label() const noexcept
constexpr operator label() const noexcept
{
return -1;
}
//- Return -1 for float
inline constexpr operator float() const noexcept
constexpr operator float() const noexcept
{
return -1;
}
//- Return -1 for double
inline constexpr operator double() const noexcept
constexpr operator double() const noexcept
{
return -1;
}
@ -137,15 +140,16 @@ public:
Class one::null Declaration
\*---------------------------------------------------------------------------*/
//- A one class with a null output adapter.
//- A Foam::one class with a null output adapter.
class one::null
:
public one
{
public:
typedef null value_type;
//- Null constructible
//- Default construct
constexpr null() noexcept {}
//- Construct from Istream consumes no content.
@ -155,6 +159,9 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Global one (1)
static constexpr const one One;
// IOstream Operators
//- Read from Istream consumes no content.
@ -182,6 +189,7 @@ inline constexpr Ostream& operator<<(Ostream& os, const one::null&) noexcept
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Global Operators, Functions
#include "oneI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,8 +28,9 @@ Class
Foam::zero
Description
A class representing the concept of 0 (zero), which can be used to avoid
manipulating objects that are known to be \em zero at compile-time.
A class representing the concept of 0 (zero) that can be used to avoid
manipulating objects known to be \em zero at compile-time.
It is also used for tagged dispatch.
SourceFiles
zero.C
@ -50,7 +51,7 @@ SeeAlso
namespace Foam
{
// Forward declarations
// Forward Declarations
class zero;
class Istream;
class Ostream;
@ -62,12 +63,13 @@ class Ostream;
class zero
{
public:
typedef zero value_type;
// Forward declarations
// Forward Declarations
class null;
//- Null constructible
//- Default construct
constexpr zero() noexcept {}
//- Construct from Istream consumes no content.
@ -75,25 +77,25 @@ public:
//- Return false (0) for bool
inline constexpr operator bool() const noexcept
constexpr operator bool() const noexcept
{
return false;
}
//- Return 0 for label
inline constexpr operator label() const noexcept
constexpr operator label() const noexcept
{
return 0;
}
//- Return 0 for float
inline constexpr operator float() const noexcept
constexpr operator float() const noexcept
{
return 0;
}
//- Return 0 for double
inline constexpr operator double() const noexcept
constexpr operator double() const noexcept
{
return 0;
}
@ -110,6 +112,7 @@ class zero::null
public zero
{
public:
typedef null value_type;
//- A static zero::null for dereferencing as a dummy element
@ -125,7 +128,7 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Global zero
//- Global zero (0)
static constexpr const zero Zero;
// IOstream Operators
@ -149,6 +152,7 @@ inline constexpr Ostream& operator<<(Ostream& os, const zero::null&) noexcept
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Global Operators, Functions
#include "zeroI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //