[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
At this point, we’re still working on some scaffolding, but we can begin to define our classes, with the functionality to be added later.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This is an important design step; whether to use class Object
as
a base class for your new classes.
As described in the chapter on the ns-3 Object model, classes that
inherit from class Object
get special properties:
Classes that derive from class ObjectBase
get the first two
properties above, but do not get smart pointers. Classes that
derive from class RefCountBase
get only the smart-pointer
reference counting system.
In practice, class Object
is the variant of the three above that
the ns-3 developer will most commonly encounter.
In our case, we want to make use of the attribute system, and we
will be passing instances of this object across the ns-3 public API,
so class Object
is appropriate for us.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
One way to proceed is to start by defining the bare minimum functions and see if they will compile. Let’s review what all is needed to implement when we derive from class Object.
#ifndef ERROR_MODEL_H #define ERROR_MODEL_H #include "ns3/object.h" namespace ns3 { class ErrorModel : public Object { public: static TypeId GetTypeId (void); ErrorModel (); virtual ~ErrorModel (); }; class RateErrorModel : public ErrorModel { public: static TypeId GetTypeId (void); RateErrorModel (); virtual ~RateErrorModel (); }; #endif
A few things to note here. We need to include object.h
. The
convention in ns-3 is that if the header file is co-located in the
same directory, it may be included without any path prefix. Therefore,
if we were implementing ErrorModel in src/core
directory, we could
have just said "#include "object.h"
". But we are in src/common
,
so we must include it as "#include "ns3/object.h"
". Note also
that this goes outside the namespace declaration.
Second, each class must implement a static public member function
called GetTypeId (void)
.
Third, it is a good idea to implement constructors and destructors rather than to let the compiler generate them, and to make the destructor virtual. In C++, note also that copy assignment operator and copy constructors are auto-generated if they are not defined, so if you do not want those, you should implement those as private members. This aspect of C++ is discussed in Scott Meyers’ Effective C++ book. item 45.
Let’s now look at some corresponding skeletal implementation code in the .cc file.
#include "error-model.h" namespace ns3 { NS_OBJECT_ENSURE_REGISTERED (ErrorModel); TypeId ErrorModel::GetTypeId (void) { static TypeId tid = TypeId ("ns3::ErrorModel") .SetParent<Object> () ; return tid; } ErrorModel::ErrorModel () { } ErrorModel::~ErrorModel () { } NS_OBJECT_ENSURE_REGISTERED (RateErrorModel); TypeId RateErrorModel::GetTypeId (void) { static TypeId tid = TypeId ("ns3::RateErrorModel") .SetParent<ErrorModel> () .AddConstructor<RateErrorModel> () ; return tid; } RateErrorModel::RateErrorModel () { } RateErrorModel::~RateErrorModel () { }
What is the GetTypeId (void)
function? This function does a few
things. It registers a unique string into the TypeId system. It establishes
the hierarchy of objects in the attribute system (via SetParent
).
It also declares that certain objects can be created via the object
creation framework (AddConstructor
).
The macro NS_OBJECT_ENSURE_REGISTERED (classname)
is needed also
once for every class that defines a new GetTypeId method, and it does
the actual registration of the class into the system.
The Object model chapter discusses this in more detail.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here, write a bit about adding ns-3 logging macros. Note that |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
|
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated on April 21, 2010 using texi2html 1.82.