ENH: added constexpr, noexcept for bool, Switch

- rationalized some method naming.
  Eg, c_str() instead of asText()
This commit is contained in:
Mark Olesen
2018-03-03 20:32:49 +01:00
parent 13ea73c31c
commit 53ab527b45
14 changed files with 188 additions and 257 deletions

View File

@ -267,7 +267,7 @@ void Foam::controlMeshRefinement::initialMeshPopulation
Info<< "Inserting points from " << controlFunction.name() Info<< "Inserting points from " << controlFunction.name()
<< " (" << controlFunction.type() << ")" << endl; << " (" << controlFunction.type() << ")" << endl;
Info<< " Force insertion is " << forceInsertion.asText() << endl; Info<< " Force insertion is " << forceInsertion.c_str() << endl;
pointField pts; pointField pts;
scalarField sizes; scalarField sizes;
@ -453,7 +453,7 @@ void Foam::controlMeshRefinement::initialMeshPopulation
Info<< "Inserting points from " << controlFunction.name() Info<< "Inserting points from " << controlFunction.name()
<< " (" << controlFunction.type() << ")" << endl; << " (" << controlFunction.type() << ")" << endl;
Info<< " Force insertion is " << forceInsertion.asText() << endl; Info<< " Force insertion is " << forceInsertion.c_str() << endl;
DynamicList<Foam::point> extraPts; DynamicList<Foam::point> extraPts;
DynamicList<scalar> extraSizes; DynamicList<scalar> extraSizes;

View File

@ -538,13 +538,13 @@ Foam::featurePointConformer::featurePointConformer
Info<< incrIndent Info<< incrIndent
<< indent << "Circulating edges is: " << indent << "Circulating edges is: "
<< foamyHexMeshControls_.circulateEdges().asText() << nl << foamyHexMeshControls_.circulateEdges().c_str() << nl
<< indent << "Guarding feature points is: " << indent << "Guarding feature points is: "
<< foamyHexMeshControls_.guardFeaturePoints().asText() << nl << foamyHexMeshControls_.guardFeaturePoints().c_str() << nl
<< indent << "Snapping to feature points is: " << indent << "Snapping to feature points is: "
<< foamyHexMeshControls_.snapFeaturePoints().asText() << nl << foamyHexMeshControls_.snapFeaturePoints().c_str() << nl
<< indent << "Specialising feature points is: " << indent << "Specialising feature points is: "
<< foamyHexMeshControls_.specialiseFeaturePoints().asText() << foamyHexMeshControls_.specialiseFeaturePoints().c_str()
<< decrIndent << decrIndent
<< endl; << endl;

View File

@ -66,7 +66,7 @@ pointFile::pointFile
readScalar(detailsDict().lookup("randomPerturbationCoeff")) readScalar(detailsDict().lookup("randomPerturbationCoeff"))
) )
{ {
Info<< " Inside/Outside check is " << insideOutsideCheck_.asText() Info<< " Inside/Outside check is " << insideOutsideCheck_.c_str()
<< endl; << endl;
} }

View File

@ -84,7 +84,7 @@ Foam::surfaceFeaturesExtraction::extractFromFile::features
Info<< nl << "Reading existing feature edges from file " Info<< nl << "Reading existing feature edges from file "
<< featureEdgeFile_ << nl << featureEdgeFile_ << nl
<< "Selecting edges based purely on geometric tests: " << "Selecting edges based purely on geometric tests: "
<< geometricTestOnly().asText() << endl; << geometricTestOnly().c_str() << endl;
return autoPtr<surfaceFeatures> return autoPtr<surfaceFeatures>
( (

View File

@ -78,7 +78,7 @@ Foam::surfaceFeaturesExtraction::extractFromSurface::features
Info<< nl << "Constructing feature set from included angle " Info<< nl << "Constructing feature set from included angle "
<< includedAngle() << nl << includedAngle() << nl
<< "Selecting edges based purely on geometric tests: " << "Selecting edges based purely on geometric tests: "
<< geometricTestOnly().asText() << endl; << geometricTestOnly().c_str() << endl;
return autoPtr<surfaceFeatures> return autoPtr<surfaceFeatures>
( (

View File

@ -19,9 +19,7 @@ $(fileOps)/collatedFileOperation/OFstreamCollator.C
bools = primitives/bools bools = primitives/bools
$(bools)/bool/bool.C $(bools)/bool/bool.C
$(bools)/bool/boolIO.C
$(bools)/Switch/Switch.C $(bools)/Switch/Switch.C
$(bools)/Switch/SwitchIO.C
$(bools)/lists/boolList.C $(bools)/lists/boolList.C
chars = primitives/chars chars = primitives/chars

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.
@ -26,29 +26,29 @@ License
#include "Switch.H" #include "Switch.H"
#include "error.H" #include "error.H"
#include "dictionary.H" #include "dictionary.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char* Foam::Switch::names[Foam::Switch::INVALID+1] = const char* Foam::Switch::names[9] =
{ {
"false", "true", "false", "true",
"no", "yes", "no", "yes",
"off", "on", "off", "on",
"none", "true", // Is there a reasonable counterpart to "none"? "none", "(unused)",
"invalid" "invalid"
}; };
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
Foam::Switch::switchType Foam::Switch::asEnum Foam::Switch::switchType Foam::Switch::parse
( (
const std::string& str, const std::string& str,
const bool allowInvalid bool allowBad
) )
{ {
const std::string::size_type len = str.size(); switch (str.size())
switch (len)
{ {
case 1: // (f|n|t|y) - single-character forms case 1: // (f|n|t|y) - single-character forms
{ {
@ -86,7 +86,7 @@ Foam::Switch::switchType Foam::Switch::asEnum
} }
} }
if (!allowInvalid) if (!allowBad)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Unknown switch word " << str << nl << "Unknown switch word " << str << nl
@ -108,15 +108,29 @@ Foam::Switch Foam::Switch::lookupOrAddToDict
} }
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::Switch::Switch(Istream& is)
{
is >> *this;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
bool Foam::Switch::valid() const bool Foam::Switch::valid() const noexcept
{ {
return switch_ <= switchType::NONE; 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_]; 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;
}
// ************************************************************************* // // ************************************************************************* //

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.
@ -30,7 +30,6 @@ Description
SourceFiles SourceFiles
Switch.C Switch.C
SwitchIO.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -40,36 +39,7 @@ SourceFiles
#include "bool.H" #include "bool.H"
#include "word.H" #include "word.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Avoid any pre-processor conflicts with enum names
namespace Foam
{
// Forward declaration of friend functions and operators
class Switch;
class dictionary;
Istream& operator>>(Istream& is, Switch& s);
Ostream& operator<<(Ostream& is, const Switch& s);
/*---------------------------------------------------------------------------*\
Class Switch Declaration
\*---------------------------------------------------------------------------*/
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 FALSE
#undef TRUE #undef TRUE
#undef NO #undef NO
@ -77,63 +47,84 @@ public:
#undef OFF #undef OFF
#undef ON #undef ON
#undef NONE #undef NONE
#undef PLACEHOLDER
#undef INVALID
//- The various text representations for a switch value. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// These also correspond to the entries in names.
namespace Foam
{
// Forward declarations
class Switch;
class dictionary;
// IOstream Operators
Istream& operator>>(Istream& is, Switch& sw);
Ostream& operator<<(Ostream& is, const Switch& sw);
/*---------------------------------------------------------------------------*\
Class Switch Declaration
\*---------------------------------------------------------------------------*/
class Switch
{
public:
//- 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 enum switchType
{ {
FALSE = 0, TRUE = 1, FALSE = 0 /*!< "false" */, TRUE = 1 /*!< "true" */,
NO = 2, YES = 3, NO = 2 /*!< "no" */, YES = 3 /*!< "yes" */,
OFF = 4, ON = 5, OFF = 4 /*!< "off" */, ON = 5 /*!< "on" */,
NONE = 6, PLACEHOLDER = 7, NONE = 6 /*!< "none" */,
INVALID 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:
// Private Data
//- The logic and enumerated text representation stored in a byte
unsigned char switch_;
//- The set of names corresponding to the switchType enumeration.
// Includes extra entries for "invalid".
static const char* names[9];
// Static Member Functions // Static Member Functions
//- Return a switchType representation of an input string //- Return enum value for input string
static switchType asEnum static switchType parse(const std::string& str, bool allowBad);
(
const std::string& str,
const bool allowInvalid
);
public: public:
// Constructors // Constructors
//- Construct null as false //- Null constructible as false
Switch() constexpr Switch() noexcept
: :
switch_(switchType::FALSE) switch_(switchType::FALSE)
{} {}
//- Construct from enumerated value //- Construct from enumerated value
Switch(const switchType sw) constexpr Switch(const switchType sw) noexcept
: :
switch_(sw) switch_(sw)
{} {}
//- Construct from bool //- Construct from bool
Switch(const bool b) constexpr Switch(const bool b) noexcept
: :
switch_(b ? switchType::TRUE : switchType::FALSE) switch_(b ? switchType::TRUE : switchType::FALSE)
{} {}
//- Construct from integer values (treats integer as bool value) //- 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) switch_(i ? switchType::TRUE : switchType::FALSE)
{} {}
@ -141,32 +132,35 @@ public:
//- Construct from string - catches bad input. //- Construct from string - catches bad input.
explicit Switch(const std::string& str) explicit Switch(const std::string& str)
: :
switch_(asEnum(str, false)) switch_(parse(str, false))
{} {}
//- Construct from character array - catches bad input. //- Construct from character array - catches bad input.
explicit Switch(const char* str) explicit Switch(const char* str)
: :
switch_(asEnum(std::string(str), false)) switch_(parse(str, false))
{} {}
//- Construct from string. //- Construct from string.
// Optionally allow bad words, and catch the error elsewhere // 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. //- Construct from character array.
// Optionally allow bad words, and catch the error elsewhere // 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 //- Construct from Istream
Switch(Istream& is); Switch(Istream& is);
// Helpers
//- Construct from dictionary, supplying default value so that if the //- Construct from dictionary, supplying default value so that if the
// value is not found, it is added into the dictionary. // value is not found, it is added into the dictionary.
static Switch lookupOrAddToDict static Switch lookupOrAddToDict
@ -180,10 +174,13 @@ public:
// Member Functions // Member Functions
//- Return true if the Switch has a valid value //- Return true if the Switch has a valid value
bool valid() const; bool valid() const noexcept;
//- Return a text representation of the Switch //- A string representation of the Switch value
const char* asText() const; const char* c_str() const noexcept;
//- A string representation of the Switch value
std::string str() const;
//- Update the value of the Switch if it is found in the dictionary //- Update the value of the Switch if it is found in the dictionary
bool readIfPresent(const word& name, const dictionary& dict); bool readIfPresent(const word& name, const dictionary& dict);
@ -192,28 +189,29 @@ public:
// Member Operators // Member Operators
//- Conversion to bool //- Conversion to bool
operator bool() const operator bool() const noexcept
{ {
return (switch_ & 0x1); return (switch_ & 0x1);
} }
//- Assignment to enumerated value //- Assignment from enumerated value
void operator=(const switchType sw) void operator=(const switchType sw) noexcept
{ {
switch_ = sw; switch_ = sw;
} }
//- Assignment to bool //- Assignment from bool
void operator=(const bool b) void operator=(const bool b) noexcept
{ {
switch_ = (b ? Switch::TRUE : Switch::FALSE); switch_ = (b ? Switch::TRUE : Switch::FALSE);
} }
// IOstream Operators // Housekeeping
friend Istream& operator>>(Istream& is, Switch& s); //- A text representation of the Switch value
friend Ostream& operator<<(Ostream& os, const Switch& s); // \deprecated - use c_str() instead (MAR-2018)
inline const char* asText() const { return c_str(); };
}; };

View File

@ -1,95 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //

View File

@ -24,6 +24,9 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "bool.H" #include "bool.H"
#include "Switch.H"
#include "error.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -44,4 +47,34 @@ Foam::pTraits<bool>::pTraits(Istream& is)
is >> p_; 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 >> b;
return b;
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -28,7 +28,7 @@ Description
System bool System bool
SourceFiles SourceFiles
boolIO.C bool.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/

View File

@ -1,63 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 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 "bool.H"
#include "Switch.H"
#include "error.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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)
{
// we could also write as text string without any difficulty
// os << (b ? "true" : "false");
os.write(label(b));
os.check(FUNCTION_NAME);
return os;
}
bool Foam::readBool(Istream& is)
{
bool b;
is >> b;
return b;
}
// ************************************************************************* //

View File

@ -21,9 +21,6 @@ License
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Specialisation of List\<T\> for bool.
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "boolList.H" #include "boolList.H"
@ -34,13 +31,9 @@ Description
namespace Foam namespace Foam
{ {
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
defineCompoundTypeName(List<bool>, boolList); defineCompoundTypeName(List<bool>, boolList);
addCompoundToRunTimeSelectionTable(List<bool>, boolList); addCompoundToRunTimeSelectionTable(List<bool>, boolList);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam } // End namespace Foam
// ************************************************************************* // // ************************************************************************* //

View File

@ -111,7 +111,7 @@ void Foam::polyMeshFilterSettings::writeSettings(Ostream& os) const
<< endl; << endl;
} }
os << "Control mesh quality = " << controlMeshQuality().asText() << endl; os << "Control mesh quality = " << controlMeshQuality().c_str() << endl;
if (controlMeshQuality()) if (controlMeshQuality())
{ {