From ee00d7fb606b88b0463d67cf76d7a1d19cfb4f22 Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Fri, 9 Jun 2006 12:58:44 +0000 Subject: [PATCH] simplify Functor::operator== implementation. Thanks to Eric Beyeler git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@676 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/Functor.h | 79 +++++++++--------------------------------- 1 file changed, 16 insertions(+), 63 deletions(-) diff --git a/include/loki/Functor.h b/include/loki/Functor.h index 6e363b4..e9f4165 100644 --- a/include/loki/Functor.h +++ b/include/loki/Functor.h @@ -38,7 +38,6 @@ //#define LOKI_FUNCTORS_ARE_COMPARABLE #endif -//#define LOKI_FUNCTOR_DEBUG /// \namespace Loki /// All classes of Loki are in the Loki namespace @@ -92,11 +91,7 @@ namespace Loki static U* Clone(U* pObj) { if (!pObj) return 0; -#ifdef LOKI_FUNCTOR_DEBUG - U* pClone = dynamic_cast(pObj->DoClone()); -#else U* pClone = static_cast(pObj->DoClone()); -#endif assert(typeid(*pClone) == typeid(*pObj)); return pClone; } @@ -105,10 +100,7 @@ namespace Loki #ifdef LOKI_FUNCTORS_ARE_COMPARABLE virtual bool operator==(const FunctorImplBase&) const = 0; - - // there is no static information if Functor holds a member function - // or a free function; this is the main difference to tr1::function - virtual bool isMemberFuncPtr() const = 0; + #endif }; @@ -961,20 +953,15 @@ namespace Loki #ifdef LOKI_FUNCTORS_ARE_COMPARABLE - bool isMemberFuncPtr() const - { - return false; - } bool operator==(const typename Base::FunctorImplBaseType& rhs) const { - if( rhs.isMemberFuncPtr() ) - return false; // cannot be equal -#ifdef LOKI_FUNCTOR_DEBUG - const FunctorHandler& fh = dynamic_cast(rhs); -#else + // there is no static information if Functor holds a member function + // or a free function; this is the main difference to tr1::function + if(typeid(*this) != typeid(rhs)) + return false; // cannot be equal + const FunctorHandler& fh = static_cast(rhs); -#endif // if this line gives a compiler error, you are using a function object. // you need to implement bool MyFnObj::operator == (const MyFnObj&) const; return f_==fh.f_; @@ -1092,21 +1079,12 @@ namespace Loki #ifdef LOKI_FUNCTORS_ARE_COMPARABLE - bool isMemberFuncPtr() const - { - return true; - } - bool operator==(const typename Base::FunctorImplBaseType& rhs) const { - if(!rhs.isMemberFuncPtr()) - return false; - -#ifdef LOKI_FUNCTOR_DEBUG - const MemFunHandler& mfh = dynamic_cast(rhs); -#else + if(typeid(*this) != typeid(rhs)) + return false; // cannot be equal + const MemFunHandler& mfh = static_cast(rhs); -#endif // if this line gives a compiler error, you are using a function object. // you need to implement bool MyFnObj::operator == (const MyFnObj&) const; return pObj_==mfh.pObj_ && pMemFn_==mfh.pMemFn_; @@ -1316,14 +1294,6 @@ namespace Loki #ifdef LOKI_FUNCTORS_ARE_COMPARABLE - bool isMemberFuncPtr() const - { - if(spImpl_.get()!=0) - return spImpl_.get()->isMemberFuncPtr(); - else - return false; - } - bool operator==(const Functor& rhs) const { if(spImpl_.get()==0 && rhs.spImpl_.get()==0) @@ -1556,24 +1526,14 @@ namespace Loki #ifdef LOKI_FUNCTORS_ARE_COMPARABLE - bool isMemberFuncPtr() const - { - return f_.isMemberFuncPtr(); - } - bool operator==(const typename Base::FunctorImplBaseType& rhs) const { - isMemberFuncPtr(); + if(typeid(*this) != typeid(rhs)) + return false; // cannot be equal // if this line gives a compiler error, you are using a function object. // you need to implement bool MyFnObj::operator == (const MyFnObj&) const; -#ifdef LOKI_FUNCTOR_DEBUG - return f_ == ((dynamic_cast (rhs)).f_) && - b_ == ((dynamic_cast (rhs)).b_); - -#else return f_ == ((static_cast (rhs)).f_) && b_ == ((static_cast (rhs)).b_); -#endif } #endif @@ -1698,24 +1658,14 @@ namespace Loki #ifdef LOKI_FUNCTORS_ARE_COMPARABLE - bool isMemberFuncPtr() const - { - assert(0); - return false; - } - bool operator==(const typename Base::Impl::FunctorImplBaseType& rhs) const { + if(typeid(*this) != typeid(rhs)) + return false; // cannot be equal // if this line gives a compiler error, you are using a function object. // you need to implement bool MyFnObj::operator == (const MyFnObj&) const; -#ifdef LOKI_FUNCTOR_DEBUG - return f1_ == ((dynamic_cast (rhs)).f2_) && - f2_ == ((dynamic_cast (rhs)).f1_); - -#else return f1_ == ((static_cast (rhs)).f2_) && f2_ == ((static_cast (rhs)).f1_); -#endif } #endif @@ -1845,6 +1795,9 @@ namespace Loki #endif // FUNCTOR_INC_ // $Log$ +// Revision 1.22 2006/06/09 12:58:44 syntheticpp +// simplify Functor::operator== implementation. Thanks to Eric Beyeler +// // Revision 1.21 2006/06/01 12:33:05 syntheticpp // add operator== to Functor, initiated by Eric Beyeler //