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 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
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
#define TypeNameNoDebug(TypeNameString) \
......@@ -80,7 +80,7 @@ namespace Foam
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
//- 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>
inline To& dynamicCast(From& r)
{
......@@ -101,7 +101,7 @@ inline To& dynamicCast(From& r)
//- 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>
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.
// 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>
inline To& refCast(From& r)
{
......@@ -143,7 +144,8 @@ inline To& refCast(From& r)
//- 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>
inline To& refCast(From& r, const dictionary& d)
{
......@@ -163,23 +165,47 @@ inline To& refCast(From& r, const dictionary& d)
}
//- Check the typeid
template<class TestType, class Type>
inline bool isType(const Type& t)
//- Check if dynamic_cast to TargetType is possible
template<class TargetType, class Type>
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
template<class TestType, class Type>
inline bool isA(const Type& t)
//- 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)
{
const Type* tPtr = &t;
return dynamic_cast<const TestType*>(tPtr);
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
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -21,11 +21,12 @@ License
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
InClass
Foam::Pstream
InNamespace
Foam
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,
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) \
\
template<class T1, class T2> \
class opName##Op2 \
{ \
public: \
\
template<class T1, class T2> \
struct opName##Op2 \
{ \
void operator()(T1& x, const T2& y) const \
{ \
op; \
} \
}; \
\
template<class T> \
class opName##Op \
{ \
public: \
}; \
\
template<class T> \
struct opName##Op \
{ \
void operator()(T& x, const T& y) const \
{ \
op; \
} \
};
};
EqOp(eq, x = y)
EqOp(plusEq, x += y)
......@@ -91,19 +93,22 @@ EqOp(nopEq, (void)x)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Warning about unused result
#if __GNUC__
#define WARNRETURN __attribute__((warn_unused_result))
#else
#define WARNRETURN
#endif
// Operation taking two parameters, returning the first type.
// Neither parameter is altered.
// Eg, plusOp for (x + y)
#define Op(opName, op) \
\
template<class T, class T1, class T2> \
class opName##Op3 \
struct opName##Op3 \
{ \
public: \
\
T operator()(const T1& x, const T2& y) const WARNRETURN \
{ \
return op; \
......@@ -111,10 +116,8 @@ EqOp(nopEq, (void)x)
}; \
\
template<class T1, class T2> \
class opName##Op2 \
struct opName##Op2 \
{ \
public: \
\
T1 operator()(const T1& x, const T2& y) const WARNRETURN \
{ \
return op; \
......@@ -122,10 +125,8 @@ EqOp(nopEq, (void)x)
}; \
\
template<class T> \
class opName##Op \
struct opName##Op \
{ \
public: \
\
T operator()(const T& x, const T& y) const WARNRETURN \
{ \
return op; \
......@@ -133,9 +134,11 @@ EqOp(nopEq, (void)x)
};
// ...
#define weightedOp(opName, op) \
\
template<class Type, class CombineOp> \
template<class T, class CombineOp> \
class opName##WeightedOp \
{ \
const CombineOp& cop_; \
......@@ -149,9 +152,9 @@ EqOp(nopEq, (void)x)
\
void operator() \
( \
Type& x, \
T& x, \
const label index, \
const Type& y, \
const T& y, \
const scalar weight \
) const \
{ \
......@@ -187,6 +190,37 @@ weightedOp(multiply, (weight*y))
#undef Op
#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
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
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