diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/controlMeshRefinement/controlMeshRefinement.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/controlMeshRefinement/controlMeshRefinement.C index 79e1531468..a3ca84da2c 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/controlMeshRefinement/controlMeshRefinement.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/controlMeshRefinement/controlMeshRefinement.C @@ -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 extraPts; DynamicList extraSizes; diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformalVoronoiMesh/featurePointConformer/featurePointConformer.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformalVoronoiMesh/featurePointConformer/featurePointConformer.C index 0e07acfebd..a546d0c834 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformalVoronoiMesh/featurePointConformer/featurePointConformer.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/conformalVoronoiMesh/featurePointConformer/featurePointConformer.C @@ -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; diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/initialPointsMethod/pointFile/pointFile.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/initialPointsMethod/pointFile/pointFile.C index a09153cf20..49728805b2 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/initialPointsMethod/pointFile/pointFile.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/initialPointsMethod/pointFile/pointFile.C @@ -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; } diff --git a/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromFile.C b/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromFile.C index 7b2b0c8ad0..5dd577420f 100644 --- a/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromFile.C +++ b/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromFile.C @@ -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 ( diff --git a/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromSurface.C b/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromSurface.C index 35b5dc0e3f..f5a0354b62 100644 --- a/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromSurface.C +++ b/applications/utilities/surface/surfaceFeatureExtract/extractionMethod/extractFromSurface.C @@ -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 ( diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 5d2dc3f178..ae8af888bf 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -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 diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.C b/src/OpenFOAM/primitives/bools/Switch/Switch.C index 9ddb0d95c1..e40b62f10b 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.C +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C @@ -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; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index 256944b936..f2ef0d5439 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -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 - }; - - // Static data members - - //- The set of names corresponding to the switchType enumeration - // Includes an extra entry for "invalid". - static const char* names[INVALID+1]; + //- 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" */ + }; 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 - //- Return a switchType representation of an input string - static switchType asEnum - ( - const std::string& str, - const bool allowInvalid - ); - + //- 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; - //- Return a text representation of the Switch - const char* asText() const; + //- A string representation of the Switch value + 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 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(); }; }; diff --git a/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C b/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C deleted file mode 100644 index b0bda84beb..0000000000 --- a/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C +++ /dev/null @@ -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 . - -\*---------------------------------------------------------------------------*/ - -#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; -} - - -// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/bools/bool/bool.C b/src/OpenFOAM/primitives/bools/bool/bool.C index 267a8c1611..7dc08dcbaa 100644 --- a/src/OpenFOAM/primitives/bools/bool/bool.C +++ b/src/OpenFOAM/primitives/bools/bool/bool.C @@ -24,6 +24,9 @@ License \*---------------------------------------------------------------------------*/ #include "bool.H" +#include "Switch.H" +#include "error.H" +#include "IOstreams.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -44,4 +47,34 @@ Foam::pTraits::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 >> b; + + return b; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/bools/bool/bool.H b/src/OpenFOAM/primitives/bools/bool/bool.H index b3cbe5567c..55ac2cfbff 100644 --- a/src/OpenFOAM/primitives/bools/bool/bool.H +++ b/src/OpenFOAM/primitives/bools/bool/bool.H @@ -28,7 +28,7 @@ Description System bool SourceFiles - boolIO.C + bool.C \*---------------------------------------------------------------------------*/ diff --git a/src/OpenFOAM/primitives/bools/bool/boolIO.C b/src/OpenFOAM/primitives/bools/bool/boolIO.C deleted file mode 100644 index 8f19c58bf3..0000000000 --- a/src/OpenFOAM/primitives/bools/bool/boolIO.C +++ /dev/null @@ -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 . - -\*---------------------------------------------------------------------------*/ - -#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; -} - - -// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/bools/lists/boolList.C b/src/OpenFOAM/primitives/bools/lists/boolList.C index 93332d7879..37fb536f85 100644 --- a/src/OpenFOAM/primitives/bools/lists/boolList.C +++ b/src/OpenFOAM/primitives/bools/lists/boolList.C @@ -21,9 +21,6 @@ License You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . -Description - Specialisation of List\ for bool. - \*---------------------------------------------------------------------------*/ #include "boolList.H" @@ -34,13 +31,9 @@ Description namespace Foam { -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - defineCompoundTypeName(List, boolList); addCompoundToRunTimeSelectionTable(List, boolList); -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - } // End namespace Foam // ************************************************************************* // diff --git a/src/dynamicMesh/polyMeshFilter/polyMeshFilterSettings.C b/src/dynamicMesh/polyMeshFilter/polyMeshFilterSettings.C index fd3076d26b..74082b280f 100644 --- a/src/dynamicMesh/polyMeshFilter/polyMeshFilterSettings.C +++ b/src/dynamicMesh/polyMeshFilter/polyMeshFilterSettings.C @@ -111,7 +111,7 @@ void Foam::polyMeshFilterSettings::writeSettings(Ostream& os) const << endl; } - os << "Control mesh quality = " << controlMeshQuality().asText() << endl; + os << "Control mesh quality = " << controlMeshQuality().c_str() << endl; if (controlMeshQuality()) {