safe_format.hpp 1.3 KB

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