ENH: cleanup, extend zero/one classes

- constexpr, noexcept on various bits

- addition of a 'one::minus' class that returns '-1' instead of '1'.
  There are no additional operations defined for this class,
  but it can be used in various places to signal alternative behaviour
  such as "initialize to a negative or other invalid value"
This commit is contained in:
Mark Olesen
2018-03-02 10:04:56 +01:00
parent b4703f4a08
commit 13ea73c31c
4 changed files with 146 additions and 119 deletions

View File

@ -25,8 +25,8 @@ Class
Foam::one Foam::one
Description Description
A class representing the concept of 1 (or 1.0) used to avoid unnecessary A class representing the concept of 1 (one), which can be used to avoid
manipulations for objects that are known to be one at compile-time. manipulating objects that are known to be 'one' at compile-time.
SourceFiles SourceFiles
oneI.H oneI.H
@ -43,10 +43,10 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declaration of classes, friend functions and operators // Forward declarations
class one;
class Istream; class Istream;
class Ostream; class Ostream;
class one;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class one Declaration Class one Declaration
@ -57,43 +57,76 @@ class one
public: public:
typedef one value_type; typedef one value_type;
// Forward declaration // Forward declarations
class minus;
class null; class null;
// Constructors
//- Null constructible //- Null constructible
one() constexpr one() noexcept {}
{}
//- Construct null from Istream. Consumes no content. //- Construct from Istream consumes no content.
explicit one(Istream&) explicit constexpr one(Istream&) noexcept {}
{}
// Member operators
//- Return 1 for label //- Return 1 for label
inline operator label() const inline constexpr operator label() const noexcept
{ {
return 1; return 1;
} }
//- Return 1 for float //- Return 1 for float
inline operator float() const inline constexpr operator float() const noexcept
{ {
return 1; return 1;
} }
//- Return 1 for double //- Return 1 for double
inline operator double() const inline constexpr operator double() const noexcept
{ {
return 1; return 1;
} }
}; };
/*---------------------------------------------------------------------------*\
Class one::minus Declaration
\*---------------------------------------------------------------------------*/
//- A class representing the concept of -1.
// Note that this class must never be derived from 'one', since this could
// potentially mask its behaviour.
class one::minus
{
public:
typedef minus value_type;
//- Null constructible
constexpr minus() noexcept {}
//- Construct from Istream consumes no content.
explicit constexpr minus(Istream&) noexcept {}
//- Return -1 for label
inline constexpr operator label() const noexcept
{
return -1;
}
//- Return -1 for float
inline constexpr operator float() const noexcept
{
return -1;
}
//- Return -1 for double
inline constexpr operator double() const noexcept
{
return -1;
}
};
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class one::null Declaration Class one::null Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -107,25 +140,31 @@ public:
typedef null value_type; typedef null value_type;
//- Null constructible //- Null constructible
null() constexpr null() noexcept {}
{}
//- Construct null from Istream without consuming any content. //- Construct from Istream consumes no content.
explicit null(Istream&) explicit constexpr null(Istream&) noexcept {}
{}
}; };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// IOstream Operators // IOstream Operators
//- Read from Istream consumes no content. //- Read from Istream consumes no content.
inline Istream& operator>>(Istream& is, one&) inline constexpr Istream& operator>>(Istream& is, one&) noexcept
{
return is;
}
//- Read from Istream consumes no content.
inline constexpr Istream& operator>>(Istream& is, one::minus&) noexcept
{ {
return is; return is;
} }
//- Write to Ostream emits no content. //- Write to Ostream emits no content.
inline Ostream& operator<<(Ostream& os, const one::null&) inline constexpr Ostream& operator<<(Ostream& os, const one::null&) noexcept
{ {
return os; return os;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -23,7 +23,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "one.H"
#include "scalar.H" #include "scalar.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -41,44 +40,45 @@ public:
typedef arg2 type; typedef arg2 type;
}; };
inline const one& operator*(const one& o, const one&)
inline constexpr const one& operator*(const one& o, const one&) noexcept
{ {
return o; return o;
} }
template<class Type> template<class Type>
inline const Type& operator*(const Type& t, const one&) inline constexpr const Type& operator*(const Type& val, const one&) noexcept
{ {
return t; return val;
} }
template<class Type> template<class Type>
inline const Type& operator*(const one&, const Type& t) inline constexpr const Type& operator*(const one&, const Type& val) noexcept
{ {
return t; return val;
} }
template<class Type> template<class Type>
inline const Type& operator&(const one&, const Type& t) inline constexpr const Type& operator&(const one&, const Type& val) noexcept
{ {
return t; return val;
} }
inline const one& operator/(const one& o, const one&) inline constexpr const one& operator/(const one& o, const one&) noexcept
{ {
return o; return o;
} }
template<class Type> template<class Type>
inline Type operator/(const one&, const Type& t) inline Type operator/(const one&, const Type& val)
{ {
return scalar(1)/t; return scalar(1)/val;
} }
template<class Type> template<class Type>
inline const Type& operator/(const Type& t, const one&) inline constexpr const Type& operator/(const Type& val, const one&) noexcept
{ {
return t; return val;
} }

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -25,8 +25,8 @@ Class
Foam::zero Foam::zero
Description Description
A class representing the concept of 0 used to avoid unnecessary A class representing the concept of 0 (zero), which can be used to avoid
manipulations for objects that are known to be zero at compile-time. manipulating objects that are known to be 'zero' at compile-time.
SourceFiles SourceFiles
zeroI.H zeroI.H
@ -44,11 +44,10 @@ namespace Foam
{ {
// Forward declarations // Forward declarations
class zero;
class Istream; class Istream;
class Ostream; class Ostream;
class zero;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class zero Declaration Class zero Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -58,47 +57,39 @@ class zero
public: public:
typedef zero value_type; typedef zero value_type;
// Forward declaration // Forward declarations
class null; class null;
// Constructors
//- Null constructible //- Null constructible
zero() constexpr zero() noexcept {}
{}
//- Construct null from Istream. Consumes no content. //- Construct from Istream consumes no content.
explicit zero(Istream&) explicit constexpr zero(Istream&) noexcept {}
{}
// Member operators //- Return false (0) for bool
inline constexpr operator bool() const noexcept
//- Return 0 for bool
inline operator bool() const
{ {
return 0; return false;
} }
//- Return 0 for label //- Return 0 for label
inline operator label() const inline constexpr operator label() const noexcept
{ {
return 0; return 0;
} }
//- Return 0 for float //- Return 0 for float
inline operator float() const inline constexpr operator float() const noexcept
{ {
return 0; return 0;
} }
//- Return 0 for double //- Return 0 for double
inline operator double() const inline constexpr operator double() const noexcept
{ {
return 0; return 0;
} }
}; };
@ -115,34 +106,33 @@ public:
typedef null value_type; typedef null value_type;
//- Null constructible //- Null constructible
null() constexpr null() noexcept{}
{}
//- Construct null from Istream without consuming any content. //- Construct from Istream consumes no content.
explicit null(Istream&) explicit constexpr null(Istream&) noexcept {}
{}
}; };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Global zero
static constexpr const zero Zero;
// IOstream Operators // IOstream Operators
//- Read from Istream consumes no content. //- Read from Istream consumes no content.
inline Istream& operator>>(Istream& is, zero&) inline constexpr Istream& operator>>(Istream& is, zero&) noexcept
{ {
return is; return is;
} }
//- Write to Ostream emits no content. //- Write to Ostream emits no content.
inline Ostream& operator<<(Ostream& os, const zero::null&) inline constexpr Ostream& operator<<(Ostream& os, const zero::null&) noexcept
{ {
return os; return os;
} }
// Global zero
static const zero Zero;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam } // End namespace Foam

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -23,8 +23,6 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "zero.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam
@ -32,59 +30,59 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
inline zero operator+(const zero&, const zero&) inline constexpr zero operator+(const zero&, const zero&) noexcept
{ {
return Zero; return Zero;
} }
template<class Type> template<class Type>
inline const Type& operator+(const Type& t, const zero&) inline constexpr const Type& operator+(const Type& val, const zero&) noexcept
{ {
return t; return val;
} }
template<class Type> template<class Type>
inline const Type& operator+(const zero&, const Type& t) inline constexpr const Type& operator+(const zero&, const Type& val) noexcept
{ {
return t; return val;
} }
inline zero operator-(const zero&, const zero&) inline constexpr zero operator-(const zero&, const zero&) noexcept
{ {
return Zero; return Zero;
} }
template<class Type> template<class Type>
inline const Type& operator-(const Type& t, const zero&) inline constexpr const Type& operator-(const Type& val, const zero&) noexcept
{ {
return t; return val;
} }
template<class Type> template<class Type>
inline Type operator-(const zero&, const Type& t) inline constexpr Type operator-(const zero&, const Type& val)
{ {
return -t; return -val;
} }
inline zero operator*(const zero&, const zero&) inline constexpr zero operator*(const zero&, const zero&) noexcept
{ {
return Zero; return Zero;
} }
template<class Type> template<class Type>
inline zero operator*(const Type& t, const zero&) inline constexpr zero operator*(const Type& val, const zero&) noexcept
{ {
return Zero; return Zero;
} }
template<class Type> template<class Type>
inline zero operator*(const zero&, const Type& t) inline constexpr zero operator*(const zero&, const Type& val) noexcept
{ {
return Zero; return Zero;
} }
template<class Type> template<class Type>
inline zero operator/(const zero&, const Type& t) inline constexpr zero operator/(const zero&, const Type& val) noexcept
{ {
return Zero; return Zero;
} }