A Discrete-Event Network Simulator
API
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 
28 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE("AttributeIterator");
32 
34 {
35 }
36 
38 {
39 }
40 
41 void
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 
55 bool
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 
68 std::string
69 AttributeIterator::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 
83 std::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 
94 void
96 {
97 }
98 
99 void
101 {
102 }
103 
104 void
106  std::string name,
107  Ptr<Object> item)
108 {
109 }
110 
111 void
113 {
114 }
115 
116 void
118  std::string name,
119  const ObjectPtrContainerValue& vector)
120 {
121 }
122 
123 void
125 {
126 }
127 
128 void
130  uint32_t index,
131  Ptr<Object> item)
132 {
133 }
134 
135 void
137 {
138 }
139 
140 void
142 {
143  m_currentPath.push_back(name);
144  DoVisitAttribute(object, name);
145  m_currentPath.pop_back();
146 }
147 
148 void
150 {
151  m_currentPath.push_back("$" + object->GetInstanceTypeId().GetName());
152  DoStartVisitObject(object);
153 }
154 
155 void
157 {
158  m_currentPath.pop_back();
160 }
161 
162 void
164  std::string name,
166 {
167  m_currentPath.push_back(name);
168  m_currentPath.push_back("$" + value->GetInstanceTypeId().GetName());
169  DoStartVisitPointerAttribute(object, name, value);
170 }
171 
172 void
174 {
175  m_currentPath.pop_back();
176  m_currentPath.pop_back();
178 }
179 
180 void
182  std::string name,
183  const ObjectPtrContainerValue& vector)
184 {
185  m_currentPath.push_back(name);
186  DoStartVisitArrayAttribute(object, name, vector);
187 }
188 
189 void
191 {
192  m_currentPath.pop_back();
194 }
195 
196 void
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 
208 void
210 {
211  m_currentPath.pop_back();
212  m_currentPath.pop_back();
214 }
215 
216 void
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  {
229  struct TypeId::AttributeInformation info = tid.GetAttribute(i);
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();
306  EndVisitObject();
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
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: object.cc:82
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:206
a unique identifier for an interface.
Definition: type-id.h:60
bool HasParent() const
Check if this TypeId has a parent.
Definition: type-id.cc:967
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:65
@ ATTR_SET
The attribute can be written.
Definition: type-id.h:66
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1112
std::size_t GetAttributeN() const
Get the number of attributes.
Definition: type-id.cc:1104
TypeId GetParent() const
Get the parent of this TypeId.
Definition: type-id.cc:959
std::string GetName() const
Get the name.
Definition: type-id.cc:995
#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
value
Definition: second.py:41
Attribute implementation.
Definition: type-id.h:82
std::string name
Attribute name.
Definition: type-id.h:84
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:94
uint32_t flags
AttributeFlags value.
Definition: type-id.h:88
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:96