A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
attribute-iterator.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
16 */
17
18#include "attribute-iterator.h"
19
20#include "ns3/config.h"
21#include "ns3/log.h"
22#include "ns3/object-ptr-container.h"
23#include "ns3/pointer.h"
24#include "ns3/string.h"
25
26#include <fstream>
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("AttributeIterator");
32
34{
35}
36
38{
39}
40
41void
43{
44 for (uint32_t i = 0; i < Config::GetRootNamespaceObjectN(); ++i)
45 {
47 StartVisitObject(object);
48 DoIterate(object);
50 }
51 NS_ASSERT(m_currentPath.empty());
52 NS_ASSERT(m_examined.empty());
53}
54
55bool
57{
58 for (uint32_t i = 0; i < m_examined.size(); ++i)
59 {
60 if (object == m_examined[i])
61 {
62 return true;
63 }
64 }
65 return false;
66}
67
68std::string
69AttributeIterator::GetCurrentPath(std::string attr) const
70{
71 std::ostringstream oss;
72 for (uint32_t i = 0; i < m_currentPath.size(); ++i)
73 {
74 oss << "/" << m_currentPath[i];
75 }
76 if (!attr.empty())
77 {
78 oss << "/" << attr;
79 }
80 return oss.str();
81}
82
83std::string
85{
86 std::ostringstream oss;
87 for (uint32_t i = 0; i < m_currentPath.size(); ++i)
88 {
89 oss << "/" << m_currentPath[i];
90 }
91 return oss.str();
92}
93
94void
96{
97}
98
99void
101{
102}
103
104void
106 std::string name,
107 Ptr<Object> item)
108{
109}
110
111void
113{
114}
115
116void
118 std::string name,
119 const ObjectPtrContainerValue& vector)
120{
121}
122
123void
125{
126}
127
128void
130 uint32_t index,
131 Ptr<Object> item)
132{
133}
134
135void
137{
138}
139
140void
142{
143 m_currentPath.push_back(name);
144 DoVisitAttribute(object, name);
145 m_currentPath.pop_back();
146}
147
148void
150{
151 m_currentPath.push_back("$" + object->GetInstanceTypeId().GetName());
152 DoStartVisitObject(object);
153}
154
155void
157{
158 m_currentPath.pop_back();
160}
161
162void
164 std::string name,
165 Ptr<Object> value)
166{
167 m_currentPath.push_back(name);
168 m_currentPath.push_back("$" + value->GetInstanceTypeId().GetName());
169 DoStartVisitPointerAttribute(object, name, value);
170}
171
172void
174{
175 m_currentPath.pop_back();
176 m_currentPath.pop_back();
178}
179
180void
182 std::string name,
183 const ObjectPtrContainerValue& vector)
184{
185 m_currentPath.push_back(name);
186 DoStartVisitArrayAttribute(object, name, vector);
187}
188
189void
191{
192 m_currentPath.pop_back();
194}
195
196void
198 uint32_t index,
199 Ptr<Object> item)
200{
201 std::ostringstream oss;
202 oss << index;
203 m_currentPath.push_back(oss.str());
204 m_currentPath.push_back("$" + item->GetInstanceTypeId().GetName());
205 DoStartVisitArrayItem(vector, index, item);
206}
207
208void
210{
211 m_currentPath.pop_back();
212 m_currentPath.pop_back();
214}
215
216void
218{
219 if (IsExamined(object))
220 {
221 return;
222 }
223 TypeId tid;
224 for (tid = object->GetInstanceTypeId(); tid.HasParent(); tid = tid.GetParent())
225 {
226 NS_LOG_DEBUG("store " << tid.GetName());
227 for (uint32_t i = 0; i < tid.GetAttributeN(); ++i)
228 {
230 const PointerChecker* ptrChecker =
231 dynamic_cast<const PointerChecker*>(PeekPointer(info.checker));
232 if (ptrChecker != nullptr)
233 {
234 NS_LOG_DEBUG("pointer attribute " << info.name);
235 PointerValue ptr;
236 object->GetAttribute(info.name, ptr);
237 Ptr<Object> tmp = ptr.Get<Object>();
238 if (tmp)
239 {
240 StartVisitPointerAttribute(object, info.name, tmp);
241 m_examined.push_back(object);
242 DoIterate(tmp);
243 m_examined.pop_back();
245 }
246 continue;
247 }
248 // attempt to cast to an object container
249 const ObjectPtrContainerChecker* vectorChecker =
250 dynamic_cast<const ObjectPtrContainerChecker*>(PeekPointer(info.checker));
251 if (vectorChecker != nullptr)
252 {
253 NS_LOG_DEBUG("ObjectPtrContainer attribute " << info.name);
255 object->GetAttribute(info.name, vector);
256 StartVisitArrayAttribute(object, info.name, vector);
258 for (it = vector.Begin(); it != vector.End(); ++it)
259 {
260 uint32_t j = (*it).first;
261 NS_LOG_DEBUG("ObjectPtrContainer attribute item " << j);
262 Ptr<Object> tmp = (*it).second;
263 if (tmp)
264 {
265 StartVisitArrayItem(vector, j, tmp);
266 m_examined.push_back(object);
267 DoIterate(tmp);
268 m_examined.pop_back();
270 }
271 }
273 continue;
274 }
275 if ((info.flags & TypeId::ATTR_GET) && info.accessor->HasGetter() &&
276 (info.flags & TypeId::ATTR_SET) && info.accessor->HasSetter())
277 {
278 VisitAttribute(object, info.name);
279 }
280 else
281 {
282 NS_LOG_DEBUG("could not store " << info.name);
283 }
284 }
285 }
286 Object::AggregateIterator iter = object->GetAggregateIterator();
287 bool recursiveAggregate = false;
288 while (iter.HasNext())
289 {
290 Ptr<const Object> tmp = iter.Next();
291 if (IsExamined(tmp))
292 {
293 recursiveAggregate = true;
294 }
295 }
296 if (!recursiveAggregate)
297 {
298 iter = object->GetAggregateIterator();
299 while (iter.HasNext())
300 {
301 Ptr<Object> tmp = const_cast<Object*>(PeekPointer(iter.Next()));
302 StartVisitObject(tmp);
303 m_examined.push_back(object);
304 DoIterate(tmp);
305 m_examined.pop_back();
307 }
308 }
309}
310
311} // 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:106
Ptr< const Object > Next()
Get the next Aggregated Object.
Definition: object.cc:66
bool HasNext() const
Check if there are more Aggregates to iterate over.
Definition: object.cc:59
A base class which provides memory management and object aggregation.
Definition: object.h:89
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:97
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Ptr< T > Get() const
Definition: pointer.h:202
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
a unique identifier for an interface.
Definition: type-id.h:59
bool HasParent() const
Check if this TypeId has a parent.
Definition: type-id.cc:968
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:64
@ ATTR_SET
The attribute can be written.
Definition: type-id.h:65
std::size_t GetAttributeN() const
Get the number of attributes.
Definition: type-id.cc:1105
TypeId GetParent() const
Get the parent of this TypeId.
Definition: type-id.cc:960
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1113
std::string GetName() const
Get the name.
Definition: type-id.cc:996
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< Object > GetRootNamespaceObject(uint32_t i)
Definition: config.cc:1027
std::size_t GetRootNamespaceObjectN()
Definition: config.cc:1020
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:488
Attribute implementation.
Definition: type-id.h:81
std::string name
Attribute name.
Definition: type-id.h:83
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:93
uint32_t flags
AttributeFlags value.
Definition: type-id.h:87
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:95