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
Description
A class representing the concept of 1 (or 1.0) used to avoid unnecessary
manipulations for objects that are known to be one at compile-time.
A class representing the concept of 1 (one), which can be used to avoid
manipulating objects that are known to be 'one' at compile-time.
SourceFiles
oneI.H
@ -43,13 +43,13 @@ SourceFiles
namespace Foam
{
// Forward declaration of classes, friend functions and operators
// Forward declarations
class one;
class Istream;
class Ostream;
class one;
/*---------------------------------------------------------------------------*\
Class one Declaration
Class one Declaration
\*---------------------------------------------------------------------------*/
class one
@ -57,40 +57,73 @@ class one
public:
typedef one value_type;
// Forward declaration
// Forward declarations
class minus;
class null;
//- Null constructible
constexpr one() noexcept {}
// Constructors
//- Null constructible
one()
{}
//- Construct null from Istream. Consumes no content.
explicit one(Istream&)
{}
//- Construct from Istream consumes no content.
explicit constexpr one(Istream&) noexcept {}
// Member operators
//- Return 1 for label
inline constexpr operator label() const noexcept
{
return 1;
}
//- Return 1 for label
inline operator label() const
{
return 1;
}
//- Return 1 for float
inline constexpr operator float() const noexcept
{
return 1;
}
//- Return 1 for float
inline operator float() const
{
return 1;
}
//- Return 1 for double
inline constexpr operator double() const noexcept
{
return 1;
}
};
//- Return 1 for double
inline operator double() const
{
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;
}
};
@ -107,25 +140,31 @@ public:
typedef null value_type;
//- Null constructible
null()
{}
constexpr null() noexcept {}
//- Construct null from Istream without consuming any content.
explicit null(Istream&)
{}
//- Construct from Istream consumes no content.
explicit constexpr null(Istream&) noexcept {}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// IOstream Operators
//- 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;
}
//- 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;
}

View File

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

View File

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