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

View File

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

View File

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

View File

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