21 #include "singleton.h"
23 #include "global-value.h"
24 #include "object-ptr-container.h"
37 MatchContainer::MatchContainer ()
41 MatchContainer::MatchContainer (
const std::vector<
Ptr<Object> > &objects,
42 const std::vector<std::string> &contexts,
44 : m_objects (objects),
45 m_contexts (contexts),
50 MatchContainer::Iterator
54 return m_objects.begin ();
56 MatchContainer::Iterator
60 return m_objects.end ();
66 return m_objects.size ();
91 for (Iterator tmp =
Begin (); tmp !=
End (); ++tmp)
101 NS_ASSERT (m_objects.size () == m_contexts.size ());
102 for (uint32_t i = 0; i < m_objects.size (); ++i)
105 std::string ctx = m_contexts[i] + name;
106 object->TraceConnect (name, ctx, cb);
114 for (Iterator tmp =
Begin (); tmp !=
End (); ++tmp)
124 NS_ASSERT (m_objects.size () == m_contexts.size ());
125 for (uint32_t i = 0; i < m_objects.size (); ++i)
128 std::string ctx = m_contexts[i] + name;
129 object->TraceDisconnect (name, ctx, cb);
136 for (Iterator tmp =
Begin (); tmp !=
End (); ++tmp)
149 bool Matches (uint32_t i)
const;
151 bool StringToUint32 (std::string str, uint32_t *value)
const;
152 std::string m_element;
156 ArrayMatcher::ArrayMatcher (std::string element)
157 : m_element (element)
162 ArrayMatcher::Matches (uint32_t i)
const
165 if (m_element ==
"*")
170 std::string::size_type tmp;
171 tmp = m_element.find (
"|");
172 if (tmp != std::string::npos)
174 std::string left = m_element.substr (0, tmp-0);
175 std::string right = m_element.substr (tmp+1, m_element.size () - (tmp + 1));
176 ArrayMatcher matcher = ArrayMatcher (left);
177 if (matcher.Matches (i))
182 matcher = ArrayMatcher (right);
183 if (matcher.Matches (i))
188 NS_LOG_DEBUG (
"Array "<<i<<
" does not match "<<m_element);
191 std::string::size_type leftBracket = m_element.find (
"[");
192 std::string::size_type rightBracket = m_element.find (
"]");
193 std::string::size_type dash = m_element.find (
"-");
194 if (leftBracket == 0 && rightBracket == m_element.size () - 1 &&
195 dash > leftBracket && dash < rightBracket)
197 std::string lowerBound = m_element.substr (leftBracket + 1, dash - (leftBracket + 1));
198 std::string upperBound = m_element.substr (dash + 1, rightBracket - (dash + 1));
201 if (StringToUint32 (lowerBound, &min) &&
202 StringToUint32 (upperBound, &max) &&
203 i >= min && i <= max)
215 if (StringToUint32 (m_element, &value) &&
221 NS_LOG_DEBUG (
"Array "<<i<<
" does not match "<<m_element);
226 ArrayMatcher::StringToUint32 (std::string str, uint32_t *value)
const
229 std::istringstream iss;
232 return !iss.bad () && !iss.fail ();
244 void Canonicalize (
void);
245 void DoResolve (std::string path,
Ptr<Object> root);
248 std::string GetResolvedPath (
void)
const;
249 virtual void DoOne (
Ptr<Object> object, std::string path) = 0;
250 std::vector<std::string> m_workStack;
254 Resolver::Resolver (std::string path)
260 Resolver::~Resolver ()
265 Resolver::Canonicalize (
void)
270 std::string::size_type tmp = m_path.find (
"/");
274 m_path =
"/" + m_path;
276 tmp = m_path.find_last_of (
"/");
277 if (tmp != (m_path.size () - 1))
280 m_path = m_path +
"/";
285 Resolver::Resolve (Ptr<Object> root)
289 DoResolve (m_path, root);
293 Resolver::GetResolvedPath (
void)
const
297 std::string fullPath =
"/";
298 for (std::vector<std::string>::const_iterator i = m_workStack.begin (); i != m_workStack.end (); i++)
300 fullPath += *i +
"/";
306 Resolver::DoResolveOne (Ptr<Object>
object)
311 DoOne (
object, GetResolvedPath ());
315 Resolver::DoResolve (std::string path, Ptr<Object> root)
319 std::string::size_type next = path.find (
"/", 1);
321 if (next == std::string::npos)
337 std::string item = path.substr (1, next-1);
338 std::string pathLeft = path.substr (next, path.size ()-next);
349 std::string::size_type offset = path.find (
"/Names");
352 m_workStack.push_back (item);
353 DoResolve (pathLeft, root);
354 m_workStack.pop_back ();
365 Ptr<Object> namedObject = Names::Find<Object> (root, item);
368 NS_LOG_DEBUG (
"Name system resolved item = " << item <<
" to " << namedObject);
369 m_workStack.push_back (item);
370 DoResolve (pathLeft, namedObject);
371 m_workStack.pop_back ();
386 std::string::size_type dollarPos = item.find (
"$");
390 std::string tidString = item.substr (1, item.size () - 1);
391 NS_LOG_DEBUG (
"GetObject="<<tidString<<
" on path="<<GetResolvedPath ());
393 Ptr<Object>
object = root->GetObject<Object> (tid);
396 NS_LOG_DEBUG (
"GetObject ("<<tidString<<
") failed on path="<<GetResolvedPath ());
399 m_workStack.push_back (item);
400 DoResolve (pathLeft,
object);
401 m_workStack.pop_back ();
406 TypeId tid = root->GetInstanceTypeId ();
407 bool foundMatch =
false;
408 for (uint32_t i = 0; i < tid.GetAttributeN(); i++)
410 struct TypeId::AttributeInformation info;
411 info = tid.GetAttribute(i);
412 if (info.name != item && item !=
"*")
417 const PointerChecker *ptr =
dynamic_cast<const PointerChecker *
> (PeekPointer (info.checker));
420 NS_LOG_DEBUG (
"GetAttribute(ptr)="<<info.name<<
" on path="<<GetResolvedPath ());
422 root->GetAttribute (info.name, ptr);
423 Ptr<Object>
object = ptr.Get<Object> ();
427 "\" exists on path=\""<<GetResolvedPath ()<<
"\""
432 m_workStack.push_back (info.name);
433 DoResolve (pathLeft,
object);
434 m_workStack.pop_back ();
437 const ObjectPtrContainerChecker *vectorChecker =
438 dynamic_cast<const ObjectPtrContainerChecker *
> (PeekPointer (info.checker));
439 if (vectorChecker != 0)
441 NS_LOG_DEBUG (
"GetAttribute(vector)="<<info.name<<
" on path="<<GetResolvedPath () << pathLeft);
443 ObjectPtrContainerValue vector;
444 root->GetAttribute (info.name, vector);
445 m_workStack.push_back (info.name);
446 DoArrayResolve (pathLeft, vector);
447 m_workStack.pop_back ();
454 NS_LOG_DEBUG (
"Requested item="<<item<<
" does not exist on path="<<GetResolvedPath ());
461 Resolver::DoArrayResolve (std::string path,
const ObjectPtrContainerValue &container)
466 std::string::size_type next = path.find (
"/", 1);
467 if (next == std::string::npos)
471 std::string item = path.substr (1, next-1);
472 std::string pathLeft = path.substr (next, path.size ()-next);
474 ArrayMatcher matcher = ArrayMatcher (item);
475 ObjectPtrContainerValue::Iterator it;
476 for (it = container.Begin (); it != container.End (); ++it)
478 if (matcher.Matches ((*it).first))
480 std::ostringstream oss;
482 m_workStack.push_back (oss.str ());
483 DoResolve (pathLeft, (*it).second);
484 m_workStack.pop_back ();
494 void ConnectWithoutContext (std::string path,
const CallbackBase &cb);
495 void Connect (std::string path,
const CallbackBase &cb);
496 void DisconnectWithoutContext (std::string path,
const CallbackBase &cb);
497 void Disconnect (std::string path,
const CallbackBase &cb);
500 void RegisterRootNamespaceObject (
Ptr<Object> obj);
501 void UnregisterRootNamespaceObject (
Ptr<Object> obj);
503 uint32_t GetRootNamespaceObjectN (
void)
const;
504 Ptr<Object> GetRootNamespaceObject (uint32_t i)
const;
507 void ParsePath (std::string path, std::string *root, std::string *leaf)
const;
508 typedef std::vector<Ptr<Object> > Roots;
513 ConfigImpl::ParsePath (std::string path, std::string *root, std::string *leaf)
const
517 std::string::size_type slash = path.find_last_of (
"/");
519 *root = path.substr (0, slash);
520 *leaf = path.substr (slash+1, path.size ()-(slash+1));
525 ConfigImpl::Set (std::string path,
const AttributeValue &value)
529 std::string root, leaf;
530 ParsePath (path, &root, &leaf);
531 Config::MatchContainer container = LookupMatches (root);
532 container.Set (leaf, value);
535 ConfigImpl::ConnectWithoutContext (std::string path,
const CallbackBase &cb)
538 std::string root, leaf;
539 ParsePath (path, &root, &leaf);
540 Config::MatchContainer container = LookupMatches (root);
541 container.ConnectWithoutContext (leaf, cb);
544 ConfigImpl::DisconnectWithoutContext (std::string path,
const CallbackBase &cb)
547 std::string root, leaf;
548 ParsePath (path, &root, &leaf);
549 Config::MatchContainer container = LookupMatches (root);
550 container.DisconnectWithoutContext (leaf, cb);
553 ConfigImpl::Connect (std::string path,
const CallbackBase &cb)
557 std::string root, leaf;
558 ParsePath (path, &root, &leaf);
559 Config::MatchContainer container = LookupMatches (root);
560 container.Connect (leaf, cb);
563 ConfigImpl::Disconnect (std::string path,
const CallbackBase &cb)
567 std::string root, leaf;
568 ParsePath (path, &root, &leaf);
569 Config::MatchContainer container = LookupMatches (root);
570 container.Disconnect (leaf, cb);
573 Config::MatchContainer
574 ConfigImpl::LookupMatches (std::string path)
577 class LookupMatchesResolver :
public Resolver
580 LookupMatchesResolver (std::string path)
583 virtual void DoOne (Ptr<Object>
object, std::string path) {
584 m_objects.push_back (
object);
585 m_contexts.push_back (path);
587 std::vector<Ptr<Object> > m_objects;
588 std::vector<std::string> m_contexts;
589 } resolver = LookupMatchesResolver (path);
590 for (Roots::const_iterator i = m_roots.begin (); i != m_roots.end (); i++)
592 resolver.Resolve (*i);
600 resolver.Resolve (0);
602 return Config::MatchContainer (resolver.m_objects, resolver.m_contexts, path);
606 ConfigImpl::RegisterRootNamespaceObject (Ptr<Object> obj)
609 m_roots.push_back (obj);
613 ConfigImpl::UnregisterRootNamespaceObject (Ptr<Object> obj)
617 for (std::vector<Ptr<Object> >::iterator i = m_roots.begin (); i != m_roots.end (); i++)
628 ConfigImpl::GetRootNamespaceObjectN (
void)
const
631 return m_roots.size ();
634 ConfigImpl::GetRootNamespaceObject (uint32_t i)
const
658 (*i)->ResetInitialValue ();
678 std::string::size_type pos = fullName.rfind (
"::");
679 if (pos == std::string::npos)
683 std::string tidName = fullName.substr (0, pos);
684 std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2));
694 if (tmp.name == paramName)
uint32_t GetAttributeN(void) const
#define NS_LOG_FUNCTION(parameters)
bool SetDefaultFailSafe(std::string fullName, const AttributeValue &value)
Hold a value for an Attribute.
void Disconnect(std::string path, const CallbackBase &cb)
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
std::string GetMatchedPath(uint32_t i) const
void Disconnect(std::string name, const CallbackBase &cb)
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
void UnregisterRootNamespaceObject(Ptr< Object > obj)
#define NS_LOG_FUNCTION_NOARGS()
void Set(std::string path, const AttributeValue &value)
Ptr< Object > GetRootNamespaceObject(uint32_t i)
void Connect(std::string path, const CallbackBase &cb)
#define NS_FATAL_ERROR(msg)
fatal error handling
MatchContainer::Iterator End(void) const
Config::MatchContainer LookupMatches(std::string path)
void ConnectWithoutContext(std::string name, const CallbackBase &cb)
static uint32_t GetRegisteredN(void)
static Iterator Begin(void)
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
static TypeId GetRegistered(uint32_t i)
static void Bind(std::string name, const AttributeValue &value)
uint32_t GetN(void) const
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
void SetDefault(std::string name, const AttributeValue &value)
Ptr< Object > Get(uint32_t i) const
hold a set of objects which match a specific search string.
void Connect(std::string name, const CallbackBase &cb)
void RegisterRootNamespaceObject(Ptr< Object > obj)
bool SetAttributeInitialValue(uint32_t i, Ptr< const AttributeValue > initialValue)
void DisconnectWithoutContext(std::string name, const CallbackBase &cb)
bool SetGlobalFailSafe(std::string name, const AttributeValue &value)
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
static Iterator End(void)
static bool BindFailSafe(std::string name, const AttributeValue &value)
#define NS_LOG_DEBUG(msg)
uint32_t GetRootNamespaceObjectN(void)
#define NS_LOG_ERROR(msg)
void Set(std::string name, const AttributeValue &value)
MatchContainer::Iterator Begin(void) const
contain a set of ns3::Object pointers.
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
std::string GetPath(void) const
void SetAttribute(std::string name, const AttributeValue &value)
a unique identifier for an interface.
void SetGlobal(std::string name, const AttributeValue &value)
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
static TypeId LookupByName(std::string name)