ATLAS Offline Software
Loading...
Searching...
No Matches
BitPacker.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5// $Id$
6/**
7 * @file CxxUtils/BitPacker.icc
8 * @author scott snyder <snyder@bnl.gov>
9 * @date Nov, 2014
10 * @brief Helper to pack a set of values bitwise into a stream.
11 */
12
13
14namespace CxxUtils {
15
16
17/**
18 * @brief Constructor.
19 * @param nbits Number of bits per item to use in the packed representation.
20 * @param stream Output stream object.
21 */
22template <class STREAM>
23inline
24BitPacker<STREAM>::BitPacker (uint8_t nbits, STREAM& stream)
25 : m_buf(0),
26 m_nbuf(0),
27 m_nbits(nbits),
28 m_stream (stream)
29{
30 assert (m_nbits > 0 && m_nbits <= 32);
31}
32
33
34/**
35 * @brief Destructor.
36 *
37 * This may flush buffered data to the output stream.
38 */
39template <class STREAM>
40inline
41BitPacker<STREAM>::~BitPacker()
42{
43 if (m_nbuf > 0)
44 m_stream << m_buf;
45}
46
47
48/**
49 * @brief Pack one value to the stream.
50 * @param x The value to pack.
51 * The upper bits should all be clear.
52 */
53template <class STREAM>
54inline
55void BitPacker<STREAM>::pack (uint32_t dat)
56{
57 const uint8_t totbits = 8*sizeof(m_buf);
58
59 m_buf |= (dat << m_nbuf);
60 m_nbuf += m_nbits;
61 if (m_nbuf >= totbits) {
62 m_stream << m_buf;
63 m_nbuf -= totbits;
64 if (m_nbuf == 0)
65 m_buf = 0;
66 else
67 m_buf = dat >> (m_nbits - m_nbuf);
68 }
69}
70
71
72/**
73 * @brief Constructor.
74 * @param stream Output stream object.
75 */
76template <class STREAM>
77inline
78BitPacker8<STREAM>::BitPacker8 (STREAM& stream)
79 : m_buf(0),
80 m_nbuf(0),
81 m_stream (stream)
82{
83}
84
85
86/**
87 * @brief Constructor.
88 * @param nbits Must be 8.
89 * @param stream Output stream object.
90 */
91template <class STREAM>
92inline
93BitPacker8<STREAM>::BitPacker8 (uint8_t /*nbits*/, STREAM& stream)
94 : m_buf(0),
95 m_nbuf(0),
96 m_stream (stream)
97{
98}
99
100
101/**
102 * @brief Destructor.
103 *
104 * This may flush buffered data to the output stream.
105 */
106template <class STREAM>
107inline
108BitPacker8<STREAM>::~BitPacker8()
109{
110 if (m_nbuf > 0)
111 m_stream << m_buf;
112}
113
114
115/**
116 * @brief Pack one value to the stream.
117 * @param x The value to pack.
118 * The upper bits should all be clear.
119 */
120template <class STREAM>
121inline
122void BitPacker8<STREAM>::pack (uint32_t dat)
123{
124 m_buf |= (dat << m_nbuf);
125 m_nbuf += 8;
126 if (m_nbuf == 32) {
127 m_stream << m_buf;
128 m_buf = 0;
129 m_nbuf = 0;
130 }
131}
132
133
134/**
135 * @brief Constructor.
136 * @param stream Output stream object.
137 */
138template <class STREAM>
139inline
140BitPacker16<STREAM>::BitPacker16 (STREAM& stream)
141 : m_buf(0),
142 m_nbuf(0),
143 m_stream (stream)
144{
145}
146
147
148/**
149 * @brief Constructor.
150 * @param nbits Must be 16.
151 * @param stream Output stream object.
152 */
153template <class STREAM>
154inline
155BitPacker16<STREAM>::BitPacker16 (uint8_t /*nbits*/, STREAM& stream)
156 : m_buf(0),
157 m_nbuf(0),
158 m_stream (stream)
159{
160}
161
162
163/**
164 * @brief Destructor.
165 *
166 * This may flush buffered data to the output stream.
167 */
168template <class STREAM>
169inline
170BitPacker16<STREAM>::~BitPacker16()
171{
172 if (m_nbuf > 0)
173 m_stream << m_buf;
174}
175
176
177/**
178 * @brief Pack one value to the stream.
179 * @param x The value to pack.
180 * The upper bits should all be clear.
181 */
182template <class STREAM>
183inline
184void BitPacker16<STREAM>::pack (uint32_t dat)
185{
186 m_buf |= (dat << m_nbuf);
187 m_nbuf += 16;
188 if (m_nbuf == 32) {
189 m_stream << m_buf;
190 m_buf = 0;
191 m_nbuf = 0;
192 }
193}
194
195
196} // namespace CxxUtils