src/core/callback.h
changeset 2531 b451b5fc8b57
parent 2514 9402a946ac3d
child 2630 17b545238ab3
     1.1 --- a/src/core/callback.h	Sat Mar 01 05:37:56 2008 +0100
     1.2 +++ b/src/core/callback.h	Sun Mar 02 21:00:37 2008 +0100
     1.3 @@ -237,6 +237,52 @@
     1.4    MEM_PTR m_memPtr;
     1.5  };
     1.6  
     1.7 +// an impl for Bound Functors:
     1.8 +template <typename T, typename R, typename TX, typename T1, typename T2, typename T3, typename T4,typename T5>
     1.9 +class BoundFunctorCallbackImpl : public CallbackImpl<R,T1,T2,T3,T4,T5,empty> {
    1.10 +public:
    1.11 +  template <typename FUNCTOR, typename ARG>
    1.12 +  BoundFunctorCallbackImpl (FUNCTOR functor, ARG a)
    1.13 +      : m_functor (functor), m_a (a) {}
    1.14 +  virtual ~BoundFunctorCallbackImpl () {}
    1.15 +  R operator() (void) {
    1.16 +      return m_functor (m_a);
    1.17 +  }
    1.18 +  R operator() (T1 a1) {
    1.19 +      return m_functor (m_a,a1);
    1.20 +  }
    1.21 +  R operator() (T1 a1,T2 a2) {
    1.22 +      return m_functor (m_a,a1,a2);
    1.23 +  }
    1.24 +  R operator() (T1 a1,T2 a2,T3 a3) {
    1.25 +      return m_functor (m_a,a1,a2,a3);
    1.26 +  }
    1.27 +  R operator() (T1 a1,T2 a2,T3 a3,T4 a4) {
    1.28 +      return m_functor (m_a,a1,a2,a3,a4);
    1.29 +  }
    1.30 +  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) {
    1.31 +      return m_functor (m_a,a1,a2,a3,a4,a5);
    1.32 +  }
    1.33 +  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
    1.34 +    BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *otherDerived = 
    1.35 +      dynamic_cast<BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *> (PeekPointer (other));
    1.36 +    if (otherDerived == 0)
    1.37 +      {
    1.38 +        return false;
    1.39 +      }
    1.40 +    else if (otherDerived->m_functor != m_functor ||
    1.41 +             otherDerived->m_a != m_a)
    1.42 +      {
    1.43 +        return false;
    1.44 +      }
    1.45 +    return true;
    1.46 +  }
    1.47 +private:
    1.48 +  T m_functor;
    1.49 +  typename TypeTraits<TX>::ReferencedType m_a;
    1.50 +};
    1.51 +
    1.52 +
    1.53  class CallbackBase {
    1.54  public:
    1.55    CallbackBase () : m_impl () {}
    1.56 @@ -299,6 +345,16 @@
    1.57      : CallbackBase (impl)
    1.58    {}
    1.59  
    1.60 +  template <typename T>
    1.61 +  Callback<R,T2,T3,T4,T5,T6> Bind (T a) {
    1.62 +    Ptr<CallbackImpl<R,T2,T3,T4,T5,T6,empty> > impl =
    1.63 +      Ptr<CallbackImpl<R,T2,T3,T4,T5,T6,empty> > (
    1.64 +                                            new BoundFunctorCallbackImpl<
    1.65 +                                            Callback<R,T1,T2,T3,T4,T5,T6>,
    1.66 +                                            R,T1,T2,T3,T4,T5,T6> (*this, a), false);
    1.67 +    return Callback<R,T2,T3,T4,T5,T6> (impl);
    1.68 +  }
    1.69 +
    1.70    bool IsNull (void) const {
    1.71      return (DoPeekImpl () == 0)?true:false;
    1.72    }
    1.73 @@ -363,6 +419,15 @@
    1.74    }
    1.75  };
    1.76  
    1.77 +
    1.78 +template <typename R, typename T1, typename T2,
    1.79 +          typename T3, typename T4,
    1.80 +          typename T5, typename T6>
    1.81 +bool operator != (Callback<R,T1,T2,T3,T4,T5,T6> a, Callback<R,T1,T2,T3,T4,T5,T6> b)
    1.82 +{
    1.83 +  return !a.IsEqual (b);
    1.84 +}
    1.85 +
    1.86  /**
    1.87   * \ingroup core
    1.88   * \defgroup MakeCallback MakeCallback
    1.89 @@ -645,50 +710,6 @@
    1.90   * not yet determined whether or not it is really useful and whether
    1.91   * or not we really want to use it.
    1.92   */
    1.93 -// an impl for Bound Functors:
    1.94 -template <typename T, typename R, typename TX, typename T1, typename T2, typename T3, typename T4,typename T5>
    1.95 -class BoundFunctorCallbackImpl : public CallbackImpl<R,T1,T2,T3,T4,T5,empty> {
    1.96 -public:
    1.97 -  template <typename FUNCTOR, typename ARG>
    1.98 -  BoundFunctorCallbackImpl (FUNCTOR functor, ARG a)
    1.99 -      : m_functor (functor), m_a (a) {}
   1.100 -  virtual ~BoundFunctorCallbackImpl () {}
   1.101 -  R operator() (void) {
   1.102 -      return m_functor (m_a);
   1.103 -  }
   1.104 -  R operator() (T1 a1) {
   1.105 -      return m_functor (m_a,a1);
   1.106 -  }
   1.107 -  R operator() (T1 a1,T2 a2) {
   1.108 -      return m_functor (m_a,a1,a2);
   1.109 -  }
   1.110 -  R operator() (T1 a1,T2 a2,T3 a3) {
   1.111 -      return m_functor (m_a,a1,a2,a3);
   1.112 -  }
   1.113 -  R operator() (T1 a1,T2 a2,T3 a3,T4 a4) {
   1.114 -      return m_functor (m_a,a1,a2,a3,a4);
   1.115 -  }
   1.116 -  R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) {
   1.117 -      return m_functor (m_a,a1,a2,a3,a4,a5);
   1.118 -  }
   1.119 -  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
   1.120 -    BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *otherDerived = 
   1.121 -      dynamic_cast<BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *> (PeekPointer (other));
   1.122 -    if (otherDerived == 0)
   1.123 -      {
   1.124 -        return false;
   1.125 -      }
   1.126 -    else if (otherDerived->m_functor != m_functor ||
   1.127 -             otherDerived->m_a != m_a)
   1.128 -      {
   1.129 -        return false;
   1.130 -      }
   1.131 -    return true;
   1.132 -  }
   1.133 -private:
   1.134 -  T m_functor;
   1.135 -  typename TypeTraits<TX>::ReferencedType m_a;
   1.136 -};
   1.137  
   1.138  template <typename R, typename TX, typename ARG>
   1.139  Callback<R> MakeBoundCallback (R (*fnPtr) (TX), ARG a) {