[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

34.3 Initial Implementation

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] [ ? ]

34.3.1 use of class Object?

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] [ ? ]

34.3.2 initial classes

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] [ ? ]

34.3.3 how to include files from elsewhere


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

34.3.4 log component

Here, write a bit about adding ns-3 logging macros. Note that
LOG_COMPONENT_DEFINE is done outside the namespace ns3


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

34.3.5 constructor, empty function prototypes


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

34.3.6 key variables (default values, attributes)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

34.3.7 test program 1


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

34.3.8 Object Framework

 
 
  static const ClassId cid;


const InterfaceId ErrorModel::iid =
  MakeInterfaceId ("ErrorModel", Object::iid);

const ClassId ErrorModel::cid =
  MakeClassId<ErrorModel> ("ErrorModel", ErrorModel::iid);

[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by root on May 3, 2010 using texi2html 1.82.