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 
64 class Object : public SimpleRefCount<Object,ObjectBase,ObjectDeleter>
65 {
66 public:
71  static TypeId GetTypeId (void);
72 
82  {
83 public:
85 
90  bool HasNext (void) const;
91 
95  Ptr<const Object> Next (void);
96 private:
97  friend class Object;
100  uint32_t m_current;
101  };
102 
103  Object ();
104  virtual ~Object ();
105 
106  /*
107  * Implement the GetInstanceTypeId method defined in ObjectBase.
108  */
109  virtual TypeId GetInstanceTypeId (void) const;
110 
114  template <typename T>
115  inline Ptr<T> GetObject (void) const;
120  template <typename T>
121  Ptr<T> GetObject (TypeId tid) const;
134  void Dispose (void);
148  void AggregateObject (Ptr<Object> other);
149 
159 
168  void Initialize (void);
169 
170 protected:
178  virtual void NotifyNewAggregate (void);
188  virtual void DoInitialize (void);
202  virtual void DoDispose (void);
220  Object (const Object &o);
221 private:
222 
223  template <typename T>
224  friend Ptr<T> CopyObject (Ptr<T> object);
225  template <typename T>
226  friend Ptr<T> CopyObject (Ptr<const T> object);
227  template <typename T>
228  friend Ptr<T> CompleteConstruct (T *object);
229 
230  friend class ObjectFactory;
231  friend class AggregateIterator;
232  friend struct ObjectDeleter;
233 
244  struct Aggregates {
245  uint32_t n;
247  };
248 
255  Ptr<Object> DoGetObject (TypeId tid) const;
259  bool Check (void) const;
270  bool CheckLoose (void) const;
278  void SetTypeId (TypeId tid);
287  void Construct (const AttributeConstructionList &attributes);
288 
295  void UpdateSortedArray (struct Aggregates *aggregates, uint32_t i) const;
302  void DoDelete (void);
303 
332 };
333 
341 template <typename T>
343 template <typename T>
344 Ptr<T> CopyObject (Ptr<T> object);
345 
346 } // namespace ns3
347 
348 namespace ns3 {
349 
350 void
352 {
353  object->DoDelete ();
354 }
355 
356 /*************************************************************************
357  * The Object implementation which depends on templates
358  *************************************************************************/
359 
360 template <typename T>
361 Ptr<T>
363 {
364  // This is an optimization: if the cast works (which is likely),
365  // things will be pretty fast.
366  T *result = dynamic_cast<T *> (m_aggregates->buffer[0]);
367  if (result != 0)
368  {
369  return Ptr<T> (result);
370  }
371  // if the cast does not work, we try to do a full type check.
372  Ptr<Object> found = DoGetObject (T::GetTypeId ());
373  if (found != 0)
374  {
375  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
376  }
377  return 0;
378 }
379 
380 template <typename T>
381 Ptr<T>
383 {
384  Ptr<Object> found = DoGetObject (tid);
385  if (found != 0)
386  {
387  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
388  }
389  return 0;
390 }
391 
392 /*************************************************************************
393  * The helper functions which need templates.
394  *************************************************************************/
395 
396 template <typename T>
398 {
399  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
400  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
401  return p;
402 }
403 
404 template <typename T>
405 Ptr<T> CopyObject (Ptr<const T> object)
406 {
407  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
408  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
409  return p;
410 }
411 
412 template <typename T>
414 {
415  p->SetTypeId (T::GetTypeId ());
416  p->Object::Construct (AttributeConstructionList ());
417  return Ptr<T> (p, false);
418 }
419 
420 template <typename T>
422 {
423  return CompleteConstruct (new T ());
424 }
425 
426 template <typename T, typename T1>
428 {
429  return CompleteConstruct (new T (a1));
430 }
431 
432 template <typename T, typename T1, typename T2>
433 Ptr<T> CreateObject (T1 a1, T2 a2)
434 {
435  return CompleteConstruct (new T (a1,a2));
436 }
437 
438 template <typename T, typename T1, typename T2, typename T3>
439 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
440 {
441  return CompleteConstruct (new T (a1,a2,a3));
442 }
443 
444 template <typename T, typename T1, typename T2, typename T3, typename T4>
445 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
446 {
447  return CompleteConstruct (new T (a1,a2,a3,a4));
448 }
449 
450 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
451 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
452 {
453  return CompleteConstruct (new T (a1,a2,a3,a4,a5));
454 }
455 
456 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
457 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
458 {
459  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6));
460 }
461 
462 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
463 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
464 {
465  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6,a7));
466 }
467 
468 
469 } // namespace ns3
470 
471 #endif /* OBJECT_H */
472 
bool CheckLoose(void) const
Definition: object.cc:363
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
virtual ~Object()
Definition: object.cc:103
virtual TypeId GetInstanceTypeId(void) const
Definition: object.cc:76
bool Check(void) const
Definition: object.cc:349
void DoDelete(void)
Attempt to delete this object.
Definition: object.cc:376
bool m_disposed
Set to true when the DoDispose method of the object has run, false otherwise.
Definition: object.h:312
uint32_t m_getObjectCount
Indicates the number of times the object was accessed with a call to GetObject.
Definition: object.h:331
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
struct Aggregates * m_aggregates
a pointer to an array of 'aggregates'.
Definition: object.h:324
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
friend Ptr< T > CompleteConstruct(T *object)
Definition: object.h:413
bool m_initialized
Set to true once the DoInitialize method has run, false otherwise.
Definition: object.h:317
Object * buffer[1]
Definition: object.h:246
Ptr< T > CreateObject(void)
Definition: object.h:421
Ptr< const Object > Next(void)
Definition: object.cc:60
void SetTypeId(TypeId tid)
Definition: object.cc:327
Ptr< const Object > m_object
Parent Object.
Definition: object.h:99
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:280
friend Ptr< T > CopyObject(Ptr< T > object)
Definition: object.h:397
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
virtual void NotifyNewAggregate(void)
This method is invoked whenever two sets of objects are aggregated together.
Definition: object.cc:314
TypeId m_tid
Identifies the type of this object instance.
Definition: object.h:307
Ptr< T > CompleteConstruct(T *p)
Definition: object.h:413
static void Delete(Object *object)
Definition: object.h:351
AggregateIterator GetAggregateIterator(void) const
Definition: object.cc:320
instantiate subclasses of ns3::Object.
Ptr< T > CopyObject(Ptr< const T > object)
Definition: object.h:397
bool HasNext(void) const
Definition: object.cc:54
static TypeId GetTypeId(void)
Register this type.
Definition: object.cc:83
uint32_t m_current
Current position in parent's aggegrates.
Definition: object.h:100
void Construct(const AttributeConstructionList &attributes)
Definition: object.cc:138
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:179
Ptr< Object > DoGetObject(TypeId tid) const
Find an object of TypeId tid in the aggregates of this Object.
Definition: object.cc:145
a base class which provides memory management and object aggregation
Definition: object.h:64
Ptr< T > GetObject(void) const
Definition: object.h:362
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:229
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:204
Iterate over the objects aggregated to an ns3::Object.
Definition: object.h:81
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:342
This data structure uses a classic C-style trick to hold an array of variable size without performing...
Definition: object.h:244