detect duplicate attributes, even in parent classes
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 16 Apr 2009 10:55:56 +0200
changeset 4320 da28becd0a92
parent 4319 f4a73242f151
child 4321 cbbc344586fc
detect duplicate attributes, even in parent classes
src/core/type-id.cc
--- a/src/core/type-id.cc	Thu Apr 16 10:55:42 2009 +0200
+++ b/src/core/type-id.cc	Thu Apr 16 10:55:56 2009 +0200
@@ -72,6 +72,7 @@
 
 private:
   bool HasTraceSource (uint16_t uid, std::string name);
+  bool HasAttribute (uint16_t uid, std::string name);
 
   struct AttributeInformation {
     std::string name;
@@ -233,6 +234,33 @@
   return i + 1;
 }
 
+bool
+IidManager::HasAttribute (uint16_t uid,
+                          std::string name)
+{
+  struct IidInformation *information  = LookupInformation (uid);
+  while (true)
+    {
+      for (std::vector<struct AttributeInformation>::const_iterator i = information->attributes.begin ();
+           i != information->attributes.end (); ++i)
+        {
+          if (i->name == name)
+            {
+              return true;
+            }
+        }
+      struct IidInformation *parent = LookupInformation (information->parent);
+      if (parent == information)
+        {
+          // top of inheritance tree
+          return false;
+        }
+      // check parent
+      information = parent;
+    }
+  return false;
+}
+
 void 
 IidManager::AddAttribute (uint16_t uid, 
                           std::string name,
@@ -243,14 +271,10 @@
                           ns3::Ptr<const ns3::AttributeChecker> checker)
 {
   struct IidInformation *information = LookupInformation (uid);
-  for (std::vector<struct AttributeInformation>::const_iterator j = information->attributes.begin ();
-       j != information->attributes.end (); j++)
+  if (HasAttribute (uid, name))
     {
-      if (j->name == name)
-        {
-          NS_FATAL_ERROR ("Registered the same attribute twice name=\""<<name<<"\" in TypeId=\""<<information->name<<"\"");
-          return;
-        }
+      NS_FATAL_ERROR ("Attribute \"" << name << "\" already registered on tid=\"" << 
+                      information->name << "\"");
     }
   struct AttributeInformation param;
   param.name = name;