Commit 608e5897 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: added constexpr, noexcept for bool, Switch

- rationalized some method naming.
  Eg, c_str() instead of asText()
parent c2d5f353
......@@ -267,7 +267,7 @@ void Foam::controlMeshRefinement::initialMeshPopulation
Info<< "Inserting points from " << controlFunction.name()
<< " (" << controlFunction.type() << ")" << endl;
Info<< " Force insertion is " << forceInsertion.asText() << endl;
Info<< " Force insertion is " << forceInsertion.c_str() << endl;
pointField pts;
scalarField sizes;
......@@ -453,7 +453,7 @@ void Foam::controlMeshRefinement::initialMeshPopulation
Info<< "Inserting points from " << controlFunction.name()
<< " (" << controlFunction.type() << ")" << endl;
Info<< " Force insertion is " << forceInsertion.asText() << endl;
Info<< " Force insertion is " << forceInsertion.c_str() << endl;
DynamicList<Foam::point> extraPts;
DynamicList<scalar> extraSizes;
......
......@@ -538,13 +538,13 @@ Foam::featurePointConformer::featurePointConformer
Info<< incrIndent
<< indent << "Circulating edges is: "
<< foamyHexMeshControls_.circulateEdges().asText() << nl
<< foamyHexMeshControls_.circulateEdges().c_str() << nl
<< indent << "Guarding feature points is: "
<< foamyHexMeshControls_.guardFeaturePoints().asText() << nl
<< foamyHexMeshControls_.guardFeaturePoints().c_str() << nl
<< indent << "Snapping to feature points is: "
<< foamyHexMeshControls_.snapFeaturePoints().asText() << nl
<< foamyHexMeshControls_.snapFeaturePoints().c_str() << nl
<< indent << "Specialising feature points is: "
<< foamyHexMeshControls_.specialiseFeaturePoints().asText()
<< foamyHexMeshControls_.specialiseFeaturePoints().c_str()
<< decrIndent
<< endl;
......
......@@ -66,7 +66,7 @@ pointFile::pointFile
readScalar(detailsDict().lookup("randomPerturbationCoeff"))
)
{
Info<< " Inside/Outside check is " << insideOutsideCheck_.asText()
Info<< " Inside/Outside check is " << insideOutsideCheck_.c_str()
<< endl;
}
......
......@@ -84,7 +84,7 @@ Foam::surfaceFeaturesExtraction::extractFromFile::features
Info<< nl << "Reading existing feature edges from file "
<< featureEdgeFile_ << nl
<< "Selecting edges based purely on geometric tests: "
<< geometricTestOnly().asText() << endl;
<< geometricTestOnly().c_str() << endl;
return autoPtr<surfaceFeatures>
(
......
......@@ -78,7 +78,7 @@ Foam::surfaceFeaturesExtraction::extractFromSurface::features
Info<< nl << "Constructing feature set from included angle "
<< includedAngle() << nl
<< "Selecting edges based purely on geometric tests: "
<< geometricTestOnly().asText() << endl;
<< geometricTestOnly().c_str() << endl;
return autoPtr<surfaceFeatures>
(
......
......@@ -19,9 +19,7 @@ $(fileOps)/collatedFileOperation/OFstreamCollator.C
bools = primitives/bools
$(bools)/bool/bool.C
$(bools)/bool/boolIO.C
$(bools)/Switch/Switch.C
$(bools)/Switch/SwitchIO.C
$(bools)/lists/boolList.C
chars = primitives/chars
......
......@@ -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.
......@@ -26,29 +26,29 @@ License
#include "Switch.H"
#include "error.H"
#include "dictionary.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* Foam::Switch::names[Foam::Switch::INVALID+1] =
const char* Foam::Switch::names[9] =
{
"false", "true",
"no", "yes",
"off", "on",
"none", "true", // Is there a reasonable counterpart to "none"?
"none", "(unused)",
"invalid"
};
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
Foam::Switch::switchType Foam::Switch::asEnum
Foam::Switch::switchType Foam::Switch::parse
(
const std::string& str,
const bool allowInvalid
bool allowBad
)
{
const std::string::size_type len = str.size();
switch (len)
switch (str.size())
{
case 1: // (f|n|t|y) - single-character forms
{
......@@ -86,7 +86,7 @@ Foam::Switch::switchType Foam::Switch::asEnum
}
}
if (!allowInvalid)
if (!allowBad)
{
FatalErrorInFunction
<< "Unknown switch word " << str << nl
......@@ -108,15 +108,29 @@ Foam::Switch Foam::Switch::lookupOrAddToDict
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::Switch::Switch(Istream& is)
{
is >> *this;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
bool Foam::Switch::valid() const
bool Foam::Switch::valid() const noexcept
{
return switch_ <= switchType::NONE;
}
const char* Foam::Switch::asText() const
const char* Foam::Switch::c_str() const noexcept
{
return names[switch_];
}
std::string Foam::Switch::str() const
{
return names[switch_];
}
......@@ -128,4 +142,57 @@ bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, Switch& sw)
{
token t(is);
if (!t.good())
{
is.setBad();
return is;
}
if (t.isLabel())
{
sw = bool(t.labelToken());
}
else if (t.isWord())
{
// Allow reading invalid value, but report immediately
sw = Switch(t.wordToken(), true);
if (!sw.valid())
{
is.setBad();
FatalIOErrorInFunction(is)
<< "expected 'true/false', 'on/off' ... found " << t.wordToken()
<< exit(FatalIOError);
return is;
}
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected bool, found " << t
<< exit(FatalIOError);
return is;
}
is.check(FUNCTION_NAME);
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& sw)
{
os << sw.c_str();
return os;
}
// ************************************************************************* //
......@@ -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.
......@@ -30,7 +30,6 @@ Description
SourceFiles
Switch.C
SwitchIO.C
\*---------------------------------------------------------------------------*/
......@@ -40,19 +39,28 @@ SourceFiles
#include "bool.H"
#include "word.H"
// Avoid any pre-processor conflicts with enum names
#undef FALSE
#undef TRUE
#undef NO
#undef YES
#undef OFF
#undef ON
#undef NONE
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of friend functions and operators
// Forward declarations
class Switch;
class dictionary;
Istream& operator>>(Istream& is, Switch& s);
Ostream& operator<<(Ostream& is, const Switch& s);
// IOstream Operators
Istream& operator>>(Istream& is, Switch& sw);
Ostream& operator<<(Ostream& is, const Switch& sw);
/*---------------------------------------------------------------------------*\
Class Switch Declaration
......@@ -60,80 +68,63 @@ Ostream& operator<<(Ostream& is, const Switch& s);
class Switch
{
// Private data
//- The logic and enumerated text representation stored as a single byte
unsigned char switch_;
public:
// Public data types
// Avoid issues with possible pre-processor defines
#undef FALSE
#undef TRUE
#undef NO
#undef YES
#undef OFF
#undef ON
#undef NONE
#undef PLACEHOLDER
#undef INVALID
//- The various text representations for a switch value.
// These also correspond to the entries in names.
enum switchType
{
FALSE = 0, TRUE = 1,
NO = 2, YES = 3,
OFF = 4, ON = 5,
NONE = 6, PLACEHOLDER = 7,
INVALID
};
//- Switch enumerations corresponding to common text representations.
// \note the values specified here are critical for its proper behaviour.
// The lower bit is tested for the true/false condition.
// The values correspond to an index into the predefined output names
// for the c_str() method.
enum switchType
{
FALSE = 0 /*!< "false" */, TRUE = 1 /*!< "true" */,
NO = 2 /*!< "no" */, YES = 3 /*!< "yes" */,
OFF = 4 /*!< "off" */, ON = 5 /*!< "on" */,
NONE = 6 /*!< "none" */,
INVALID = 8 /*!< "invalid" */
};
// Static data members
//- The set of names corresponding to the switchType enumeration
// Includes an extra entry for "invalid".
static const char* names[INVALID+1];
private:
// Private Data
private:
//- The logic and enumerated text representation stored in a byte
unsigned char switch_;
// Static Member Functions
//- The set of names corresponding to the switchType enumeration.
// Includes extra entries for "invalid".
static const char* names[9];
//- Return a switchType representation of an input string
static switchType asEnum
(
const std::string& str,
const bool allowInvalid
);
// Static Member Functions
//- Return enum value for input string
static switchType parse(const std::string& str, bool allowBad);
public:
// Constructors
//- Construct null as false
Switch()
//- Null constructible as false
constexpr Switch() noexcept
:
switch_(switchType::FALSE)
{}
//- Construct from enumerated value
Switch(const switchType sw)
constexpr Switch(const switchType sw) noexcept
:
switch_(sw)
{}
//- Construct from bool
Switch(const bool b)
constexpr Switch(const bool b) noexcept
:
switch_(b ? switchType::TRUE : switchType::FALSE)
{}
//- Construct from integer values (treats integer as bool value)
Switch(const int i)
constexpr Switch(const int i) noexcept
:
switch_(i ? switchType::TRUE : switchType::FALSE)
{}
......@@ -141,32 +132,35 @@ public:
//- Construct from string - catches bad input.
explicit Switch(const std::string& str)
:
switch_(asEnum(str, false))
switch_(parse(str, false))
{}
//- Construct from character array - catches bad input.
explicit Switch(const char* str)
:
switch_(asEnum(std::string(str), false))
switch_(parse(str, false))
{}
//- Construct from string.
// Optionally allow bad words, and catch the error elsewhere
Switch(const std::string& str, const bool allowInvalid)
Switch(const std::string& str, const bool allowBad)
:
switch_(asEnum(str, allowInvalid))
switch_(parse(str, allowBad))
{}
//- Construct from character array.
// Optionally allow bad words, and catch the error elsewhere
Switch(const char* str, const bool allowInvalid)
Switch(const char* str, const bool allowBad)
:
switch_(asEnum(std::string(str), allowInvalid))
switch_(parse(str, allowBad))
{}
//- Construct from Istream
Switch(Istream& is);
// Helpers
//- Construct from dictionary, supplying default value so that if the
// value is not found, it is added into the dictionary.
static Switch lookupOrAddToDict
......@@ -180,10 +174,13 @@ public:
// Member Functions
//- Return true if the Switch has a valid value
bool valid() const;
bool valid() const noexcept;
//- A string representation of the Switch value
const char* c_str() const noexcept;
//- Return a text representation of the Switch
const char* asText() const;
//- A string representation of the Switch value
std::string str() const;
//- Update the value of the Switch if it is found in the dictionary
bool readIfPresent(const word& name, const dictionary& dict);
......@@ -192,28 +189,29 @@ public:
// Member Operators
//- Conversion to bool
operator bool() const
operator bool() const noexcept
{
return (switch_ & 0x1);
}
//- Assignment to enumerated value
void operator=(const switchType sw)
//- Assignment from enumerated value
void operator=(const switchType sw) noexcept
{
switch_ = sw;
}
//- Assignment to bool
void operator=(const bool b)
//- Assignment from bool
void operator=(const bool b) noexcept
{
switch_ = (b ? Switch::TRUE : Switch::FALSE);
}
// IOstream Operators
// Housekeeping
friend Istream& operator>>(Istream& is, Switch& s);
friend Ostream& operator<<(Ostream& os, const Switch& s);
//- A text representation of the Switch value
// \deprecated - use c_str() instead (MAR-2018)
inline const char* asText() const { return c_str(); };
};
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "Switch.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::Switch::Switch(Istream& is)
{
is >> *this;
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, Switch& s)
{
token t(is);
if (!t.good())
{
is.setBad();
return is;
}
if (t.isLabel())
{
s = bool(t.labelToken());
}
else if (t.isWord())
{
// allow invalid values, but catch after for correct error message
Switch sw(t.wordToken(), true);
if (sw.valid())
{
s = sw;
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "expected 'true/false', 'on/off' ... found " << t.wordToken()
<< exit(FatalIOError);
return is;
}
}
else
{
is.setBad();
FatalIOErrorInFunction(is)
<< "wrong token type - expected bool, found " << t
<< exit(FatalIOError);
return is;
}
is.check(FUNCTION_NAME);
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& s)
{
os << Switch::names[s.switch_];
os.check(FUNCTION_NAME);
return os;
}
// ************************************************************************* //
......@@ -24,6 +24,9 @@ License
\*---------------------------------------------------------------------------*/
#include "bool.H"
#include "Switch.H"
#include "error.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -44,4 +47,34 @@ Foam::pTraits<bool>::pTraits(Istream& is)
is >> p_;
}
Foam::Istream& Foam::operator>>(Istream& is, bool& b)
{
if (is.good())
{
b = Switch(is);
}
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const bool b)
{
// Emit as label (not byte etc) for proper send/receive in parallel
os.write(label(b));
os.check(FUNCTION_NAME);
return os;
}
bool Foam::readBool(Istream& is)
{
bool b;
is >>