]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/test/tools/detail/print_helper.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / test / tools / detail / print_helper.hpp
1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // File : $RCSfile$
9 //
10 // Version : $Revision: 74248 $
11 //
12 // Description : defines level of indiration facilitating workarounds for non printable types
13 // ***************************************************************************
14
15 #ifndef BOOST_TEST_TOOLS_IMPL_COMMON_HPP_012705GER
16 #define BOOST_TEST_TOOLS_IMPL_COMMON_HPP_012705GER
17
18 // Boost.Test
19 #include <boost/test/detail/config.hpp>
20 #include <boost/test/detail/global_typedef.hpp>
21 #include <boost/test/detail/workaround.hpp>
22
23 // Boost
24 #include <boost/mpl/or.hpp>
25 #include <boost/static_assert.hpp>
26 #include <boost/type_traits/is_array.hpp>
27 #include <boost/type_traits/is_function.hpp>
28 #include <boost/type_traits/is_abstract.hpp>
29 #include <boost/type_traits/has_left_shift.hpp>
30
31 #include <limits>
32
33 #if !defined(BOOST_NO_CXX11_NULLPTR)
34 #include <cstddef>
35 #endif
36
37 #include <boost/test/detail/suppress_warnings.hpp>
38
39 //____________________________________________________________________________//
40
41 namespace boost {
42 namespace test_tools {
43 namespace tt_detail {
44
45 // ************************************************************************** //
46 // ************** boost_test_print_type ************** //
47 // ************************************************************************** //
48
49 namespace impl {
50 template <class T>
51 std::ostream& boost_test_print_type(std::ostream& ostr, T const& t) {
52 BOOST_STATIC_ASSERT_MSG( (boost::has_left_shift<std::ostream,T>::value),
53 "Type has to implement operator<< to be printable");
54 ostr << t;
55 return ostr;
56 }
57
58 struct boost_test_print_type_impl {
59 template <class R>
60 std::ostream& operator()(std::ostream& ostr, R const& r) const {
61 return boost_test_print_type(ostr, r);
62 }
63 };
64 }
65
66 // To avoid ODR violations, see N4381
67 template <class T> struct static_const { static const T value; };
68 template <class T> const T static_const<T>::value = T();
69
70 namespace {
71 static const impl::boost_test_print_type_impl& boost_test_print_type =
72 static_const<impl::boost_test_print_type_impl>::value;
73 }
74
75
76 // ************************************************************************** //
77 // ************** print_log_value ************** //
78 // ************************************************************************** //
79
80 template<typename T>
81 struct print_log_value {
82 void operator()( std::ostream& ostr, T const& t )
83 {
84 typedef typename mpl::or_<is_array<T>,is_function<T>,is_abstract<T> >::type cant_use_nl;
85
86 std::streamsize old_precision = set_precision( ostr, cant_use_nl() );
87
88 //ostr << t;
89 using boost::test_tools::tt_detail::boost_test_print_type;
90 boost_test_print_type(ostr, t);
91
92 if( old_precision != (std::streamsize)-1 )
93 ostr.precision( old_precision );
94 }
95
96 std::streamsize set_precision( std::ostream& ostr, mpl::false_ )
97 {
98 if( std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::radix == 2 )
99 return ostr.precision( 2 + std::numeric_limits<T>::digits * 301/1000 );
100 else if ( std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::radix == 10 ) {
101 #ifdef BOOST_NO_CXX11_NUMERIC_LIMITS
102 // (was BOOST_NO_NUMERIC_LIMITS_LOWEST but now deprecated).
103 // No support for std::numeric_limits<double>::max_digits10,
104 // so guess that a couple of guard digits more than digits10 will display any difference.
105 return ostr.precision( 2 + std::numeric_limits<T>::digits10 );
106 #else
107 // std::numeric_limits<double>::max_digits10; IS supported.
108 // Any noisy or guard digits needed to display any difference are included in max_digits10.
109 return ostr.precision( std::numeric_limits<T>::max_digits10 );
110 #endif
111 }
112 // else if T is not specialized for std::numeric_limits<>,
113 // then will just get the default precision of 6 digits.
114 return (std::streamsize)-1;
115 }
116
117 std::streamsize set_precision( std::ostream&, mpl::true_ ) { return (std::streamsize)-1; }
118 };
119
120 //____________________________________________________________________________//
121
122 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
123 template<typename T, std::size_t N >
124 struct print_log_value< T[N] > {
125 void operator()( std::ostream& ostr, T const* t )
126 {
127 ostr << t;
128 }
129 };
130 #endif
131
132 //____________________________________________________________________________//
133
134 template<>
135 struct BOOST_TEST_DECL print_log_value<bool> {
136 void operator()( std::ostream& ostr, bool t );
137 };
138
139 //____________________________________________________________________________//
140
141 template<>
142 struct BOOST_TEST_DECL print_log_value<char> {
143 void operator()( std::ostream& ostr, char t );
144 };
145
146 //____________________________________________________________________________//
147
148 template<>
149 struct BOOST_TEST_DECL print_log_value<unsigned char> {
150 void operator()( std::ostream& ostr, unsigned char t );
151 };
152
153 //____________________________________________________________________________//
154
155 template<>
156 struct BOOST_TEST_DECL print_log_value<char const*> {
157 void operator()( std::ostream& ostr, char const* t );
158 };
159
160 //____________________________________________________________________________//
161
162 template<>
163 struct BOOST_TEST_DECL print_log_value<wchar_t const*> {
164 void operator()( std::ostream& ostr, wchar_t const* t );
165 };
166
167 #if !defined(BOOST_NO_CXX11_NULLPTR)
168 template<>
169 struct print_log_value<std::nullptr_t> {
170 // declaration and definition is here because of #12969 https://svn.boost.org/trac10/ticket/12969
171 void operator()( std::ostream& ostr, std::nullptr_t /*t*/ ) {
172 ostr << "nullptr";
173 }
174 };
175 #endif
176
177 //____________________________________________________________________________//
178
179 // ************************************************************************** //
180 // ************** print_helper ************** //
181 // ************************************************************************** //
182 // Adds level of indirection to the output operation, allowing us to customize
183 // it for types that do not support operator << directly or for any other reason
184
185 template<typename T>
186 struct print_helper_t {
187 explicit print_helper_t( T const& t ) : m_t( t ) {}
188
189 T const& m_t;
190 };
191
192 //____________________________________________________________________________//
193
194 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
195 // Borland suffers premature pointer decay passing arrays by reference
196 template<typename T, std::size_t N >
197 struct print_helper_t< T[N] > {
198 explicit print_helper_t( T const * t ) : m_t( t ) {}
199
200 T const * m_t;
201 };
202 #endif
203
204 //____________________________________________________________________________//
205
206 template<typename T>
207 inline print_helper_t<T>
208 print_helper( T const& t )
209 {
210 return print_helper_t<T>( t );
211 }
212
213 //____________________________________________________________________________//
214
215 template<typename T>
216 inline std::ostream&
217 operator<<( std::ostream& ostr, print_helper_t<T> const& ph )
218 {
219 print_log_value<T>()( ostr, ph.m_t );
220
221 return ostr;
222 }
223
224 //____________________________________________________________________________//
225
226 } // namespace tt_detail
227
228 // ************************************************************************** //
229 // ************** BOOST_TEST_DONT_PRINT_LOG_VALUE ************** //
230 // ************************************************************************** //
231
232 #define BOOST_TEST_DONT_PRINT_LOG_VALUE( the_type ) \
233 namespace boost{ namespace test_tools{ namespace tt_detail{ \
234 template<> \
235 struct print_log_value<the_type > { \
236 void operator()( std::ostream&, the_type const& ) {} \
237 }; \
238 }}} \
239 /**/
240
241 } // namespace test_tools
242 } // namespace boost
243
244 #include <boost/test/detail/enable_warnings.hpp>
245
246 #endif // BOOST_TEST_TOOLS_IMPL_COMMON_HPP_012705GER