A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA, Gustavo Carneiro
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Gustavo Carneiro <gjcarneiro@gmail.com>,
18 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20#ifndef OBJECT_H
21#define OBJECT_H
22
24#include "attribute.h"
25#include "object-base.h"
26#include "ptr.h"
27#include "simple-ref-count.h"
28
29#include <stdint.h>
30#include <string>
31#include <vector>
32
40namespace ns3
41{
42
43class Object;
44class AttributeAccessor;
45class AttributeValue;
46class TraceSourceAccessor;
47
61{
70 inline static void Delete(Object* object);
71};
72
88class Object : public SimpleRefCount<Object, ObjectBase, ObjectDeleter>
89{
90 public:
95 static TypeId GetTypeId();
96
106 {
107 public:
110
117 bool HasNext() const;
118
125
126 private:
128 friend class Object;
140 };
141
143 Object();
145 ~Object() override;
146
147 TypeId GetInstanceTypeId() const override;
148
157 template <typename T>
158 inline Ptr<T> GetObject() const;
168 template <typename T>
169 Ptr<T> GetObject(TypeId tid) const;
184 void Dispose();
200 void AggregateObject(Ptr<Object> other);
201
213
224 void Initialize();
225
232 bool IsInitialized() const;
233
234 protected:
248 virtual void NotifyNewAggregate();
260 virtual void DoInitialize();
276 virtual void DoDispose();
297 Object(const Object& o);
298
299 private:
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;
331
347 {
352 };
353
360 Ptr<Object> DoGetObject(TypeId tid) const;
365 bool Check() const;
378 bool CheckLoose() const;
388 void SetTypeId(TypeId tid);
399 void Construct(const AttributeConstructionList& attributes);
400
407 void UpdateSortedArray(Aggregates* aggregates, uint32_t i) const;
415 void DoDelete();
416
447};
448
449template <typename T>
451template <typename T>
452Ptr<T> CopyObject(Ptr<T> object);
453
454} // namespace ns3
455
456namespace ns3
457{
458
459/*************************************************************************
460 * The Object implementation which depends on templates
461 *************************************************************************/
462
463void
465{
466 object->DoDelete();
467}
468
469template <typename T>
470Ptr<T>
472{
473 // This is an optimization: if the cast works (which is likely),
474 // things will be pretty fast.
475 T* result = dynamic_cast<T*>(m_aggregates->buffer[0]);
476 if (result != nullptr)
477 {
478 return Ptr<T>(result);
479 }
480 // if the cast does not work, we try to do a full type check.
481 Ptr<Object> found = DoGetObject(T::GetTypeId());
482 if (found)
483 {
484 return Ptr<T>(static_cast<T*>(PeekPointer(found)));
485 }
486 return nullptr;
487}
488
495template <>
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)
508 {
509 return Ptr<T>(static_cast<T*>(PeekPointer(found)));
510 }
511 return nullptr;
512}
513
521template <>
522inline Ptr<Object>
524{
525 if (tid == Object::GetTypeId())
526 {
527 return Ptr<Object>(const_cast<Object*>(this));
528 }
529 else
530 {
531 return DoGetObject(tid);
532 }
533}
534
535/*************************************************************************
536 * The helper functions which need templates.
537 *************************************************************************/
538
539template <typename T>
540Ptr<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>
549Ptr<T>
551{
552 Ptr<T> p = Ptr<T>(new T(*PeekPointer(object)), false);
553 NS_ASSERT(p->GetInstanceTypeId() == object->GetInstanceTypeId());
554 return p;
555}
556
557template <typename T>
558Ptr<T>
560{
561 object->SetTypeId(T::GetTypeId());
562 object->Object::Construct(AttributeConstructionList());
563 return Ptr<T>(object, false);
564}
565
577template <typename T, typename... Args>
578Ptr<T>
579CreateObject(Args&&... args)
580{
581 return CompleteConstruct(new T(std::forward<Args>(args)...));
582}
583
586} // namespace ns3
587
588#endif /* OBJECT_H */
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:106
Ptr< const Object > Next()
Get the next Aggregated Object.
Definition: object.cc:66
Ptr< const Object > m_object
Parent Object.
Definition: object.h:138
AggregateIterator()
Default constructor, which has no Object.
Definition: object.cc:51
uint32_t m_current
Current position in parent's aggregates.
Definition: object.h:139
bool HasNext() const
Check if there are more Aggregates to iterate over.
Definition: object.cc:59
Instantiate subclasses of ns3::Object.
A base class which provides memory management and object aggregation.
Definition: object.h:89
Aggregates * m_aggregates
A pointer to an array of 'aggregates'.
Definition: object.h:438
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
friend Ptr< T > CopyObject(Ptr< T > object)
Copy an Object.
Definition: object.h:541
void UpdateSortedArray(Aggregates *aggregates, uint32_t i) const
Keep the list of aggregates in most-recently-used order.
Definition: object.cc:245
void Construct(const AttributeConstructionList &attributes)
Initialize all member variables registered as Attributes of this TypeId.
Definition: object.cc:144
bool Check() const
Verify that this Object is still live, by checking it's reference count.
Definition: object.cc:367
Ptr< Object > DoGetObject(TypeId tid) const
Find an Object of TypeId tid in the aggregates of this Object.
Definition: object.cc:151
bool m_disposed
Set to true when the DoDispose() method of the Object has run, false otherwise.
Definition: object.h:425
virtual void NotifyNewAggregate()
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:332
bool CheckLoose() const
Check if any aggregated Objects have non-zero reference counts.
Definition: object.cc:381
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
~Object() override
Destructor.
Definition: object.cc:107
AggregateIterator GetAggregateIterator() const
Get an iterator to the Objects aggregated to this one.
Definition: object.cc:338
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:360
void SetTypeId(TypeId tid)
Set the TypeId of this Object.
Definition: object.cc:345
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: object.cc:82
static TypeId GetTypeId()
Register this type.
Definition: object.cc:89
void Dispose()
Dispose of this Object.
Definition: object.cc:219
TypeId m_tid
Identifies the type of this Object instance.
Definition: object.h:420
Object()
Constructor.
Definition: object.cc:95
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
bool IsInitialized() const
Check if the object has been initialized.
Definition: object.cc:212
uint32_t m_getObjectCount
The number of times the Object was accessed with a call to GetObject().
Definition: object.h:446
friend Ptr< T > CompleteConstruct(T *object)
Set the TypeId and construct all Attributes of an Object.
Definition: object.h:559
void DoDelete()
Attempt to delete this Object.
Definition: object.cc:399
bool m_initialized
Set to true once the DoInitialize() method has run, false otherwise.
Definition: object.h:430
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:66
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:579
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:488
Ptr< T > CopyObject(Ptr< const T > object)
Definition: object.h:541
Ptr< T > CompleteConstruct(T *object)
Definition: object.h:559
ns3::ObjectBase declaration and NS_OBJECT_ENSURE_REGISTERED() macro 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:347
uint32_t n
The number of entries in buffer.
Definition: object.h:349
Object * buffer[1]
The array of Objects.
Definition: object.h:351
Standard Object deleter, used by SimpleRefCount to delete an Object when the reference count drops to...
Definition: object.h:61
static void Delete(Object *object)
Smart pointer deleter implementation for Object.
Definition: object.h:464