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,
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 
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  {
230  const auto ptrChecker = dynamic_cast<const PointerChecker*>(PeekPointer(info.checker));
231  if (ptrChecker != nullptr)
232  {
233  NS_LOG_DEBUG("pointer attribute " << info.name);
234  PointerValue ptr;
235  object->GetAttribute(info.name, ptr);
236  Ptr<Object> tmp = ptr.Get<Object>();
237  if (tmp)
238  {
239  StartVisitPointerAttribute(object, info.name, tmp);
240  m_examined.push_back(object);
241  DoIterate(tmp);
242  m_examined.pop_back();
244  }
245  continue;
246  }
247  // attempt to cast to an object container
248  const auto vectorChecker =
249  dynamic_cast<const ObjectPtrContainerChecker*>(PeekPointer(info.checker));
250  if (vectorChecker != nullptr)
251  {
252  NS_LOG_DEBUG("ObjectPtrContainer attribute " << info.name);
254  object->GetAttribute(info.name, vector);
255  StartVisitArrayAttribute(object, info.name, vector);
257  for (it = vector.Begin(); it != vector.End(); ++it)
258  {
259  uint32_t j = (*it).first;
260  NS_LOG_DEBUG("ObjectPtrContainer attribute item " << j);
261  Ptr<Object> tmp = (*it).second;
262  if (tmp)
263  {
264  StartVisitArrayItem(vector, j, tmp);
265  m_examined.push_back(object);
266  DoIterate(tmp);
267  m_examined.pop_back();
269  }
270  }
272  continue;
273  }
274  if ((info.flags & TypeId::ATTR_GET) && info.accessor->HasGetter() &&
275  (info.flags & TypeId::ATTR_SET) && info.accessor->HasSetter())
276  {
277  VisitAttribute(object, info.name);
278  }
279  else
280  {
281  NS_LOG_DEBUG("could not store " << info.name);
282  }
283  }
284  }
285  Object::AggregateIterator iter = object->GetAggregateIterator();
286  bool recursiveAggregate = false;
287  while (iter.HasNext())
288  {
289  Ptr<const Object> tmp = iter.Next();
290  if (IsExamined(tmp))
291  {
292  recursiveAggregate = true;
293  }
294  }
295  if (!recursiveAggregate)
296  {
297  iter = object->GetAggregateIterator();
298  while (iter.HasNext())
299  {
300  Ptr<Object> tmp = const_cast<Object*>(PeekPointer(iter.Next()));
301  StartVisitObject(tmp);
302  m_examined.push_back(object);
303  DoIterate(tmp);
304  m_examined.pop_back();
305  EndVisitObject();
306  }
307  }
308 }
309 
310 } // 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
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.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Ptr< T > Get() const
Definition: pointer.h:202
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:963
@ 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:1100
TypeId GetParent() const
Get the parent of this TypeId.
Definition: type-id.cc:955
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1108
std::string GetName() const
Get the name.
Definition: type-id.cc:991
#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:1026
std::size_t GetRootNamespaceObjectN()
Definition: config.cc:1019
#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:449
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