A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
object.h
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"
30 #include "attribute-construction-list.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:
66  static TypeId GetTypeId (void);
67 
77  {
78 public:
80 
85  bool HasNext (void) const;
86 
90  Ptr<const Object> Next (void);
91 private:
92  friend class Object;
94  Ptr<const Object> m_object;
95  uint32_t m_current;
96  };
97 
98  Object ();
99  virtual ~Object ();
100 
101  /*
102  * Implement the GetInstanceTypeId method defined in ObjectBase.
103  */
104  virtual TypeId GetInstanceTypeId (void) const;
105 
109  template <typename T>
110  inline Ptr<T> GetObject (void) const;
115  template <typename T>
116  Ptr<T> GetObject (TypeId tid) const;
129  void Dispose (void);
143  void AggregateObject (Ptr<Object> other);
144 
154 
163  void Initialize (void);
164 
165 protected:
173  virtual void NotifyNewAggregate (void);
183  virtual void DoInitialize (void);
197  virtual void DoDispose (void);
215  Object (const Object &o);
216 private:
217 
218  template <typename T>
219  friend Ptr<T> CopyObject (Ptr<T> object);
220  template <typename T>
221  friend Ptr<T> CopyObject (Ptr<const T> object);
222  template <typename T>
223  friend Ptr<T> CompleteConstruct (T *object);
224 
225  friend class ObjectFactory;
226  friend class AggregateIterator;
227  friend struct ObjectDeleter;
228 
239  struct Aggregates {
240  uint32_t n;
241  Object *buffer[1];
242  };
243 
244  Ptr<Object> DoGetObject (TypeId tid) const;
245  bool Check (void) const;
246  bool CheckLoose (void) const;
254  void SetTypeId (TypeId tid);
263  void Construct (const AttributeConstructionList &attributes);
264 
265  void UpdateSortedArray (struct Aggregates *aggregates, uint32_t i) const;
272  void DoDelete (void);
273 
302 };
303 
311 template <typename T>
313 template <typename T>
314 Ptr<T> CopyObject (Ptr<T> object);
315 
316 } // namespace ns3
317 
318 namespace ns3 {
319 
320 void
321 ObjectDeleter::Delete (Object *object)
322 {
323  object->DoDelete ();
324 }
325 
326 /*************************************************************************
327  * The Object implementation which depends on templates
328  *************************************************************************/
329 
330 template <typename T>
331 Ptr<T>
333 {
334  // This is an optimization: if the cast works (which is likely),
335  // things will be pretty fast.
336  T *result = dynamic_cast<T *> (m_aggregates->buffer[0]);
337  if (result != 0)
338  {
339  return Ptr<T> (result);
340  }
341  // if the cast does not work, we try to do a full type check.
342  Ptr<Object> found = DoGetObject (T::GetTypeId ());
343  if (found != 0)
344  {
345  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
346  }
347  return 0;
348 }
349 
350 template <typename T>
351 Ptr<T>
353 {
354  Ptr<Object> found = DoGetObject (tid);
355  if (found != 0)
356  {
357  return Ptr<T> (static_cast<T *> (PeekPointer (found)));
358  }
359  return 0;
360 }
361 
362 /*************************************************************************
363  * The helper functions which need templates.
364  *************************************************************************/
365 
366 template <typename T>
368 {
369  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
370  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
371  return p;
372 }
373 
374 template <typename T>
375 Ptr<T> CopyObject (Ptr<const T> object)
376 {
377  Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
378  NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
379  return p;
380 }
381 
382 template <typename T>
383 Ptr<T> CompleteConstruct (T *p)
384 {
385  p->SetTypeId (T::GetTypeId ());
386  p->Object::Construct (AttributeConstructionList ());
387  return Ptr<T> (p, false);
388 }
389 
390 template <typename T>
391 Ptr<T> CreateObject (void)
392 {
393  return CompleteConstruct (new T ());
394 }
395 
396 template <typename T, typename T1>
397 Ptr<T> CreateObject (T1 a1)
398 {
399  return CompleteConstruct (new T (a1));
400 }
401 
402 template <typename T, typename T1, typename T2>
403 Ptr<T> CreateObject (T1 a1, T2 a2)
404 {
405  return CompleteConstruct (new T (a1,a2));
406 }
407 
408 template <typename T, typename T1, typename T2, typename T3>
409 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
410 {
411  return CompleteConstruct (new T (a1,a2,a3));
412 }
413 
414 template <typename T, typename T1, typename T2, typename T3, typename T4>
415 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
416 {
417  return CompleteConstruct (new T (a1,a2,a3,a4));
418 }
419 
420 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
421 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
422 {
423  return CompleteConstruct (new T (a1,a2,a3,a4,a5));
424 }
425 
426 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
427 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
428 {
429  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6));
430 }
431 
432 template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
433 Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
434 {
435  return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6,a7));
436 }
437 
438 
439 } // namespace ns3
440 
441 #endif /* OBJECT_H */
442 
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
virtual TypeId GetInstanceTypeId(void) const
Definition: object.cc:76
void DoDelete(void)
Definition: object.cc:376
bool m_disposed
Definition: object.h:282
uint32_t m_getObjectCount
Definition: object.h:301
#define NS_ASSERT(condition)
Definition: assert.h:64
struct Aggregates * m_aggregates
Definition: object.h:294
virtual void DoDispose(void)
Definition: object.cc:335
bool m_initialized
Definition: object.h:287
Ptr< const Object > Next(void)
Definition: object.cc:60
void SetTypeId(TypeId tid)
Definition: object.cc:327
friend Ptr< T > CopyObject(Ptr< T > object)
Definition: object.h:367
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
virtual void NotifyNewAggregate(void)
Definition: object.cc:314
TypeId m_tid
Definition: object.h:277
AggregateIterator GetAggregateIterator(void) const
Definition: object.cc:320
instantiate subclasses of ns3::Object.
Ptr< T > CopyObject(Ptr< const T > object)
Definition: object.h:367
bool HasNext(void) const
Definition: object.cc:54
void Construct(const AttributeConstructionList &attributes)
Definition: object.cc:138
void Initialize(void)
Definition: object.cc:179
a base class which provides memory management and object aggregation
Definition: object.h:63
Ptr< T > GetObject(void) const
Definition: object.h:332
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:44
void Dispose(void)
Definition: object.cc:204
Iterate over the objects aggregated to an ns3::Object.
Definition: object.h:76
virtual void DoInitialize(void)
Definition: object.cc:342