diff --git a/applications/test/predicates/Make/files b/applications/test/predicates/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..6197f71dfaf0cd13fd79cd56683dc968c3e47a8f --- /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 0000000000000000000000000000000000000000..9d6f459ad89282e21723bb39b2c290797efb0edb --- /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 0000000000000000000000000000000000000000..9b9e77ddbb303a86988a472da1277f898d2f481c --- /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 <http://www.gnu.org/licenses/>. + +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<class ListType, class UnaryPredicate> +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 f3ab4a37197833235dbf003d1559508f055fc844..be1af08a5497ecf699bc0e539cc57c6501146620 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 25b79905ac78e3dbb218daf7d030f7911bdba920..63ef5ead5276c2a5516b82a251dfe2d72b8f123e 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 e716820d5f0f0c015d0025421f97e1f0d386282b..b0bda84bebb12d3dae2903d1388fc8d3d65b08e2 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 19d8fb63de0805d39b462ca061241f2e55f04a07..b3cbe5567ca6995781cb9de951dfebf03366398b 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<bool> +// Template specialisation for pTraits<bool> template<> class pTraits<bool> { @@ -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 60518849cfa5381e527cdc6a3b7714e23951df12..f098a3e1864e74bbebc61ca0247a29cc73cd6a44 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 <http://www.gnu.org/licenses/>. -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 b382c20aee4101084af51d96b1fa37eb3abcee09..8f8c8c27277ca3e7bffc32e81fc38cb529542b51 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 f8a8812a01c722b4e8a4ecaa39c42ec89731beb2..e73351593329d81092cb9abe93248bff8d81064a 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 0000000000000000000000000000000000000000..8731fb451899bfc348202d340d0c9d71a608c006 --- /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 <http://www.gnu.org/licenses/>. + +Namespace + Foam::predicates + +Description + Various constant predicate types. + +SourceFiles + predicates.H + +\*---------------------------------------------------------------------------*/ + +#ifndef predicates_H +#define predicates_H + +#include <string> + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +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<class T> + inline bool operator()(const T&) const + { + return true; + } + + //- Binary predicate returning false + template<class T1, class T2> + 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<class T> + inline bool operator()(const T&) const + { + return false; + } + + //- Binary predicate returning false + template<class T1, class T2> + 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 + +// ************************************************************************* //