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>
boost_test_print_type(std::ostream & ostr,T const & 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>
operator ()boost::test_tools::tt_detail::impl::boost_test_print_type_impl60             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 {
operator ()boost::test_tools::tt_detail::print_log_value82     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 
set_precisionboost::test_tools::tt_detail::print_log_value96     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 
set_precisionboost::test_tools::tt_detail::print_log_value117     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] > {
operator ()boost::test_tools::tt_detail::print_log_value125     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 BOOST_TEST_DECL print_log_value<std::nullptr_t> {
170     void    operator()( std::ostream& ostr, std::nullptr_t t );
171 };
172 #endif
173 
174 //____________________________________________________________________________//
175 
176 // ************************************************************************** //
177 // **************                 print_helper                 ************** //
178 // ************************************************************************** //
179 // Adds level of indirection to the output operation, allowing us to customize
180 // it for types that do not support operator << directly or for any other reason
181 
182 template<typename T>
183 struct print_helper_t {
print_helper_tboost::test_tools::tt_detail::print_helper_t184     explicit    print_helper_t( T const& t ) : m_t( t ) {}
185 
186     T const&    m_t;
187 };
188 
189 //____________________________________________________________________________//
190 
191 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
192 // Borland suffers premature pointer decay passing arrays by reference
193 template<typename T, std::size_t N >
194 struct print_helper_t< T[N] > {
print_helper_tboost::test_tools::tt_detail::print_helper_t195     explicit    print_helper_t( T const * t ) : m_t( t ) {}
196 
197     T const *   m_t;
198 };
199 #endif
200 
201 //____________________________________________________________________________//
202 
203 template<typename T>
204 inline print_helper_t<T>
print_helper(T const & t)205 print_helper( T const& t )
206 {
207     return print_helper_t<T>( t );
208 }
209 
210 //____________________________________________________________________________//
211 
212 template<typename T>
213 inline std::ostream&
operator <<(std::ostream & ostr,print_helper_t<T> const & ph)214 operator<<( std::ostream& ostr, print_helper_t<T> const& ph )
215 {
216     print_log_value<T>()( ostr, ph.m_t );
217 
218     return ostr;
219 }
220 
221 //____________________________________________________________________________//
222 
223 } // namespace tt_detail
224 
225 // ************************************************************************** //
226 // **************       BOOST_TEST_DONT_PRINT_LOG_VALUE        ************** //
227 // ************************************************************************** //
228 
229 #define BOOST_TEST_DONT_PRINT_LOG_VALUE( the_type )         \
230 namespace boost{ namespace test_tools{ namespace tt_detail{ \
231 template<>                                                  \
232 struct print_log_value<the_type > {                         \
233     void    operator()( std::ostream&, the_type const& ) {} \
234 };                                                          \
235 }}}                                                         \
236 /**/
237 
238 } // namespace test_tools
239 } // namespace boost
240 
241 #include <boost/test/detail/enable_warnings.hpp>
242 
243 #endif // BOOST_TEST_TOOLS_IMPL_COMMON_HPP_012705GER
244