Commit 8d018e79 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

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'.
parent a8d2ebf2
Test-predicates.C
EXE = $(FOAM_USER_APPBIN)/Test-predicates
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //
......@@ -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;
......
......@@ -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);
};
......
......@@ -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;
}
......
......@@ -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
......
......@@ -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;
}
......
......@@ -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);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -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;
}
......
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
// ************************************************************************* //
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment