Make the CallbackBase class more useful than it was.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed Jan 02 13:39:56 2008 +0100 (2008-01-02)
changeset 2206481d653a1a1e
parent 2205 57710f066013
child 2207 d841cca00990
child 2236 a3a52c5ca2b4
Make the CallbackBase class more useful than it was.
src/common/chunk-registry.cc
src/common/chunk-registry.h
src/common/packet-printer.h
src/core/callback.h
     1.1 --- a/src/common/chunk-registry.cc	Sun Dec 30 19:36:44 2007 -0800
     1.2 +++ b/src/common/chunk-registry.cc	Wed Jan 02 13:39:56 2008 +0100
     1.3 @@ -105,7 +105,7 @@
     1.4  void 
     1.5  ChunkRegistry::InvokePrintCallback (uint32_t uid, uint8_t *instance, std::ostream &os,
     1.6  				    uint32_t packetUid, uint32_t size, 
     1.7 -				    Ptr<CallbackImplBase> callback)
     1.8 +                                    CallbackBase callback)
     1.9  {
    1.10    InfoVector *vec = GetInfoVector ();
    1.11    NS_ASSERT (uid >= 1 && uid <= vec->size ());
     2.1 --- a/src/common/chunk-registry.h	Sun Dec 30 19:36:44 2007 -0800
     2.2 +++ b/src/common/chunk-registry.h	Wed Jan 02 13:39:56 2008 +0100
     2.3 @@ -54,7 +54,7 @@
     2.4    static bool IsTrailer (uint32_t uid);
     2.5    static void InvokePrintCallback (uint32_t uid, uint8_t *instance, std::ostream &os,
     2.6  				   uint32_t packetUid, uint32_t size, 
     2.7 -				   Ptr<CallbackImplBase> callback);
     2.8 +				   CallbackBase callback);
     2.9  private:
    2.10    typedef uint8_t *(*GetStaticInstanceCb) (void);
    2.11    typedef uint32_t (*DeserializeCb) (uint8_t *, Buffer::Iterator);
    2.12 @@ -62,7 +62,7 @@
    2.13    typedef std::string (*GetNameCb) (uint8_t *);
    2.14    typedef void (*InvokePrintCallbackCb) (uint8_t *instance, std::ostream &os,
    2.15  					 uint32_t packetUid, uint32_t size, 
    2.16 -					 Ptr<CallbackImplBase> callback);
    2.17 +					 CallbackBase callback);
    2.18    struct Info {
    2.19      std::string uidString;
    2.20      bool isHeader;
    2.21 @@ -85,7 +85,7 @@
    2.22    template <typename T>
    2.23    static void DoInvokePrintCallback (uint8_t *instance, std::ostream &os,
    2.24  				     uint32_t packetUid, uint32_t size, 
    2.25 -				     Ptr<CallbackImplBase> callback);
    2.26 +                                     CallbackBase callback);
    2.27    template <typename T>
    2.28    static uint32_t GetUid (bool isHeader, std::string uidString);
    2.29  
    2.30 @@ -167,7 +167,7 @@
    2.31  void 
    2.32  ChunkRegistry::DoInvokePrintCallback (uint8_t *instance, std::ostream &os,
    2.33  				      uint32_t packetUid, uint32_t size, 
    2.34 -				      Ptr<CallbackImplBase> callback)
    2.35 +				      CallbackBase callback)
    2.36  {
    2.37    T *obj = reinterpret_cast<T *> (instance);
    2.38    Callback<void,std::ostream&,uint32_t,uint32_t,const T*> cb;
     3.1 --- a/src/common/packet-printer.h	Sun Dec 30 19:36:44 2007 -0800
     3.2 +++ b/src/common/packet-printer.h	Wed Jan 02 13:39:56 2008 +0100
     3.3 @@ -134,7 +134,7 @@
     3.4    struct Printer
     3.5    {
     3.6      uint32_t m_chunkUid;
     3.7 -    Ptr<CallbackImplBase> m_printer;
     3.8 +    CallbackBase m_printer;
     3.9      Callback<void,std::ostream &,uint32_t,uint32_t,std::string &,
    3.10               struct PacketPrinter::FragmentInformation> m_fragmentPrinter;
    3.11    };
    3.12 @@ -158,7 +158,7 @@
    3.13  {
    3.14    Printer p;
    3.15    p.m_chunkUid = T::GetUid ();
    3.16 -  p.m_printer = printer.GetImpl ();
    3.17 +  p.m_printer = printer;
    3.18    p.m_fragmentPrinter = fragmentPrinter;
    3.19    m_printerList.push_back (p);
    3.20  }
    3.21 @@ -170,7 +170,7 @@
    3.22  {
    3.23    Printer p;
    3.24    p.m_chunkUid = T::GetUid ();
    3.25 -  p.m_printer = printer.GetImpl ();
    3.26 +  p.m_printer = printer;
    3.27    p.m_fragmentPrinter = fragmentPrinter;
    3.28    m_printerList.push_back (p);
    3.29  }
     4.1 --- a/src/core/callback.h	Sun Dec 30 19:36:44 2007 -0800
     4.2 +++ b/src/core/callback.h	Wed Jan 02 13:39:56 2008 +0100
     4.3 @@ -75,18 +75,18 @@
     4.4    CallbackImplBase ()
     4.5      : m_count (1) {}
     4.6    virtual ~CallbackImplBase () {}
     4.7 -  void Ref (void) {
     4.8 +  void Ref (void) const {
     4.9      m_count++;
    4.10    }
    4.11 -  void Unref (void) {
    4.12 +  void Unref (void) const {
    4.13      m_count--;
    4.14      if (m_count == 0) {
    4.15        delete this;
    4.16      }
    4.17    }
    4.18 -  virtual bool IsEqual (CallbackImplBase const *other) const = 0;
    4.19 +  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const = 0;
    4.20  private:
    4.21 -  uint32_t m_count;
    4.22 +  mutable uint32_t m_count;
    4.23  };
    4.24  
    4.25  // declare the CallbackImpl class
    4.26 @@ -171,9 +171,9 @@
    4.27    R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) {
    4.28      return m_functor (a1,a2,a3,a4,a5,a6);
    4.29    }
    4.30 -  virtual bool IsEqual (CallbackImplBase const *other) const {
    4.31 +  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
    4.32      FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6> const *otherDerived = 
    4.33 -      dynamic_cast<FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6> const *> (other);
    4.34 +      dynamic_cast<FunctorCallbackImpl<T,R,T1,T2,T3,T4,T5,T6> const *> (PeekPointer(other));
    4.35      if (otherDerived == 0)
    4.36        {
    4.37          return false;
    4.38 @@ -216,9 +216,9 @@
    4.39    R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5,T6 a6) {
    4.40      return ((CallbackTraits<OBJ_PTR>::GetReference (m_objPtr)).*m_memPtr) (a1, a2, a3, a4, a5, a6);
    4.41    }
    4.42 -  virtual bool IsEqual (CallbackImplBase const *other) const {
    4.43 +  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
    4.44      MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> const *otherDerived = 
    4.45 -      dynamic_cast<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> const *> (other);
    4.46 +      dynamic_cast<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> const *> (PeekPointer (other));
    4.47      if (otherDerived == 0)
    4.48        {
    4.49          return false;
    4.50 @@ -237,9 +237,11 @@
    4.51  
    4.52  class CallbackBase {
    4.53  public:
    4.54 -  virtual ~CallbackBase () {}
    4.55 -  virtual CallbackImplBase *PeekImpl (void) const = 0;
    4.56 -  virtual Ptr<CallbackImplBase> GetImpl (void) const = 0;
    4.57 +  CallbackBase () : m_impl () {}
    4.58 +  Ptr<CallbackImplBase> GetImpl (void) const {return m_impl;}
    4.59 +protected:
    4.60 +  CallbackBase (Ptr<CallbackImplBase> impl) : m_impl (impl) {}
    4.61 +  Ptr<CallbackImplBase> m_impl;
    4.62  };
    4.63  
    4.64  /**
    4.65 @@ -277,59 +279,69 @@
    4.66     typename T5 = empty, typename T6 = empty>
    4.67  class Callback : public CallbackBase {
    4.68  public:
    4.69 +  Callback () {}
    4.70 +
    4.71    // There are two dummy args below to ensure that this constructor is
    4.72    // always properly disambiguited by the c++ compiler
    4.73    template <typename FUNCTOR>
    4.74    Callback (FUNCTOR const &functor, bool, bool) 
    4.75 -    : m_impl (Create<FunctorCallbackImpl<FUNCTOR,R,T1,T2,T3,T4,T5,T6> > (functor))
    4.76 +    : CallbackBase (Create<FunctorCallbackImpl<FUNCTOR,R,T1,T2,T3,T4,T5,T6> > (functor))
    4.77    {}
    4.78  
    4.79    template <typename OBJ_PTR, typename MEM_PTR>
    4.80    Callback (OBJ_PTR const &objPtr, MEM_PTR mem_ptr)
    4.81 -    : m_impl (Create<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> > (objPtr, mem_ptr))
    4.82 +    : CallbackBase (Create<MemPtrCallbackImpl<OBJ_PTR,MEM_PTR,R,T1,T2,T3,T4,T5,T6> > (objPtr, mem_ptr))
    4.83    {}
    4.84  
    4.85    Callback (Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6> > const &impl)
    4.86 -      : m_impl (impl)
    4.87 +    : CallbackBase (impl)
    4.88    {}
    4.89  
    4.90    bool IsNull (void) const {
    4.91 -    return (PeekImpl () == 0)?true:false;
    4.92 +    return (DoPeekImpl () == 0)?true:false;
    4.93    }
    4.94    void Nullify (void) {
    4.95      m_impl = 0;
    4.96    }
    4.97  
    4.98 -  Callback () : m_impl () {}
    4.99    R operator() (void) const {
   4.100 -    return (*(PeekImpl ())) ();
   4.101 +    return (*(DoPeekImpl ())) ();
   4.102    }
   4.103    R operator() (T1 a1) const {
   4.104 -    return (*(PeekImpl ())) (a1);
   4.105 +    return (*(DoPeekImpl ())) (a1);
   4.106    }
   4.107    R operator() (T1 a1, T2 a2) const {
   4.108 -    return (*(PeekImpl ())) (a1,a2);
   4.109 +    return (*(DoPeekImpl ())) (a1,a2);
   4.110    }
   4.111    R operator() (T1 a1, T2 a2, T3 a3) const {
   4.112 -    return (*(PeekImpl ())) (a1,a2,a3);
   4.113 +    return (*(DoPeekImpl ())) (a1,a2,a3);
   4.114    }
   4.115    R operator() (T1 a1, T2 a2, T3 a3, T4 a4) const {
   4.116 -    return (*(PeekImpl ())) (a1,a2,a3,a4);
   4.117 +    return (*(DoPeekImpl ())) (a1,a2,a3,a4);
   4.118    }
   4.119    R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5) const {
   4.120 -    return (*(PeekImpl ())) (a1,a2,a3,a4,a5);
   4.121 +    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5);
   4.122    }
   4.123    R operator() (T1 a1, T2 a2, T3 a3, T4 a4,T5 a5,T6 a6) const {
   4.124 -    return (*(PeekImpl ())) (a1,a2,a3,a4,a5,a6);
   4.125 +    return (*(DoPeekImpl ())) (a1,a2,a3,a4,a5,a6);
   4.126    }
   4.127  
   4.128 -  bool IsEqual (CallbackBase const &other) const {
   4.129 -    return PeekImpl ()->IsEqual (other.PeekImpl ());
   4.130 +  bool IsEqual (const CallbackBase &other) const {
   4.131 +    return m_impl->IsEqual (other.GetImpl ());
   4.132    }
   4.133  
   4.134 -  bool CheckType (CallbackBase const& other) const {
   4.135 -    CallbackImplBase *otherBase = other.PeekImpl ();
   4.136 -    if (dynamic_cast<CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (otherBase) != 0)
   4.137 +  bool CheckType (const CallbackBase & other) const {
   4.138 +    return DoCheckType (other.GetImpl ());
   4.139 +  }
   4.140 +  void Assign (const CallbackBase &other) {
   4.141 +    DoAssign (other.GetImpl ());
   4.142 +  }
   4.143 +private:
   4.144 +  CallbackImpl<R,T1,T2,T3,T4,T5,T6> *DoPeekImpl (void) const {
   4.145 +    return static_cast<CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (PeekPointer (m_impl));
   4.146 +  }
   4.147 +  bool DoCheckType (Ptr<const CallbackImplBase> other) const {
   4.148 +    if (dynamic_cast<const CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (PeekPointer (other)) != 0)
   4.149        {
   4.150          return true;
   4.151        }
   4.152 @@ -338,34 +350,15 @@
   4.153          return false;
   4.154        }
   4.155    }
   4.156 -  void Assign (CallbackBase const &other) {
   4.157 -    if (!CheckType (other))
   4.158 +  void DoAssign (Ptr<const CallbackImplBase> other) {
   4.159 +    if (!DoCheckType (other))
   4.160        {
   4.161          NS_FATAL_ERROR ("Incompatible types. (feed to \"c++filt -t\")"
   4.162                          " got=" << typeid (other).name () << 
   4.163                          ", expected=" << typeid (*this).name ());
   4.164        }
   4.165 -    const Callback<R, T1,T2,T3,T4,T5,T6> *goodType = static_cast<const Callback<R,T1,T2,T3,T4,T5,T6> *> (&other);
   4.166 -    *this = *goodType;
   4.167 +    m_impl = const_cast<CallbackImplBase *> (PeekPointer (other));
   4.168    }
   4.169 -  void Assign (Ptr<CallbackImplBase> other) {
   4.170 -    CallbackImpl<R,T1,T2,T3,T4,T5,T6> *impl = dynamic_cast<CallbackImpl<R,T1,T2,T3,T4,T5,T6> *> (PeekPointer (other));
   4.171 -    if (other == 0)
   4.172 -      {
   4.173 -        NS_FATAL_ERROR ("Incompatible types. (feed to \"c++filt -t\")"
   4.174 -                        " got=" << typeid (other).name () << 
   4.175 -                        ", expected=" << typeid (*impl).name ());
   4.176 -      }
   4.177 -    *this = Callback<R,T1,T2,T3,T4,T5,T6> (impl);
   4.178 -  }
   4.179 -  virtual Ptr<CallbackImplBase>GetImpl (void) const {
   4.180 -    return m_impl;
   4.181 -  }
   4.182 -private:
   4.183 -  virtual CallbackImpl<R,T1,T2,T3,T4,T5,T6> *PeekImpl (void) const {
   4.184 -    return PeekPointer (m_impl);
   4.185 -  }
   4.186 -  Ptr<CallbackImpl<R,T1,T2,T3,T4,T5,T6> > m_impl;
   4.187  };
   4.188  
   4.189  /**
   4.190 @@ -675,9 +668,9 @@
   4.191    R operator() (T1 a1,T2 a2,T3 a3,T4 a4,T5 a5) {
   4.192        return m_functor (m_a,a1,a2,a3,a4,a5);
   4.193    }
   4.194 -  virtual bool IsEqual (CallbackImplBase const *other) const {
   4.195 +  virtual bool IsEqual (Ptr<const CallbackImplBase> other) const {
   4.196      BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *otherDerived = 
   4.197 -      dynamic_cast<BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *> (other);
   4.198 +      dynamic_cast<BoundFunctorCallbackImpl<T,R,TX,T1,T2,T3,T4,T5> const *> (PeekPointer (other));
   4.199      if (otherDerived == 0)
   4.200        {
   4.201          return false;