From 8d018e7950b82f2959bfc2b1c27e85c8f831b48f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 17 May 2017 10:18:14 +0200 Subject: [PATCH] ENH: added constant predicates - predicates::always and predicates::never returning true and false, respectively. These simple classes make it easier when writing templated code. As well as unary and binary predicate forms, they also contain a match(std::string) method for compatibility with regex-based classes. STYLE: write bool and direction as primitive 'int' not as 'label'. --- applications/test/predicates/Make/files | 3 + applications/test/predicates/Make/options | 1 + .../test/predicates/Test-predicates.C | 112 ++++++++++++++ applications/test/wordRe/Test-wordRe.C | 2 + src/OpenFOAM/primitives/bools/Switch/Switch.H | 22 +-- .../primitives/bools/Switch/SwitchIO.C | 7 +- src/OpenFOAM/primitives/bools/bool/bool.H | 12 +- src/OpenFOAM/primitives/bools/bool/boolIO.C | 12 +- src/OpenFOAM/primitives/direction/direction.H | 8 +- .../primitives/direction/directionIO.C | 8 +- .../primitives/predicates/predicates.H | 142 ++++++++++++++++++ 11 files changed, 291 insertions(+), 38 deletions(-) create mode 100644 applications/test/predicates/Make/files create mode 100644 applications/test/predicates/Make/options create mode 100644 applications/test/predicates/Test-predicates.C create mode 100644 src/OpenFOAM/primitives/predicates/predicates.H diff --git a/applications/test/predicates/Make/files b/applications/test/predicates/Make/files new file mode 100644 index 0000000000..6197f71dfa --- /dev/null +++ b/applications/test/predicates/Make/files @@ -0,0 +1,3 @@ +Test-predicates.C + +EXE = $(FOAM_USER_APPBIN)/Test-predicates diff --git a/applications/test/predicates/Make/options b/applications/test/predicates/Make/options new file mode 100644 index 0000000000..9d6f459ad8 --- /dev/null +++ b/applications/test/predicates/Make/options @@ -0,0 +1 @@ +/**/ diff --git a/applications/test/predicates/Test-predicates.C b/applications/test/predicates/Test-predicates.C new file mode 100644 index 0000000000..9b9e77ddbb --- /dev/null +++ b/applications/test/predicates/Test-predicates.C @@ -0,0 +1,112 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\/ 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 . + +Application + Test-predicates + +Description + Simple tests using predicates + +\*---------------------------------------------------------------------------*/ + +#include "IOstreams.H" +#include "labelList.H" +#include "wordList.H" +#include "predicates.H" +#include "FlatOutput.H" +#include "regExp.H" + +using namespace Foam; + + +template +label printMatching(const ListType& list, const UnaryPredicate& pred) +{ + label count = 0; + + Info<< "("; + + for (const auto& val : list) + { + if (pred(val)) + { + if (count) Info<< ' '; + Info<< val; + ++count; + } + } + + Info<< ") => " << count << nl; + + return count; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + wordList words + { + "abc", + "def", + "hij", + "abc_", + "def_", + "hij_", + }; + + labelRange range(-10, 40); + labelList values(range.begin(), range.end()); + + Info<<"words: " << flatOutput(words) << endl; + Info<<"values: " << flatOutput(values) << endl; + + regExp matcher(".*_.*"); + + Info<<"With '_': "; + printMatching(words, matcher); + + Info<<"All: "; + printMatching(words, predicates::always()); + + Info<<"None: "; + printMatching(words, predicates::never()); + + Info<<"Neg values: "; + printMatching(values, [](const label v) { return v < 0; }); + + Info<<"Even values: "; + printMatching(values, [](const label v) { return !(v % 2); }); + + Info<<"All: "; + printMatching(values, predicates::always()); + + Info<<"None: "; + printMatching(values, predicates::never()); + + return 0; +} + +// ************************************************************************* // diff --git a/applications/test/wordRe/Test-wordRe.C b/applications/test/wordRe/Test-wordRe.C index f3ab4a3719..be1af08a54 100644 --- a/applications/test/wordRe/Test-wordRe.C +++ b/applications/test/wordRe/Test-wordRe.C @@ -33,6 +33,7 @@ Description #include "keyType.H" #include "wordRe.H" #include "wordRes.H" +#include "predicates.H" using namespace Foam; @@ -62,6 +63,7 @@ int main(int argc, char *argv[]) Info<< "match xyz: " << wrelist("xyz") << endl; Info<< "match zyx: " << wrelist("zyx") << endl; Info<< "match xyz: " << wrelist.match("xyz") << endl; + Info<< "match any: " << predicates::always()("any junk") << endl; Info<< "keyre match: " << keyre("xyz") << endl; Info<< "string match: " << string("this").match("xyz") << endl; Info<< "string match: " << string("x.*")("xyz") << endl; diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index 25b79905ac..63ef5ead52 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -50,8 +50,8 @@ namespace Foam class Switch; class dictionary; -Istream& operator>>(Istream&, Switch&); -Ostream& operator<<(Ostream&, const Switch&); +Istream& operator>>(Istream& is, Switch& s); +Ostream& operator<<(Ostream& is, const Switch& s); /*---------------------------------------------------------------------------*\ @@ -108,8 +108,12 @@ private: // Static Member Functions - //- Return a switchType representation of a word - static switchType asEnum(const std::string&, const bool allowInvalid); + //- Return a switchType representation of an input string + static switchType asEnum + ( + const std::string& str, + const bool allowInvalid + ); public: @@ -161,8 +165,8 @@ public: // value is not found, it is added into the dictionary. static Switch lookupOrAddToDict ( - const word&, - dictionary&, + const word& name, + dictionary& dict, const Switch& defaultValue = false ); @@ -176,7 +180,7 @@ public: const char* asText() const; //- Update the value of the Switch if it is found in the dictionary - bool readIfPresent(const word&, const dictionary&); + bool readIfPresent(const word& name, const dictionary& dict); // Member Operators @@ -202,8 +206,8 @@ public: // IOstream Operators - friend Istream& operator>>(Istream&, Switch&); - friend Ostream& operator<<(Ostream&, const Switch&); + friend Istream& operator>>(Istream& is, Switch& s); + friend Ostream& operator<<(Ostream& os, const Switch& s); }; diff --git a/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C b/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C index e716820d5f..b0bda84beb 100644 --- a/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C +++ b/src/OpenFOAM/primitives/bools/Switch/SwitchIO.C @@ -79,10 +79,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& s) return is; } - - // Check state of Istream - is.check("Istream& operator>>(Istream&, Switch&)"); - + is.check(FUNCTION_NAME); return is; } @@ -90,7 +87,7 @@ Foam::Istream& Foam::operator>>(Istream& is, Switch& s) Foam::Ostream& Foam::operator<<(Ostream& os, const Switch& s) { os << Switch::names[s.switch_]; - os.check("Ostream& operator<<(Ostream&, const Switch&)"); + os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/primitives/bools/bool/bool.H b/src/OpenFOAM/primitives/bools/bool/bool.H index 19d8fb63de..b3cbe5567c 100644 --- a/src/OpenFOAM/primitives/bools/bool/bool.H +++ b/src/OpenFOAM/primitives/bools/bool/bool.H @@ -45,10 +45,10 @@ class Ostream; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -Istream& operator>>(Istream&, bool&); -Ostream& operator<<(Ostream&, const bool); +Istream& operator>>(Istream& is, bool& b); +Ostream& operator<<(Ostream& os, const bool b); -bool readBool(Istream&); +bool readBool(Istream& is); } // End namespace Foam @@ -61,7 +61,7 @@ bool readBool(Istream&); namespace Foam { -// template specialisation for pTraits +// Template specialisation for pTraits template<> class pTraits { @@ -95,10 +95,10 @@ public: // Constructors //- Construct from primitive - explicit pTraits(const bool&); + explicit pTraits(const bool& p); //- Construct from Istream - pTraits(Istream&); + pTraits(Istream& is); // Member Functions diff --git a/src/OpenFOAM/primitives/bools/bool/boolIO.C b/src/OpenFOAM/primitives/bools/bool/boolIO.C index 60518849cf..f098a3e186 100644 --- a/src/OpenFOAM/primitives/bools/bool/boolIO.C +++ b/src/OpenFOAM/primitives/bools/bool/boolIO.C @@ -21,17 +21,11 @@ License You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . -Description - Reads an bool from an input stream, for a given version number and file - format. If an ASCII file is being read, then the line numbers are counted - and an erroneous read is reported. - \*---------------------------------------------------------------------------*/ -#include "error.H" - #include "bool.H" #include "Switch.H" +#include "error.H" #include "IOstreams.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -51,8 +45,8 @@ 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("Ostream& operator<<(Ostream&, const bool)"); + os.write(int(b)); + os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/primitives/direction/direction.H b/src/OpenFOAM/primitives/direction/direction.H index b382c20aee..8f8c8c2727 100644 --- a/src/OpenFOAM/primitives/direction/direction.H +++ b/src/OpenFOAM/primitives/direction/direction.H @@ -48,10 +48,10 @@ class Ostream; typedef uint8_t direction; -direction readDirection(Istream&); -Istream& operator>>(Istream&, direction&); -Ostream& operator<<(Ostream&, const direction); -std::ostream& operator<<(std::ostream&, const direction); +direction readDirection(Istream& is); +Istream& operator>>(Istream& is, direction& d); +Ostream& operator<<(Ostream& os, const direction d); +std::ostream& operator<<(std::ostream& os, const direction d); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/direction/directionIO.C b/src/OpenFOAM/primitives/direction/directionIO.C index f8a8812a01..e733515933 100644 --- a/src/OpenFOAM/primitives/direction/directionIO.C +++ b/src/OpenFOAM/primitives/direction/directionIO.C @@ -61,17 +61,15 @@ Foam::Istream& Foam::operator>>(Istream& is, direction& d) return is; } - // Check state of Istream - is.check("Istream& operator>>(Istream&, direction&)"); - + is.check(FUNCTION_NAME); return is; } Foam::Ostream& Foam::operator<<(Ostream& os, const direction d) { - os.write(label(d)); - os.check("Ostream& operator<<(Ostream&, const direction)"); + os.write(int(d)); + os.check(FUNCTION_NAME); return os; } diff --git a/src/OpenFOAM/primitives/predicates/predicates.H b/src/OpenFOAM/primitives/predicates/predicates.H new file mode 100644 index 0000000000..8731fb4518 --- /dev/null +++ b/src/OpenFOAM/primitives/predicates/predicates.H @@ -0,0 +1,142 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 OpenCFD Ltd. + \\/ 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 . + +Namespace + Foam::predicates + +Description + Various constant predicate types. + +SourceFiles + predicates.H + +\*---------------------------------------------------------------------------*/ + +#ifndef predicates_H +#define predicates_H + +#include + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +namespace predicates +{ + +/*---------------------------------------------------------------------------*\ + Class always Declaration +\*---------------------------------------------------------------------------*/ + +//- Unary and binary predicates returning true, useful for templating. +class always +{ +public: + typedef always value_type; + + //- Construct null + inline always() + {} + + //- Evalulated as a bool - return true + inline operator bool() const + { + return true; + } + + //- Unary predicate returning true + template + inline bool operator()(const T&) const + { + return true; + } + + //- Binary predicate returning false + template + inline bool operator()(const T1&, const T2&) const + { + return true; + } + + //- String matching returning true + inline bool match(const std::string& unused, bool literal=false) const + { + return true; + } +}; + + +/*---------------------------------------------------------------------------*\ + Class never Declaration +\*---------------------------------------------------------------------------*/ + +//- Unary and binary predicates returning false, useful for templating. +class never +{ +public: + typedef never value_type; + + //- Construct null + inline never() + {} + + //- Evalulated as a bool - return false + inline operator bool() const + { + return false; + } + + //- Unary predicate returning false + template + inline bool operator()(const T&) const + { + return false; + } + + //- Binary predicate returning false + template + inline bool operator()(const T1&, const T2&) const + { + return false; + } + + //- String matching returning false + inline bool match(const std::string& unused, bool literal=false) const + { + return false; + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace predicates + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* //