make Disconnect work with trace contexts.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 07 Mar 2008 12:18:34 -0800
changeset 2569d5cff2968984
parent 2568 fe7da55d00a0
child 2570 15d5421022a1
make Disconnect work with trace contexts.
src/core/config.cc
src/core/config.h
src/core/object.cc
src/core/object.h
src/core/trace-source-accessor.h
src/core/traced-callback.h
src/core/traced-value.h
     1.1 --- a/src/core/config.cc	Wed Mar 05 00:16:39 2008 +0100
     1.2 +++ b/src/core/config.cc	Fri Mar 07 12:18:34 2008 -0800
     1.3 @@ -269,6 +269,7 @@
     1.4    void Connect (std::string path, const CallbackBase &cb);
     1.5    void ConnectWithContext (std::string path, const CallbackBase &cb);
     1.6    void Disconnect (std::string path, const CallbackBase &cb);
     1.7 +  void DisconnectWithContext (std::string path, const CallbackBase &cb);
     1.8  
     1.9    void RegisterRootNamespaceObject (Ptr<Object> obj);
    1.10    void UnregisterRootNamespaceObject (Ptr<Object> obj);
    1.11 @@ -359,6 +360,26 @@
    1.12      }
    1.13  }
    1.14  void 
    1.15 +ConfigImpl::DisconnectWithContext (std::string path, const CallbackBase &cb)
    1.16 +{
    1.17 +  class DisconnectWithContextResolver : public Resolver 
    1.18 +  {
    1.19 +  public:
    1.20 +    DisconnectWithContextResolver (std::string path, const CallbackBase &cb)
    1.21 +      : Resolver (path),
    1.22 +	m_cb (cb) {}
    1.23 +  private:
    1.24 +    virtual void DoOne (Ptr<Object> object, std::string path, std::string name) {
    1.25 +      object->TraceSourceDisconnectWithContext (name, path, m_cb);
    1.26 +    }
    1.27 +    CallbackBase m_cb;
    1.28 +  } resolver = DisconnectWithContextResolver (path, cb);
    1.29 +  for (Roots::const_iterator i = m_roots.begin (); i != m_roots.end (); i++)
    1.30 +    {
    1.31 +      resolver.Resolve (*i);
    1.32 +    }
    1.33 +}
    1.34 +void 
    1.35  ConfigImpl::RegisterRootNamespaceObject (Ptr<Object> obj)
    1.36  {
    1.37    m_roots.push_back (obj);
    1.38 @@ -405,6 +426,11 @@
    1.39  {
    1.40    Singleton<ConfigImpl>::Get ()->ConnectWithContext (path, cb);
    1.41  }
    1.42 +void 
    1.43 +DisconnectWithContext (std::string path, const CallbackBase &cb)
    1.44 +{
    1.45 +  Singleton<ConfigImpl>::Get ()->DisconnectWithContext (path, cb);
    1.46 +}
    1.47  
    1.48  void RegisterRootNamespaceObject (Ptr<Object> obj)
    1.49  {
    1.50 @@ -693,10 +719,8 @@
    1.51    d3->SetAttribute ("Source", Integer (-3));
    1.52    NS_TEST_ASSERT_EQUAL (m_traceNotification, -3);
    1.53    NS_TEST_ASSERT_EQUAL (m_tracePath, "/NodeA/NodeB/NodesB/3/Source");
    1.54 -  // Yes, disconnection _cannot_ work with 'context-based connection.
    1.55 -  // XXX: what do we do about this ?
    1.56 -  Config::Disconnect ("/NodeA/NodeB/NodesB/[0-1]|3/Source", 
    1.57 -		      MakeCallback (&ConfigTest::ChangeNotificationWithPath, this));
    1.58 +  Config::DisconnectWithContext ("/NodeA/NodeB/NodesB/[0-1]|3/Source", 
    1.59 +				 MakeCallback (&ConfigTest::ChangeNotificationWithPath, this));
    1.60    m_traceNotification = 0;
    1.61    // this should _not_ trigger a notification
    1.62    d1->SetAttribute ("Source", Integer (-4));
     2.1 --- a/src/core/config.h	Wed Mar 05 00:16:39 2008 +0100
     2.2 +++ b/src/core/config.h	Fri Mar 07 12:18:34 2008 -0800
     2.3 @@ -16,6 +16,7 @@
     2.4  void Connect (std::string path, const CallbackBase &cb);
     2.5  void Disconnect (std::string path, const CallbackBase &cb);
     2.6  void ConnectWithContext (std::string path, const CallbackBase &cb);
     2.7 +void DisconnectWithContext (std::string path, const CallbackBase &cb);
     2.8  
     2.9  void RegisterRootNamespaceObject (Ptr<Object> obj);
    2.10  void UnregisterRootNamespaceObject (Ptr<Object> obj);
     3.1 --- a/src/core/object.cc	Wed Mar 05 00:16:39 2008 +0100
     3.2 +++ b/src/core/object.cc	Fri Mar 07 12:18:34 2008 -0800
     3.3 @@ -1128,6 +1128,17 @@
     3.4    bool ok = accessor->Disconnect (this, cb);
     3.5    return ok;
     3.6  }
     3.7 +bool 
     3.8 +Object::TraceSourceDisconnectWithContext (std::string name, std::string context, const CallbackBase &cb)
     3.9 +{
    3.10 +  Ptr<const TraceSourceAccessor> accessor = m_tid.LookupTraceSourceByName (name);
    3.11 +  if (accessor == 0)
    3.12 +    {
    3.13 +      return false;
    3.14 +    }
    3.15 +  bool ok = accessor->DisconnectWithContext (this, context, cb);
    3.16 +  return ok;
    3.17 +}
    3.18  
    3.19  TypeId 
    3.20  Object::GetRealTypeId (void) const
     4.1 --- a/src/core/object.h	Wed Mar 05 00:16:39 2008 +0100
     4.2 +++ b/src/core/object.h	Fri Mar 07 12:18:34 2008 -0800
     4.3 @@ -397,6 +397,7 @@
     4.4    bool TraceSourceConnect (std::string name, const CallbackBase &cb);
     4.5    bool TraceSourceConnectWithContext (std::string name, std::string context, const CallbackBase &cb);
     4.6    bool TraceSourceDisconnect (std::string name, const CallbackBase &cb);
     4.7 +  bool TraceSourceDisconnectWithContext (std::string name, std::string context, const CallbackBase &cb);
     4.8  
     4.9    TypeId GetRealTypeId (void) const;
    4.10  
     5.1 --- a/src/core/trace-source-accessor.h	Wed Mar 05 00:16:39 2008 +0100
     5.2 +++ b/src/core/trace-source-accessor.h	Fri Mar 07 12:18:34 2008 -0800
     5.3 @@ -19,6 +19,7 @@
     5.4    virtual bool Connect (ObjectBase *obj, const CallbackBase &cb) const = 0;
     5.5    virtual bool ConnectWithContext (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
     5.6    virtual bool Disconnect (ObjectBase *obj, const CallbackBase &cb) const = 0;
     5.7 +  virtual bool DisconnectWithContext (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
     5.8  private:
     5.9    mutable uint32_t m_count;
    5.10  };
    5.11 @@ -63,6 +64,15 @@
    5.12        (p->*m_source).Disconnect (cb);
    5.13        return true;      
    5.14      }
    5.15 +    virtual bool DisconnectWithContext (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
    5.16 +      T *p = dynamic_cast<T*> (obj);
    5.17 +      if (p == 0)
    5.18 +	{
    5.19 +	  return false;
    5.20 +	}
    5.21 +      (p->*m_source).DisconnectWithContext (cb, context);
    5.22 +      return true;      
    5.23 +    }
    5.24      SOURCE T::*m_source;
    5.25    } *accessor = new Accessor ();
    5.26    accessor->m_source = a;
     6.1 --- a/src/core/traced-callback.h	Wed Mar 05 00:16:39 2008 +0100
     6.2 +++ b/src/core/traced-callback.h	Fri Mar 07 12:18:34 2008 -0800
     6.3 @@ -43,6 +43,7 @@
     6.4    void Connect (const CallbackBase & callback);
     6.5    void ConnectWithContext (const CallbackBase & callback, std::string path);
     6.6    void Disconnect (const CallbackBase & callback);
     6.7 +  void DisconnectWithContext (const CallbackBase & callback, std::string path);
     6.8    void operator() (void) const;
     6.9    void operator() (T1 a1) const;
    6.10    void operator() (T1 a1, T2 a2) const;
    6.11 @@ -105,6 +106,16 @@
    6.12  template<typename T1, typename T2, 
    6.13           typename T3, typename T4>
    6.14  void 
    6.15 +TracedCallback<T1,T2,T3,T4>::DisconnectWithContext (const CallbackBase & callback, std::string path)
    6.16 +{
    6.17 +  Callback<void,std::string,T1,T2,T3,T4> cb;
    6.18 +  cb.Assign (callback);
    6.19 +  Callback<void,T1,T2,T3,T4> realCb = cb.Bind (path);
    6.20 +  Disconnect (realCb);
    6.21 +}
    6.22 +template<typename T1, typename T2, 
    6.23 +         typename T3, typename T4>
    6.24 +void 
    6.25  TracedCallback<T1,T2,T3,T4>::operator() (void) const
    6.26  {
    6.27    for (typename CallbackList::const_iterator i = m_callbackList.begin ();
     7.1 --- a/src/core/traced-value.h	Wed Mar 05 00:16:39 2008 +0100
     7.2 +++ b/src/core/traced-value.h	Fri Mar 07 12:18:34 2008 -0800
     7.3 @@ -59,6 +59,9 @@
     7.4    void Disconnect (const CallbackBase &cb) {
     7.5      m_cb.Disconnect (cb);
     7.6    }
     7.7 +  void DisconnectWithContext (const CallbackBase &cb, std::string path) {
     7.8 +    m_cb.DisconnectWithContext (cb, path);
     7.9 +  }
    7.10    void Set (const T &v) {
    7.11      if (m_v != v)
    7.12        {