A Discrete-Event Network Simulator
API
tag-buffer.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 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 TAG_BUFFER_H
21 #define TAG_BUFFER_H
22 
23 #include <stdint.h>
24 
25 #define TAG_BUFFER_USE_INLINE 1
26 
27 #ifdef TAG_BUFFER_USE_INLINE
28 #define TAG_BUFFER_INLINE inline
29 #else
30 #define TAG_BUFFER_INLINE
31 #endif
32 
33 namespace ns3 {
34 
51 class TagBuffer
52 {
53 public:
54 
60  TagBuffer (uint8_t *start, uint8_t *end);
61 
66  void TrimAtEnd (uint32_t trim);
67 
72  void CopyFrom (TagBuffer o);
73 
79  TAG_BUFFER_INLINE void WriteU8 (uint8_t v);
85  TAG_BUFFER_INLINE void WriteU16 (uint16_t v);
91  TAG_BUFFER_INLINE void WriteU32 (uint32_t v);
97  void WriteU64 (uint64_t v);
104  void WriteDouble (double v);
112  void Write (const uint8_t *buffer, uint32_t size);
119  TAG_BUFFER_INLINE uint8_t ReadU8 (void);
126  TAG_BUFFER_INLINE uint16_t ReadU16 (void);
133  TAG_BUFFER_INLINE uint32_t ReadU32 (void);
140  uint64_t ReadU64 (void);
147  double ReadDouble (void);
156  void Read (uint8_t *buffer, uint32_t size);
157 private:
158 
159  uint8_t *m_current;
160  uint8_t *m_end;
161 };
162 
163 } // namespace ns3
164 
165 #ifdef TAG_BUFFER_USE_INLINE
166 
167 #include "ns3/assert.h"
168 
169 namespace ns3 {
170 
171 void
173 {
174  NS_ASSERT (m_current + 1 <= m_end);
175  *m_current = v;
176  m_current++;
177 }
178 
179 void
181 {
182  WriteU8 ((data >> 0) & 0xff);
183  WriteU8 ((data >> 8) & 0xff);
184 }
185 void
187 {
188  WriteU8 ((data >> 0) & 0xff);
189  WriteU8 ((data >> 8) & 0xff);
190  WriteU8 ((data >> 16) & 0xff);
191  WriteU8 ((data >> 24) & 0xff);
192 }
193 
194 uint8_t
196 {
197  NS_ASSERT (m_current + 1 <= m_end);
198  uint8_t v;
199  v = *m_current;
200  m_current++;
201  return v;
202 }
203 
204 uint16_t
206 {
207  uint8_t byte0 = ReadU8 ();
208  uint8_t byte1 = ReadU8 ();
209  uint16_t data = byte1;
210  data <<= 8;
211  data |= byte0;
212  return data;
213 }
214 uint32_t
216 {
217  uint8_t byte0 = ReadU8 ();
218  uint8_t byte1 = ReadU8 ();
219  uint8_t byte2 = ReadU8 ();
220  uint8_t byte3 = ReadU8 ();
221  uint32_t data = byte3;
222  data <<= 8;
223  data |= byte2;
224  data <<= 8;
225  data |= byte1;
226  data <<= 8;
227  data |= byte0;
228  return data;
229 }
230 
231 } // namespace ns3
232 
233 #endif /* TAG_BUFFER_USE_INLINE */
234 
235 #endif /* TAG_BUFFER_H */
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
void WriteU64(uint64_t v)
Definition: tag-buffer.cc:102
def start()
Definition: core.py:1844
void WriteDouble(double v)
Definition: tag-buffer.cc:115
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
void TrimAtEnd(uint32_t trim)
Trim some space from the end.
Definition: tag-buffer.cc:191
uint8_t * m_current
current TagBuffer position
Definition: tag-buffer.h:159
TAG_BUFFER_INLINE uint32_t ReadU32(void)
Definition: tag-buffer.h:215
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:195
double ReadDouble(void)
Definition: tag-buffer.cc:164
uint8_t * m_end
end TagBuffer position
Definition: tag-buffer.h:160
uint8_t data[writeSize]
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition: tag-buffer.h:186
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
#define TAG_BUFFER_INLINE
Definition: tag-buffer.h:28
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint64_t ReadU64(void)
Definition: tag-buffer.cc:134
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
read and write tag data
Definition: tag-buffer.h:51
TagBuffer(uint8_t *start, uint8_t *end)
Constructor.
Definition: tag-buffer.cc:183
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
TAG_BUFFER_INLINE uint16_t ReadU16(void)
Definition: tag-buffer.h:205
void CopyFrom(TagBuffer o)
Copy the nternal structure of another TagBuffer.
Definition: tag-buffer.cc:199