ENH: add isAOp, isTypeOp functors

- can be used in predicate matching

- getNameOp, getTypeOp for accessing the name() and type() of objects
This commit is contained in:
Mark Olesen
2018-08-03 15:19:22 +02:00
parent 319e09e32f
commit 91e2fc4dc7
2 changed files with 125 additions and 65 deletions

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 | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -59,7 +59,7 @@ Description
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// declarations (for use in header files) // Declarations (for use in header files)
//- Declare a ClassNameNoDebug() with extra virtual type info //- Declare a ClassNameNoDebug() with extra virtual type info
#define TypeNameNoDebug(TypeNameString) \ #define TypeNameNoDebug(TypeNameString) \
@ -80,7 +80,7 @@ namespace Foam
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- Reference type cast template function, //- Reference type cast template function,
// wraps dynamic_cast to handle bad_cast exception and generate a FatalError. //- wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
template<class To, class From> template<class To, class From>
inline To& dynamicCast(From& r) inline To& dynamicCast(From& r)
{ {
@ -101,7 +101,7 @@ inline To& dynamicCast(From& r)
//- Reference type cast template function, //- Reference type cast template function,
// wraps dynamic_cast to handle bad_cast exception and generate a FatalError. //- wraps dynamic_cast to handle bad_cast exception and generate a FatalError.
template<class To, class From> template<class To, class From>
inline To& dynamicCast(From& r, const dictionary& d) inline To& dynamicCast(From& r, const dictionary& d)
{ {
@ -122,7 +122,8 @@ inline To& dynamicCast(From& r, const dictionary& d)
//- Reference type cast template function. //- Reference type cast template function.
// As per dynamicCast, but handles type names via the virtual type() method. // As per dynamicCast, but handles type names for the error messages
// via the virtual type() method.
template<class To, class From> template<class To, class From>
inline To& refCast(From& r) inline To& refCast(From& r)
{ {
@ -143,7 +144,8 @@ inline To& refCast(From& r)
//- Reference type cast template function. //- Reference type cast template function.
// As per dynamicCast, but handles type names via the virtual type() method. // As per dynamicCast, but handles type names for the error messages
// via the virtual type() method.
template<class To, class From> template<class To, class From>
inline To& refCast(From& r, const dictionary& d) inline To& refCast(From& r, const dictionary& d)
{ {
@ -163,23 +165,47 @@ inline To& refCast(From& r, const dictionary& d)
} }
//- Check the typeid //- Check if dynamic_cast to TargetType is possible
template<class TestType, class Type> template<class TargetType, class Type>
inline bool isType(const Type& t)
{
return typeid(t) == typeid(TestType);
}
//- Check if a dynamic_cast to typeid is possible
template<class TestType, class Type>
inline bool isA(const Type& t) inline bool isA(const Type& t)
{ {
const Type* tPtr = &t; const Type* p = &t;
return dynamic_cast<const TestType*>(tPtr); return dynamic_cast<const TargetType*>(p);
} }
//- Check if dynamic_cast to TargetType is possible, as a functor
template<class TargetType>
struct isAOp
{
template<class Type>
inline bool operator()(const Type& t) const
{
return isA<TargetType,Type>(t);
}
};
//- Check is typeid is identical to the TargetType
template<class TargetType, class Type>
inline bool isType(const Type& t)
{
return typeid(TargetType) == typeid(t);
}
//- Check is typeid is identical to the TargetType, as a functor
template<class TargetType>
struct isTypeOp
{
template<class Type>
inline bool operator()(const Type& t) const
{
return isType<TargetType,Type>(t);
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam } // End namespace Foam

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 | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -21,11 +21,12 @@ 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/>.
InClass InNamespace
Foam::Pstream Foam
Description Description
Combination-Reduction operation for a parallel run. Various binary and unary operations, which can be used for example,
for parallel combine-reduce operations.
The information from all nodes is collected on the master node, The information from all nodes is collected on the master node,
combined using the given combination function and the result is combined using the given combination function and the result is
@ -43,29 +44,30 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Assignment operation taking two parameters, returning void.
// Alters the value of the first parameter.
// Eg, plusEqOp for (x += y)
#define EqOp(opName, op) \ #define EqOp(opName, op) \
\ \
template<class T1, class T2> \ template<class T1, class T2> \
class opName##Op2 \ struct opName##Op2 \
{ \ { \
public: \
\
void operator()(T1& x, const T2& y) const \ void operator()(T1& x, const T2& y) const \
{ \ { \
op; \ op; \
} \ } \
}; \ }; \
\
template<class T> \
class opName##Op \
{ \
public: \
\ \
template<class T> \
struct opName##Op \
{ \
void operator()(T& x, const T& y) const \ void operator()(T& x, const T& y) const \
{ \ { \
op; \ op; \
} \ } \
}; };
EqOp(eq, x = y) EqOp(eq, x = y)
EqOp(plusEq, x += y) EqOp(plusEq, x += y)
@ -91,19 +93,22 @@ EqOp(nopEq, (void)x)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Warning about unused result
#if __GNUC__ #if __GNUC__
#define WARNRETURN __attribute__((warn_unused_result)) #define WARNRETURN __attribute__((warn_unused_result))
#else #else
#define WARNRETURN #define WARNRETURN
#endif #endif
// Operation taking two parameters, returning the first type.
// Neither parameter is altered.
// Eg, plusOp for (x + y)
#define Op(opName, op) \ #define Op(opName, op) \
\ \
template<class T, class T1, class T2> \ template<class T, class T1, class T2> \
class opName##Op3 \ struct opName##Op3 \
{ \ { \
public: \
\
T operator()(const T1& x, const T2& y) const WARNRETURN \ T operator()(const T1& x, const T2& y) const WARNRETURN \
{ \ { \
return op; \ return op; \
@ -111,10 +116,8 @@ EqOp(nopEq, (void)x)
}; \ }; \
\ \
template<class T1, class T2> \ template<class T1, class T2> \
class opName##Op2 \ struct opName##Op2 \
{ \ { \
public: \
\
T1 operator()(const T1& x, const T2& y) const WARNRETURN \ T1 operator()(const T1& x, const T2& y) const WARNRETURN \
{ \ { \
return op; \ return op; \
@ -122,10 +125,8 @@ EqOp(nopEq, (void)x)
}; \ }; \
\ \
template<class T> \ template<class T> \
class opName##Op \ struct opName##Op \
{ \ { \
public: \
\
T operator()(const T& x, const T& y) const WARNRETURN \ T operator()(const T& x, const T& y) const WARNRETURN \
{ \ { \
return op; \ return op; \
@ -133,9 +134,11 @@ EqOp(nopEq, (void)x)
}; };
// ...
#define weightedOp(opName, op) \ #define weightedOp(opName, op) \
\ \
template<class Type, class CombineOp> \ template<class T, class CombineOp> \
class opName##WeightedOp \ class opName##WeightedOp \
{ \ { \
const CombineOp& cop_; \ const CombineOp& cop_; \
@ -149,9 +152,9 @@ EqOp(nopEq, (void)x)
\ \
void operator() \ void operator() \
( \ ( \
Type& x, \ T& x, \
const label index, \ const label index, \
const Type& y, \ const T& y, \
const scalar weight \ const scalar weight \
) const \ ) const \
{ \ { \
@ -187,6 +190,37 @@ weightedOp(multiply, (weight*y))
#undef Op #undef Op
#undef weightedOp #undef weightedOp
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- General get operation to extract the 'name' from an object as a word.
// The default implementation uses the 'name()' method commonly used within
// OpenFOAM.
template<class T>
struct getNameOp
{
word operator()(const T& x) const WARNRETURN
{
return x.name();
}
};
//- General get operation to extract the 'type' from an object as a word.
// The default implementation uses the 'type()' method commonly used within
// OpenFOAM.
template<class T>
struct getTypeOp
{
word operator()(const T& x) const WARNRETURN
{
return x.type();
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#undef WARNRETURN #undef WARNRETURN
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //