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
std::string GetName(void) const
Get the name.
Definition: type-id.cc:969
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...
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.
#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
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:564
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
virtual void DoEndVisitPointerAttribute(void)
End the visit to the attribute of type ns3::PointerValue.
virtual void DoEndVisitArrayAttribute(void)
End the visit to the attribute of type ns3::ObjectVectorValue.
bool HasParent(void) const
Check if this TypeId has a parent.
Definition: type-id.cc:943
bool IsExamined(Ptr< const Object > object)
Check if this object has already been examined.
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:936
std::vector< std::string > m_currentPath
current attribute path
The attribute can be read.
Definition: type-id.h:63
void EndVisitArrayItem(void)
End the visit to the array item.
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 poi...
Ptr< const Object > Next(void)
Get the next Aggregated Object.
Definition: object.cc:63
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:88
void StartVisitObject(Ptr< Object > object)
Start to visit an object to visit its attributes.
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.
std::vector< Ptr< Object > > m_examined
list of attributes examined
Ptr< Object > GetRootNamespaceObject(uint32_t i)
Definition: config.cc:878
AttributeChecker implementation for ObjectPtrContainerValue.
Iterator End(void) const
Get an iterator to the past-the-end Object.
Attribute implementation.
Definition: type-id.h:76
virtual void DoStartVisitObject(Ptr< Object > object)
This method is called to start the process of visiting the input object.
uint32_t flags
AttributeFlags value.
Definition: type-id.h:82
void VisitAttribute(Ptr< Object > object, std::string name)
Visit attribute to perform a config store operation on it.
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:90
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
std::string name
Attribute name.
Definition: type-id.h:78
std::size_t GetRootNamespaceObjectN(void)
Definition: config.cc:872
The attribute can be written.
Definition: type-id.h:64
std::string GetCurrentPath(void) const
Get the current attribute path.
bool HasNext(void) const
Check if there are more Aggregates to iterate over.
Definition: object.cc:57
virtual void DoEndVisitArrayItem(void)
End the visit to the array item.
void DoIterate(Ptr< Object > object)
Perform the iteration.
AttributeChecker implementation for PointerValue.
Definition: pointer.h:97
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: object.cc:79
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1069
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.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1076
Ptr< T > Get(void) const
Definition: pointer.h:194
void EndVisitArrayAttribute(void)
End the visit to the attribute of type ns3::ObjectVectorValue.
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...
A base class which provides memory management and object aggregation.
Definition: object.h:87
Container for a set of ns3::Object pointers.
void Iterate(void)
Start the process of iterating all objects from the root namespace 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 poi...
a unique identifier for an interface.
Definition: type-id.h:58
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 StartVisitArrayItem(const ObjectPtrContainerValue &vector, uint32_t index, Ptr< Object > item)
Start to visit the object found in the input array at the provided index.
Iterate over the Objects aggregated to an ns3::Object.
Definition: object.h:104
void EndVisitObject(void)
End the visit to the object.