44 std::map<std::string, NameNode *> m_nameMap;
48 : m_parent (0), m_name (
""), m_object (0)
52 NameNode::NameNode (
const NameNode &nameNode)
54 m_parent = nameNode.m_parent;
55 m_name = nameNode.m_name;
56 m_object = nameNode.m_object;
57 m_nameMap = nameNode.m_nameMap;
61 NameNode::operator = (
const NameNode &rhs)
63 m_parent = rhs.m_parent;
65 m_object = rhs.m_object;
66 m_nameMap = rhs.m_nameMap;
70 NameNode::NameNode (NameNode *parent, std::string name, Ptr<Object>
object)
71 : m_parent (parent), m_name (name), m_object (object)
76 NameNode::~NameNode ()
88 bool Add (std::string path, std::string name,
Ptr<Object> object);
91 bool Rename (std::string oldpath, std::string newname);
92 bool Rename (std::string path, std::string oldname, std::string newname);
93 bool Rename (
Ptr<Object> context, std::string oldname, std::string newname);
101 Ptr<Object> Find (std::string path, std::string name);
109 bool IsDuplicateName (
NameNode *node, std::string name);
112 std::map<Ptr<Object>,
NameNode *> m_objectMap;
116 NamesPriv::Get (
void)
123 NamesPriv::NamesPriv ()
128 m_root.m_name =
"Names";
132 NamesPriv::~NamesPriv ()
140 NamesPriv::Clear (
void)
147 for (std::map<Ptr<Object>, NameNode *>::iterator i = m_objectMap.begin (); i != m_objectMap.end (); ++i)
153 m_objectMap.clear ();
156 m_root.m_name =
"Names";
158 m_root.m_nameMap.clear ();
162 NamesPriv::Add (std::string name, Ptr<Object>
object)
180 std::string namespaceName =
"/Names";
181 std::string::size_type offset = name.find (namespaceName);
188 offset = name.find (
"/");
191 NS_ASSERT_MSG (
false,
"NamesPriv::Add(): Name begins with '/' but not \"/Names\"");
195 name =
"/Names/" + name;
205 std::string::size_type i = name.rfind (
"/");
206 NS_ASSERT_MSG (i != std::string::npos,
"NamesPriv::Add(): Internal error. Can't find '/' in name");
213 NS_ASSERT_MSG (i != 0,
"NamesPriv::Add(): Can't find a name in the path string");
220 return Add (name.substr (0, i), name.substr (i + 1), object);
224 NamesPriv::Add (std::string path, std::string name, Ptr<Object>
object)
227 if (path ==
"/Names")
229 return Add (Ptr<Object> (0,
false), name,
object);
231 return Add (Find (path), name,
object);
235 NamesPriv::Add (Ptr<Object> context, std::string name, Ptr<Object>
object)
239 if (IsNamed (
object))
248 node = IsNamed (context);
249 NS_ASSERT_MSG (node,
"NamesPriv::Name(): context must point to a previously named node");
256 if (IsDuplicateName (node, name))
262 NameNode *newNode =
new NameNode (node, name,
object);
263 node->m_nameMap[name] = newNode;
264 m_objectMap[object] = newNode;
270 NamesPriv::Rename (std::string oldpath, std::string newname)
290 std::string namespaceName =
"/Names";
291 std::string::size_type offset = oldpath.find (namespaceName);
298 offset = oldpath.find (
"/");
301 NS_ASSERT_MSG (
false,
"NamesPriv::Add(): Name begins with '/' but not \"/Names\"");
305 oldpath =
"/Names/" + oldpath;
315 std::string::size_type i = oldpath.rfind (
"/");
316 NS_ASSERT_MSG (i != std::string::npos,
"NamesPriv::Add(): Internal error. Can't find '/' in name");
323 NS_ASSERT_MSG (i != 0,
"NamesPriv::Add(): Can't find a name in the path string");
330 return Rename (oldpath.substr (0, i), oldpath.substr (i + 1), newname);
334 NamesPriv::Rename (std::string path, std::string oldname, std::string newname)
337 if (path ==
"/Names")
339 return Rename (Ptr<Object> (0,
false), oldname, newname);
341 return Rename (Find (path), oldname, newname);
345 NamesPriv::Rename (Ptr<Object> context, std::string oldname, std::string newname)
352 node = IsNamed (context);
353 NS_ASSERT_MSG (node,
"NamesPriv::Name(): context must point to a previously named node");
360 if (IsDuplicateName (node, newname))
366 std::map<std::string, NameNode *>::iterator i = node->m_nameMap.find (oldname);
367 if (i == node->m_nameMap.end ())
383 NameNode *changeNode = i->second;
384 node->m_nameMap.erase (i);
385 changeNode->m_name = newname;
386 node->m_nameMap[newname] = changeNode;
392 NamesPriv::FindName (Ptr<Object>
object)
396 std::map<Ptr<Object>, NameNode *>::iterator i = m_objectMap.find (
object);
397 if (i == m_objectMap.end ())
405 return i->second->m_name;
410 NamesPriv::FindPath (Ptr<Object>
object)
414 std::map<Ptr<Object>, NameNode *>::iterator i = m_objectMap.find (
object);
415 if (i == m_objectMap.end ())
421 NameNode *p = i->second;
422 NS_ASSERT_MSG (p,
"NamesPriv::FindFullName(): Internal error: Invalid NameNode pointer from map");
428 path =
"/" + p->m_name + path;
431 while ((p = p->m_parent) != 0);
438 NamesPriv::Find (std::string path)
456 std::string namespaceName =
"/Names/";
457 std::string remaining;
459 std::string::size_type offset = path.find (namespaceName);
463 remaining = path.substr (namespaceName.size ());
471 NameNode *node = &m_root;
482 NS_LOG_LOGIC (
"Looking for the object of name " << remaining);
483 offset = remaining.find (
"/");
484 if (offset == std::string::npos)
490 std::map<std::string, NameNode *>::iterator i = node->m_nameMap.find (remaining);
491 if (i == node->m_nameMap.end ())
499 return i->second->m_object;
508 offset = remaining.find (
"/");
509 std::string segment = remaining.substr (0, offset);
511 std::map<std::string, NameNode *>::iterator i = node->m_nameMap.find (segment);
512 if (i == node->m_nameMap.end ())
520 remaining = remaining.substr (offset + 1);
527 NS_ASSERT_MSG (node,
"NamesPriv::Find(): Internal error: this can't happen");
532 NamesPriv::Find (std::string path, std::string name)
536 if (path ==
"/Names")
538 return Find (Ptr<Object> (0,
false), name);
540 return Find (Find (path), name);
544 NamesPriv::Find (Ptr<Object> context, std::string name)
557 node = IsNamed (context);
560 NS_LOG_LOGIC (
"Context does not point to a previously named node");
565 std::map<std::string, NameNode *>::iterator i = node->m_nameMap.find (name);
566 if (i == node->m_nameMap.end ())
574 return i->second->m_object;
579 NamesPriv::IsNamed (Ptr<Object>
object)
583 std::map<Ptr<Object>, NameNode *>::iterator i = m_objectMap.find (
object);
584 if (i == m_objectMap.end ())
586 NS_LOG_LOGIC (
"Object does not exist in object map, returning NameNode 0");
591 NS_LOG_LOGIC (
"Object exists in object map, returning NameNode " << &i->second);
597 NamesPriv::IsDuplicateName (NameNode *node, std::string name)
601 std::map<std::string, NameNode *>::iterator i = node->m_nameMap.find (name);
602 if (i == node->m_nameMap.end ())
618 bool result = NamesPriv::Get ()->Add (name,
object);
626 bool result = NamesPriv::Get ()->Rename (oldpath, newname);
627 NS_ABORT_MSG_UNLESS (result,
"Names::Rename(): Error renaming " << oldpath <<
" to " << newname);
634 bool result = NamesPriv::Get ()->Add (path, name,
object);
642 bool result = NamesPriv::Get ()->Rename (path, oldname, newname);
643 NS_ABORT_MSG_UNLESS (result,
"Names::Rename (): Error renaming " << path <<
" " << oldname <<
" to " << newname);
650 bool result = NamesPriv::Get ()->Add (context, name,
object);
651 NS_ABORT_MSG_UNLESS (result,
"Names::Add(): Error adding name " << name <<
" under context " << &context);
658 bool result = NamesPriv::Get ()->Rename (context, oldname, newname);
659 NS_ABORT_MSG_UNLESS (result,
"Names::Rename (): Error renaming " << oldname <<
" to " << newname <<
" under context " <<
667 return NamesPriv::Get ()->FindName (
object);
674 return NamesPriv::Get ()->FindPath (
object);
681 return NamesPriv::Get ()->Clear ();
688 return NamesPriv::Get ()->Find (name);
695 return NamesPriv::Get ()->Find (path, name);
702 return NamesPriv::Get ()->Find (context, name);
#define NS_LOG_FUNCTION(parameters)
#define NS_LOG_COMPONENT_DEFINE(name)
#define NS_LOG_FUNCTION_NOARGS()
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
A directory of name and Ptr<Object> associations that allows us to give any ns3 Object a name...
#define NS_LOG_LOGIC(msg)
static void Rename(std::string oldpath, std::string newname)
Rename a previously associated name.
static std::string FindPath(Ptr< Object > object)
#define NS_ASSERT_MSG(condition, message)
static std::string FindName(Ptr< Object > object)
static Ptr< Object > FindInternal(std::string path)
Non-templated internal version of Names::Find.