rewrite interface id metadata
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 02 Jan 2008 12:24:25 +0100
changeset 2235 fb93067a3c2e
parent 2234 c1af59565992
child 2236 a3a52c5ca2b4
rewrite interface id metadata
src/core/object.cc
src/core/object.h
--- a/src/core/object.cc	Wed Jan 02 11:08:11 2008 +0100
+++ b/src/core/object.cc	Wed Jan 02 12:24:25 2008 +0100
@@ -21,7 +21,6 @@
 #include "object.h"
 #include "assert.h"
 #include "singleton.h"
-#include "uid-manager.h"
 #include "trace-resolver.h"
 #include "log.h"
 #include <vector>
@@ -34,30 +33,89 @@
 
 namespace {
 
-class IidManager : public ns3::UidManager
-{};
-
-class IidTree
+class IidManager
 {
 public:
-  void SetParent (uint16_t child, uint16_t parent);
-  uint16_t LookupParent (uint16_t child);
+  uint16_t AllocateUid (std::string name);
+  void SetParent (uint16_t uid, uint16_t parent);
+  uint16_t GetUid (std::string name) const;
+  std::string GetName (uint16_t uid) const;
+  uint16_t GetParent (uint16_t uid) const;
+private:
+  struct IidInformation {
+    std::string name;
+    uint16_t parent;
+  };
+  typedef std::vector<struct IidInformation>::const_iterator Iterator;
+
+  struct IidManager::IidInformation *LookupInformation (uint16_t uid) const;
+
+  std::vector<struct IidInformation> m_information;
+};
 
-private:
-  std::vector<uint16_t> m_parents;
-};
+uint16_t 
+IidManager::AllocateUid (std::string name)
+{
+  uint16_t j = 1;
+  for (Iterator i = m_information.begin (); i != m_information.end (); i++)
+    {
+      if (i->name == name)
+        {
+          NS_FATAL_ERROR ("Trying to allocate twice the same uid: " << name);
+          return 0;
+        }
+      j++;
+    }
+  struct IidInformation information;
+  information.name = name;
+  information.parent = 0;
+  m_information.push_back (information);
+  uint32_t uid = m_information.size ();
+  NS_ASSERT (uid <= 0xffff);
+  return uid;
+}
+
+struct IidManager::IidInformation *
+IidManager::LookupInformation (uint16_t uid) const
+{
+  NS_ASSERT (uid <= m_information.size ());
+  return const_cast<struct IidInformation *> (&m_information[uid-1]);
+}
 
 void 
-IidTree::SetParent (uint16_t child, uint16_t parent)
+IidManager::SetParent (uint16_t uid, uint16_t parent)
+{
+  NS_ASSERT (parent <= m_information.size ());
+  struct IidInformation *information = LookupInformation (uid);
+  information->parent = parent;
+}
+
+uint16_t 
+IidManager::GetUid (std::string name) const
 {
-  m_parents.resize (child+1);
-  m_parents[child] = parent;
+  uint32_t j = 1;
+  for (Iterator i = m_information.begin (); i != m_information.end (); i++)
+    {
+      if (i->name == name)
+        {
+          NS_ASSERT (j <= 0xffff);
+          return j;
+        }
+      j++;
+    }
+  return 0;
+}
+std::string 
+IidManager::GetName (uint16_t uid) const
+{
+  struct IidInformation *information = LookupInformation (uid);
+  return information->name;
 }
 uint16_t 
-IidTree::LookupParent (uint16_t child)
+IidManager::GetParent (uint16_t uid) const
 {
-  NS_ASSERT (child < m_parents.size ());
-  return m_parents[child];
+  struct IidInformation *information = LookupInformation (uid);
+  return information->parent;
 }
 
 } // anonymous namespace
@@ -142,19 +200,20 @@
 InterfaceId 
 InterfaceId::LookupByName (std::string name)
 {
-  uint32_t uid = Singleton<IidManager>::Get ()->LookupByName (name);
-  NS_ASSERT (uid != 0 && uid <= 0xffff);
+  uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
+  NS_ASSERT (uid != 0);
   return InterfaceId (uid);
 }
 InterfaceId 
-InterfaceId::LookupParent (InterfaceId iid)
+InterfaceId::GetParent (void) const
 {
-  return Singleton<IidTree>::Get ()->LookupParent (iid.m_iid);
+  uint16_t parent = Singleton<IidManager>::Get ()->GetParent (m_iid);
+  return InterfaceId (parent);
 }
 std::string 
 InterfaceId::GetName (void) const
 {
-  std::string name = Singleton<IidManager>::Get ()->LookupByUid (m_iid);
+  std::string name = Singleton<IidManager>::Get ()->GetName (m_iid);
   return name;
 }
 
@@ -171,19 +230,19 @@
 InterfaceId
 MakeInterfaceId (std::string name, InterfaceId parent)
 {
-  uint32_t uid = Singleton<IidManager>::Get ()->Allocate (name);
-  NS_ASSERT (uid <= 0xffff);
-  InterfaceId iid = uid;
-  Singleton<IidTree>::Get ()->SetParent (iid.m_iid, parent.m_iid);
-  return iid;
+  uint16_t uid = Singleton<IidManager>::Get ()->AllocateUid (name);
+  NS_ASSERT (uid != 0);
+  Singleton<IidManager>::Get ()->SetParent (uid, parent.m_iid);
+  return InterfaceId (uid);
 }
 
 InterfaceId
 MakeObjectInterfaceId (void)
 {
-  InterfaceId iid = Singleton<IidManager>::Get ()->Allocate ("Object");
-  Singleton<IidTree>::Get ()->SetParent (iid.m_iid, iid.m_iid);
-  return iid;
+  uint16_t uid = Singleton<IidManager>::Get ()->AllocateUid ("Object");
+  NS_ASSERT (uid == 1);
+  Singleton<IidManager>::Get ()->SetParent (uid, uid);
+  return InterfaceId (uid);
 }
 
 
@@ -221,7 +280,7 @@
     InterfaceId cur = currentObject->m_iid;
     while (cur != iid && cur != Object::iid ())
       {
-        cur = InterfaceId::LookupParent (cur);
+        cur = cur.GetParent ();
       }
     if (cur == iid)
       {
@@ -379,7 +438,7 @@
           fullpath.append (name);
           NS_LOG_LOGIC("collect: " << fullpath);
           current->GetTraceResolver ()->CollectSources (fullpath, context, collection);
-          cur = InterfaceId::LookupParent (cur);
+          cur = cur.GetParent ();
         }
       current = current->m_next;
     }
--- a/src/core/object.h	Wed Jan 02 11:08:11 2008 +0100
+++ b/src/core/object.h	Wed Jan 02 12:24:25 2008 +0100
@@ -59,7 +59,7 @@
    * This method cannot fail: it will crash if the input
    * id is not a valid interface id.
    */
-  static InterfaceId LookupParent (InterfaceId iid);
+  InterfaceId GetParent (void) const;
 
   /**
    * \returns the name of this interface.