From be92d3a11f520095aafec8afd0512e553b3754cc Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 9 Apr 2015 20:41:48 +1000 Subject: [PATCH] stream: add numeric ostream iomanip --- stream.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/stream.hpp b/stream.hpp index 66cd1a9a..75e75cdc 100644 --- a/stream.hpp +++ b/stream.hpp @@ -21,9 +21,37 @@ #define __UTIL_STREAM_HPP #include +#include "types/bits.hpp" namespace util { namespace stream { + //--------------------------------------------------------------------- + template + struct numeric + { + numeric (T _val): val (_val) { ; } + T val; + }; + + template + std::ostream& operator<< (std::ostream &os, numeric n) + { + static_assert (std::is_fundamental::value, + "numeric streamer is intended for chars"); + + using integral_t = typename std::conditional< + std::is_floating_point::value, + T, + typename std::conditional< + std::is_signed::value, + sized_type< intmax_t>::sint, + sized_type::uint + >::type + >::type; + + return os << (integral_t)n.val; + } + //--------------------------------------------------------------------- class null : public std::ostream { public: