A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tag-buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef TAG_BUFFER_H
20#define TAG_BUFFER_H
21
22#include <stdint.h>
23
24#define TAG_BUFFER_USE_INLINE 1
25
26#ifdef TAG_BUFFER_USE_INLINE
27#define TAG_BUFFER_INLINE inline
28#else
29#define TAG_BUFFER_INLINE
30#endif
31
32namespace ns3
33{
34
35/**
36 * \ingroup packet
37 *
38 * \brief read and write tag data
39 *
40 * This class allows subclasses of the ns3::Tag base class
41 * to serialize and deserialize their data through a stream-like
42 * API. This class keeps track of the "current" point in the
43 * buffer and advances that "current" point every time data is
44 * written. The in-memory format of the data written by
45 * this class is unspecified.
46 *
47 * If the user attempts to write more data in the buffer than
48 * he allocated with Tag::GetSerializedSize, he will trigger
49 * an NS_ASSERT error.
50 */
52{
53 public:
54 /**
55 * \brief Constructor
56 * \param start start position
57 * \param end end position
58 */
59 TagBuffer(uint8_t* start, uint8_t* end);
60
61 /**
62 * \brief Trim some space from the end
63 * \param trim space to remove
64 */
65 void TrimAtEnd(uint32_t trim);
66
67 /**
68 * \brief Copy the nternal structure of another TagBuffer
69 * \param o the TagBuffer to copy from
70 */
71 void CopyFrom(TagBuffer o);
72
73 /**
74 * \param v the value to write
75 *
76 * Write one byte and advance the "current" point by one.
77 */
78 TAG_BUFFER_INLINE void WriteU8(uint8_t v);
79 /**
80 * \param v the value to write
81 *
82 * Write two bytes and advance the "current" point by two.
83 */
84 TAG_BUFFER_INLINE void WriteU16(uint16_t v);
85 /**
86 * \param v the value to write
87 *
88 * Write four bytes and advance the "current" point by four.
89 */
91 /**
92 * \param v the value to write
93 *
94 * Write eight bytes and advance the "current" point by eight.
95 */
96 void WriteU64(uint64_t v);
97 /**
98 * \param v the value to write
99 *
100 * Write a double and advance the "current" point by the size of the
101 * data written.
102 */
103 void WriteDouble(double v);
104 /**
105 * \param buffer a pointer to data to write
106 * \param size the size of the data to write
107 *
108 * Write all the input data and advance the "current" point by the size of the
109 * data written.
110 */
111 void Write(const uint8_t* buffer, uint32_t size);
112 /**
113 * \returns the value read
114 *
115 * Read one byte, advance the "current" point by one,
116 * and return the value read.
117 */
118 TAG_BUFFER_INLINE uint8_t ReadU8();
119 /**
120 * \returns the value read
121 *
122 * Read two bytes, advance the "current" point by two,
123 * and return the value read.
124 */
125 TAG_BUFFER_INLINE uint16_t ReadU16();
126 /**
127 * \returns the value read
128 *
129 * Read four bytes, advance the "current" point by four,
130 * and return the value read.
131 */
133 /**
134 * \returns the value read
135 *
136 * Read eight bytes, advance the "current" point by eight,
137 * and return the value read.
138 */
139 uint64_t ReadU64();
140 /**
141 * \returns the value read
142 *
143 * Read a double, advance the "current" point by the size
144 * of the data read, and, return the value read.
145 */
146 double ReadDouble();
147 /**
148 * \param buffer a pointer to the buffer where data should be
149 * written.
150 * \param size the number of bytes to read.
151 *
152 * Read the number of bytes requested, advance the "current"
153 * point by the number of bytes read, return.
154 */
155 void Read(uint8_t* buffer, uint32_t size);
156
157 private:
158 uint8_t* m_current; //!< current TagBuffer position
159 uint8_t* m_end; //!< end TagBuffer position
160};
161
162} // namespace ns3
163
164#ifdef TAG_BUFFER_USE_INLINE
165
166#include "ns3/assert.h"
167
168namespace ns3
169{
170
171void
173{
174 NS_ASSERT(m_current + 1 <= m_end);
175 *m_current = v;
176 m_current++;
177}
178
179void
181{
182 WriteU8((data >> 0) & 0xff);
183 WriteU8((data >> 8) & 0xff);
184}
185
186void
188{
189 WriteU8((data >> 0) & 0xff);
190 WriteU8((data >> 8) & 0xff);
191 WriteU8((data >> 16) & 0xff);
192 WriteU8((data >> 24) & 0xff);
193}
194
195uint8_t
197{
198 NS_ASSERT(m_current + 1 <= m_end);
199 uint8_t v;
200 v = *m_current;
201 m_current++;
202 return v;
203}
204
205uint16_t
207{
208 uint8_t byte0 = ReadU8();
209 uint8_t byte1 = ReadU8();
210 uint16_t data = byte1;
211 data <<= 8;
212 data |= byte0;
213 return data;
214}
215
218{
219 uint8_t byte0 = ReadU8();
220 uint8_t byte1 = ReadU8();
221 uint8_t byte2 = ReadU8();
222 uint8_t byte3 = ReadU8();
223 uint32_t data = byte3;
224 data <<= 8;
225 data |= byte2;
226 data <<= 8;
227 data |= byte1;
228 data <<= 8;
229 data |= byte0;
230 return data;
231}
232
233} // namespace ns3
234
235#endif /* TAG_BUFFER_USE_INLINE */
236
237#endif /* TAG_BUFFER_H */
read and write tag data
Definition: tag-buffer.h:52
TAG_BUFFER_INLINE uint32_t ReadU32()
Definition: tag-buffer.h:217
void WriteU64(uint64_t v)
Definition: tag-buffer.cc:104
void TrimAtEnd(uint32_t trim)
Trim some space from the end.
Definition: tag-buffer.cc:199
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:183
void WriteDouble(double v)
Definition: tag-buffer.cc:118
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition: tag-buffer.h:196
TAG_BUFFER_INLINE uint16_t ReadU16()
Definition: tag-buffer.h:206
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition: tag-buffer.h:187
uint64_t ReadU64()
Definition: tag-buffer.cc:139
uint8_t * m_current
current TagBuffer position
Definition: tag-buffer.h:158
uint8_t * m_end
end TagBuffer position
Definition: tag-buffer.h:159
double ReadDouble()
Definition: tag-buffer.cc:170
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:129
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
void CopyFrom(TagBuffer o)
Copy the nternal structure of another TagBuffer.
Definition: tag-buffer.cc:207
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
#define TAG_BUFFER_INLINE
Definition: tag-buffer.h:27