A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
config.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "config.h"
9
10#include "global-value.h"
11#include "log.h"
12#include "names.h"
14#include "object.h"
15#include "pointer.h"
16#include "singleton.h"
17
18#include <sstream>
19
20/**
21 * \file
22 * \ingroup config-impl
23 * ns3::Config implementations.
24 */
25
26/**
27 * \defgroup config-impl Config implementations
28 * \ingroup config
29 */
30namespace ns3
31{
32
34
35namespace Config
36{
37
42
43MatchContainer::MatchContainer(const std::vector<Ptr<Object>>& objects,
44 const std::vector<std::string>& contexts,
45 std::string path)
46 : m_objects(objects),
47 m_contexts(contexts),
48 m_path(path)
49{
50 NS_LOG_FUNCTION(this << &objects << &contexts << path);
51}
52
55{
56 NS_LOG_FUNCTION(this);
57 return m_objects.begin();
58}
59
62{
63 NS_LOG_FUNCTION(this);
64 return m_objects.end();
65}
66
67std::size_t
69{
70 NS_LOG_FUNCTION(this);
71 return m_objects.size();
72}
73
75MatchContainer::Get(std::size_t i) const
76{
77 NS_LOG_FUNCTION(this << i);
78 return m_objects[i];
79}
80
81std::string
83{
84 NS_LOG_FUNCTION(this << i);
85 return m_contexts[i];
86}
87
88std::string
90{
91 NS_LOG_FUNCTION(this);
92 return m_path;
93}
94
95void
96MatchContainer::Set(std::string name, const AttributeValue& value)
97{
98 NS_LOG_FUNCTION(this << name << &value);
99 for (auto tmp = Begin(); tmp != End(); ++tmp)
100 {
101 Ptr<Object> object = *tmp;
102 // Let ObjectBase::SetAttribute raise any errors
103 object->SetAttribute(name, value);
104 }
105}
106
107bool
108MatchContainer::SetFailSafe(std::string name, const AttributeValue& value)
109{
110 NS_LOG_FUNCTION(this << name << &value);
111 bool ok = false;
112 for (auto tmp = Begin(); tmp != End(); ++tmp)
113 {
114 Ptr<Object> object = *tmp;
115 ok |= object->SetAttributeFailSafe(name, value);
116 }
117 return ok;
118}
119
120void
121MatchContainer::Connect(std::string name, const CallbackBase& cb)
122{
123 if (!ConnectFailSafe(name, cb))
124 {
125 NS_FATAL_ERROR("Could not connect callback to " << name);
126 }
127}
128
129bool
131{
132 NS_LOG_FUNCTION(this << name << &cb);
133 NS_ASSERT(m_objects.size() == m_contexts.size());
134 bool ok = false;
135 for (uint32_t i = 0; i < m_objects.size(); ++i)
136 {
137 Ptr<Object> object = m_objects[i];
138 std::string ctx = m_contexts[i] + name;
139 ok |= object->TraceConnect(name, ctx, cb);
140 }
141 return ok;
142}
143
144void
146{
147 if (!ConnectWithoutContextFailSafe(name, cb))
148 {
149 NS_FATAL_ERROR("Could not connect callback to " << name);
150 }
151}
152
153bool
155{
156 NS_LOG_FUNCTION(this << name << &cb);
157 bool ok = false;
158 for (auto tmp = Begin(); tmp != End(); ++tmp)
159 {
160 Ptr<Object> object = *tmp;
161 ok |= object->TraceConnectWithoutContext(name, cb);
162 }
163 return ok;
164}
165
166void
167MatchContainer::Disconnect(std::string name, const CallbackBase& cb)
168{
169 NS_LOG_FUNCTION(this << name << &cb);
170 NS_ASSERT(m_objects.size() == m_contexts.size());
171 for (uint32_t i = 0; i < m_objects.size(); ++i)
172 {
173 Ptr<Object> object = m_objects[i];
174 std::string ctx = m_contexts[i] + name;
175 object->TraceDisconnect(name, ctx, cb);
176 }
177}
178
179void
181{
182 NS_LOG_FUNCTION(this << name << &cb);
183 for (auto tmp = Begin(); tmp != End(); ++tmp)
184 {
185 Ptr<Object> object = *tmp;
186 object->TraceDisconnectWithoutContext(name, cb);
187 }
188}
189
190/**
191 * \ingroup config-impl
192 * Helper to test if an array entry matches a config path specification.
193 */
195{
196 public:
197 /**
198 * Construct from a Config path specification.
199 *
200 * \param [in] element The Config path specification.
201 */
202 ArrayMatcher(std::string element);
203 /**
204 * Test if a specific index matches the Config Path.
205 *
206 * \param [in] i The index.
207 * \returns \c true if the index matches the Config Path.
208 */
209 bool Matches(std::size_t i) const;
210
211 private:
212 /**
213 * Convert a string to an \c uint32_t.
214 *
215 * \param [in] str The string.
216 * \param [in] value The location to store the \c uint32_t.
217 * \returns \c true if the string could be converted.
218 */
219 bool StringToUint32(std::string str, uint32_t* value) const;
220 /** The Config path element. */
221 std::string m_element;
222
223}; // class ArrayMatcher
224
225ArrayMatcher::ArrayMatcher(std::string element)
226 : m_element(element)
227{
228 NS_LOG_FUNCTION(this << element);
229}
230
231bool
232ArrayMatcher::Matches(std::size_t i) const
233{
234 NS_LOG_FUNCTION(this << i);
235 if (m_element == "*")
236 {
237 NS_LOG_DEBUG("Array " << i << " matches *");
238 return true;
239 }
240 std::string::size_type tmp;
241 tmp = m_element.find('|');
242 if (tmp != std::string::npos)
243 {
244 std::string left = m_element.substr(0, tmp - 0);
245 std::string right = m_element.substr(tmp + 1, m_element.size() - (tmp + 1));
246 ArrayMatcher matcher = ArrayMatcher(left);
247 if (matcher.Matches(i))
248 {
249 NS_LOG_DEBUG("Array " << i << " matches " << left);
250 return true;
251 }
252 matcher = ArrayMatcher(right);
253 if (matcher.Matches(i))
254 {
255 NS_LOG_DEBUG("Array " << i << " matches " << right);
256 return true;
257 }
258 NS_LOG_DEBUG("Array " << i << " does not match " << m_element);
259 return false;
260 }
261 std::string::size_type leftBracket = m_element.find('[');
262 std::string::size_type rightBracket = m_element.find(']');
263 std::string::size_type dash = m_element.find('-');
264 if (leftBracket == 0 && rightBracket == m_element.size() - 1 && dash > leftBracket &&
265 dash < rightBracket)
266 {
267 std::string lowerBound = m_element.substr(leftBracket + 1, dash - (leftBracket + 1));
268 std::string upperBound = m_element.substr(dash + 1, rightBracket - (dash + 1));
269 uint32_t min;
270 uint32_t max;
271 if (StringToUint32(lowerBound, &min) && StringToUint32(upperBound, &max) && i >= min &&
272 i <= max)
273 {
274 NS_LOG_DEBUG("Array " << i << " matches " << m_element);
275 return true;
276 }
277 else
278 {
279 NS_LOG_DEBUG("Array " << i << " does not " << m_element);
280 return false;
281 }
282 }
283 uint32_t value;
284 if (StringToUint32(m_element, &value) && i == value)
285 {
286 NS_LOG_DEBUG("Array " << i << " matches " << m_element);
287 return true;
288 }
289 NS_LOG_DEBUG("Array " << i << " does not match " << m_element);
290 return false;
291}
292
293bool
294ArrayMatcher::StringToUint32(std::string str, uint32_t* value) const
295{
296 NS_LOG_FUNCTION(this << str << value);
297 std::istringstream iss;
298 iss.str(str);
299 iss >> (*value);
300 return !iss.bad() && !iss.fail();
301}
302
303/**
304 * \ingroup config-impl
305 * Abstract class to parse Config paths into object references.
306 */
308{
309 public:
310 /**
311 * Construct from a base Config path.
312 *
313 * \param [in] path The Config path.
314 */
315 Resolver(std::string path);
316 /** Destructor. */
317 virtual ~Resolver();
318
319 /**
320 * Parse the stored Config path into an object reference,
321 * beginning at the indicated root object.
322 *
323 * \param [in] root The object corresponding to the current position in
324 * in the Config path.
325 */
326 void Resolve(Ptr<Object> root);
327
328 private:
329 /** Ensure the Config path starts and ends with a '/'. */
330 void Canonicalize();
331 /**
332 * Parse the next element in the Config path.
333 *
334 * \param [in] path The remaining portion of the Config path.
335 * \param [in] root The object corresponding to the current position
336 * in the Config path.
337 */
338 void DoResolve(std::string path, Ptr<Object> root);
339 /**
340 * Parse an index on the Config path.
341 *
342 * \param [in] path The remaining Config path.
343 * \param [in,out] vector The resulting list of matching objects.
344 */
345 void DoArrayResolve(std::string path, const ObjectPtrContainerValue& vector);
346 /**
347 * Handle one object found on the path.
348 *
349 * \param [in] object The current object on the Config path.
350 */
351 void DoResolveOne(Ptr<Object> object);
352 /**
353 * Get the current Config path.
354 *
355 * \returns The current Config path.
356 */
357 std::string GetResolvedPath() const;
358 /**
359 * Handle one found object.
360 *
361 * \param [in] object The found object.
362 * \param [in] path The matching Config path context.
363 */
364 virtual void DoOne(Ptr<Object> object, std::string path) = 0;
365
366 /** Current list of path tokens. */
367 std::vector<std::string> m_workStack;
368 /** The Config path. */
369 std::string m_path;
370
371}; // class Resolver
372
373Resolver::Resolver(std::string path)
374 : m_path(path)
375{
376 NS_LOG_FUNCTION(this << path);
377 Canonicalize();
378}
379
384
385void
387{
388 NS_LOG_FUNCTION(this);
389
390 // ensure that we start and end with a '/'
391 std::string::size_type tmp = m_path.find('/');
392 if (tmp != 0)
393 {
394 // no slash at start
395 m_path = "/" + m_path;
396 }
397 tmp = m_path.find_last_of('/');
398 if (tmp != (m_path.size() - 1))
399 {
400 // no slash at end
401 m_path = m_path + "/";
402 }
403}
404
405void
407{
408 NS_LOG_FUNCTION(this << root);
409
410 DoResolve(m_path, root);
411}
412
413std::string
415{
416 NS_LOG_FUNCTION(this);
417
418 std::string fullPath = "/";
419 for (auto i = m_workStack.begin(); i != m_workStack.end(); i++)
420 {
421 fullPath += *i + "/";
422 }
423 return fullPath;
424}
425
426void
428{
429 NS_LOG_FUNCTION(this << object);
430
431 NS_LOG_DEBUG("resolved=" << GetResolvedPath());
432 DoOne(object, GetResolvedPath());
433}
434
435void
436Resolver::DoResolve(std::string path, Ptr<Object> root)
437{
438 NS_LOG_FUNCTION(this << path << root);
439 NS_ASSERT((path.find('/')) == 0);
440 std::string::size_type next = path.find('/', 1);
441
442 if (next == std::string::npos)
443 {
444 //
445 // If root is zero, we're beginning to see if we can use the object name
446 // service to resolve this path. It is impossible to have a object name
447 // associated with the root of the object name service since that root
448 // is not an object. This path must be referring to something in another
449 // namespace and it will have been found already since the name service
450 // is always consulted last.
451 //
452 if (root)
453 {
454 DoResolveOne(root);
455 }
456 return;
457 }
458 std::string item = path.substr(1, next - 1);
459 std::string pathLeft = path.substr(next, path.size() - next);
460
461 //
462 // If root is zero, we're beginning to see if we can use the object name
463 // service to resolve this path. In this case, we must see the name space
464 // "/Names" on the front of this path. There is no object associated with
465 // the root of the "/Names" namespace, so we just ignore it and move on to
466 // the next segment.
467 //
468 if (!root)
469 {
470 std::string::size_type offset = path.find("/Names");
471 if (offset == 0)
472 {
473 m_workStack.push_back(item);
474 DoResolve(pathLeft, root);
475 m_workStack.pop_back();
476 return;
477 }
478 }
479
480 //
481 // We have an item (possibly a segment of a namespace path. Check to see if
482 // we can determine that this segment refers to a named object. If root is
483 // zero, this means to look in the root of the "/Names" name space, otherwise
484 // it refers to a name space context (level).
485 //
486 Ptr<Object> namedObject = Names::Find<Object>(root, item);
487 if (namedObject)
488 {
489 NS_LOG_DEBUG("Name system resolved item = " << item << " to " << namedObject);
490 m_workStack.push_back(item);
491 DoResolve(pathLeft, namedObject);
492 m_workStack.pop_back();
493 return;
494 }
495
496 //
497 // We're done with the object name service hooks, so proceed down the path
498 // of types and attributes; but only if root is nonzero. If root is zero
499 // and we find ourselves here, we are trying to check in the namespace for
500 // a path that is not in the "/Names" namespace. We will have previously
501 // found any matches, so we just bail out.
502 //
503 if (!root)
504 {
505 return;
506 }
507 std::string::size_type dollarPos = item.find('$');
508 if (dollarPos == 0)
509 {
510 // This is a call to GetObject
511 std::string tidString = item.substr(1, item.size() - 1);
512 NS_LOG_DEBUG("GetObject=" << tidString << " on path=" << GetResolvedPath());
513 TypeId tid = TypeId::LookupByName(tidString);
514 Ptr<Object> object = root->GetObject<Object>(tid);
515 if (!object)
516 {
517 NS_LOG_DEBUG("GetObject (" << tidString << ") failed on path=" << GetResolvedPath());
518 return;
519 }
520 m_workStack.push_back(item);
521 DoResolve(pathLeft, object);
522 m_workStack.pop_back();
523 }
524 else
525 {
526 // this is a normal attribute.
527 TypeId tid;
528 TypeId nextTid = root->GetInstanceTypeId();
529 bool foundMatch = false;
530
531 do
532 {
533 tid = nextTid;
534
535 for (uint32_t i = 0; i < tid.GetAttributeN(); i++)
536 {
538 info = tid.GetAttribute(i);
539 if (info.name != item && item != "*")
540 {
541 continue;
542 }
543 // attempt to cast to a pointer checker.
544 const auto pChecker =
545 dynamic_cast<const PointerChecker*>(PeekPointer(info.checker));
546 if (pChecker != nullptr)
547 {
548 NS_LOG_DEBUG("GetAttribute(ptr)=" << info.name
549 << " on path=" << GetResolvedPath());
550 PointerValue pValue;
551 root->GetAttribute(info.name, pValue);
552 Ptr<Object> object = pValue.Get<Object>();
553 if (!object)
554 {
555 NS_LOG_ERROR("Requested object name=\"" << item << "\" exists on path=\""
556 << GetResolvedPath()
557 << "\""
558 " but is null.");
559 continue;
560 }
561 foundMatch = true;
562 m_workStack.push_back(info.name);
563 DoResolve(pathLeft, object);
564 m_workStack.pop_back();
565 }
566 // attempt to cast to an object vector.
567 const auto vectorChecker =
568 dynamic_cast<const ObjectPtrContainerChecker*>(PeekPointer(info.checker));
569 if (vectorChecker != nullptr)
570 {
571 NS_LOG_DEBUG("GetAttribute(vector)=" << info.name << " on path="
572 << GetResolvedPath() << pathLeft);
573 foundMatch = true;
575 root->GetAttribute(info.name, vector);
576 m_workStack.push_back(info.name);
577 DoArrayResolve(pathLeft, vector);
578 m_workStack.pop_back();
579 }
580 // this could be anything else and we don't know what to do with it.
581 // So, we just ignore it.
582 }
583
584 nextTid = tid.GetParent();
585 } while (nextTid != tid);
586
587 if (!foundMatch)
588 {
589 NS_LOG_DEBUG("Requested item=" << item
590 << " does not exist on path=" << GetResolvedPath());
591 return;
592 }
593 }
594}
595
596void
597Resolver::DoArrayResolve(std::string path, const ObjectPtrContainerValue& container)
598{
599 NS_LOG_FUNCTION(this << path << &container);
600 NS_ASSERT(!path.empty());
601 NS_ASSERT((path.find('/')) == 0);
602 std::string::size_type next = path.find('/', 1);
603 if (next == std::string::npos)
604 {
605 return;
606 }
607 std::string item = path.substr(1, next - 1);
608 std::string pathLeft = path.substr(next, path.size() - next);
609
610 ArrayMatcher matcher = ArrayMatcher(item);
612 for (it = container.Begin(); it != container.End(); ++it)
613 {
614 if (matcher.Matches((*it).first))
615 {
616 std::ostringstream oss;
617 oss << (*it).first;
618 m_workStack.push_back(oss.str());
619 DoResolve(pathLeft, (*it).second);
620 m_workStack.pop_back();
621 }
622 }
623}
624
625/**
626 * \ingroup config-impl
627 * Config system implementation class.
628 */
629class ConfigImpl : public Singleton<ConfigImpl>
630{
631 public:
632 // Keep Set and SetFailSafe since their errors are triggered
633 // by the underlying ObjectBase functions.
634 /** \copydoc ns3::Config::Set() */
635 void Set(std::string path, const AttributeValue& value);
636 /** \copydoc ns3::Config::SetFailSafe() */
637 bool SetFailSafe(std::string path, const AttributeValue& value);
638 /** \copydoc ns3::Config::ConnectWithoutContextFailSafe() */
639 bool ConnectWithoutContextFailSafe(std::string path, const CallbackBase& cb);
640 /** \copydoc ns3::Config::ConnectFailSafe() */
641 bool ConnectFailSafe(std::string path, const CallbackBase& cb);
642 /** \copydoc ns3::Config::DisconnectWithoutContext() */
643 void DisconnectWithoutContext(std::string path, const CallbackBase& cb);
644 /** \copydoc ns3::Config::Disconnect() */
645 void Disconnect(std::string path, const CallbackBase& cb);
646 /** \copydoc ns3::Config::LookupMatches() */
647 MatchContainer LookupMatches(std::string path);
648
649 /** \copydoc ns3::Config::RegisterRootNamespaceObject() */
651 /** \copydoc ns3::Config::UnregisterRootNamespaceObject() */
653
654 /** \copydoc ns3::Config::GetRootNamespaceObjectN() */
655 std::size_t GetRootNamespaceObjectN() const;
656 /** \copydoc ns3::Config::GetRootNamespaceObject() */
657 Ptr<Object> GetRootNamespaceObject(std::size_t i) const;
658
659 private:
660 /**
661 * Break a Config path into the leading path and the last leaf token.
662 * \param [in] path The Config path.
663 * \param [in,out] root The leading part of the \pname{path},
664 * up to the final slash.
665 * \param [in,out] leaf The trailing part of the \pname{path}.
666 */
667 void ParsePath(std::string path, std::string* root, std::string* leaf) const;
668
669 /** Container type to hold the root Config path tokens. */
670 typedef std::vector<Ptr<Object>> Roots;
671
672 /** The list of Config path roots. */
674
675}; // class ConfigImpl
676
677void
678ConfigImpl::ParsePath(std::string path, std::string* root, std::string* leaf) const
679{
680 NS_LOG_FUNCTION(this << path << root << leaf);
681
682 std::string::size_type slash = path.find_last_of('/');
683 NS_ASSERT(slash != std::string::npos);
684 *root = path.substr(0, slash);
685 *leaf = path.substr(slash + 1, path.size() - (slash + 1));
686 NS_LOG_FUNCTION(path << *root << *leaf);
687}
688
689void
690ConfigImpl::Set(std::string path, const AttributeValue& value)
691{
692 NS_LOG_FUNCTION(this << path << &value);
693
694 std::string root;
695 std::string leaf;
696 ParsePath(path, &root, &leaf);
697 MatchContainer container = LookupMatches(root);
698 container.Set(leaf, value);
699}
700
701bool
702ConfigImpl::SetFailSafe(std::string path, const AttributeValue& value)
703{
704 NS_LOG_FUNCTION(this << path << &value);
705
706 std::string root;
707 std::string leaf;
708 ParsePath(path, &root, &leaf);
709 MatchContainer container = LookupMatches(root);
710 return container.SetFailSafe(leaf, value);
711}
712
713bool
715{
716 NS_LOG_FUNCTION(this << path << &cb);
717 std::string root;
718 std::string leaf;
719 ParsePath(path, &root, &leaf);
720 MatchContainer container = LookupMatches(root);
721 return container.ConnectWithoutContextFailSafe(leaf, cb);
722}
723
724void
726{
727 NS_LOG_FUNCTION(this << path << &cb);
728 std::string root;
729 std::string leaf;
730 ParsePath(path, &root, &leaf);
731 MatchContainer container = LookupMatches(root);
732 if (container.GetN() == 0)
733 {
734 std::size_t lastFwdSlash = root.rfind('/');
735 NS_LOG_WARN("Failed to disconnect "
736 << leaf << ", the Requested object name = " << root.substr(lastFwdSlash + 1)
737 << " does not exits on path " << root.substr(0, lastFwdSlash));
738 }
739 container.DisconnectWithoutContext(leaf, cb);
740}
741
742bool
743ConfigImpl::ConnectFailSafe(std::string path, const CallbackBase& cb)
744{
745 NS_LOG_FUNCTION(this << path << &cb);
746
747 std::string root;
748 std::string leaf;
749 ParsePath(path, &root, &leaf);
750 MatchContainer container = LookupMatches(root);
751 return container.ConnectFailSafe(leaf, cb);
752}
753
754void
755ConfigImpl::Disconnect(std::string path, const CallbackBase& cb)
756{
757 NS_LOG_FUNCTION(this << path << &cb);
758
759 std::string root;
760 std::string leaf;
761 ParsePath(path, &root, &leaf);
762 MatchContainer container = LookupMatches(root);
763 if (container.GetN() == 0)
764 {
765 std::size_t lastFwdSlash = root.rfind('/');
766 NS_LOG_WARN("Failed to disconnect "
767 << leaf << ", the Requested object name = " << root.substr(lastFwdSlash + 1)
768 << " does not exits on path " << root.substr(0, lastFwdSlash));
769 }
770 container.Disconnect(leaf, cb);
771}
772
775{
776 NS_LOG_FUNCTION(this << path);
777
778 class LookupMatchesResolver : public Resolver
779 {
780 public:
781 LookupMatchesResolver(std::string path)
782 : Resolver(path)
783 {
784 }
785
786 void DoOne(Ptr<Object> object, std::string path) override
787 {
788 m_objects.push_back(object);
789 m_contexts.push_back(path);
790 }
791
792 std::vector<Ptr<Object>> m_objects;
793 std::vector<std::string> m_contexts;
794 } resolver = LookupMatchesResolver(path);
795
796 for (auto i = m_roots.begin(); i != m_roots.end(); i++)
797 {
798 resolver.Resolve(*i);
799 }
800
801 //
802 // See if we can do something with the object name service. Starting with
803 // the root pointer zeroed indicates to the resolver that it should start
804 // looking at the root of the "/Names" namespace during this go.
805 //
806 resolver.Resolve(nullptr);
807
808 return MatchContainer(resolver.m_objects, resolver.m_contexts, path);
809}
810
811void
813{
814 NS_LOG_FUNCTION(this << obj);
815 m_roots.push_back(obj);
816}
817
818void
820{
821 NS_LOG_FUNCTION(this << obj);
822
823 for (auto i = m_roots.begin(); i != m_roots.end(); i++)
824 {
825 if (*i == obj)
826 {
827 m_roots.erase(i);
828 return;
829 }
830 }
831}
832
833std::size_t
835{
836 NS_LOG_FUNCTION(this);
837 return m_roots.size();
838}
839
842{
843 NS_LOG_FUNCTION(this << i);
844 return m_roots[i];
845}
846
847void
849{
851 // First, let's reset the initial value of every attribute
852 for (uint16_t i = 0; i < TypeId::GetRegisteredN(); i++)
853 {
855 for (uint32_t j = 0; j < tid.GetAttributeN(); j++)
856 {
859 }
860 }
861 // now, let's reset the initial value of every global value.
862 for (auto i = GlobalValue::Begin(); i != GlobalValue::End(); ++i)
863 {
864 (*i)->ResetInitialValue();
865 }
866}
867
868void
869Set(std::string path, const AttributeValue& value)
870{
871 NS_LOG_FUNCTION(path << &value);
872 ConfigImpl::Get()->Set(path, value);
873}
874
875bool
876SetFailSafe(std::string path, const AttributeValue& value)
877{
878 NS_LOG_FUNCTION(path << &value);
879 return ConfigImpl::Get()->SetFailSafe(path, value);
880}
881
882void
883SetDefault(std::string name, const AttributeValue& value)
884{
885 NS_LOG_FUNCTION(name << &value);
886 if (!SetDefaultFailSafe(name, value))
887 {
888 NS_FATAL_ERROR("Could not set default value for " << name);
889 }
890}
891
892bool
893SetDefaultFailSafe(std::string fullName, const AttributeValue& value)
894{
895 NS_LOG_FUNCTION(fullName << &value);
896 std::string::size_type pos = fullName.rfind("::");
897 if (pos == std::string::npos)
898 {
899 return false;
900 }
901 std::string tidName = fullName.substr(0, pos);
902 std::string paramName = fullName.substr(pos + 2, fullName.size() - (pos + 2));
903 TypeId tid;
904 bool ok = TypeId::LookupByNameFailSafe(tidName, &tid);
905 if (!ok)
906 {
907 return false;
908 }
910 tid.LookupAttributeByName(paramName, &info);
911 for (uint32_t j = 0; j < tid.GetAttributeN(); j++)
912 {
914 if (tmp.name == paramName)
915 {
916 Ptr<AttributeValue> v = tmp.checker->CreateValidValue(value);
917 if (!v)
918 {
919 return false;
920 }
921 tid.SetAttributeInitialValue(j, v);
922 return true;
923 }
924 }
925 return false;
926}
927
928void
929SetGlobal(std::string name, const AttributeValue& value)
930{
931 NS_LOG_FUNCTION(name << &value);
932 GlobalValue::Bind(name, value);
933}
934
935bool
936SetGlobalFailSafe(std::string name, const AttributeValue& value)
937{
938 NS_LOG_FUNCTION(name << &value);
939 return GlobalValue::BindFailSafe(name, value);
940}
941
942void
943ConnectWithoutContext(std::string path, const CallbackBase& cb)
944{
945 NS_LOG_FUNCTION(path << &cb);
946 if (!ConnectWithoutContextFailSafe(path, cb))
947 {
948 NS_FATAL_ERROR("Could not connect callback to " << path);
949 }
950}
951
952bool
953ConnectWithoutContextFailSafe(std::string path, const CallbackBase& cb)
954{
955 NS_LOG_FUNCTION(path << &cb);
957}
958
959void
960DisconnectWithoutContext(std::string path, const CallbackBase& cb)
961{
962 NS_LOG_FUNCTION(path << &cb);
964}
965
966void
967Connect(std::string path, const CallbackBase& cb)
968{
969 NS_LOG_FUNCTION(path << &cb);
970 if (!ConnectFailSafe(path, cb))
971 {
972 NS_FATAL_ERROR("Could not connect callback to " << path);
973 }
974}
975
976bool
977ConnectFailSafe(std::string path, const CallbackBase& cb)
978{
979 NS_LOG_FUNCTION(path << &cb);
980 return ConfigImpl::Get()->ConnectFailSafe(path, cb);
981}
982
983void
984Disconnect(std::string path, const CallbackBase& cb)
985{
986 NS_LOG_FUNCTION(path << &cb);
987 ConfigImpl::Get()->Disconnect(path, cb);
988}
989
990MatchContainer
991LookupMatches(std::string path)
992{
993 NS_LOG_FUNCTION(path);
994 return ConfigImpl::Get()->LookupMatches(path);
995}
996
997void
1003
1004void
1010
1011std::size_t
1017
1024
1025} // namespace Config
1026
1027} // namespace ns3
Hold a value for an Attribute.
Definition attribute.h:59
Base class for Callback class.
Definition callback.h:344
Helper to test if an array entry matches a config path specification.
Definition config.cc:195
ArrayMatcher(std::string element)
Construct from a Config path specification.
Definition config.cc:225
bool StringToUint32(std::string str, uint32_t *value) const
Convert a string to an uint32_t.
Definition config.cc:294
bool Matches(std::size_t i) const
Test if a specific index matches the Config Path.
Definition config.cc:232
std::string m_element
The Config path element.
Definition config.cc:221
Config system implementation class.
Definition config.cc:630
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition config.cc:819
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:725
Roots m_roots
The list of Config path roots.
Definition config.cc:673
Ptr< Object > GetRootNamespaceObject(std::size_t i) const
Definition config.cc:841
bool ConnectFailSafe(std::string path, const CallbackBase &cb)
Definition config.cc:743
std::size_t GetRootNamespaceObjectN() const
Definition config.cc:834
bool SetFailSafe(std::string path, const AttributeValue &value)
Definition config.cc:702
void Set(std::string path, const AttributeValue &value)
Definition config.cc:690
void ParsePath(std::string path, std::string *root, std::string *leaf) const
Break a Config path into the leading path and the last leaf token.
Definition config.cc:678
void Disconnect(std::string path, const CallbackBase &cb)
Definition config.cc:755
std::vector< Ptr< Object > > Roots
Container type to hold the root Config path tokens.
Definition config.cc:670
bool ConnectWithoutContextFailSafe(std::string path, const CallbackBase &cb)
Definition config.cc:714
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition config.cc:812
MatchContainer LookupMatches(std::string path)
Definition config.cc:774
hold a set of objects which match a specific search string.
Definition config.h:184
bool SetFailSafe(std::string name, const AttributeValue &value)
Definition config.cc:108
void Connect(std::string name, const CallbackBase &cb)
Definition config.cc:121
void DisconnectWithoutContext(std::string name, const CallbackBase &cb)
Definition config.cc:180
void Set(std::string name, const AttributeValue &value)
Definition config.cc:96
Ptr< Object > Get(std::size_t i) const
Definition config.cc:75
void Disconnect(std::string name, const CallbackBase &cb)
Definition config.cc:167
std::string GetMatchedPath(uint32_t i) const
Definition config.cc:82
MatchContainer::Iterator End() const
Definition config.cc:61
bool ConnectFailSafe(std::string name, const CallbackBase &cb)
Definition config.cc:130
std::string GetPath() const
Definition config.cc:89
bool ConnectWithoutContextFailSafe(std::string name, const CallbackBase &cb)
Definition config.cc:154
std::string m_path
The path used to perform the object matching.
Definition config.h:342
void ConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition config.cc:145
std::size_t GetN() const
Definition config.cc:68
std::vector< Ptr< Object > >::const_iterator Iterator
Const iterator over the objects in this container.
Definition config.h:187
std::vector< Ptr< Object > > m_objects
The list of objects in this container.
Definition config.h:338
MatchContainer::Iterator Begin() const
Definition config.cc:54
std::vector< std::string > m_contexts
The context for each object.
Definition config.h:340
Abstract class to parse Config paths into object references.
Definition config.cc:308
std::vector< std::string > m_workStack
Current list of path tokens.
Definition config.cc:367
std::string GetResolvedPath() const
Get the current Config path.
Definition config.cc:414
Resolver(std::string path)
Construct from a base Config path.
Definition config.cc:373
void Resolve(Ptr< Object > root)
Parse the stored Config path into an object reference, beginning at the indicated root object.
Definition config.cc:406
void DoResolveOne(Ptr< Object > object)
Handle one object found on the path.
Definition config.cc:427
virtual void DoOne(Ptr< Object > object, std::string path)=0
Handle one found object.
void DoResolve(std::string path, Ptr< Object > root)
Parse the next element in the Config path.
Definition config.cc:436
std::string m_path
The Config path.
Definition config.cc:369
void Canonicalize()
Ensure the Config path starts and ends with a '/'.
Definition config.cc:386
virtual ~Resolver()
Destructor.
Definition config.cc:380
void DoArrayResolve(std::string path, const ObjectPtrContainerValue &vector)
Parse an index on the Config path.
Definition config.cc:597
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
static Iterator Begin()
The Begin iterator.
static bool BindFailSafe(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
static Iterator End()
The End iterator.
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition names.h:443
A base class which provides memory management and object aggregation.
Definition object.h:78
AttributeChecker implementation for ObjectPtrContainerValue.
Container for a set of ns3::Object pointers.
std::map< std::size_t, Ptr< Object > >::const_iterator Iterator
Iterator type for traversing this container.
AttributeChecker implementation for PointerValue.
Definition pointer.h:114
AttributeValue implementation for Pointer.
Ptr< T > Get() const
Definition pointer.h:223
Smart pointer class similar to boost::intrusive_ptr.
A template singleton.
Definition singleton.h:57
static ConfigImpl * Get()
Definition singleton.h:96
a unique identifier for an interface.
Definition type-id.h:48
bool SetAttributeInitialValue(std::size_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition type-id.cc:1146
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
static uint16_t GetRegisteredN()
Get the number of registered TypeIds.
Definition type-id.cc:926
std::size_t GetAttributeN() const
Get the number of attributes.
Definition type-id.cc:1170
TypeId GetParent() const
Get the parent of this TypeId.
Definition type-id.cc:1025
static TypeId GetRegistered(uint16_t i)
Get a TypeId by index.
Definition type-id.cc:933
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition type-id.cc:1178
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition type-id.cc:886
bool LookupAttributeByName(std::string name, AttributeInformation *info, bool permissive=false) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition type-id.cc:968
Declaration of the various ns3::Config functions and classes.
ns3::GlobalValue declaration.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
void Reset()
Reset the initial value of every attribute as well as the value of every global to what they were bef...
Definition config.cc:848
void SetGlobal(std::string name, const AttributeValue &value)
Definition config.cc:929
bool SetFailSafe(std::string path, const AttributeValue &value)
Definition config.cc:876
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void Disconnect(std::string path, const CallbackBase &cb)
Definition config.cc:984
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
MatchContainer LookupMatches(std::string path)
Definition config.cc:991
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:960
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:943
bool ConnectFailSafe(std::string path, const CallbackBase &cb)
Definition config.cc:977
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition config.cc:1005
Ptr< Object > GetRootNamespaceObject(uint32_t i)
Definition config.cc:1019
bool SetGlobalFailSafe(std::string name, const AttributeValue &value)
Definition config.cc:936
void Set(std::string path, const AttributeValue &value)
Definition config.cc:869
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition config.cc:998
std::size_t GetRootNamespaceObjectN()
Definition config.cc:1012
bool SetDefaultFailSafe(std::string fullName, const AttributeValue &value)
Definition config.cc:893
bool ConnectWithoutContextFailSafe(std::string path, const CallbackBase &cb)
Definition config.cc:953
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
Debug message logging.
Declaration of class ns3::Names.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition ptr.h:443
ns3::ObjectPtrContainerValue attribute value declarations and template implementations.
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::PointerValue attribute value declarations and template implementations.
ns3::Singleton declaration and template implementation.
Attribute implementation.
Definition type-id.h:70
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition type-id.h:78
std::string name
Attribute name.
Definition type-id.h:72
Ptr< const AttributeChecker > checker
Checker object.
Definition type-id.h:84