Commit 91e2fc4d authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add isAOp, isTypeOp functors

- can be used in predicate matching

- getNameOp, getTypeOp for accessing the name() and type() of objects
parent 319e09e3
...@@ -3,7 +3,7 @@ ...@@ -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 ...@@ -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 ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) inline bool isA(const Type& t)
{ {
return typeid(t) == typeid(TestType); const Type* p = &t;
return dynamic_cast<const TargetType*>(p);
} }
//- Check if a dynamic_cast to typeid is possible //- Check if dynamic_cast to TargetType is possible, as a functor
template<class TestType, class Type> template<class TargetType>
inline bool isA(const Type& t) 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)
{ {
const Type* tPtr = &t; return typeid(TargetType) == typeid(t);
return dynamic_cast<const TestType*>(tPtr);
} }
//- 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
......
...@@ -3,7 +3,7 @@ ...@@ -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 ...@@ -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 ...@@ -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 \
{ \ { \
op; \ void operator()(T1& x, const T2& y) const \
} \ { \
}; \ op; \
\ } \
template<class T> \ }; \
class opName##Op \
{ \
public: \
\ \
void operator()(T& x, const T& y) const \ template<class T> \
struct opName##Op \
{ \ { \
op; \ void operator()(T& x, const T& y) const \
} \ { \
}; 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) ...@@ -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) ...@@ -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) ...@@ -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,30 +134,32 @@ EqOp(nopEq, (void)x) ...@@ -133,30 +134,32 @@ 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_; \
\ \
public: \ public: \
\ \
opName##WeightedOp(const CombineOp& cop) \ opName##WeightedOp(const CombineOp& cop) \
: \ : \
cop_(cop) \ cop_(cop) \
{} \ {} \
\ \
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 \
{ \ { \
cop_(x, op); \ cop_(x, op); \
} \ } \
}; \ }; \
...@@ -187,6 +190,37 @@ weightedOp(multiply, (weight*y)) ...@@ -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
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
Markdown is supported
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