A Discrete-Event Network Simulator
API
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 
40 namespace ns3 {
41 
42 class Object;
43 class AttributeAccessor;
44 class AttributeValue;
45 class TraceSourceAccessor;
46 
60 {
69  inline static void Delete (Object *object);
70 };
71 
87 class Object : public SimpleRefCount<Object, ObjectBase, ObjectDeleter>
88 {
89 public:
94  static TypeId GetTypeId (void);
95 
105  {
106  public:
109 
116  bool HasNext (void) const;
117 
123  Ptr<const Object> Next (void);
124 
125  private:
126  friend class Object;
137  uint32_t m_current;
138  };
139 
141  Object ();
143  virtual ~Object ();
144 
145  virtual TypeId GetInstanceTypeId (void) const;
146 
153  template <typename T>
154  inline Ptr<T> GetObject (void) const;
162  template <typename T>
163  Ptr<T> GetObject (TypeId tid) const;
178  void Dispose (void);
194  void AggregateObject (Ptr<Object> other);
195 
207 
218  void Initialize (void);
219 
225  bool IsInitialized (void) const;
226 
227 protected:
241  virtual void NotifyNewAggregate (void);
253  virtual void DoInitialize (void);
269  virtual void DoDispose (void);
290  Object (const Object &o);
291 
292 private:
293 
304  template <typename T>
305  friend Ptr<T> CopyObject (Ptr<T> object);
306  template <typename T>
307  friend Ptr<T> CopyObject (Ptr<const T> object);
317  template <typename T>
318  friend Ptr<T> CompleteConstruct (T *object);
319 
320  friend class ObjectFactory;
321  friend class AggregateIterator;
322  friend struct ObjectDeleter;
323 
336  struct Aggregates
337  {
339  uint32_t n;
342  };
343 
350  Ptr<Object> DoGetObject (TypeId tid) const;
355  bool Check (void) const;
368  bool CheckLoose (void) const;
378  void SetTypeId (TypeId tid);
389  void Construct (const AttributeConstructionList &attributes);
390 
397  void UpdateSortedArray (struct Aggregates *aggregates, uint32_t i) const;
405  void DoDelete (void);
406 
437 };
438 
439 template <typename T>
441 template <typename T>
442 Ptr<T> CopyObject (Ptr<T> object);
443 
444 } // namespace ns3
445 
446 namespace ns3 {
447 
448 
449 /*************************************************************************
450  * The Object implementation which depends on templates
451  *************************************************************************/
452 
453 void
455 {
456  object->DoDelete ();
457 }
458 
459 template <typename T>
460 Ptr<T>
462 {
463  // This is an optimization: if the cast works (which is likely),
464  // things will be pretty fast.
465  T *result = dynamic_cast<T *> (m_aggregates->buffer[0]);
466  if (result != 0)
467  {
468  return Ptr<T> (result);
469  }
470  // if the cast does not work, we try to do a full type check.
471  Ptr<Object> found = DoGetObject (T::GetTypeId ());
472  if (found != 0)
473  {
474  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
475  }
476  return 0;
477 }
478 
479 template <typename T>
480 Ptr<T>
482 {
483  Ptr<Object> found = DoGetObject (tid);
484  if (found != 0)
485  {
486  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
487  }
488  return 0;
489 }
490 
491 /*************************************************************************
492  * The helper functions which need templates.
493  *************************************************************************/
494 
495 template <typename T>
497 {
498  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
499  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
500  return p;
501 }
502 
503 template <typename T>
504 Ptr<T> CopyObject (Ptr<const T> object)
505 {
506  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
507  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
508  return p;
509 }
510 
511 template <typename T>
513 {
514  object->SetTypeId (T::GetTypeId ());
515  object->Object::Construct (AttributeConstructionList ());
516  return Ptr<T> (object, false);
517 }
518 
529 template <typename T>
531 {
532  return CompleteConstruct (new T ());
533 }
541 template <typename T, typename T1>
543 {
544  return CompleteConstruct (new T (a1));
545 }
546 
556 template <typename T, typename T1, typename T2>
557 Ptr<T> CreateObject (T1 a1, T2 a2)
558 {
559  return CompleteConstruct (new T (a1,a2));
560 }
561 
573 template <typename T, typename T1, typename T2, typename T3>
574 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
575 {
576  return CompleteConstruct (new T (a1,a2,a3));
577 }
578 
592 template <typename T, typename T1, typename T2, typename T3, typename T4>
593 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
594 {
595  return CompleteConstruct (new T (a1,a2,a3,a4));
596 }
597 
613 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
614 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
615 {
616  return CompleteConstruct (new T (a1,a2,a3,a4,a5));
617 }
618 
636 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
637 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
638 {
639  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6));
640 }
641 
661 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
662 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
663 {
664  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6,a7));
665 }
668 } // namespace ns3
669 
670 #endif /* OBJECT_H */
671 
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void Construct(const AttributeConstructionList &attributes)
Initialize all member variables registered as Attributes of this TypeId.
Definition: object.cc:142
ns3::Ptr smart pointer declaration and implementation.
bool m_disposed
Set to true when the DoDispose() method of the Object has run, false otherwise.
Definition: object.h:415
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
uint32_t m_getObjectCount
The number of times the Object was accessed with a call to GetObject().
Definition: object.h:436
void UpdateSortedArray(struct Aggregates *aggregates, uint32_t i) const
Keep the list of aggregates in most-recently-used order.
Definition: object.cc:239
#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
bool IsInitialized(void) const
Check if the object has been initialized.
Definition: object.cc:208
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:566
struct Aggregates * m_aggregates
A pointer to an array of &#39;aggregates&#39;.
Definition: object.h:428
Ptr< Object > DoGetObject(TypeId tid) const
Find an Object of TypeId tid in the aggregates of this Object.
Definition: object.cc:149
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
friend Ptr< T > CompleteConstruct(T *object)
Set the TypeId and construct all Attributes of an Object.
Definition: object.h:512
bool m_initialized
Set to true once the DoInitialize() method has run, false otherwise.
Definition: object.h:420
Object * buffer[1]
The array of Objects.
Definition: object.h:341
Ptr< const Object > Next(void)
Get the next Aggregated Object.
Definition: object.cc:63
ns3::AttributeConstructionList declaration.
ns3::ObjectBase declaration and NS_OBJECT_ENSURE_REGISTERED() madro definition.
AggregateIterator()
Default constructor, which has no Object.
Definition: object.cc:49
Ptr< const Object > m_object
Parent Object.
Definition: object.h:136
void DoDelete(void)
Attempt to delete this Object.
Definition: object.cc:391
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
List of Attribute name, value and checker triples used to construct Objects.
friend Ptr< T > CopyObject(Ptr< T > object)
Copy an Object.
Definition: object.h:496
bool CheckLoose(void) const
Check if any aggregated Objects have non-zero reference counts.
Definition: object.cc:374
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:461
uint32_t n
The number of entries in buffer.
Definition: object.h:339
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TypeId m_tid
Identifies the type of this Object instance.
Definition: object.h:410
Standard Object deleter, used by SimpleRefCount to delete an Object when the reference count drops to...
Definition: object.h:59
void SetTypeId(TypeId tid)
Set the TypeId of this Object.
Definition: object.cc:338
static TypeId GetTypeId(void)
Register this type.
Definition: object.cc:86
static void Delete(Object *object)
Smart pointer deleter implementation for Object.
Definition: object.h:454
AggregateIterator GetAggregateIterator(void) const
Get an iterator to the Objects aggregated to this one.
Definition: object.cc:331
bool HasNext(void) const
Check if there are more Aggregates to iterate over.
Definition: object.cc:57
Instantiate subclasses of ns3::Object.
Ptr< T > CopyObject(Ptr< const T > object)
Definition: object.h:496
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: object.cc:79
uint32_t m_current
Current position in parent&#39;s aggregates.
Definition: object.h:137
virtual ~Object()
Destructor.
Definition: object.cc:107
Ptr< T > CompleteConstruct(T *object)
Definition: object.h:512
Ptr< T > CreateObject(void)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:530
bool Check(void) const
Verify that this Object is still live, by checking it&#39;s reference count.
Definition: object.cc:360
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:325
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:58
Iterate over the Objects aggregated to an ns3::Object.
Definition: object.h:104
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
ns3::SimpleRefCount declaration and template implementation.
The list of Objects aggregated to this one.
Definition: object.h:336
Object()
Constructor.
Definition: object.cc:96