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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef TAG_BUFFER_H
9#define TAG_BUFFER_H
10
11#include <stdint.h>
12
13#define TAG_BUFFER_USE_INLINE 1
14
15#ifdef TAG_BUFFER_USE_INLINE
16#define TAG_BUFFER_INLINE inline
17#else
18#define TAG_BUFFER_INLINE
19#endif
20
21namespace ns3
22{
23
24/**
25 * @ingroup packet
26 *
27 * @brief read and write tag data
28 *
29 * This class allows subclasses of the ns3::Tag base class
30 * to serialize and deserialize their data through a stream-like
31 * API. This class keeps track of the "current" point in the
32 * buffer and advances that "current" point every time data is
33 * written. The in-memory format of the data written by
34 * this class is unspecified.
35 *
36 * If the user attempts to write more data in the buffer than
37 * he allocated with Tag::GetSerializedSize, he will trigger
38 * an NS_ASSERT error.
39 */
41{
42 public:
43 /**
44 * @brief Constructor
45 * @param start start position
46 * @param end end position
47 */
48 TagBuffer(uint8_t* start, uint8_t* end);
49
50 /**
51 * @brief Trim some space from the end
52 * @param trim space to remove
53 */
54 void TrimAtEnd(uint32_t trim);
55
56 /**
57 * @brief Copy the nternal structure of another TagBuffer
58 * @param o the TagBuffer to copy from
59 */
60 void CopyFrom(TagBuffer o);
61
62 /**
63 * @param v the value to write
64 *
65 * Write one byte and advance the "current" point by one.
66 */
67 TAG_BUFFER_INLINE void WriteU8(uint8_t v);
68 /**
69 * @param v the value to write
70 *
71 * Write two bytes and advance the "current" point by two.
72 */
73 TAG_BUFFER_INLINE void WriteU16(uint16_t v);
74 /**
75 * @param v the value to write
76 *
77 * Write four bytes and advance the "current" point by four.
78 */
80 /**
81 * @param v the value to write
82 *
83 * Write eight bytes and advance the "current" point by eight.
84 */
85 void WriteU64(uint64_t v);
86 /**
87 * @param v the value to write
88 *
89 * Write a double and advance the "current" point by the size of the
90 * data written.
91 */
92 void WriteDouble(double v);
93 /**
94 * @param buffer a pointer to data to write
95 * @param size the size of the data to write
96 *
97 * Write all the input data and advance the "current" point by the size of the
98 * data written.
99 */
100 void Write(const uint8_t* buffer, uint32_t size);
101 /**
102 * @returns the value read
103 *
104 * Read one byte, advance the "current" point by one,
105 * and return the value read.
106 */
107 TAG_BUFFER_INLINE uint8_t ReadU8();
108 /**
109 * @returns the value read
110 *
111 * Read two bytes, advance the "current" point by two,
112 * and return the value read.
113 */
114 TAG_BUFFER_INLINE uint16_t ReadU16();
115 /**
116 * @returns the value read
117 *
118 * Read four bytes, advance the "current" point by four,
119 * and return the value read.
120 */
122 /**
123 * @returns the value read
124 *
125 * Read eight bytes, advance the "current" point by eight,
126 * and return the value read.
127 */
128 uint64_t ReadU64();
129 /**
130 * @returns the value read
131 *
132 * Read a double, advance the "current" point by the size
133 * of the data read, and, return the value read.
134 */
135 double ReadDouble();
136 /**
137 * @param buffer a pointer to the buffer where data should be
138 * written.
139 * @param size the number of bytes to read.
140 *
141 * Read the number of bytes requested, advance the "current"
142 * point by the number of bytes read, return.
143 */
144 void Read(uint8_t* buffer, uint32_t size);
145
146 private:
147 uint8_t* m_current; //!< current TagBuffer position
148 uint8_t* m_end; //!< end TagBuffer position
149};
150
151} // namespace ns3
152
153#ifdef TAG_BUFFER_USE_INLINE
154
155#include "ns3/assert.h"
156
157namespace ns3
158{
159
160void
162{
163 NS_ASSERT(m_current + 1 <= m_end);
164 *m_current = v;
165 m_current++;
166}
167
168void
170{
171 WriteU8((data >> 0) & 0xff);
172 WriteU8((data >> 8) & 0xff);
173}
174
175void
177{
178 WriteU8((data >> 0) & 0xff);
179 WriteU8((data >> 8) & 0xff);
180 WriteU8((data >> 16) & 0xff);
181 WriteU8((data >> 24) & 0xff);
182}
183
184uint8_t
186{
187 NS_ASSERT(m_current + 1 <= m_end);
188 uint8_t v;
189 v = *m_current;
190 m_current++;
191 return v;
192}
193
194uint16_t
196{
197 uint8_t byte0 = ReadU8();
198 uint8_t byte1 = ReadU8();
199 uint16_t data = byte1;
200 data <<= 8;
201 data |= byte0;
202 return data;
203}
204
207{
208 uint8_t byte0 = ReadU8();
209 uint8_t byte1 = ReadU8();
210 uint8_t byte2 = ReadU8();
211 uint8_t byte3 = ReadU8();
212 uint32_t data = byte3;
213 data <<= 8;
214 data |= byte2;
215 data <<= 8;
216 data |= byte1;
217 data <<= 8;
218 data |= byte0;
219 return data;
220}
221
222} // namespace ns3
223
224#endif /* TAG_BUFFER_USE_INLINE */
225
226#endif /* TAG_BUFFER_H */
read and write tag data
Definition tag-buffer.h:41
TagBuffer(uint8_t *start, uint8_t *end)
Constructor.
TAG_BUFFER_INLINE uint32_t ReadU32()
Definition tag-buffer.h:206
void WriteU64(uint64_t v)
Definition tag-buffer.cc:93
void TrimAtEnd(uint32_t trim)
Trim some space from the end.
void Read(uint8_t *buffer, uint32_t size)
void WriteDouble(double v)
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition tag-buffer.h:161
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition tag-buffer.h:185
TAG_BUFFER_INLINE uint16_t ReadU16()
Definition tag-buffer.h:195
TAG_BUFFER_INLINE void WriteU32(uint32_t v)
Definition tag-buffer.h:176
uint64_t ReadU64()
uint8_t * m_current
current TagBuffer position
Definition tag-buffer.h:147
uint8_t * m_end
end TagBuffer position
Definition tag-buffer.h:148
double ReadDouble()
void Write(const uint8_t *buffer, uint32_t size)
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition tag-buffer.h:169
void CopyFrom(TagBuffer o)
Copy the nternal structure of another TagBuffer.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
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:16