stream: add stream bit printing object
Allows one to print a given number of bits from an integral value easily using streams.
This commit is contained in:
parent
d78df821bf
commit
67d567b5c3
49
stream.cpp
49
stream.cpp
@ -19,18 +19,57 @@
|
||||
|
||||
#include "stream.hpp"
|
||||
|
||||
#include "debug.hpp"
|
||||
|
||||
using util::stream::null;
|
||||
using util::stream::bits;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::ostream&
|
||||
nullstream::put (char)
|
||||
null::put (char)
|
||||
{ return *this; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
bool
|
||||
nullstream::good (void) const
|
||||
null::good (void) const
|
||||
{ return !bad () && !eof () && !fail (); }
|
||||
|
||||
|
||||
bool nullstream::bad (void) const { return false; }
|
||||
bool nullstream::eof (void) const { return false; }
|
||||
bool nullstream::fail (void) const { return false; }
|
||||
//-----------------------------------------------------------------------------
|
||||
bool null::bad (void) const { return false; }
|
||||
bool null::eof (void) const { return false; }
|
||||
bool null::fail (void) const { return false; }
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
bits::bits (uintmax_t _value, unsigned _count):
|
||||
value (_value),
|
||||
count (_count)
|
||||
{
|
||||
CHECK_LE (count, sizeof(value) * 8);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::ostream&
|
||||
operator<< (std::ostream &os, util::stream::bits b) {
|
||||
char digits[sizeof (b.value) * 8 + 1] = { 0 };
|
||||
char *cursor = std::end (digits) - 1;
|
||||
|
||||
uintmax_t bits = b.value;
|
||||
unsigned count = b.count;
|
||||
|
||||
while (count) {
|
||||
CHECK_GE (cursor, digits);
|
||||
|
||||
--cursor;
|
||||
*cursor = (bits & 0x01) ? '1' : '0';
|
||||
|
||||
bits >>= 1;
|
||||
count -= 1;
|
||||
}
|
||||
|
||||
os << cursor << "/" << b.count;
|
||||
return os;
|
||||
}
|
||||
|
42
stream.hpp
42
stream.hpp
@ -22,25 +22,39 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace util {
|
||||
namespace stream {
|
||||
//---------------------------------------------------------------------
|
||||
class null : public std::ostream {
|
||||
public:
|
||||
std::ostream & put (char c);
|
||||
std::ostream & write (const char *s, std::streamsize n);
|
||||
|
||||
class nullstream : public std::ostream {
|
||||
public:
|
||||
std::ostream & put (char c);
|
||||
std::ostream & write (const char *s, std::streamsize n);
|
||||
std::streampos tellp (void);
|
||||
std::ostream & seekp (std::streampos pos);
|
||||
std::ostream & seekp (std::streamoff off,
|
||||
std::ios_base::seekdir dir);
|
||||
|
||||
std::streampos tellp (void);
|
||||
std::ostream & seekp (std::streampos pos);
|
||||
std::ostream & seekp (std::streamoff off,
|
||||
std::ios_base::seekdir dir);
|
||||
std::ostream & flush (void);
|
||||
|
||||
std::ostream & flush (void);
|
||||
bool good (void) const;
|
||||
bool bad (void) const;
|
||||
bool eof (void) const;
|
||||
bool fail (void) const;
|
||||
|
||||
bool good (void) const;
|
||||
bool bad (void) const;
|
||||
bool eof (void) const;
|
||||
bool fail (void) const;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
struct bits {
|
||||
bits (uintmax_t value, unsigned count);
|
||||
|
||||
uintmax_t value;
|
||||
unsigned count;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& operator<< (std::ostream&, util::stream::bits);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user