make Packet::AddTag a const method

This commit is contained in:
Mathieu Lacage
2007-12-11 13:42:09 +01:00
parent 3262a4e103
commit 457d07a50f
3 changed files with 24 additions and 12 deletions

View File

@@ -158,15 +158,27 @@ public:
template <typename T>
uint32_t RemoveTrailer (T &trailer);
/**
* \param tag a pointer to the tag to attach to this packet.
*
* Attach a tag to this packet. The tag is fully copied
* in a packet-specific internal buffer. This operation
* is expected to be really fast. The copy constructor of the
* tag is invoked to copy it into the tag buffer.
*
* \param tag a pointer to the tag to attach to this packet.
* Note that adding a tag is a const operation which is pretty
* un-intuitive. The rationale is that the content and behavior of
* a packet is _not_ changed when a tag is added to a packet: any
* code which was not aware of the new tag is going to work just
* the same if the new tag is added. The real reason why adding a
* tag was made a const operation is to allow a trace sink which gets
* a packet to tag the packet, even if the packet is const (and most
* trace sources should use const packets because it would be
* totally evil to allow a trace sink to modify the content of a
* packet).
*
*/
template <typename T>
void AddTag (T const &tag);
void AddTag (T const &tag) const;
/**
* Remove a tag from this packet. The data stored internally
* for this tag is copied in the input tag if an instance
@@ -450,7 +462,7 @@ Packet::RemoveTrailer (T &trailer)
template <typename T>
void Packet::AddTag (T const& tag)
void Packet::AddTag (T const& tag) const
{
const Tag *parent;
// if the following assignment fails, it is because the

View File

@@ -30,7 +30,7 @@ struct Tags::TagData *Tags::gFree = 0;
uint32_t Tags::gN_free = 0;
struct Tags::TagData *
Tags::AllocData (void)
Tags::AllocData (void) const
{
struct Tags::TagData *retval;
if (gFree != 0)
@@ -47,7 +47,7 @@ Tags::AllocData (void)
}
void
Tags::FreeData (struct TagData *data)
Tags::FreeData (struct TagData *data) const
{
if (gN_free > 1000)
{
@@ -61,7 +61,7 @@ Tags::FreeData (struct TagData *data)
}
#else
struct Tags::TagData *
Tags::AllocData (void)
Tags::AllocData (void) const
{
struct Tags::TagData *retval;
retval = new struct Tags::TagData ();
@@ -69,7 +69,7 @@ Tags::AllocData (void)
}
void
Tags::FreeData (struct TagData *data)
Tags::FreeData (struct TagData *data) const
{
delete data;
}

View File

@@ -44,7 +44,7 @@ public:
inline ~Tags ();
template <typename T>
void Add (T const&tag);
void Add (T const&tag) const;
template <typename T>
bool Remove (T &tag);
@@ -71,8 +71,8 @@ private:
};
bool Remove (uint32_t id);
struct Tags::TagData *AllocData (void);
void FreeData (struct TagData *data);
struct Tags::TagData *AllocData (void) const;
void FreeData (struct TagData *data) const;
static struct Tags::TagData *gFree;
static uint32_t gN_free;
@@ -96,7 +96,7 @@ namespace ns3 {
template <typename T>
void
Tags::Add (T const&tag)
Tags::Add (T const&tag) const
{
const Tag *parent;
// if the following assignment fails, it is because the
@@ -116,7 +116,7 @@ Tags::Add (T const&tag)
void *buf = &newStart->m_data;
new (buf) T (tag);
newStart->m_next = m_next;
m_next = newStart;
const_cast<Tags *> (this)->m_next = newStart;
}
template <typename T>