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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#ifndef BITSERIALIZER_H_
10#define BITSERIALIZER_H_
11
12#include <cstdint>
13#include <vector>
14
15namespace ns3
16{
17
18/**
19 * @ingroup packet
20 *
21 * @brief Bit serializer. See also \sa ns3::BitDeserializer
22 *
23 * This class helps converting a variable number, variable sized
24 * number of bit-boundary fields to its final byte array representation.
25 *
26 * The typical use-case is:
27 * @verbatim
28 aaa: A field
29 bbb: B field
30 ccc: C field
31 ddd: D field
32 000: padding
33
34 |aaaa abbb bbcc cdd0|
35 \endverbatim
36 *
37 *
38 * Padding can be automatically added at the end or at the start
39 * of the byte blob to reach a multiple of 8 bits.
40 * By default the padding is added at the end of the byte blob.
41 *
42 * This class should be used in two cases:
43 * - When the number of fields is large.
44 * - When the number of fields is variable.
45 * In the other cases it's more convenient to use a simple bitmask.
46 */
47
49{
50 public:
52
53 /**
54 * Toggles the padding insertion policy.
55 * @param padAtEnd true if the padding have to be inserted at the end of the bit blob.
56 */
58
59 /**
60 * Pushes a number of bits in the blob.
61 * @param value the bits to be inserted.
62 * @param significantBits Number of bits to insert.
63 */
64 void PushBits(uint64_t value, uint8_t significantBits);
65
66 /**
67 * Get the bytes representation of the blob.
68 * Note that this operation \b automatically add the
69 * needed padding at the end (or start) of the blob.
70 *
71 * @warning {This operation clears the stored data.}
72 *
73 * @returns The byte representation of the blob.
74 */
75 std::vector<uint8_t> GetBytes();
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 * @param [out] buffer The buffer where to store the return data.
85 * @param [in] size The size of the buffer.
86 * @returns The number of bytes actually used in the buffer.
87 */
88 uint8_t GetBytes(uint8_t* buffer, uint32_t size);
89
90 private:
91 /**
92 * Add the padding at the start of the blob.
93 */
94 void PadAtStart();
95
96 /**
97 * Add the padding at the end of the blob.
98 */
99 void PadAtEnd();
100
101 std::vector<bool> m_blob; //!< Blob of serialized bits.
102 bool m_padAtEnd; //!< True if the padding must be added at the end of the blob.
103};
104
105} // namespace ns3
106
107#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.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static unsigned int value(char c)