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
40namespace ns3 {
41
42class Object;
43class AttributeAccessor;
44class AttributeValue;
45class TraceSourceAccessor;
46
60{
69 inline static void Delete (Object *object);
70};
71
87class Object : public SimpleRefCount<Object, ObjectBase, ObjectDeleter>
88{
89public:
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:
127 friend class Object;
139 };
140
142 Object ();
144 virtual ~Object ();
145
146 virtual TypeId GetInstanceTypeId (void) const;
147
156 template <typename T>
157 inline Ptr<T> GetObject (void) const;
167 template <typename T>
168 Ptr<T> GetObject (TypeId tid) const;
183 void Dispose (void);
199 void AggregateObject (Ptr<Object> other);
200
212
223 void Initialize (void);
224
231 bool IsInitialized (void) const;
232
233protected:
247 virtual void NotifyNewAggregate (void);
259 virtual void DoInitialize (void);
275 virtual void DoDispose (void);
296 Object (const Object &o);
297
298private:
299
311 template <typename T>
312 friend Ptr<T> CopyObject (Ptr<T> object);
313 template <typename T>
314 friend Ptr<T> CopyObject (Ptr<const T> object);
324 template <typename T>
325 friend Ptr<T> CompleteConstruct (T *object);
326
328 friend class ObjectFactory;
329 friend class AggregateIterator;
330 friend struct ObjectDeleter;
346 {
351 };
352
359 Ptr<Object> DoGetObject (TypeId tid) const;
364 bool Check (void) const;
377 bool CheckLoose (void) const;
387 void SetTypeId (TypeId tid);
398 void Construct (const AttributeConstructionList &attributes);
399
406 void UpdateSortedArray (struct Aggregates *aggregates, uint32_t i) const;
414 void DoDelete (void);
415
446};
447
448template <typename T>
450template <typename T>
451Ptr<T> CopyObject (Ptr<T> object);
452
453} // namespace ns3
454
455namespace ns3 {
456
457
458/*************************************************************************
459 * The Object implementation which depends on templates
460 *************************************************************************/
461
462void
464{
465 object->DoDelete ();
466}
467
468template <typename T>
469Ptr<T>
471{
472 // This is an optimization: if the cast works (which is likely),
473 // things will be pretty fast.
474 T *result = dynamic_cast<T *> (m_aggregates->buffer[0]);
475 if (result != 0)
476 {
477 return Ptr<T> (result);
478 }
479 // if the cast does not work, we try to do a full type check.
480 Ptr<Object> found = DoGetObject (T::GetTypeId ());
481 if (found != 0)
482 {
483 return Ptr<T> (static_cast<T *> (PeekPointer (found)));
484 }
485 return 0;
486}
487
494template
495<>
496inline Ptr<Object>
498{
499 return Ptr<Object> (const_cast<Object *> (this));
500}
501
502template <typename T>
503Ptr<T>
505{
506 Ptr<Object> found = DoGetObject (tid);
507 if (found != 0)
508 {
509 return Ptr<T> (static_cast<T *> (PeekPointer (found)));
510 }
511 return 0;
512}
513
521template
522<>
523inline Ptr<Object>
525{
526 if (tid == Object::GetTypeId ())
527 {
528 return Ptr<Object> (const_cast<Object *> (this));
529 }
530 else
531 {
532 return DoGetObject (tid);
533 }
534}
535
536/*************************************************************************
537 * The helper functions which need templates.
538 *************************************************************************/
539
540template <typename T>
542{
543 Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
544 NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
545 return p;
546}
547
548template <typename T>
550{
551 Ptr<T> p = Ptr<T> (new T (*PeekPointer (object)), false);
552 NS_ASSERT (p->GetInstanceTypeId () == object->GetInstanceTypeId ());
553 return p;
554}
555
556template <typename T>
558{
559 object->SetTypeId (T::GetTypeId ());
560 object->Object::Construct (AttributeConstructionList ());
561 return Ptr<T> (object, false);
562}
563
575template <typename T, typename... Args>
576Ptr<T> CreateObject (Args&&... args)
577{
578 return CompleteConstruct (new T (std::forward<Args> (args)...));
579}
582} // namespace ns3
583
584#endif /* OBJECT_H */
585
ns3::AttributeConstructionList declaration.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
List of Attribute name, value and checker triples used to construct Objects.
Iterate over the Objects aggregated to an ns3::Object.
Definition: object.h:105
Ptr< const Object > m_object
Parent Object.
Definition: object.h:137
AggregateIterator()
Default constructor, which has no Object.
Definition: object.cc:49
Ptr< const Object > Next(void)
Get the next Aggregated Object.
Definition: object.cc:63
uint32_t m_current
Current position in parent's aggregates.
Definition: object.h:138
bool HasNext(void) const
Check if there are more Aggregates to iterate over.
Definition: object.cc:57
Instantiate subclasses of ns3::Object.
A base class which provides memory management and object aggregation.
Definition: object.h:88
friend Ptr< T > CopyObject(Ptr< T > object)
Copy an Object.
Definition: object.h:541
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: object.cc:79
struct Aggregates * m_aggregates
A pointer to an array of 'aggregates'.
Definition: object.h:437
bool IsInitialized(void) const
Check if the object has been initialized.
Definition: object.cc:208
AggregateIterator GetAggregateIterator(void) const
Get an iterator to the Objects aggregated to this one.
Definition: object.cc:331
void Construct(const AttributeConstructionList &attributes)
Initialize all member variables registered as Attributes of this TypeId.
Definition: object.cc:142
Ptr< Object > DoGetObject(TypeId tid) const
Find an Object of TypeId tid in the aggregates of this Object.
Definition: object.cc:149
bool m_disposed
Set to true when the DoDispose() method of the Object has run, false otherwise.
Definition: object.h:424
bool CheckLoose(void) const
Check if any aggregated Objects have non-zero reference counts.
Definition: object.cc:374
static TypeId GetTypeId(void)
Register this type.
Definition: object.cc:86
void UpdateSortedArray(struct Aggregates *aggregates, uint32_t i) const
Keep the list of aggregates in most-recently-used order.
Definition: object.cc:239
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
bool Check(void) const
Verify that this Object is still live, by checking it's reference count.
Definition: object.cc:360
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
virtual ~Object()
Destructor.
Definition: object.cc:107
void SetTypeId(TypeId tid)
Set the TypeId of this Object.
Definition: object.cc:338
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
TypeId m_tid
Identifies the type of this Object instance.
Definition: object.h:419
Object()
Constructor.
Definition: object.cc:96
void DoDelete(void)
Attempt to delete this Object.
Definition: object.cc:391
uint32_t m_getObjectCount
The number of times the Object was accessed with a call to GetObject().
Definition: object.h:445
friend Ptr< T > CompleteConstruct(T *object)
Set the TypeId and construct all Attributes of an Object.
Definition: object.h:557
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:325
bool m_initialized
Set to true once the DoInitialize() method has run, false otherwise.
Definition: object.h:429
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:59
#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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:576
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:415
Ptr< T > CopyObject(Ptr< const T > object)
Definition: object.h:541
Ptr< T > CompleteConstruct(T *object)
Definition: object.h:557
ns3::ObjectBase declaration and NS_OBJECT_ENSURE_REGISTERED() madro definition.
ns3::Ptr smart pointer declaration and implementation.
ns3::SimpleRefCount declaration and template implementation.
The list of Objects aggregated to this one.
Definition: object.h:346
uint32_t n
The number of entries in buffer.
Definition: object.h:348
Object * buffer[1]
The array of Objects.
Definition: object.h:350
Standard Object deleter, used by SimpleRefCount to delete an Object when the reference count drops to...
Definition: object.h:60
static void Delete(Object *object)
Smart pointer deleter implementation for Object.
Definition: object.h:463