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;
220 
221 private:
229  static
230  TagData * CreateTagData (size_t dataSize);
231 
243  typedef bool (PacketTagList::*COWWriter)
244  (Tag & tag, bool preMerge,
245  struct TagData * cur, struct TagData ** prevNext);
253  bool COWTraverse (Tag & tag, PacketTagList::COWWriter Writer);
265  bool RemoveWriter (Tag & tag, bool preMerge,
266  struct TagData * cur, struct TagData ** prevNext);
278  bool ReplaceWriter (Tag & tag, bool preMerge,
279  struct TagData * cur, struct TagData ** prevNext);
280 
284  struct TagData *m_next;
285 };
286 
287 } // namespace ns3
288 
289 /****************************************************
290  * Implementation of inline methods for performance
291  ****************************************************/
292 
293 namespace ns3 {
294 
296  : m_next ()
297 {
298 }
299 
301  : m_next (o.m_next)
302 {
303  if (m_next != 0)
304  {
305  m_next->count++;
306  }
307 }
308 
311 {
312  // self assignment
313  if (m_next == o.m_next)
314  {
315  return *this;
316  }
317  RemoveAll ();
318  m_next = o.m_next;
319  if (m_next != 0)
320  {
321  m_next->count++;
322  }
323  return *this;
324 }
325 
327 {
328  RemoveAll ();
329 }
330 
331 void
333 {
334  struct TagData *prev = 0;
335  for (struct TagData *cur = m_next; cur != 0; cur = cur->next)
336  {
337  cur->count--;
338  if (cur->count > 0)
339  {
340  break;
341  }
342  if (prev != 0)
343  {
344  prev->~TagData ();
345  std::free (prev);
346  }
347  prev = cur;
348  }
349  if (prev != 0)
350  {
351  prev->~TagData ();
352  std::free (prev);
353  }
354  m_next = 0;
355 }
356 
357 } // namespace ns3
358 
359 #endif /* PACKET_TAG_LIST_H */
~PacketTagList()
Destructor.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
struct TagData * next
Pointer to next in list.
const struct PacketTagList::TagData * Head(void) const
List of the packet tags stored in a packet.
bool Peek(Tag &tag) const
Find a tag and return its value.
static TagData * CreateTagData(size_t dataSize)
Allocate and construct a TagData struct, sizing the data area large enough to serialize dataSize byte...
void Add(Tag const &tag) const
Add a tag to the head of this branch.
bool ReplaceWriter(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Copy-on-write implementing Replace.
Tree node for sharing serialized tags.
bool(PacketTagList::* COWWriter)(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Typedef of method function pointer for copy-on-write operations.
bool Replace(Tag &tag)
Replace the value of a tag.
bool RemoveWriter(Tag &tag, bool preMerge, struct TagData *cur, struct TagData **prevNext)
Copy-on-write implementing Remove.
uint32_t size
Size of the data buffer.
PacketTagList()
Create a new PacketTagList.
uint8_t data[1]
Serialization buffer.
tag a set of bytes in a packet
Definition: tag.h:36
bool COWTraverse(Tag &tag, PacketTagList::COWWriter Writer)
Traverse the list implementing copy-on-write, using Writer.
struct TagData * m_next
Pointer to first TagData on the list.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TypeId tid
Type of the tag serialized into data.
PacketTagList & operator=(PacketTagList const &o)
Assignment.
void RemoveAll(void)
Remove all tags from this list (up to the first merge).
a unique identifier for an interface.
Definition: type-id.h:58
uint32_t count
Number of incoming links.