A Discrete-Event Network Simulator
API
packet-tag-list.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef PACKET_TAG_LIST_H
21 #define PACKET_TAG_LIST_H
22 
28 #include <stdint.h>
29 #include <ostream>
30 #include "ns3/type-id.h"
31 
32 namespace ns3 {
33 
34 class Tag;
35 
124 {
125 public:
141  struct TagData
142  {
143  struct TagData * next;
144  uint32_t count;
146  uint32_t size;
147  uint8_t data[1];
148  }; /* struct TagData */
149 
153  inline PacketTagList ();
162  inline PacketTagList (PacketTagList const &o);
172  inline PacketTagList &operator = (PacketTagList const &o);
178  inline ~PacketTagList ();
179 
185  void Add (Tag const&tag) const;
193  bool Remove (Tag &tag);
203  bool Replace (Tag &tag);
211  bool Peek (Tag &tag) const;
215  inline void RemoveAll (void);
219  const struct PacketTagList::TagData *Head (void) const;
225  uint32_t GetSerializedSize (void) const;
234  uint32_t Serialize (uint32_t* buffer, uint32_t maxSize) const;
243  uint32_t Deserialize (const uint32_t* buffer, uint32_t size);
244 
245 private:
253  static
254  TagData * CreateTagData (size_t dataSize);
255 
267  typedef bool (PacketTagList::*COWWriter)
268  (Tag & tag, bool preMerge,
269  struct TagData * cur, struct TagData ** prevNext);
277  bool COWTraverse (Tag & tag, PacketTagList::COWWriter Writer);
289  bool RemoveWriter (Tag & tag, bool preMerge,
290  struct TagData * cur, struct TagData ** prevNext);
302  bool ReplaceWriter (Tag & tag, bool preMerge,
303  struct TagData * cur, struct TagData ** prevNext);
304 
308  struct TagData *m_next;
309 };
310 
311 } // namespace ns3
312 
313 /****************************************************
314  * Implementation of inline methods for performance
315  ****************************************************/
316 
317 namespace ns3 {
318 
320  : m_next ()
321 {
322 }
323 
325  : m_next (o.m_next)
326 {
327  if (m_next != 0)
328  {
329  m_next->count++;
330  }
331 }
332 
335 {
336  // self assignment
337  if (m_next == o.m_next)
338  {
339  return *this;
340  }
341  RemoveAll ();
342  m_next = o.m_next;
343  if (m_next != 0)
344  {
345  m_next->count++;
346  }
347  return *this;
348 }
349 
351 {
352  RemoveAll ();
353 }
354 
355 void
357 {
358  struct TagData *prev = 0;
359  for (struct TagData *cur = m_next; cur != 0; cur = cur->next)
360  {
361  cur->count--;
362  if (cur->count > 0)
363  {
364  break;
365  }
366  if (prev != 0)
367  {
368  prev->~TagData ();
369  std::free (prev);
370  }
371  prev = cur;
372  }
373  if (prev != 0)
374  {
375  prev->~TagData ();
376  std::free (prev);
377  }
378  m_next = 0;
379 }
380 
381 } // namespace ns3
382 
383 #endif /* PACKET_TAG_LIST_H */
List of the packet tags stored in a packet.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
uint32_t Deserialize(const uint32_t *buffer, uint32_t size)
Deserialize tag list from the provided buffer.
uint32_t Serialize(uint32_t *buffer, uint32_t maxSize) const
Serialize the tag list into a byte buffer.
bool ReplaceWriter(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Copy-on-write implementing Replace.
struct TagData * m_next
Pointer to first TagData on the list.
bool Replace(Tag &tag)
Replace the value of a tag.
bool Peek(Tag &tag) const
Find a tag and return its value.
bool(PacketTagList::* COWWriter)(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Typedef of method function pointer for copy-on-write operations.
void RemoveAll(void)
Remove all tags from this list (up to the first merge).
bool COWTraverse(Tag &tag, PacketTagList::COWWriter Writer)
Traverse the list implementing copy-on-write, using Writer.
PacketTagList & operator=(PacketTagList const &o)
Assignment.
PacketTagList()
Create a new PacketTagList.
static TagData * CreateTagData(size_t dataSize)
Allocate and construct a TagData struct, sizing the data area large enough to serialize dataSize byte...
bool RemoveWriter(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Copy-on-write implementing Remove.
~PacketTagList()
Destructor.
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
void Add(Tag const &tag) const
Add a tag to the head of this branch.
const struct PacketTagList::TagData * Head(void) const
tag a set of bytes in a packet
Definition: tag.h:37
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Tree node for sharing serialized tags.
TypeId tid
Type of the tag serialized into data.
uint32_t size
Size of the data buffer.
uint32_t count
Number of incoming links.
struct TagData * next
Pointer to next in list.
uint8_t data[1]
Serialization buffer.