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