replace TraceResolver::PrintAvailable with TraceResolver::CollectSources
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Tue, 28 Aug 2007 09:37:10 +0200
changeset 1371 bebf690257c9
parent 1370 d5339e1c95df
child 1372 5967e5b2c737
replace TraceResolver::PrintAvailable with TraceResolver::CollectSources
src/core/array-trace-resolver.h
src/core/composite-trace-resolver.cc
src/core/composite-trace-resolver.h
src/core/object.cc
src/core/object.h
src/core/trace-context.cc
src/core/trace-resolver.cc
src/core/trace-resolver.h
utils/print-trace-sources.cc
--- a/src/core/array-trace-resolver.h	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/array-trace-resolver.h	Tue Aug 28 09:37:10 2007 +0200
@@ -62,7 +62,8 @@
   // inherited from TraceResolver
   virtual void Connect (std::string path, CallbackBase const &cb, const TraceContext &context);
   virtual void Disconnect (std::string path, CallbackBase const &cb);
-  virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os);
+  virtual void CollectSources (std::string path, const TraceContext &context, 
+                               SourceCollection *collection);
 
 private:
   class IteratorBase
@@ -166,7 +167,8 @@
 }
 template <typename INDEX>
 void 
-ArrayTraceResolver<INDEX>::PrintAvailable (std::string path, const TraceContext &context, std::ostream &os)
+ArrayTraceResolver<INDEX>::CollectSources (std::string path, const TraceContext &context, 
+                                           SourceCollection *collection)
 {
   path.append ("/[0-n]");
   uint32_t j = 0;
@@ -176,11 +178,12 @@
         INDEX index = j;
         tmp.AddElement (index);
         Ptr<Object> obj = m_iter->Get ();
-        obj->GetTraceResolver ()->PrintAvailable (path, tmp, os);
+        obj->GetTraceResolver ()->CollectSources (path, tmp, collection);
         j++;
     }
 }
 
+
 }//namespace ns3
 
 #endif /* ARRAY_TRACE_RESOLVER_H */
--- a/src/core/composite-trace-resolver.cc	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/composite-trace-resolver.cc	Tue Aug 28 09:37:10 2007 +0200
@@ -54,7 +54,15 @@
     {maker ()->Connect (subpath, cb, context);}
     virtual void Disconnect (std::string subpath, const CallbackBase &cb)
     {maker ()->Disconnect (subpath, cb);}
-
+    virtual void CollectSources (std::string path, const TraceContext &context, 
+                                 SourceCollection *collection)
+    {
+      path.append ("/");
+      path.append (this->name);
+      TraceContext ctx = context;
+      ctx.Union (this->context);
+      this->maker ()->CollectSources (path, ctx, collection);
+    }
     Callback<Ptr<TraceResolver> > maker;
   } *item = new MakerCompositeItem ();
   item->name = name;
@@ -82,13 +90,15 @@
     {if (subpath == "") {trace->AddCallback (cb, context);}}
     virtual void Disconnect (std::string subpath, const CallbackBase &cb)
     {if (subpath == "") {trace->RemoveCallback (cb);}}
-    virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os)
+    virtual void CollectSources (std::string path, const TraceContext &context, 
+                                 SourceCollection *collection)
     {
-      os << path << "/" << this->name << " [";
+      path.append ("/");
+      path.append (this->name);
       TraceContext ctx = context;
       ctx.Union (this->context);
-      ctx.PrintAvailable (os, ",");
-      os << "]" << std::endl;
+      // XXX help string
+      collection->AddUnique (path, ctx, "");
     }
     TraceSource *trace;
   } *item = new SourceCompositeItem ();
@@ -116,13 +126,14 @@
     {child->GetTraceResolver ()->Connect (subpath, cb, context);}
     virtual void Disconnect (std::string subpath, const CallbackBase &cb)
     {child->TraceDisconnect (subpath, cb);}
-    virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os)
+    virtual void CollectSources (std::string path, const TraceContext &context, 
+                                 SourceCollection *collection)
     {
       path.append ("/");
       path.append (this->name);
       TraceContext ctx = context;
       ctx.Union (this->context);
-      this->child->GetTraceResolver ()->PrintAvailable (path, ctx, os);
+      this->child->GetTraceResolver ()->CollectSources (path, ctx, collection);
     }
 
     Ptr<Object> child;
@@ -264,20 +275,23 @@
   } operation  = DisconnectOperation (cb);
   DoRecursiveOperation (path, operation);
 }
+
 void 
-CompositeTraceResolver::PrintAvailable (std::string path, const TraceContext &context, std::ostream &os)
+CompositeTraceResolver::CollectSources (std::string path, const TraceContext &context, 
+                                        SourceCollection *collection)
 {
   for (TraceItems::const_iterator i = m_items.begin (); i != m_items.end (); i++)
     {
       NS_DEBUG ("print " << (*i)->name);
-      (*i)->PrintAvailable (path, context, os);
+      (*i)->CollectSources (path, context, collection);
     }
   if (m_parent != 0)
     {
-      m_parent->PrintAvailable (path, context, os);
+      m_parent->CollectSources (path, context, collection);
     }
 }
 
+
 }//namespace ns3
 
 #ifdef RUN_SELF_TESTS
--- a/src/core/composite-trace-resolver.h	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/composite-trace-resolver.h	Tue Aug 28 09:37:10 2007 +0200
@@ -91,7 +91,8 @@
 
   virtual void Connect (std::string path, CallbackBase const &cb, const TraceContext &context);
   virtual void Disconnect (std::string path, CallbackBase const &cb);
-  virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os);
+  virtual void CollectSources (std::string path, const TraceContext &context, 
+                               SourceCollection *collection);
 
 private:
   class CompositeItem 
@@ -100,7 +101,8 @@
     virtual ~CompositeItem () {}
     virtual void Connect (std::string subpath, const CallbackBase &cb, const TraceContext &context) = 0;
     virtual void Disconnect (std::string subpath, const CallbackBase &cb) = 0;
-    virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os) {}
+    virtual void CollectSources (std::string path, const TraceContext &context, 
+                                 SourceCollection *collection) = 0;
 
     std::string name;
     TraceContext context;
@@ -157,13 +159,16 @@
     {array->Connect (subpath, cb, context);}
     virtual void Disconnect (std::string subpath, const CallbackBase &cb)
     {array->Disconnect (subpath, cb);}
-    virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os)
+    virtual void CollectSources (std::string path, const TraceContext &context, 
+                                 SourceCollection *collection)
     {
       path.append ("/");
       path.append (this->name);
       TraceContext ctx = context;
       ctx.Union (this->context);
-      array->PrintAvailable (path, ctx, os);}
+      array->CollectSources (path, ctx, collection);
+    }
+
     Ptr<ArrayTraceResolver<INDEX> > array;
   } *item = new ArrayCompositeItem ();
   item->name = name;
--- a/src/core/object.cc	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/object.cc	Tue Aug 28 09:37:10 2007 +0200
@@ -63,7 +63,8 @@
   InterfaceIdTraceResolver (Ptr<Object> aggregate);
   virtual void Connect (std::string path, CallbackBase const &cb, const TraceContext &context);
   virtual void Disconnect (std::string path, CallbackBase const &cb);
-  virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os);
+  virtual void CollectSources (std::string path, const TraceContext &context, 
+                               SourceCollection *collection);
 private:
   Ptr<Object> ParseForInterface (std::string path);
   Ptr<Object> m_aggregate;
@@ -105,9 +106,10 @@
     }
 }
 void 
-InterfaceIdTraceResolver::PrintAvailable (std::string path, const TraceContext &context, std::ostream &os)
+InterfaceIdTraceResolver::CollectSources (std::string path, const TraceContext &context, 
+                                          SourceCollection *collection)
 {
-  
+  m_aggregate->DoCollectSources (path, context, collection);
 }
 
 
@@ -128,6 +130,12 @@
 {
   return Singleton<IidTree>::Get ()->LookupParent (iid.m_iid);
 }
+std::string 
+InterfaceId::GetName (void) const
+{
+  std::string name = Singleton<IidManager>::Get ()->LookupByUid (m_iid);
+  return name;
+}
 
 bool operator == (const InterfaceId &a, const InterfaceId &b)
 {
@@ -284,6 +292,34 @@
   } while (current != end);
 }
 
+void 
+Object::DoCollectSources (std::string path, const TraceContext &context, 
+                          TraceResolver::SourceCollection *collection)
+{
+  Object *current = this->m_next;
+  if (collection->IsFlagSet ())
+    {
+      return;
+    }
+  collection->SetFlag ();
+  while (current != this)
+    {
+      NS_ASSERT (current != 0);
+      InterfaceId cur = current->m_iid;
+      while (cur != Object::iid)
+        {
+          std::string name = cur.GetName ();
+          std::string fullpath = path;
+          fullpath.append ("/$");
+          fullpath.append (name);
+          current->GetTraceResolver ()->CollectSources (fullpath, context, collection);
+          cur = InterfaceId::LookupParent (cur);
+        }
+      current = current->m_next;
+    }
+  collection->ClearFlag ();
+}
+
 } // namespace ns3
 
 
--- a/src/core/object.h	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/object.h	Tue Aug 28 09:37:10 2007 +0200
@@ -24,10 +24,10 @@
 #include <stdint.h>
 #include <string>
 #include "ptr.h"
+#include "trace-resolver.h"
 
 namespace ns3 {
 
-class TraceResolver;
 class TraceContext;
 class CallbackBase;
 
@@ -60,6 +60,11 @@
    * id is not a valid interface id.
    */
   static InterfaceId LookupParent (InterfaceId iid);
+
+  /**
+   * \returns the name of this interface.
+   */
+  std::string GetName (void) const;
   ~InterfaceId ();
 private:
   InterfaceId (uint16_t iid);
@@ -156,7 +161,10 @@
    */
   virtual void DoDispose (void);
 private:
+  friend class InterfaceIdTraceResolver;
   Ptr<Object> DoQueryInterface (InterfaceId iid) const;
+  void DoCollectSources (std::string path, const TraceContext &context, 
+                         TraceResolver::SourceCollection *collection);
   bool Check (void) const;
   void MaybeDelete (void) const;
   mutable uint32_t m_count;
--- a/src/core/trace-context.cc	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/trace-context.cc	Tue Aug 28 09:37:10 2007 +0200
@@ -272,16 +272,23 @@
   uint16_t myI = 0;
   uint8_t otherCurrentUid;
   uint16_t otherI = 0;
-  do {
-    myCurrentUid = m_data->data[myI];
-    otherCurrentUid = o.m_data->data[otherI];
-    uint8_t mySize = ElementRegistry::GetSize (myCurrentUid);
-    uint8_t otherSize = ElementRegistry::GetSize (otherCurrentUid);
-    myI += 1 + mySize;
-    otherI += 1 + otherSize;
-  } while (myCurrentUid == otherCurrentUid && 
-           myCurrentUid != 0 && 
-           otherCurrentUid != 0);
+
+  myCurrentUid = m_data->data[myI];
+  otherCurrentUid = o.m_data->data[otherI];
+
+  while (myCurrentUid == otherCurrentUid && 
+         myCurrentUid != 0 && 
+         otherCurrentUid != 0 &&
+         myI < m_data->size &&
+         otherI < o.m_data->size)
+    {
+      uint8_t mySize = ElementRegistry::GetSize (myCurrentUid);
+      uint8_t otherSize = ElementRegistry::GetSize (otherCurrentUid);
+      myI += 1 + mySize;
+      otherI += 1 + otherSize;
+      myCurrentUid = m_data->data[myI];
+      otherCurrentUid = o.m_data->data[otherI];
+    }
   if (myCurrentUid == 0 && otherCurrentUid == 0)
     {
       return true;
--- a/src/core/trace-resolver.cc	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/trace-resolver.cc	Tue Aug 28 09:37:10 2007 +0200
@@ -105,4 +105,22 @@
     }
 }
 
+void 
+TraceResolver::SourceCollection::SetFlag (void)
+{
+  m_flag = true;
+}
+
+void 
+TraceResolver::SourceCollection::ClearFlag (void)
+{
+  m_flag = false;
+}
+bool 
+TraceResolver::SourceCollection::IsFlagSet (void)
+{
+  return m_flag;
+}
+
+
 }//namespace ns3
--- a/src/core/trace-resolver.h	Mon Aug 27 20:46:10 2007 +0200
+++ b/src/core/trace-resolver.h	Tue Aug 28 09:37:10 2007 +0200
@@ -70,15 +70,15 @@
    */
   virtual void Disconnect (std::string path, CallbackBase const &cb) = 0;
 
-  virtual void PrintAvailable (std::string path, const TraceContext &context, std::ostream &os) = 0;
-
-
   class SourceCollection
   {
   public:
     void Print (std::ostream &os) const;
     void AddUnique (std::string path, const TraceContext &context,
                     std::string help);
+    void SetFlag (void);
+    void ClearFlag (void);
+    bool IsFlagSet (void);
   private:
     struct Source
     {
@@ -88,9 +88,10 @@
     };
     typedef std::vector<struct Source> SourceVector;
     SourceVector m_sources;
+    bool m_flag;
   };
   virtual void CollectSources (std::string path, const TraceContext &context, 
-                               SourceCollection *collection) {}
+                               SourceCollection *collection) = 0;
 protected:
   /**
    * \param path a namespace path
--- a/utils/print-trace-sources.cc	Mon Aug 27 20:46:10 2007 +0200
+++ b/utils/print-trace-sources.cc	Tue Aug 28 09:37:10 2007 +0200
@@ -17,7 +17,9 @@
   Ptr<CsmaCdNetDevice> csma = Create<CsmaCdNetDevice> (node);
   csma->AddQueue (Queue::CreateDefault ());
 
-  NodeList::GetTraceResolver ()->PrintAvailable ("", TraceContext (), std::cout);
+  TraceResolver::SourceCollection collection;
+  NodeList::GetTraceResolver ()->CollectSources ("", TraceContext (), &collection);
+  collection.Print (std::cout);
 
   return 0;
 }