Signal/Geometry Processing Library (SPL)  1.1.24
bitStream.hpp
Go to the documentation of this file.
1 // Copyright (c) 2013 Michael D. Adams
2 // All rights reserved.
3 
4 // __START_OF_LICENSE__
5 //
6 // Copyright (c) 2015 Michael D. Adams
7 // All rights reserved.
8 //
9 // This file is part of the Signal Processing Library (SPL).
10 //
11 // This program is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 3,
14 // or (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public
22 // License along with this program; see the file LICENSE. If not,
23 // see <http://www.gnu.org/licenses/>.
24 //
25 // __END_OF_LICENSE__
26 
32 #ifndef SPL_bitStream_hpp
33 #define SPL_bitStream_hpp
34 
36 // Header files
38 
39 #include <SPL/config.hpp>
40 #include <cassert>
41 #include <iostream>
42 
43 namespace SPL {
44 
50 // Bit stream class
53 
61 class BitStream
62 {
63 
64 public:
65 
69  typedef unsigned IoState;
70 
71  /*
72  * The I/O state bits.
73  */
75  static const IoState eofBit = 1;
77  static const IoState limitBit = 2;
79  static const IoState badBit = 4;
81  static const IoState allIoBits = eofBit | limitBit | badBit;
82 
86  typedef unsigned long long Size;
87 
91  typedef long long Offset;
92 
99  bool isOkay() const;
100 
107  bool isEof() const;
108 
113  bool isLimit() const;
114 
120  IoState getIoState() const;
121 
127  void setIoState(IoState state);
128 
134  void setIoStateBits(IoState state);
135 
141  void clearIoStateBits(IoState state = allIoBits);
142 
143 protected:
144 
145  // Default constructor
146  BitStream() : ioState_(0) {}
147 
148  // I/O state of bit stream (a bitmask).
149  IoState ioState_;
150 
151 private:
152 
153  // The copy constructor is declared but not defined in order to
154  // prevent copying.
155  BitStream(const BitStream&);
156 
157  // The copy assignment operator is declared but not defined in order to
158  // prevent copy assignment.
159  BitStream& operator=(const BitStream&);
160 
161 };
162 
163 inline bool BitStream::isOkay() const
164 {
165  return !(ioState_ & (badBit | limitBit));
166 }
167 
168 inline bool BitStream::isEof() const
169 {
170  return ioState_ & eofBit;
171 }
172 
173 inline bool BitStream::isLimit() const
174 {
175  return ioState_ & limitBit;
176 }
177 
179 {
180  assert(!(state & (~allIoBits)));
181  ioState_ = state;
182 }
183 
185 {
186  return ioState_;
187 }
188 
190 {
191  assert(!(mask & (~allIoBits)));
192  ioState_ &= ~mask;
193 }
194 
196 {
197  assert(!(mask & (~allIoBits)));
198  ioState_ |= mask;
199 }
200 
202 // Input Bit Stream Class
204 
209 class InputBitStream : public BitStream
210 {
211 
212 public:
213 
215  // Constructors and destructor
217 
222  InputBitStream();
223 
228  InputBitStream(std::istream& in);
229 
233  ~InputBitStream();
234 
236  // Set and get various state
238 
242  std::istream* getInput() const;
243 
247  void setInput(std::istream* in);
248 
257  Offset getReadLimit() const;
258 
267  void setReadLimit(Offset readLimit);
268 
272  Size getReadCount() const;
273 
277  void clearReadCount();
278 
280  // Input
282 
290  long getBits(int numBits);
291 
293  // Alignment
295 
302  void align();
303 
305  // Debugging
307 
312  void dump(std::ostream& out) const;
313 
314 private:
315 
316  // The copy constructor is declared but not defined in order to
317  // prevent copying.
319 
320  // The copy assignment operator is declared but not defined in order to
321  // prevent copy assignment.
322  InputBitStream& operator=(const InputBitStream&);
323 
324  bool fillBuf();
325 
326  // The underlying character stream.
327  std::istream* in_;
328 
329  // The input buffer.
330  unsigned char buf_;
331 
332  // The number of bits remaining before the buffer is empty.
333  int count_;
334 
335  // The maximum number of bits that may still be read from the bit
336  // stream. If this quantity is negative, there is no limit.
337  Offset readLimit_;
338 
339  // The number of bits read from the bit stream so far.
340  Size readCount_;
341 
342 };
343 
345 {
346  readCount_ = 0;
347 }
348 
350 {
351  return readCount_;
352 }
353 
355 {
356  readLimit_ = readLimit;
357 }
358 
360 {
361  return readLimit_;
362 }
363 
365 // Output Bit Stream Class
367 
373 {
374 
375 public:
376 
378  // Constructors and destructor
380 
385  OutputBitStream();
386 
391  OutputBitStream(std::ostream& out);
392 
397 
399  // Get and set various bit stream state.
401 
406  std::ostream* getOutput() const;
407 
412  void setOutput(std::ostream* out);
413 
417  void clearWriteCount();
418 
422  Size getWriteCount() const;
423 
432  void setWriteLimit(Offset writeLimit);
433 
442  Offset getWriteLimit() const;
443 
445  // Output
447 
454  int putBits(long data, int numBits);
455 
457  // Alignment and flushing
459 
464  void align();
465 
472  void flush();
473 
475  // Debugging
477 
482  void dump(std::ostream& out) const;
483 
484 private:
485 
486  // The copy constructor is declared but not defined in order to
487  // prevent copying.
489 
490  // The copy assignment operator is declared but not defined in order to
491  // prevent copy assignment.
492  OutputBitStream& operator=(const OutputBitStream&);
493 
494  bool flushBuf();
495 
496  // The underlying character stream.
497  std::ostream* out_;
498 
499  // The output buffer.
500  unsigned char buf_;
501 
502  // The number of bits remaining before the buffer is full.
503  int count_;
504 
505  // The number of bits written to the underlying stream.
506  Size writeCount_;
507 
508  // The maximum number of bits that may still be written to the underlying
509  // stream. If this quantity is negative, there is no limit.
510  Offset writeLimit_;
511 
512 };
513 
515 {
516  return writeCount_;
517 }
518 
520 {
521  writeCount_ = 0;
522 }
523 
525 {
526  return writeLimit_;
527 }
528 
530 {
531  writeLimit_ = writeLimit;
532 }
533 
535 //
537 
542 }
543 
544 #endif
void setIoStateBits(IoState state)
Set the specified bits in the I/O state of a bit stream.
Definition: bitStream.hpp:195
bool isOkay() const
Test if the bitstream in an okay (i.e., non-error) state.
Definition: bitStream.hpp:163
InputBitStream()
Create a bit stream that is not initially bound to any (character) stream.
Definition: bitStream.cpp:42
static const IoState eofBit
end of file (EOF) reached on input
Definition: bitStream.hpp:75
Size getWriteCount() const
Get the number of bits written to the bit stream.
Definition: bitStream.hpp:514
unsigned IoState
The type used for the error state for a stream.
Definition: bitStream.hpp:69
Definition: Arcball.hpp:48
bool isLimit() const
Test if the bitstream has encountered a read/write limit.
Definition: bitStream.hpp:173
std::istream * getInput() const
Get the (character) stream from which data is read.
IoState getIoState() const
Get the I/O state of a bit stream.
Definition: bitStream.hpp:184
~InputBitStream()
Destroy a bit stream.
Definition: bitStream.cpp:56
bool isEof() const
Test if the bitstream has encountered end-of-file (EOF).
Definition: bitStream.hpp:168
void setWriteLimit(Offset writeLimit)
Set the number of bits that may still be written to the bit stream.
Definition: bitStream.hpp:529
Size getReadCount() const
Get the number of bits read from the bit stream so far.
Definition: bitStream.hpp:349
void dump(std::ostream &out) const
Dump the internal state of the bit stream to a (character) stream for debugging purposes.
Definition: bitStream.cpp:143
Output bit stream class.
Definition: bitStream.hpp:372
void setInput(std::istream *in)
Set the (character) stream from which data is read.
Definition: bitStream.cpp:64
static const IoState allIoBits
all error bits
Definition: bitStream.hpp:81
long getBits(int numBits)
Read the specified number of bits from the bit stream.
Definition: bitStream.cpp:75
void flush()
Flush any pending output to the underlying (character) stream.
Definition: bitStream.cpp:249
OutputBitStream()
Create a bit stream that is not initially bound to any (character) stream.
Definition: bitStream.cpp:158
~OutputBitStream()
Destroy a bit stream.
Definition: bitStream.cpp:172
void clearIoStateBits(IoState state=allIoBits)
Clear the specified bits in the I/O state of a bit stream.
Definition: bitStream.hpp:189
void setOutput(std::ostream *out)
Set the output (character) stream associated with the bit stream.
Definition: bitStream.cpp:181
Input bit stream class.
Definition: bitStream.hpp:209
static const IoState limitBit
read/write limit exceeded
Definition: bitStream.hpp:77
void clearReadCount()
Set the read count to zero.
Definition: bitStream.hpp:344
void align()
Force byte-alignment of the bit stream.
Definition: bitStream.cpp:112
void align()
Align the bit stream output position to the nearest byte boundary.
Definition: bitStream.cpp:256
A common base class for the input and output bit stream classes.
Definition: bitStream.hpp:61
void dump(std::ostream &out) const
Dump the internal state of the bit stream to the specified (character) stream for debugging purposes...
Definition: bitStream.cpp:266
void setReadLimit(Offset readLimit)
Specify the maximum allowable number of bits that may be read from the bit stream.
Definition: bitStream.hpp:354
void setIoState(IoState state)
Set the I/O state of a bit stream.
Definition: bitStream.hpp:178
int putBits(long data, int numBits)
Output the specified number of bits to the bit stream.
Definition: bitStream.cpp:197
void clearWriteCount()
Clear the count of the number of bits written to the bit stream.
Definition: bitStream.hpp:519
std::ostream * getOutput() const
Get the output (character) stream associated with the bit stream.
Definition: bitStream.cpp:188
Offset getWriteLimit() const
Get the number of bits that may still be written to the underlying (character) stream.
Definition: bitStream.hpp:524
unsigned long long Size
An unsigned integral type (used for sizes/counts).
Definition: bitStream.hpp:86
static const IoState badBit
I/O error.
Definition: bitStream.hpp:79
long long Offset
A signed integral type (used for differences).
Definition: bitStream.hpp:91
Offset getReadLimit() const
Get the number of bits that still may be read from the bit stream before the read limit is reached...
Definition: bitStream.hpp:359