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)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision: 74248 $
12 // Description : defines level of indiration facilitating workarounds for non printable types
13 // ***************************************************************************
15 #ifndef BOOST_TEST_TOOLS_IMPL_COMMON_HPP_012705GER
16 #define BOOST_TEST_TOOLS_IMPL_COMMON_HPP_012705GER
19 #include <boost/test/detail/config.hpp>
20 #include <boost/test/detail/global_typedef.hpp>
21 #include <boost/test/detail/workaround.hpp>
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>
33 #if !defined(BOOST_NO_CXX11_NULLPTR)
37 #include <boost/test/detail/suppress_warnings.hpp>
39 //____________________________________________________________________________//
42 namespace test_tools {
45 // ************************************************************************** //
46 // ************** boost_test_print_type ************** //
47 // ************************************************************************** //
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");
58 struct boost_test_print_type_impl {
60 std::ostream& operator()(std::ostream& ostr, R const& r) const {
61 return boost_test_print_type(ostr, r);
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();
71 static const impl::boost_test_print_type_impl& boost_test_print_type =
72 static_const<impl::boost_test_print_type_impl>::value;
76 // ************************************************************************** //
77 // ************** print_log_value ************** //
78 // ************************************************************************** //
81 struct print_log_value {
82 void operator()( std::ostream& ostr, T const& t )
84 typedef typename mpl::or_<is_array<T>,is_function<T>,is_abstract<T> >::type cant_use_nl;
86 std::streamsize old_precision = set_precision( ostr, cant_use_nl() );
89 using boost::test_tools::tt_detail::boost_test_print_type;
90 boost_test_print_type(ostr, t);
92 if( old_precision != (std::streamsize)-1 )
93 ostr.precision( old_precision );
96 std::streamsize set_precision( std::ostream& ostr, mpl::false_ )
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 );
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 );
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;
117 std::streamsize set_precision( std::ostream&, mpl::true_ ) { return (std::streamsize)-1; }
120 //____________________________________________________________________________//
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 )
132 //____________________________________________________________________________//
135 struct BOOST_TEST_DECL print_log_value<bool> {
136 void operator()( std::ostream& ostr, bool t );
139 //____________________________________________________________________________//
142 struct BOOST_TEST_DECL print_log_value<char> {
143 void operator()( std::ostream& ostr, char t );
146 //____________________________________________________________________________//
149 struct BOOST_TEST_DECL print_log_value<unsigned char> {
150 void operator()( std::ostream& ostr, unsigned char t );
153 //____________________________________________________________________________//
156 struct BOOST_TEST_DECL print_log_value<char const*> {
157 void operator()( std::ostream& ostr, char const* t );
160 //____________________________________________________________________________//
163 struct BOOST_TEST_DECL print_log_value<wchar_t const*> {
164 void operator()( std::ostream& ostr, wchar_t const* t );
167 #if !defined(BOOST_NO_CXX11_NULLPTR)
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*/ ) {
177 //____________________________________________________________________________//
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
186 struct print_helper_t {
187 explicit print_helper_t( T const& t ) : m_t( t ) {}
192 //____________________________________________________________________________//
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 ) {}
204 //____________________________________________________________________________//
207 inline print_helper_t<T>
208 print_helper( T const& t )
210 return print_helper_t<T>( t );
213 //____________________________________________________________________________//
217 operator<<( std::ostream& ostr, print_helper_t<T> const& ph )
219 print_log_value<T>()( ostr, ph.m_t );
224 //____________________________________________________________________________//
226 } // namespace tt_detail
228 // ************************************************************************** //
229 // ************** BOOST_TEST_DONT_PRINT_LOG_VALUE ************** //
230 // ************************************************************************** //
232 #define BOOST_TEST_DONT_PRINT_LOG_VALUE( the_type ) \
233 namespace boost{ namespace test_tools{ namespace tt_detail{ \
235 struct print_log_value<the_type > { \
236 void operator()( std::ostream&, the_type const& ) {} \
241 } // namespace test_tools
244 #include <boost/test/detail/enable_warnings.hpp>
246 #endif // BOOST_TEST_TOOLS_IMPL_COMMON_HPP_012705GER