A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
bit-serializer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18 */
19
20#ifndef BITSERIALIZER_H_
21#define BITSERIALIZER_H_
22
23#include <cstdint>
24#include <vector>
25
26namespace ns3
27{
28
29/**
30 * \ingroup packet
31 *
32 * \brief Bit serializer. See also \sa ns3::BitDeserializer
33 *
34 * This class helps converting a variable number, variable sized
35 * number of bit-boundary fields to its final byte array representation.
36 *
37 * The typical use-case is:
38 * \verbatim
39 aaa: A field
40 bbb: B field
41 ccc: C field
42 ddd: D field
43 000: padding
44
45 |aaaa abbb bbcc cdd0|
46 \endverbatim
47 *
48 *
49 * Padding can be automatically added at the end or at the start
50 * of the byte blob to reach a multiple of 8 bits.
51 * By default the padding is added at the end of the byte blob.
52 *
53 * This class should be used in two cases:
54 * - When the number of fields is large.
55 * - When the number of fields is variable.
56 * In the other cases it's more convenient to use a simple bitmask.
57 */
58
60{
61 public:
63
64 /**
65 * Toggles the padding insertion policy.
66 * \param padAtEnd true if the padding have to be inserted at the end of the bit blob.
67 */
68 void InsertPaddingAtEnd(bool padAtEnd);
69
70 /**
71 * Pushes a number of bits in the blob.
72 * \param value the bits to be inserted.
73 * \param significantBits Number of bits to insert.
74 */
75 void PushBits(uint64_t value, uint8_t significantBits);
76
77 /**
78 * Get the bytes representation of the blob.
79 * Note that this operation \b automatically add the
80 * needed padding at the end (or start) of the blob.
81 *
82 * \warning {This operation clears the stored data.}
83 *
84 * \returns The byte representation of the blob.
85 */
86 std::vector<uint8_t> GetBytes();
87
88 /**
89 * Get the bytes representation of the blob.
90 * Note that this operation \b automatically add the
91 * needed padding at the end (or start) of the blob.
92 *
93 * \warning {This operation clears the stored data.}
94 *
95 * \param [out] buffer The buffer where to store the return data.
96 * \param [in] size The size of the buffer.
97 * \returns The number of bytes actually used in the buffer.
98 */
99 uint8_t GetBytes(uint8_t* buffer, uint32_t size);
100
101 private:
102 /**
103 * Add the padding at the start of the blob.
104 */
105 void PadAtStart();
106
107 /**
108 * Add the padding at the end of the blob.
109 */
110 void PadAtEnd();
111
112 std::vector<bool> m_blob; //!< Blob of serialized bits.
113 bool m_padAtEnd; //!< True if the padding must be added at the end of the blob.
114};
115
116} // namespace ns3
117
118#endif /* BITSERIALIZER_H_ */
Bit serializer.
bool m_padAtEnd
True if the padding must be added at the end of the blob.
void PushBits(uint64_t value, uint8_t significantBits)
Pushes a number of bits in the blob.
void PadAtStart()
Add the padding at the start of the blob.
std::vector< uint8_t > GetBytes()
Get the bytes representation of the blob.
std::vector< bool > m_blob
Blob of serialized bits.
void InsertPaddingAtEnd(bool padAtEnd)
Toggles the padding insertion policy.
void PadAtEnd()
Add the padding at the end of the blob.
Every class exported by the ns3 library is enclosed in the ns3 namespace.