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:
69  static TypeId GetTypeId (void);
70 
80  {
81 public:
83 
88  bool HasNext (void) const;
89 
93  Ptr<const Object> Next (void);
94 private:
95  friend class Object;
98  uint32_t m_current;
99  };
100 
101  Object ();
102  virtual ~Object ();
103 
104  /*
105  * Implement the GetInstanceTypeId method defined in ObjectBase.
106  */
107  virtual TypeId GetInstanceTypeId (void) const;
108 
112  template <typename T>
113  inline Ptr<T> GetObject (void) const;
118  template <typename T>
119  Ptr<T> GetObject (TypeId tid) const;
132  void Dispose (void);
146  void AggregateObject (Ptr<Object> other);
147 
157 
166  void Initialize (void);
167 
168 protected:
176  virtual void NotifyNewAggregate (void);
186  virtual void DoInitialize (void);
200  virtual void DoDispose (void);
218  Object (const Object &o);
219 private:
220 
221  template <typename T>
222  friend Ptr<T> CopyObject (Ptr<T> object);
223  template <typename T>
224  friend Ptr<T> CopyObject (Ptr<const T> object);
225  template <typename T>
226  friend Ptr<T> CompleteConstruct (T *object);
227 
228  friend class ObjectFactory;
229  friend class AggregateIterator;
230  friend struct ObjectDeleter;
231 
242  struct Aggregates {
243  uint32_t n;
245  };
246 
253  Ptr<Object> DoGetObject (TypeId tid) const;
257  bool Check (void) const;
268  bool CheckLoose (void) const;
276  void SetTypeId (TypeId tid);
285  void Construct (const AttributeConstructionList &attributes);
286 
293  void UpdateSortedArray (struct Aggregates *aggregates, uint32_t i) const;
300  void DoDelete (void);
301 
330 };
331 
339 template <typename T>
341 template <typename T>
342 Ptr<T> CopyObject (Ptr<T> object);
343 
344 } // namespace ns3
345 
346 namespace ns3 {
347 
348 void
350 {
351  object->DoDelete ();
352 }
353 
354 /*************************************************************************
355  * The Object implementation which depends on templates
356  *************************************************************************/
357 
358 template <typename T>
359 Ptr<T>
361 {
362  // This is an optimization: if the cast works (which is likely),
363  // things will be pretty fast.
364  T *result = dynamic_cast<T *> (m_aggregates->buffer[0]);
365  if (result != 0)
366  {
367  return Ptr<T> (result);
368  }
369  // if the cast does not work, we try to do a full type check.
370  Ptr<Object> found = DoGetObject (T::GetTypeId ());
371  if (found != 0)
372  {
373  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
374  }
375  return 0;
376 }
377 
378 template <typename T>
379 Ptr<T>
381 {
382  Ptr<Object> found = DoGetObject (tid);
383  if (found != 0)
384  {
385  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
386  }
387  return 0;
388 }
389 
390 /*************************************************************************
391  * The helper functions which need templates.
392  *************************************************************************/
393 
394 template <typename T>
396 {
397  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
398  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
399  return p;
400 }
401 
402 template <typename T>
403 Ptr<T> CopyObject (Ptr<const T> object)
404 {
405  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
406  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
407  return p;
408 }
409 
410 template <typename T>
412 {
413  p->SetTypeId (T::GetTypeId ());
414  p->Object::Construct (AttributeConstructionList ());
415  return Ptr<T> (p, false);
416 }
417 
418 template <typename T>
420 {
421  return CompleteConstruct (new T ());
422 }
423 
424 template <typename T, typename T1>
426 {
427  return CompleteConstruct (new T (a1));
428 }
429 
430 template <typename T, typename T1, typename T2>
431 Ptr<T> CreateObject (T1 a1, T2 a2)
432 {
433  return CompleteConstruct (new T (a1,a2));
434 }
435 
436 template <typename T, typename T1, typename T2, typename T3>
437 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
438 {
439  return CompleteConstruct (new T (a1,a2,a3));
440 }
441 
442 template <typename T, typename T1, typename T2, typename T3, typename T4>
443 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
444 {
445  return CompleteConstruct (new T (a1,a2,a3,a4));
446 }
447 
448 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
449 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
450 {
451  return CompleteConstruct (new T (a1,a2,a3,a4,a5));
452 }
453 
454 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
455 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
456 {
457  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6));
458 }
459 
460 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
461 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
462 {
463  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6,a7));
464 }
465 
466 
467 } // namespace ns3
468 
469 #endif /* OBJECT_H */
470 
bool CheckLoose(void) const
Definition: object.cc:363
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
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)
Definition: object.cc:376
bool m_disposed
Definition: object.h:310
uint32_t m_getObjectCount
Definition: object.h:329
#define NS_ASSERT(condition)
Definition: assert.h:64
struct Aggregates * m_aggregates
Definition: object.h:322
virtual void DoDispose(void)
Definition: object.cc:335
friend Ptr< T > CompleteConstruct(T *object)
Definition: object.h:411
bool m_initialized
Definition: object.h:315
Object * buffer[1]
Definition: object.h:244
Ptr< T > CreateObject(void)
Definition: object.h:419
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:97
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:279
friend Ptr< T > CopyObject(Ptr< T > object)
Definition: object.h:395
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
virtual void NotifyNewAggregate(void)
Definition: object.cc:314
TypeId m_tid
Definition: object.h:305
Ptr< T > CompleteConstruct(T *p)
Definition: object.h:411
static void Delete(Object *object)
Definition: object.h:349
AggregateIterator GetAggregateIterator(void) const
Definition: object.cc:320
instantiate subclasses of ns3::Object.
Ptr< T > CopyObject(Ptr< const T > object)
Definition: object.h:395
bool HasNext(void) const
Definition: object.cc:54
static TypeId GetTypeId(void)
Definition: object.cc:83
uint32_t m_current
Current position in parent's aggegrates.
Definition: object.h:98
void Construct(const AttributeConstructionList &attributes)
Definition: object.cc:138
void Initialize(void)
Definition: object.cc:179
Ptr< Object > DoGetObject(TypeId tid) const
Definition: object.cc:145
a base class which provides memory management and object aggregation
Definition: object.h:63
Ptr< T > GetObject(void) const
Definition: object.h:360
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
Definition: object.cc:229
void Dispose(void)
Definition: object.cc:204
Iterate over the objects aggregated to an ns3::Object.
Definition: object.h:79
virtual void DoInitialize(void)
Definition: object.cc:342