• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BOOST_SAFE_FORMAT_HPP
2 #define BOOST_SAFE_FORMAT_HPP
3 
4 //  Copyright (c) 2015 Robert Ramey
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #include <ostream>
11 #include <typeinfo>
12 
13 #include <boost/core/demangle.hpp>
14 #include <boost/safe_numerics/safe_common.hpp>
15 
16 namespace {
17 
18 // create an output manipulator which prints variable type and limits
19 // as well as value
20 template<typename T>
21 struct safe_format_impl {
22     const T & m_t;
safe_format_impl__anon371736010111::safe_format_impl23     safe_format_impl(const T & t) :
24         m_t(t)
25     {}
26     template <class charT, class Traits>
27     friend std::basic_ostream<charT,Traits> &
operator <<(std::basic_ostream<charT,Traits> & os,const safe_format_impl<T> & f)28     operator<<(
29         std::basic_ostream<charT,Traits> & os,
30         const safe_format_impl<T> & f
31     ){
32         return os
33             << "<"
34             << boost::core::demangle(typeid(
35                 typename boost::safe_numerics::base_type<T>::type
36                 ).name()
37             )
38             << ">["
39             << std::numeric_limits<T>::min() << ","
40             << std::numeric_limits<T>::max() << "] = "
41             << f.m_t;
42     }
43 };
44 
45 } // anonymous namespace
46 
47 template<typename T>
safe_format(const T & t)48 auto safe_format(const T & t){
49     return safe_format_impl<T>(t);
50 }
51 
52 #endif // BOOST_SAFE_FORMAT_HPP
53