From 457d07a50f4e4082bc13ed5c9f7788fac35442f0 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 11 Dec 2007 13:42:09 +0100 Subject: [PATCH] make Packet::AddTag a const method --- src/common/packet.h | 18 +++++++++++++++--- src/common/tags.cc | 8 ++++---- src/common/tags.h | 10 +++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/common/packet.h b/src/common/packet.h index 7fcdd32d2..a18829f3d 100644 --- a/src/common/packet.h +++ b/src/common/packet.h @@ -158,15 +158,27 @@ public: template 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 - 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 -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 diff --git a/src/common/tags.cc b/src/common/tags.cc index e2206b7af..fe7e5befc 100644 --- a/src/common/tags.cc +++ b/src/common/tags.cc @@ -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; } diff --git a/src/common/tags.h b/src/common/tags.h index 4002ae4f8..f6b19e59e 100644 --- a/src/common/tags.h +++ b/src/common/tags.h @@ -44,7 +44,7 @@ public: inline ~Tags (); template - void Add (T const&tag); + void Add (T const&tag) const; template 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 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 (this)->m_next = newStart; } template