print_helper.hpp 8.3 KB

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