doc/manual/objects.texi
author Andrey Mazo <mazo@iitp.ru>
Sun, 14 Feb 2010 02:06:30 +0300
changeset 5990 20ee319e7e71
parent 5989 d3338a5578a5
child 6681 df822f566004
permissions -rw-r--r--
a couple of typos.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     1
@c ========================================================================
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     2
@c ns-3 Object model
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     3
@c ========================================================================
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     4
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     5
@node Object model
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     6
@chapter Object model
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     7
4415
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
     8
@menu
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
     9
* Object model::
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
    10
* Object-oriented behavior::
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
    11
* Object base classes::
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
    12
* Memory management and class Ptr::
5989
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
    13
* Object factories::
4415
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
    14
* Downcasting::
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
    15
@end menu
f7f93b5b8431 fix texinfo warnings
Tom Henderson <tomh@tomh.org>
parents: 3993
diff changeset
    16
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    17
@command{ns-3} is fundamentally a C++ object system.  Objects can
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    18
be declared and instantiated as usual, per C++ rules.  ns-3 also
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    19
adds some features to traditional C++ objects, as described below,
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    20
to provide greater functionality and features.  This manual chapter
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    21
is intended to introduce the reader to the ns-3 object model.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    22
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    23
This section describes the C++ class design for ns-3 objects.  In brief,
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    24
several design patterns in use include classic object-oriented design
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    25
(polymorphic interfaces and implementations), separation of interface
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    26
and implementation, the non-virtual public interface design pattern,
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    27
an object aggregation facility, and reference counting
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    28
for memory management.  Those familiar with component models such
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    29
as COM or Bonobo will recognize elements of the design in the 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    30
ns-3 object aggregation model, although
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    31
the ns-3 design is not strictly in accordance with either.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    32
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    33
@node Object-oriented behavior
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    34
@section Object-oriented behavior
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    35
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    36
C++ objects, in general, provide common object-oriented capabilities 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    37
(abstraction, encapsulation, inheritance, and polymorphism) that are part 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    38
of classic object-oriented design.  ns-3 objects make use of these 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    39
properties; for instance:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    40
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    41
@verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
class Address
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    43
{
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    44
public:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    45
  Address ();
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    46
  Address (uint8_t type, const uint8_t *buffer, uint8_t len);
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    47
  Address (const Address & address);
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    48
  Address &operator = (const Address &address);
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    49
  ...
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    50
private:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    51
  uint8_t m_type;
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    52
  uint8_t m_len;
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    53
  ...
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    54
};
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    55
@end verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    56
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    57
@node Object base classes
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    58
@section Object base classes
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    59
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
    60
There are three special base classes used in ns-3.  Classes that inherit
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    61
from these base classes can instantiate objects with special properties.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    62
These base classes are:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    63
@itemize @bullet
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    64
@item @code{class Object}
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    65
@item @code{class ObjectBase}
5989
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
    66
@item @code{class SimpleRefCount}
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    67
@end itemize
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    68
It is not required that ns-3 objects inherit from these class, but 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    69
those that do get special properties.  Classes deriving from 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    70
@code{class Object} get the following properties.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    71
@itemize @bullet
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    72
@item the ns-3 type and attribute system (see @ref{Attributes})
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
    73
@item an object aggregation system
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    74
@item a smart-pointer reference counting system (class Ptr)
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    75
@end itemize
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    76
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    77
Classes that derive from @code{class ObjectBase} get the first two
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    78
properties above, but do not get smart pointers.  Classes that
5989
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
    79
derive from @code{class SimpleRefCount} get only the smart-pointer
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    80
reference counting system.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    81
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    82
In practice, @code{class Object} is the variant of the three above that
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    83
the ns-3 developer will most commonly encounter.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    84
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    85
@node Memory management and class Ptr
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    86
@section Memory management and class Ptr
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    87
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    88
Memory management in a C++ program is a complex process, and is
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    89
often done incorrectly or inconsistently.  We have settled on 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    90
a reference counting design described as follows.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    91
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    92
All objects using reference counting maintain an internal reference
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    93
count to determine when an object can safely delete itself.  
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    94
Each time that a pointer is obtained to an interface, the object's
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    95
reference count is incremented by calling @code{Ref()}.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    96
It is the obligation of the
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    97
user of the pointer to explicitly @code{Unref()} the pointer when done.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    98
When the reference count falls to zero, the object is deleted.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    99
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   100
@itemize @bullet
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   101
@item When the client code obtains a pointer from the object itself
5431
01a657b8d1ef some cleanup of part 1 of manual
Tom Henderson <tomh@tomh.org>
parents: 4415
diff changeset
   102
through object creation, or via GetObject, it does not have
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   103
to increment the reference count.   
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   104
@item When client code obtains a pointer from another source (e.g.,
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   105
copying a pointer) it must call @code{Ref()} to increment the
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   106
reference count.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   107
@item All users of the object pointer must call @code{Unref()} to
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   108
release the reference.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   109
@end itemize
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   110
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   111
The burden for calling @code{Unref()} is somewhat relieved by the
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   112
use of the reference counting smart pointer class described below. 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   113
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   114
Users using a low-level API who wish to explicitly allocate
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   115
non-reference-counted objects on the heap, using operator new, 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   116
are responsible for deleting such objects.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   117
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   118
@subsection Reference counting smart pointer (Ptr)
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   119
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   120
Calling @code{Ref()} and @code{Unref()} all the time would be cumbersome,
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   121
so ns-3 provides a smart pointer @code{class Ptr} similar to 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   122
@code{Boost::intrusive_ptr}.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   123
This smart-pointer class assumes that the underlying type provides 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   124
a pair of Ref and Unref methods that are expected to increment and 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   125
decrement the internal refcount of the object instance.  
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   126
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   127
This implementation allows you to manipulate the smart pointer
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   128
as if it was a normal pointer: you can compare it with zero,
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   129
compare it against other pointers, assign zero to it, etc.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   130
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   131
It is possible to extract the raw pointer from this
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   132
smart pointer with the GetPointer and PeekPointer methods.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   133
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   134
If you want to store a newed object into a smart pointer,
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   135
we recommend you to use the CreateObject template functions
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   136
to create the object and store it in a smart pointer to avoid
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   137
memory leaks. These functions are really small convenience
5990
20ee319e7e71 a couple of typos.
Andrey Mazo <mazo@iitp.ru>
parents: 5989
diff changeset
   138
functions and their goal is just to save you a small
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   139
bit of typing.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   140
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   141
@subsection CreateObject and Create
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   142
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   143
Objects in C++ may be statically, dynamically, or automatically created.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   144
This holds true for ns-3 also, but some objects in the system 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   145
have some additional frameworks
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   146
available.  Specifically, reference counted objects are usually
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   147
allocated using a templated Create or CreateObject method, as follows.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   148
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   149
For objects deriving from @code{class Object}:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   150
@verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   151
 Ptr<WifiNetDevice> device = CreateObject<WifiNetDevice> ();
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   152
@end verbatim
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   153
Please do not create such objects using @code{operator new}; create them
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   154
using @code{CreateObject()} instead.
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   155
5989
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   156
For objects deriving from @code{class SimpleRefCount}, or other
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   157
objects that support usage of the smart pointer class,
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   158
a templated helper function is available and recommended to be used:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   159
@verbatim
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   160
  Ptr<B> b = Create<B> ();
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   161
@end verbatim  
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   162
This is simply a wrapper around operator new that correctly handles
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   163
the reference counting system.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   164
5989
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   165
In summary, use @code{Create<B>} if B is not an object but just uses
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   166
reference counting (e.g. @code{class Packet}), and use @code{CreateObject<B>}
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   167
if B derives from @code{ns3::Object}.
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   168
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   169
@subsection Aggregation
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   170
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   171
The ns-3 object aggregation system is motivated in strong part by 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   172
a recognition that a common use case for ns-2 has been the use of 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   173
inheritance and polymorphism to extend protocol models.  For instance, 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   174
specialized versions of TCP such as RenoTcpAgent derive from (and override 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   175
functions from) class TcpAgent.  
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   176
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   177
However, two problems that have arisen in the ns-2 model are downcasts
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   178
and ``weak base class.''  Downcasting refers to the procedure of using
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   179
a base class pointer to an object and querying it at run time to
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   180
find out type information, used to explicitly cast the pointer to 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   181
a subclass pointer so that the subclass API can be used.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   182
Weak base class refers to the problems that arise when a class
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   183
cannot be effectively reused (derived from) because it lacks necessary
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   184
functionality, leading the developer to have to modify the
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   185
base class and causing proliferation of base class API calls, some
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   186
of which may not be semantically correct for all subclasses.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   187
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   188
ns-3 is using a version of the query interface design pattern
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   189
to avoid these problems.  This design is based on elements of the 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   190
@uref{http://en.wikipedia.org/wiki/Component_Object_Model,,Component Object Model} 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   191
and @uref{http://en.wikipedia.org/wiki/Bonobo_(component_model),,GNOME Bonobo}
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   192
although full binary-level compatibility of replaceable components
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   193
is not supported and we have tried to simplify the syntax and impact
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   194
on model developers.  
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   195
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   196
@subsubsection Aggregation example
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   197
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   198
@code{class Node} is a good example of the use of aggregation in ns-3.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   199
Note that there are not derived classes of Nodes in ns-3 such as
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   200
class InternetNode.  Instead, components (protocols) are aggregated to 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   201
a node.  Let's look at how some Ipv4 protocols are added to a node.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   202
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   203
@verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   204
static void
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   205
AddIpv4Stack(Ptr<Node> node)
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   206
{
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   207
  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   208
  ipv4->SetNode (node);
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   209
  node->AggregateObject (ipv4);
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   210
  Ptr<Ipv4Impl> ipv4Impl = CreateObject<Ipv4Impl> ();
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   211
  ipv4Impl->SetIpv4 (ipv4);
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   212
  node->AggregateObject (ipv4Impl);
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   213
}
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   214
@end verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   215
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   216
Note that the Ipv4 protocols are created using @code{CreateObject()}.  
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   217
Then, they are aggregated to the node.  In this manner, the Node base
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   218
class does not need to be edited to allow users with a base class Node
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   219
pointer to access the Ipv4 interface; users may ask the node for a pointer
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   220
to its Ipv4 interface at runtime.  How the user asks the node is described
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   221
in the next subsection.
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   222
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   223
Note that it is a programming error to aggregate more than one object of
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   224
the same type to an ns3::Object.  So, for instance, aggregation is not
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   225
an option for storing all of the active sockets of a node.
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   226
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   227
@subsubsection GetObject example
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   228
GetObject is a type-safe way to achieve a safe downcasting 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   229
and to allow interfaces to be found on an object.  
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   230
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   231
Consider a node pointer @code{m_node} that points to a Node object 
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   232
that has an implementation of IPv4 previously aggregated to it.  The 
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   233
client code wishes to configure
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   234
a default route.  To do so, it must access an object within
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   235
the node that has an interface to the IP forwarding configuration.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   236
It performs the following:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   237
@verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   238
  Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   239
@end verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   240
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   241
If the node in fact does not have an Ipv4 object aggregated to it, then
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   242
the method will return null.  Therefore, it is good practice to check
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   243
the return value from such a function call.  If successful, the user can
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   244
now use the Ptr to the Ipv4 object that was previously aggregated to
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   245
the node.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   246
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   247
Another example of how one might use aggregation is to add optional
5431
01a657b8d1ef some cleanup of part 1 of manual
Tom Henderson <tomh@tomh.org>
parents: 4415
diff changeset
   248
models to objects.  
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   249
For instance, an existing Node object may have an ``Energy Model''
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   250
object aggregated to it at run time (without modifying
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   251
and recompiling the node class).  An existing model (such as a wireless
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   252
net device) can then later "GetObject" for the energy model and act 
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   253
appropriately if the interface has been either built in to the underlying
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   254
Node object or aggregated to it at run time.  However, other nodes need
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   255
not know anything about energy models.
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   256
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   257
We hope that this mode of programming will require much less 
3993
cac6551f95eb some corrections to the new object chapter
Tom Henderson <tomh@tomh.org>
parents: 3986
diff changeset
   258
need for developers to modify the base classes.
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   259
5989
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   260
@node Object factories
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   261
@section Object factories
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   262
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   263
A common use case is to create lots of similarly configured objects.
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   264
One can repeatedly call @code{CreateObject} but there is also a 
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   265
factory design pattern in use in the ns-3 system.  It is heavily
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   266
used in the "helper" API. 
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   267
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   268
Class @code{ObjectFactory} can be used to instantiate objects and
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   269
to configure the attributes on those objects 
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   270
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   271
@verbatim
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   272
  void SetTypeId (TypeId tid);
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   273
  void Set (std::string name, const AttributeValue &value);
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   274
  Ptr<T> Create (void) const;
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   275
@end verbatim
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   276
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   277
The first method allows one to use the ns-3 TypeId system to specify
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   278
the type of objects created.  The second allows one to set 
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   279
attributes on the objects to be created, and the third allows one
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   280
to create the objects themselves. 
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   281
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   282
For example:
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   283
@verbatim
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   284
  ObjectFactory factory;
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   285
  // Make this factory create objects of type FriisPropagationLossModel
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   286
  factory.SetTypeId ("ns3::FriisPropagationLossModel")
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   287
  // Make this factory object change a default value of an attribute, for
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   288
  // subsequently created objects
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   289
  factory.Set ("SystemLoss", DoubleValue (2.0));
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   290
  // Create one such object
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   291
  Ptr<Object> object = m_factory.Create (); 
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   292
  factory.Set ("SystemLoss", DoubleValue (3.0));
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   293
  // Create another object
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   294
  Ptr<Object> object = m_factory.Create (); 
5990
20ee319e7e71 a couple of typos.
Andrey Mazo <mazo@iitp.ru>
parents: 5989
diff changeset
   295
@end verbatim
5989
d3338a5578a5 Update Object documentation
Tom Henderson <tomh@tomh.org>
parents: 5431
diff changeset
   296
3986
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   297
@node Downcasting
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   298
@section Downcasting
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   299
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   300
A question that has arisen several times is, "If I have a base class
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   301
pointer (Ptr) to an object and I want the derived class pointer, should
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   302
I downcast (via C++ dynamic cast) to get the derived pointer, or should
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   303
I use the object aggregation system to @code{GetObject<> ()} to find
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   304
a Ptr to the interface to the subclass API?"
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   305
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   306
The answer to this is that in many situations, both techniques will work.
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   307
ns-3 provides a templated function for making the syntax of Object
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   308
dynamic casting much more user friendly:
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   309
@verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   310
template <typename T1, typename T2>
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   311
Ptr<T1>
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   312
DynamicCast (Ptr<T2> const&p)
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   313
{
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   314
  return Ptr<T1> (dynamic_cast<T1 *> (PeekPointer (p)));
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   315
}
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   316
@end verbatim
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   317
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   318
DynamicCast works when the programmer has a base type pointer and is
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   319
testing against a subclass pointer.  GetObject works when looking for
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   320
different objects aggregated, but also works with subclasses, in the same
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   321
way as DynamicCast.  If unsure, the programmer should use GetObject, as it
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   322
works in all cases.   If the programmer knows the class hierarchy of the
44d4f4d768dd Add new objects chapter to manual
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   323
object under consideration, it is more direct to just use DynamicCast.