Menu

#12 Functor operator ==

open
nobody
None
5
2006-03-01
2006-03-01
No

Functor needs an operator ==. This is not a hard
implementation, as I have made this modification in
our code. Boost::function has this as well. See my
posts in the general forum.

Discussion

  • Peter Kuemmel

    Peter Kuemmel - 2006-03-01

    Logged In: YES
    user_id=1159765

    Is it possible to submit your already tested implementation?
    That would be great.
    I could test it with vc8.

     
  • Peter Kuemmel

    Peter Kuemmel - 2006-03-07

    Logged In: YES
    user_id=1159765

    Here a first attempt inspired by Eric.
    But a full implementation is much more work.
    It also shows some problems with the design of Functor.
    it only works with msvc and breaks Function as drop in for
    boost::function.

    Index: Functor.h

    RCS file: /cvsroot/loki-lib/loki/include/loki/Functor.h,v
    retrieving revision 1.18
    diff -u -B -b -r1.18 Functor.h
    --- Functor.h 27 Feb 2006 18:53:41 -0000 1.18
    +++ Functor.h 7 Mar 2006 10:34:07 -0000
    @@ -42,6 +42,8 @@
    //#define LOKI_FUNCTOR_IS_NOT_A_SMALLOBJECT
    #endif

    +#define LOKI_ENABLE_FUNCTOR_EQUAL
    +
    namespace Loki
    {
    ////////////////////////////////////////////////////////////////////////////////
    @@ -95,6 +97,13 @@
    assert(typeid(*pClone) == typeid(*pObj));
    return pClone;
    }
    +
    +#ifdef LOKI_ENABLE_FUNCTOR_EQUAL
    + virtual bool operator==(const FunctorImplBase&) const = 0;
    + // there is no static information if Functor holds a
    member function
    + virtual bool isMemberFuncPtr() const = 0;
    +#endif
    +
    };
    }

    @@ -942,6 +951,18 @@

    LOKI_DEFINE_CLONE_FUNCTORIMPL(FunctorHandler)

    +#ifdef LOKI_ENABLE_FUNCTOR_EQUAL
    + bool isMemberFuncPtr() const
    + { return false; }
    +
    + bool operator==(const typename Base::FunctorImplBase&
    rhs) const
    + {
    + if( rhs.isMemberFuncPtr() )
    + return false; // cannot be equal
    + const FunctorHandler& fh = static_cast<const
    FunctorHandler&>(rhs);
    + return f_==fh.f_;
    + }
    +#endif
    // operator() implementations for up to 15 arguments

    ResultType operator()()
    @@ -1051,6 +1072,18 @@

    LOKI_DEFINE_CLONE_FUNCTORIMPL(MemFunHandler)

    +#ifdef LOKI_ENABLE_FUNCTOR_EQUAL
    + bool isMemberFuncPtr() const
    + { return true; }
    +
    + bool operator==(const typename Base::FunctorImplBase&
    rhs) const
    + {
    + if( !isMemberFuncPtr() )
    + return false;
    + const MemFunHandler& mfh = static_cast<const
    MemFunHandler&>(rhs);
    + return pObj_==mfh.pObj_ && pMemFn_==mfh.pMemFn_;
    + }
    +#endif
    ResultType operator()()
    { return ((*pObj_).*pMemFn_)(); }

    @@ -1223,6 +1256,18 @@
    return *this;
    }

    +#ifdef LOKI_ENABLE_FUNCTOR_EQUAL
    + bool operator==(const Functor& rhs) const
    + {
    + return *spImpl_.get() == *rhs.spImpl_.get();
    + }
    +
    + bool operator!=(const Functor& rhs) const
    + {
    + return !(*this==rhs);
    + }
    +#endif
    +

     
  • Peter Kuemmel

    Peter Kuemmel - 2006-03-07

    small test program

     
  • Peter Kuemmel

    Peter Kuemmel - 2006-03-07

    with minimalistic operator==

     
  • Peter Kuemmel

    Peter Kuemmel - 2006-05-07

    Logged In: YES
    user_id=1159765

    Relates stuff:

    http://www.codeproject.com/cpp/FastDelegate.asp

    Herb Sutter:
    http://www.ddj.com/dept/cpp/184403873
    Generalizing the Observer pattern (essentially, multicast
    delegates) using std::tr1::function. Discusses the
    limitations of the failure of boost::function to provide
    operator ==.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.