diff --git a/src/OpenFOAM/db/typeInfo/typeInfo.H b/src/OpenFOAM/db/typeInfo/typeInfo.H index f0b1af2965..95d6c79c7f 100644 --- a/src/OpenFOAM/db/typeInfo/typeInfo.H +++ b/src/OpenFOAM/db/typeInfo/typeInfo.H @@ -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 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 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 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 inline To& refCast(From& r, const dictionary& d) { @@ -163,23 +165,47 @@ inline To& refCast(From& r, const dictionary& d) } -//- Check the typeid -template -inline bool isType(const Type& t) -{ - return typeid(t) == typeid(TestType); -} - - -//- Check if a dynamic_cast to typeid is possible -template +//- Check if dynamic_cast to TargetType is possible +template inline bool isA(const Type& t) { - const Type* tPtr = &t; - return dynamic_cast(tPtr); + const Type* p = &t; + return dynamic_cast(p); } +//- Check if dynamic_cast to TargetType is possible, as a functor +template +struct isAOp +{ + template + inline bool operator()(const Type& t) const + { + return isA(t); + } +}; + + +//- Check is typeid is identical to the TargetType +template +inline bool isType(const Type& t) +{ + return typeid(TargetType) == typeid(t); +} + + +//- Check is typeid is identical to the TargetType, as a functor +template +struct isTypeOp +{ + template + inline bool operator()(const Type& t) const + { + return isType(t); + } +}; + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/primitives/ops/ops.H b/src/OpenFOAM/primitives/ops/ops.H index 664947870e..8f86cdd8d1 100644 --- a/src/OpenFOAM/primitives/ops/ops.H +++ b/src/OpenFOAM/primitives/ops/ops.H @@ -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 . -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 opName##Op2 \ -{ \ -public: \ - \ - void operator()(T1& x, const T2& y) const \ + template \ + struct opName##Op2 \ { \ - op; \ - } \ -}; \ + void operator()(T1& x, const T2& y) const \ + { \ + op; \ + } \ + }; \ \ -template \ -class opName##Op \ -{ \ -public: \ - \ - void operator()(T& x, const T& y) const \ + template \ + struct opName##Op \ { \ - 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 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 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 opName##Op \ + struct opName##Op \ { \ - public: \ - \ T operator()(const T& x, const T& y) const WARNRETURN \ { \ return op; \ @@ -133,30 +134,32 @@ EqOp(nopEq, (void)x) }; +// ... + #define weightedOp(opName, op) \ \ - template \ + template \ class opName##WeightedOp \ { \ const CombineOp& cop_; \ \ - public: \ + public: \ \ - opName##WeightedOp(const CombineOp& cop) \ - : \ - cop_(cop) \ - {} \ + opName##WeightedOp(const CombineOp& cop) \ + : \ + cop_(cop) \ + {} \ \ - void operator() \ - ( \ - Type& x, \ - const label index, \ - const Type& y, \ - const scalar weight \ - ) const \ - { \ - cop_(x, op); \ - } \ + void operator() \ + ( \ + T& x, \ + const label index, \ + const T& y, \ + const scalar weight \ + ) const \ + { \ + cop_(x, op); \ + } \ }; \ @@ -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 +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 +struct getTypeOp +{ + word operator()(const T& x) const WARNRETURN + { + return x.type(); + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + #undef WARNRETURN // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //