1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2007 INRIA, Gustavo Carneiro
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Authors: Gustavo Carneiro <gjcarneiro@gmail.com>,
19 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
28 #include "attribute.h"
29 #include "object-base.h"
30 #include "attribute-list.h"
36 class AttributeAccessor;
39 class TraceSourceAccessor;
43 * \defgroup object Object
47 * \brief a base class which provides memory management and object aggregation
49 * The memory management scheme is based on reference-counting with dispose-like
50 * functionality to break the reference cycles. The reference count is increamented
51 * and decremented with the methods Object::Ref and Object::Unref. If a reference cycle is
52 * present, the user is responsible for breaking it by calling Object::Dispose in
53 * a single location. This will eventually trigger the invocation of Object::DoDispose
54 * on itself and all its aggregates. The Object::DoDispose method is always automatically
55 * invoked from the Object::Unref method before destroying the object, even if the user
56 * did not call Object::Dispose directly.
58 class Object : public ObjectBase
61 static TypeId GetTypeId (void);
64 * \brief Iterate over the objects aggregated to an ns3::Object.
66 * This iterator does not allow you to iterate over the initial
67 * object used to call Object::GetAggregateIterator.
69 * Note: this is a java-style iterator.
71 class AggregateIterator
77 * \returns true if HasNext can be called and return a non-null
78 * pointer, false otherwise.
80 bool HasNext (void) const;
83 * \returns the next aggregated object.
85 Ptr<const Object> Next (void);
88 AggregateIterator (Ptr<const Object> first);
89 Ptr<const Object> m_first;
90 Ptr<const Object> m_current;
97 * Implement the GetInstanceTypeId method defined in ObjectBase.
99 virtual TypeId GetInstanceTypeId (void) const;
102 * Increment the reference count. This method should not be called
103 * by user code. Object instances are expected to be used in conjunction
104 * of the Ptr template which would make calling Ref unecessary and
107 inline void Ref (void) const;
109 * Decrement the reference count. This method should not be called
110 * by user code. Object instances are expected to be used in conjunction
111 * of the Ptr template which would make calling Ref unecessary and
114 inline void Unref (void) const;
117 * Get the reference count of the object. Normally not needed; for language bindings.
119 uint32_t GetReferenceCount (void) const;
122 * \returns a pointer to the requested interface or zero if it could not be found.
124 template <typename T>
125 Ptr<T> GetObject (void) const;
127 * \param tid the interface id of the requested interface
128 * \returns a pointer to the requested interface or zero if it could not be found.
130 template <typename T>
131 Ptr<T> GetObject (TypeId tid) const;
133 * Run the DoDispose methods of this object and all the
134 * objects aggregated to it.
135 * After calling this method, the object is expected to be
136 * totally unusable except for the Ref and Unref methods.
137 * It is an error to call Dispose twice on the same object
140 * This method is typically used to break reference cycles.
144 * \param other another object pointer
146 * This method aggregates the two objects together: after this
147 * method returns, it becomes possible to call GetObject
148 * on one to get the other, and vice-versa.
150 void AggregateObject (Ptr<Object> other);
153 * \returns an iterator to the first object aggregated to this
156 * If no objects are aggregated to this object, then, the returned
157 * iterator will be empty and AggregateIterator::HasNext will
158 * always return false.
160 AggregateIterator GetAggregateIterator (void) const;
164 * This function is called by the AggregateObject on all the objects connected in the listed chain.
165 * This way the new object aggregated will be used if needed by the NotifyNewAggregate corresponding
166 * to each object connected in the listed chain. It should be implemented by objects needing an
167 * additional/special behavior when aggregated to another object.
169 virtual void NotifyNewAggregate ();
171 * This method is called by Object::Dispose or by the object's
172 * destructor, whichever comes first.
174 * Subclasses are expected to implement their real destruction
175 * code in an overriden version of this method and chain
176 * up to their parent's implementation once they are done.
177 * i.e., for simplicity, the destructor of every subclass should
178 * be empty and its content should be moved to the associated
181 virtual void DoDispose (void);
183 * \param o the object to copy.
185 * Allow subclasses to implement a copy constructor.
186 * While it is technically possible to implement a copy
187 * constructor in a subclass, we strongly discourage you
188 * to do so. If you really want to do it anyway, you have
189 * to understand that this copy constructor will _not_
190 * copy aggregated objects. i.e., if your object instance
191 * is already aggregated to another object and if you invoke
192 * this copy constructor, the new object instance will be
193 * a pristine standlone object instance not aggregated to
194 * any other object. It is thus _your_ responsability
195 * as a caller of this method to do what needs to be done
196 * (if it is needed) to ensure that the object stays in a
199 Object (const Object &o);
202 template <typename T>
203 friend Ptr<T> CreateObjectWithAttributes (const AttributeList &attributes);
204 template <typename T>
205 friend Ptr<T> CopyObject (Ptr<T> object);
206 template <typename T>
207 friend Ptr<T> CopyObject (Ptr<const T> object);
208 // The following friend method declaration is used only
209 // by our python bindings to call the protected ObjectBase::Construct
211 friend void PythonCompleteConstruct (Ptr<Object> object, TypeId typeId, const AttributeList &attributes);
212 template <typename T>
213 friend Ptr<T> CompleteConstruct (T *object);
215 friend class ObjectFactory;
216 friend class AggregateIterator;
218 Ptr<Object> DoGetObject (TypeId tid) const;
219 bool Check (void) const;
220 bool CheckLoose (void) const;
222 * Attempt to delete this object. This method iterates
223 * over all aggregated objects to check if they all
224 * have a zero refcount. If yes, the object and all
225 * its aggregates are deleted. If not, nothing is done.
227 void MaybeDelete (void) const;
229 * \param tid an TypeId
231 * Invoked from ns3::CreateObject only.
232 * Initialize the m_tid member variable to
233 * keep track of the type of this object instance.
235 void SetTypeId (TypeId tid);
237 * \param attributes the attribute values used to initialize
238 * the member variables of this object's instance.
240 * Invoked from ns3::ObjectFactory::Create and ns3::CreateObject only.
241 * Initialize all the member variables which were
242 * registered with the associated TypeId.
244 void Construct (const AttributeList &attributes);
247 * The reference count for this object. Each aggregate
248 * has an individual reference count. When the global
249 * reference count (the sum of all reference counts)
250 * reaches zero, the object and all its aggregates is
253 mutable uint32_t m_count;
255 * Identifies the type of this object instance.
259 * Set to true when the DoDispose method of the object
260 * has run, false otherwise.
264 * A pointer to the next aggregate object. This is a circular
265 * linked list of aggregated objects: the last one points
266 * back to the first one. If an object is not aggregated to
267 * any other object, the value of this field is equal to the
268 * value of the 'this' pointer.
274 * \param object a pointer to the object to copy.
275 * \returns a copy of the input object.
277 * This method invoke the copy constructor of the input object
278 * and returns the new instance.
280 template <typename T>
281 Ptr<T> CopyObject (Ptr<const T> object);
282 template <typename T>
283 Ptr<T> CopyObject (Ptr<T> object);
287 * \param attributes a list of attributes to set on the
288 * object during construction.
289 * \returns a pointer to a newly allocated object.
291 * This allocates an object on the heap and initializes
292 * it with a set of attributes.
294 template <typename T>
295 Ptr<T> CreateObjectWithAttributes (const AttributeList &attributes);
298 * \param n1 name of attribute
299 * \param v1 value of attribute
300 * \param n2 name of attribute
301 * \param v2 value of attribute
302 * \param n3 name of attribute
303 * \param v3 value of attribute
304 * \param n4 name of attribute
305 * \param v4 value of attribute
306 * \param n5 name of attribute
307 * \param v5 value of attribute
308 * \param n6 name of attribute
309 * \param v6 value of attribute
310 * \param n7 name of attribute
311 * \param v7 value of attribute
312 * \param n8 name of attribute
313 * \param v8 value of attribute
314 * \param n9 name of attribute
315 * \param v9 value of attribute
316 * \returns a pointer to a newly allocated object.
318 * This allocates an object on the heap and initializes
319 * it with a set of attributes.
321 template <typename T>
323 CreateObjectWithAttributes (std::string n1 = "", const AttributeValue & v1 = EmptyAttributeValue (),
324 std::string n2 = "", const AttributeValue & v2 = EmptyAttributeValue (),
325 std::string n3 = "", const AttributeValue & v3 = EmptyAttributeValue (),
326 std::string n4 = "", const AttributeValue & v4 = EmptyAttributeValue (),
327 std::string n5 = "", const AttributeValue & v5 = EmptyAttributeValue (),
328 std::string n6 = "", const AttributeValue & v6 = EmptyAttributeValue (),
329 std::string n7 = "", const AttributeValue & v7 = EmptyAttributeValue (),
330 std::string n8 = "", const AttributeValue & v8 = EmptyAttributeValue (),
331 std::string n9 = "", const AttributeValue & v9 = EmptyAttributeValue ());
339 /*************************************************************************
340 * The Object implementation which depends on templates
341 *************************************************************************/
344 Object::Ref (void) const
349 Object::Unref (void) const
351 NS_ASSERT (Check ());
359 template <typename T>
361 Object::GetObject () const
363 Ptr<Object> found = DoGetObject (T::GetTypeId ());
366 return Ptr<T> (dynamic_cast<T *> (PeekPointer (found)));
371 template <typename T>
373 Object::GetObject (TypeId tid) const
375 Ptr<Object> found = DoGetObject (tid);
378 return Ptr<T> (dynamic_cast<T *> (PeekPointer (found)));
383 /*************************************************************************
384 * The helper functions which need templates.
385 *************************************************************************/
387 template <typename T>
388 Ptr<T> CopyObject (Ptr<T> object)
390 Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
391 NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
395 template <typename T>
396 Ptr<T> CopyObject (Ptr<const T> object)
398 Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
399 NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
403 template <typename T>
404 Ptr<T> CompleteConstruct (T *p)
406 p->SetTypeId (T::GetTypeId ());
407 p->Object::Construct (AttributeList());
408 return Ptr<T> (p, false);
410 template <typename T>
411 Ptr<T> CreateObjectWithAttributes (const AttributeList &attributes)
413 Ptr<T> p = Ptr<T> (new T (), false);
414 p->SetTypeId (T::GetTypeId ());
415 p->Object::Construct (attributes);
419 template <typename T>
421 CreateObjectWithAttributes (std::string n1 , const AttributeValue & v1,
422 std::string n2 , const AttributeValue & v2,
423 std::string n3 , const AttributeValue & v3,
424 std::string n4 , const AttributeValue & v4,
425 std::string n5 , const AttributeValue & v5,
426 std::string n6 , const AttributeValue & v6,
427 std::string n7 , const AttributeValue & v7,
428 std::string n8 , const AttributeValue & v8,
429 std::string n9 , const AttributeValue & v9)
431 AttributeList attributes;
436 attributes.SetWithTid (T::GetTypeId (), n1, v1);
441 attributes.SetWithTid (T::GetTypeId (), n2, v2);
446 attributes.SetWithTid (T::GetTypeId (), n3, v3);
451 attributes.SetWithTid (T::GetTypeId (), n4, v4);
456 attributes.SetWithTid (T::GetTypeId (), n5, v5);
461 attributes.SetWithTid (T::GetTypeId (), n6, v6);
466 attributes.SetWithTid (T::GetTypeId (), n7, v7);
471 attributes.SetWithTid (T::GetTypeId (), n8, v8);
476 attributes.SetWithTid (T::GetTypeId (), n9, v9);
478 return CreateObjectWithAttributes<T> (attributes);
481 template <typename T>
482 Ptr<T> CreateObject (void)
484 return CompleteConstruct (new T ());
487 template <typename T, typename T1>
488 Ptr<T> CreateObject (T1 a1)
490 return CompleteConstruct (new T (a1));
493 template <typename T, typename T1, typename T2>
494 Ptr<T> CreateObject (T1 a1, T2 a2)
496 return CompleteConstruct (new T (a1,a2));
499 template <typename T, typename T1, typename T2, typename T3>
500 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
502 return CompleteConstruct (new T (a1,a2,a3));
505 template <typename T, typename T1, typename T2, typename T3, typename T4>
506 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
508 return CompleteConstruct (new T (a1,a2,a3,a4));
511 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
512 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
514 return CompleteConstruct (new T (a1,a2,a3,a4,a5));
517 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
518 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
520 return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6));
523 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
524 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
526 return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6,a7));
532 #endif /* OBJECT_H */