A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
attribute-iterator.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
5 */
6
8
9#include "ns3/config.h"
10#include "ns3/log.h"
11#include "ns3/object-ptr-container.h"
12#include "ns3/pointer.h"
13#include "ns3/string.h"
14
15#include <fstream>
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("AttributeIterator");
21
25
29
30void
32{
33 for (uint32_t i = 0; i < Config::GetRootNamespaceObjectN(); ++i)
34 {
36 StartVisitObject(object);
37 DoIterate(object);
39 }
40 NS_ASSERT(m_currentPath.empty());
41 NS_ASSERT(m_examined.empty());
42}
43
44bool
46{
47 for (uint32_t i = 0; i < m_examined.size(); ++i)
48 {
49 if (object == m_examined[i])
50 {
51 return true;
52 }
53 }
54 return false;
55}
56
57std::string
58AttributeIterator::GetCurrentPath(std::string attr) const
59{
60 std::ostringstream oss;
61 for (uint32_t i = 0; i < m_currentPath.size(); ++i)
62 {
63 oss << "/" << m_currentPath[i];
64 }
65 if (!attr.empty())
66 {
67 oss << "/" << attr;
68 }
69 return oss.str();
70}
71
72std::string
74{
75 std::ostringstream oss;
76 for (uint32_t i = 0; i < m_currentPath.size(); ++i)
77 {
78 oss << "/" << m_currentPath[i];
79 }
80 return oss.str();
81}
82
83void
87
88void
92
93void
95 std::string name,
96 Ptr<Object> item)
97{
98}
99
100void
104
105void
107 std::string name,
108 const ObjectPtrContainerValue& vector)
109{
110}
111
112void
116
117void
123
124void
128
129void
131{
132 m_currentPath.push_back(name);
133 DoVisitAttribute(object, name);
134 m_currentPath.pop_back();
135}
136
137void
139{
140 m_currentPath.push_back("$" + object->GetInstanceTypeId().GetName());
141 DoStartVisitObject(object);
142}
143
144void
150
151void
153 std::string name,
154 Ptr<Object> value)
155{
156 m_currentPath.push_back(name);
157 m_currentPath.push_back("$" + value->GetInstanceTypeId().GetName());
158 DoStartVisitPointerAttribute(object, name, value);
159}
160
161void
168
169void
171 std::string name,
172 const ObjectPtrContainerValue& vector)
173{
174 m_currentPath.push_back(name);
175 DoStartVisitArrayAttribute(object, name, vector);
176}
177
178void
184
185void
187 uint32_t index,
188 Ptr<Object> item)
189{
190 std::ostringstream oss;
191 oss << index;
192 m_currentPath.push_back(oss.str());
193 m_currentPath.push_back("$" + item->GetInstanceTypeId().GetName());
194 DoStartVisitArrayItem(vector, index, item);
195}
196
197void
204
205void
207{
208 if (IsExamined(object))
209 {
210 return;
211 }
212 TypeId tid;
213 for (tid = object->GetInstanceTypeId(); tid.HasParent(); tid = tid.GetParent())
214 {
215 NS_LOG_DEBUG("store " << tid.GetName());
216 for (uint32_t i = 0; i < tid.GetAttributeN(); ++i)
217 {
219 const auto ptrChecker = dynamic_cast<const PointerChecker*>(PeekPointer(info.checker));
220 if (ptrChecker != nullptr)
221 {
222 NS_LOG_DEBUG("pointer attribute " << info.name);
223 if (info.supportLevel == TypeId::DEPRECATED ||
225 {
226 continue;
227 }
228 PointerValue ptr;
229 object->GetAttribute(info.name, ptr, true);
230 Ptr<Object> tmp = ptr.Get<Object>();
231 if (tmp)
232 {
233 StartVisitPointerAttribute(object, info.name, tmp);
234 m_examined.push_back(object);
235 DoIterate(tmp);
236 m_examined.pop_back();
238 }
239 continue;
240 }
241 // attempt to cast to an object container
242 const auto vectorChecker =
243 dynamic_cast<const ObjectPtrContainerChecker*>(PeekPointer(info.checker));
244 if (vectorChecker != nullptr)
245 {
246 NS_LOG_DEBUG("ObjectPtrContainer attribute " << info.name);
248 object->GetAttribute(info.name, vector, true);
249 StartVisitArrayAttribute(object, info.name, vector);
251 for (it = vector.Begin(); it != vector.End(); ++it)
252 {
253 uint32_t j = (*it).first;
254 NS_LOG_DEBUG("ObjectPtrContainer attribute item " << j);
255 Ptr<Object> tmp = (*it).second;
256 if (tmp)
257 {
258 StartVisitArrayItem(vector, j, tmp);
259 m_examined.push_back(object);
260 DoIterate(tmp);
261 m_examined.pop_back();
263 }
264 }
266 continue;
267 }
268 if ((info.flags & TypeId::ATTR_GET) && info.accessor->HasGetter() &&
269 (info.flags & TypeId::ATTR_SET) && info.accessor->HasSetter())
270 {
271 VisitAttribute(object, info.name);
272 }
273 else
274 {
275 NS_LOG_DEBUG("could not store " << info.name);
276 }
277 }
278 }
279 Object::AggregateIterator iter = object->GetAggregateIterator();
280 bool recursiveAggregate = false;
281 while (iter.HasNext())
282 {
283 Ptr<const Object> tmp = iter.Next();
284 if (IsExamined(tmp))
285 {
286 recursiveAggregate = true;
287 }
288 }
289 if (!recursiveAggregate)
290 {
291 iter = object->GetAggregateIterator();
292 while (iter.HasNext())
293 {
294 Ptr<Object> tmp = const_cast<Object*>(PeekPointer(iter.Next()));
295 StartVisitObject(tmp);
296 m_examined.push_back(object);
297 DoIterate(tmp);
298 m_examined.pop_back();
300 }
301 }
302}
303
304} // namespace ns3
void DoIterate(Ptr< Object > object)
Perform the iteration.
virtual void DoEndVisitArrayItem()
End the visit to the array item.
virtual void DoEndVisitPointerAttribute()
End the visit to the attribute of type ns3::PointerValue.
virtual void DoStartVisitPointerAttribute(Ptr< Object > object, std::string name, Ptr< Object > value)
Visit the attribute of type ns3::PointerValue, with the provided name, found on the object pointed to...
std::vector< std::string > m_currentPath
current attribute path
virtual void DoEndVisitArrayAttribute()
End the visit to the attribute of type ns3::ObjectVectorValue.
std::string GetCurrentPath() const
Get the current attribute path.
void StartVisitObject(Ptr< Object > object)
Start to visit an object to visit its attributes.
void EndVisitObject()
End the visit to the object.
bool IsExamined(Ptr< const Object > object)
Check if this object has already been examined.
void VisitAttribute(Ptr< Object > object, std::string name)
Visit attribute to perform a config store operation on it.
void StartVisitArrayAttribute(Ptr< Object > object, std::string name, const ObjectPtrContainerValue &vector)
Visit the attribute of type ns3::ObjectVectorValue, with the provided name, found on the object point...
virtual void DoVisitAttribute(Ptr< Object > object, std::string name)=0
This method visits and performs a config-store action (such as saving to a text file) on the attribut...
void EndVisitPointerAttribute()
End the visit to the attribute of type ns3::PointerValue.
void StartVisitArrayItem(const ObjectPtrContainerValue &vector, uint32_t index, Ptr< Object > item)
Start to visit the object found in the input array at the provided index.
void EndVisitArrayItem()
End the visit to the array item.
std::vector< Ptr< Object > > m_examined
list of attributes examined
virtual void DoStartVisitArrayItem(const ObjectPtrContainerValue &vector, uint32_t index, Ptr< Object > item)
Start to visit the object found in the input array at the provided index.
void EndVisitArrayAttribute()
End the visit to the attribute of type ns3::ObjectVectorValue.
virtual void DoStartVisitObject(Ptr< Object > object)
This method is called to start the process of visiting the input object.
void StartVisitPointerAttribute(Ptr< Object > object, std::string name, Ptr< Object > value)
Visit the attribute of type ns3::PointerValue, with the provided name, found on the object pointed to...
virtual void DoEndVisitObject()
This method is called to end the process of visiting the currently visited object.
virtual void DoStartVisitArrayAttribute(Ptr< Object > object, std::string name, const ObjectPtrContainerValue &vector)
Visit the attribute of type ns3::ObjectVectorValue, with the provided name, found on the object point...
void Iterate()
Start the process of iterating all objects from the root namespace object.
Iterate over the Objects aggregated to an ns3::Object.
Definition object.h:95
Ptr< const Object > Next()
Get the next Aggregated Object.
Definition object.cc:56
bool HasNext() const
Check if there are more Aggregates to iterate over.
Definition object.cc:48
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.
Iterator End() const
Get an iterator to the past-the-end Object.
Iterator Begin() const
Get an iterator to the first Object.
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 unique identifier for an interface.
Definition type-id.h:48
bool HasParent() const
Check if this TypeId has a parent.
Definition type-id.cc:1033
@ ATTR_GET
The attribute can be read.
Definition type-id.h:53
@ ATTR_SET
The attribute can be written.
Definition type-id.h:54
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
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition type-id.cc:1178
@ OBSOLETE
Attribute or trace source is not used anymore; simulation fails.
Definition type-id.h:65
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition type-id.h:64
std::string GetName() const
Get the name.
Definition type-id.cc:1061
#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
Ptr< Object > GetRootNamespaceObject(uint32_t i)
Definition config.cc:1019
std::size_t GetRootNamespaceObjectN()
Definition config.cc:1012
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition ptr.h:443
Attribute implementation.
Definition type-id.h:70
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition type-id.h:86
std::string name
Attribute name.
Definition type-id.h:72
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition type-id.h:82
uint32_t flags
AttributeFlags value.
Definition type-id.h:76
Ptr< const AttributeChecker > checker
Checker object.
Definition type-id.h:84