A Discrete-Event Network Simulator
API
attribute-iterator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
17  */
18 
19 #include "attribute-iterator.h"
20 #include "ns3/config.h"
21 #include "ns3/log.h"
22 #include "ns3/pointer.h"
23 #include "ns3/object-ptr-container.h"
24 #include "ns3/string.h"
25 #include <fstream>
26 
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("AttributeIterator");
31 
33 {
34 }
35 
37 {
38 }
39 
40 void
42 {
43  for (uint32_t i = 0; i < Config::GetRootNamespaceObjectN (); ++i)
44  {
46  StartVisitObject (object);
47  DoIterate (object);
48  EndVisitObject ();
49  }
50  NS_ASSERT (m_currentPath.empty ());
51  NS_ASSERT (m_examined.empty ());
52 }
53 
54 bool
56 {
57  for (uint32_t i = 0; i < m_examined.size (); ++i)
58  {
59  if (object == m_examined[i])
60  {
61  return true;
62  }
63  }
64  return false;
65 }
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 != "")
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 void
100 {
101 }
102 void
104 {
105 }
106 void
108 {
109 }
110 void
112 {
113 }
114 void
116 {
117 }
118 void
120 {
121 }
122 void
124 {
125 }
126 
127 void
129 {
130  m_currentPath.push_back (name);
131  DoVisitAttribute (object, name);
132  m_currentPath.pop_back ();
133 }
134 
135 void
137 {
138  m_currentPath.push_back ("$" + object->GetInstanceTypeId ().GetName ());
139  DoStartVisitObject (object);
140 }
141 void
143 {
144  m_currentPath.pop_back ();
145  DoEndVisitObject ();
146 }
147 void
149 {
150  m_currentPath.push_back (name);
151  m_currentPath.push_back ("$" + value->GetInstanceTypeId ().GetName ());
152  DoStartVisitPointerAttribute (object, name, value);
153 }
154 void
156 {
157  m_currentPath.pop_back ();
158  m_currentPath.pop_back ();
160 }
161 void
163 {
164  m_currentPath.push_back (name);
165  DoStartVisitArrayAttribute (object, name, vector);
166 }
167 void
169 {
170  m_currentPath.pop_back ();
172 }
173 
174 void
176 {
177  std::ostringstream oss;
178  oss << index;
179  m_currentPath.push_back (oss.str ());
180  m_currentPath.push_back ("$" + item->GetInstanceTypeId ().GetName ());
181  DoStartVisitArrayItem (vector, index, item);
182 }
183 void
185 {
186  m_currentPath.pop_back ();
187  m_currentPath.pop_back ();
189 }
190 
191 
192 void
194 {
195  if (IsExamined (object))
196  {
197  return;
198  }
199  TypeId tid;
200  for (tid = object->GetInstanceTypeId (); tid.HasParent (); tid = tid.GetParent ())
201  {
202  NS_LOG_DEBUG ("store " << tid.GetName ());
203  for (uint32_t i = 0; i < tid.GetAttributeN (); ++i)
204  {
205  struct TypeId::AttributeInformation info = tid.GetAttribute(i);
206  const PointerChecker *ptrChecker = dynamic_cast<const PointerChecker *> (PeekPointer (info.checker));
207  if (ptrChecker != 0)
208  {
209  NS_LOG_DEBUG ("pointer attribute " << info.name);
210  PointerValue ptr;
211  object->GetAttribute (info.name, ptr);
212  Ptr<Object> tmp = ptr.Get<Object> ();
213  if (tmp != 0)
214  {
215  StartVisitPointerAttribute (object, info.name,
216  tmp);
217  m_examined.push_back (object);
218  DoIterate (tmp);
219  m_examined.pop_back ();
221  }
222  continue;
223  }
224  // attempt to cast to an object container
225  const ObjectPtrContainerChecker *vectorChecker = dynamic_cast<const ObjectPtrContainerChecker *> (PeekPointer (info.checker));
226  if (vectorChecker != 0)
227  {
228  NS_LOG_DEBUG ("ObjectPtrContainer attribute " << info.name);
230  object->GetAttribute (info.name, vector);
231  StartVisitArrayAttribute (object, info.name, vector);
233  for (it = vector.Begin (); it != vector.End (); ++it)
234  {
235  uint32_t j = (*it).first;
236  NS_LOG_DEBUG ("ObjectPtrContainer attribute item " << j);
237  Ptr<Object> tmp = (*it).second;
238  if (tmp)
239  {
240  StartVisitArrayItem (vector, j, tmp);
241  m_examined.push_back (object);
242  DoIterate (tmp);
243  m_examined.pop_back ();
245  }
246  }
248  continue;
249  }
250  if ((info.flags & TypeId::ATTR_GET) && info.accessor->HasGetter () &&
251  (info.flags & TypeId::ATTR_SET) && info.accessor->HasSetter ())
252  {
253  VisitAttribute (object, info.name);
254  }
255  else
256  {
257  NS_LOG_DEBUG ("could not store " << info.name);
258  }
259  }
260  }
261  Object::AggregateIterator iter = object->GetAggregateIterator ();
262  bool recursiveAggregate = false;
263  while (iter.HasNext ())
264  {
265  Ptr<const Object> tmp = iter.Next ();
266  if (IsExamined (tmp))
267  {
268  recursiveAggregate = true;
269  }
270  }
271  if (!recursiveAggregate)
272  {
273  iter = object->GetAggregateIterator ();
274  while (iter.HasNext ())
275  {
276  Ptr<Object> tmp = const_cast<Object *> (PeekPointer (iter.Next ()));
277  StartVisitObject (tmp);
278  m_examined.push_back (object);
279  DoIterate (tmp);
280  m_examined.pop_back ();
281  EndVisitObject ();
282  }
283  }
284 }
285 
286 
287 } // namespace ns3
void DoIterate(Ptr< Object > object)
Perform the iteration.
virtual void DoEndVisitArrayAttribute(void)
End the visit to the attribute of type ns3::ObjectVectorValue.
virtual void DoEndVisitArrayItem(void)
End the visit to the array item.
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 DoEndVisitPointerAttribute(void)
End the visit to the attribute of type ns3::PointerValue.
void StartVisitObject(Ptr< Object > object)
Start to visit an object to visit its attributes.
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...
std::string GetCurrentPath(void) const
Get the current attribute path.
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 EndVisitObject(void)
End the visit to the object.
void EndVisitArrayAttribute(void)
End the visit to the attribute of type ns3::ObjectVectorValue.
void Iterate(void)
Start the process of iterating all objects from the root namespace object.
void EndVisitArrayItem(void)
End the visit to the array item.
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.
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.
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 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...
virtual void DoEndVisitObject(void)
This method is called to end the process of visiting the currently visited object.
void EndVisitPointerAttribute(void)
End the visit to the attribute of type ns3::PointerValue.
Iterate over the Objects aggregated to an ns3::Object.
Definition: object.h:105
Ptr< const Object > Next(void)
Get the next Aggregated Object.
Definition: object.cc:63
bool HasNext(void) const
Check if there are more Aggregates to iterate over.
Definition: object.cc:57
A base class which provides memory management and object aggregation.
Definition: object.h:88
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: object.cc:79
AttributeChecker implementation for ObjectPtrContainerValue.
Container for a set of ns3::Object pointers.
Iterator Begin(void) const
Get an iterator to the first Object.
std::map< std::size_t, Ptr< Object > >::const_iterator Iterator
Iterator type for traversing this container.
Iterator End(void) const
Get an iterator to the past-the-end Object.
AttributeChecker implementation for PointerValue.
Definition: pointer.h:98
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Ptr< T > Get(void) const
Definition: pointer.h:201
a unique identifier for an interface.
Definition: type-id.h:59
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1076
bool HasParent(void) const
Check if this TypeId has a parent.
Definition: type-id.cc:950
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:64
@ ATTR_SET
The attribute can be written.
Definition: type-id.h:65
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1083
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:943
std::string GetName(void) const
Get the name.
Definition: type-id.cc:976
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
Ptr< Object > GetRootNamespaceObject(uint32_t i)
Definition: config.cc:964
std::size_t GetRootNamespaceObjectN(void)
Definition: config.cc:958
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:415
Attribute implementation.
Definition: type-id.h:78
std::string name
Attribute name.
Definition: type-id.h:80
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:90
uint32_t flags
AttributeFlags value.
Definition: type-id.h:84
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:92