A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
object.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA, Gustavo Carneiro
4  *
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;
8  *
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.
13  *
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
17  *
18  * Authors: Gustavo Carneiro <gjcarneiro@gmail.com>,
19  * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  */
21 #ifndef OBJECT_H
22 #define OBJECT_H
23 
24 #include <stdint.h>
25 #include <string>
26 #include <vector>
27 #include "ptr.h"
28 #include "attribute.h"
29 #include "object-base.h"
31 #include "simple-ref-count.h"
32 
33 
34 namespace ns3 {
35 
36 class Object;
37 class AttributeAccessor;
38 class AttributeValue;
39 class TraceSourceAccessor;
40 
42 {
43  inline static void Delete (Object *object);
44 };
45 
63 class Object : public SimpleRefCount<Object,ObjectBase,ObjectDeleter>
64 {
65 public:
70  static TypeId GetTypeId (void);
71 
81  {
82 public:
84 
89  bool HasNext (void) const;
90 
94  Ptr<const Object> Next (void);
95 private:
96  friend class Object;
99  uint32_t m_current;
100  };
101 
102  Object ();
103  virtual ~Object ();
104 
105  /*
106  * Implement the GetInstanceTypeId method defined in ObjectBase.
107  */
108  virtual TypeId GetInstanceTypeId (void) const;
109 
113  template <typename T>
114  inline Ptr<T> GetObject (void) const;
119  template <typename T>
120  Ptr<T> GetObject (TypeId tid) const;
133  void Dispose (void);
147  void AggregateObject (Ptr<Object> other);
148 
158 
167  void Initialize (void);
168 
169 protected:
177  virtual void NotifyNewAggregate (void);
187  virtual void DoInitialize (void);
201  virtual void DoDispose (void);
219  Object (const Object &o);
220 private:
221 
222  template <typename T>
223  friend Ptr<T> CopyObject (Ptr<T> object);
224  template <typename T>
225  friend Ptr<T> CopyObject (Ptr<const T> object);
226  template <typename T>
227  friend Ptr<T> CompleteConstruct (T *object);
228 
229  friend class ObjectFactory;
230  friend class AggregateIterator;
231  friend struct ObjectDeleter;
232 
243  struct Aggregates {
244  uint32_t n;
246  };
247 
254  Ptr<Object> DoGetObject (TypeId tid) const;
258  bool Check (void) const;
269  bool CheckLoose (void) const;
277  void SetTypeId (TypeId tid);
286  void Construct (const AttributeConstructionList &attributes);
287 
294  void UpdateSortedArray (struct Aggregates *aggregates, uint32_t i) const;
301  void DoDelete (void);
302 
331 };
332 
340 template <typename T>
342 template <typename T>
343 Ptr<T> CopyObject (Ptr<T> object);
344 
345 } // namespace ns3
346 
347 namespace ns3 {
348 
349 void
351 {
352  object->DoDelete ();
353 }
354 
355 /*************************************************************************
356  * The Object implementation which depends on templates
357  *************************************************************************/
358 
359 template <typename T>
360 Ptr<T>
362 {
363  // This is an optimization: if the cast works (which is likely),
364  // things will be pretty fast.
365  T *result = dynamic_cast<T *> (m_aggregates->buffer[0]);
366  if (result != 0)
367  {
368  return Ptr<T> (result);
369  }
370  // if the cast does not work, we try to do a full type check.
371  Ptr<Object> found = DoGetObject (T::GetTypeId ());
372  if (found != 0)
373  {
374  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
375  }
376  return 0;
377 }
378 
379 template <typename T>
380 Ptr<T>
382 {
383  Ptr<Object> found = DoGetObject (tid);
384  if (found != 0)
385  {
386  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
387  }
388  return 0;
389 }
390 
391 /*************************************************************************
392  * The helper functions which need templates.
393  *************************************************************************/
394 
395 template <typename T>
397 {
398  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
399  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
400  return p;
401 }
402 
403 template <typename T>
404 Ptr<T> CopyObject (Ptr<const T> object)
405 {
406  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
407  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
408  return p;
409 }
410 
411 template <typename T>
413 {
414  p->SetTypeId (T::GetTypeId ());
415  p->Object::Construct (AttributeConstructionList ());
416  return Ptr<T> (p, false);
417 }
418 
419 template <typename T>
421 {
422  return CompleteConstruct (new T ());
423 }
424 
425 template <typename T, typename T1>
427 {
428  return CompleteConstruct (new T (a1));
429 }
430 
431 template <typename T, typename T1, typename T2>
432 Ptr<T> CreateObject (T1 a1, T2 a2)
433 {
434  return CompleteConstruct (new T (a1,a2));
435 }
436 
437 template <typename T, typename T1, typename T2, typename T3>
438 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
439 {
440  return CompleteConstruct (new T (a1,a2,a3));
441 }
442 
443 template <typename T, typename T1, typename T2, typename T3, typename T4>
444 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
445 {
446  return CompleteConstruct (new T (a1,a2,a3,a4));
447 }
448 
449 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
450 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
451 {
452  return CompleteConstruct (new T (a1,a2,a3,a4,a5));
453 }
454 
455 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
456 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
457 {
458  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6));
459 }
460 
461 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
462 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
463 {
464  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6,a7));
465 }
466 
467 
468 } // namespace ns3
469 
470 #endif /* OBJECT_H */
471 
bool CheckLoose(void) const
Definition: object.cc:364
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
virtual ~Object()
Definition: object.cc:104
virtual TypeId GetInstanceTypeId(void) const
Definition: object.cc:77
bool Check(void) const
Definition: object.cc:350
void DoDelete(void)
Attempt to delete this object.
Definition: object.cc:377
bool m_disposed
Set to true when the DoDispose method of the object has run, false otherwise.
Definition: object.h:311
uint32_t m_getObjectCount
Indicates the number of times the object was accessed with a call to GetObject.
Definition: object.h:330
#define NS_ASSERT(condition)
Definition: assert.h:64
struct Aggregates * m_aggregates
a pointer to an array of 'aggregates'.
Definition: object.h:323
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
friend Ptr< T > CompleteConstruct(T *object)
Definition: object.h:412
bool m_initialized
Set to true once the DoInitialize method has run, false otherwise.
Definition: object.h:316
Object * buffer[1]
Definition: object.h:245
Ptr< T > CreateObject(void)
Definition: object.h:420
Ptr< const Object > Next(void)
Definition: object.cc:61
void SetTypeId(TypeId tid)
Definition: object.cc:328
Ptr< const Object > m_object
Parent Object.
Definition: object.h:98
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:279
friend Ptr< T > CopyObject(Ptr< T > object)
Definition: object.h:396
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
virtual void NotifyNewAggregate(void)
This method is invoked whenever two sets of objects are aggregated together.
Definition: object.cc:315
TypeId m_tid
Identifies the type of this object instance.
Definition: object.h:306
Ptr< T > CompleteConstruct(T *p)
Definition: object.h:412
static void Delete(Object *object)
Definition: object.h:350
AggregateIterator GetAggregateIterator(void) const
Definition: object.cc:321
instantiate subclasses of ns3::Object.
Ptr< T > CopyObject(Ptr< const T > object)
Definition: object.h:396
bool HasNext(void) const
Definition: object.cc:55
static TypeId GetTypeId(void)
Register this type.
Definition: object.cc:84
uint32_t m_current
Current position in parent's aggegrates.
Definition: object.h:99
void Construct(const AttributeConstructionList &attributes)
Definition: object.cc:139
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:180
Ptr< Object > DoGetObject(TypeId tid) const
Find an object of TypeId tid in the aggregates of this Object.
Definition: object.cc:146
a base class which provides memory management and object aggregation
Definition: object.h:63
Ptr< T > GetObject(void) const
Definition: object.h:361
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:49
void UpdateSortedArray(struct Aggregates *aggregates, uint32_t i) const
Keep the list of aggregates in most-recently-used order.
Definition: object.cc:230
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:205
Iterate over the objects aggregated to an ns3::Object.
Definition: object.h:80
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:343
This data structure uses a classic C-style trick to hold an array of variable size without performing...
Definition: object.h:243