From 67d567b5c3713441951740f41b030b7df276f153 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 15 Jul 2014 19:50:08 +1000 Subject: [PATCH] stream: add stream bit printing object Allows one to print a given number of bits from an integral value easily using streams. --- stream.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++----- stream.hpp | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/stream.cpp b/stream.cpp index cbe71261..c2f4bf7a 100644 --- a/stream.cpp +++ b/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; +} diff --git a/stream.hpp b/stream.hpp index e0570913..45400fb8 100644 --- a/stream.hpp +++ b/stream.hpp @@ -22,25 +22,39 @@ #include +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