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