lazy_ostream.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Description : contains definition for all test tools in test toolbox
  8. // ***************************************************************************
  9. #ifndef BOOST_TEST_UTILS_LAZY_OSTREAM_HPP
  10. #define BOOST_TEST_UTILS_LAZY_OSTREAM_HPP
  11. // Boost.Test
  12. #include <boost/test/detail/config.hpp>
  13. #include <boost/test/tools/detail/print_helper.hpp>
  14. // STL
  15. #include <iosfwd>
  16. #include <boost/test/detail/suppress_warnings.hpp>
  17. //____________________________________________________________________________//
  18. // ************************************************************************** //
  19. // ************** lazy_ostream ************** //
  20. // ************************************************************************** //
  21. namespace boost {
  22. namespace unit_test {
  23. class BOOST_TEST_DECL lazy_ostream {
  24. public:
  25. virtual ~lazy_ostream() {}
  26. static lazy_ostream& instance() { return inst; }
  27. #if !defined(BOOST_EMBTC)
  28. friend std::ostream& operator<<( std::ostream& ostr, lazy_ostream const& o ) { return o( ostr ); }
  29. #else
  30. friend std::ostream& operator<<( std::ostream& ostr, lazy_ostream const& o );
  31. #endif
  32. // access method
  33. bool empty() const { return m_empty; }
  34. // actual printing interface; to be accessed only by this class and children
  35. virtual std::ostream& operator()( std::ostream& ostr ) const { return ostr; }
  36. protected:
  37. explicit lazy_ostream( bool p_empty = true ) : m_empty( p_empty ) {}
  38. private:
  39. // Data members
  40. bool m_empty;
  41. static lazy_ostream inst;
  42. };
  43. #if defined(BOOST_EMBTC)
  44. inline std::ostream& operator<<( std::ostream& ostr, lazy_ostream const& o ) { return o( ostr ); }
  45. #endif
  46. //____________________________________________________________________________//
  47. template<typename PrevType, typename T, typename StorageT=T const&>
  48. class lazy_ostream_impl : public lazy_ostream {
  49. public:
  50. lazy_ostream_impl( PrevType const& prev, T const& value )
  51. : lazy_ostream( false )
  52. , m_prev( prev )
  53. , m_value( value )
  54. {
  55. }
  56. std::ostream& operator()( std::ostream& ostr ) const BOOST_OVERRIDE
  57. {
  58. return m_prev(ostr) << test_tools::tt_detail::print_helper(m_value);
  59. }
  60. private:
  61. // Data members
  62. PrevType const& m_prev;
  63. StorageT m_value;
  64. };
  65. //____________________________________________________________________________//
  66. template<typename T>
  67. inline lazy_ostream_impl<lazy_ostream,T>
  68. operator<<( lazy_ostream const& prev, T const& v )
  69. {
  70. return lazy_ostream_impl<lazy_ostream,T>( prev, v );
  71. }
  72. //____________________________________________________________________________//
  73. template<typename PrevPrevType, typename TPrev, typename T>
  74. inline lazy_ostream_impl<lazy_ostream_impl<PrevPrevType,TPrev>,T>
  75. operator<<( lazy_ostream_impl<PrevPrevType,TPrev> const& prev, T const& v )
  76. {
  77. typedef lazy_ostream_impl<PrevPrevType,TPrev> PrevType;
  78. return lazy_ostream_impl<PrevType,T>( prev, v );
  79. }
  80. //____________________________________________________________________________//
  81. #if BOOST_TEST_USE_STD_LOCALE
  82. template<typename R,typename S>
  83. inline lazy_ostream_impl<lazy_ostream,R& (BOOST_TEST_CALL_DECL *)(S&),R& (BOOST_TEST_CALL_DECL *)(S&)>
  84. operator<<( lazy_ostream const& prev, R& (BOOST_TEST_CALL_DECL *man)(S&) )
  85. {
  86. typedef R& (BOOST_TEST_CALL_DECL * ManipType)(S&);
  87. return lazy_ostream_impl<lazy_ostream,ManipType,ManipType>( prev, man );
  88. }
  89. //____________________________________________________________________________//
  90. template<typename PrevPrevType, typename TPrev,typename R,typename S>
  91. inline lazy_ostream_impl<lazy_ostream_impl<PrevPrevType,TPrev>,R& (BOOST_TEST_CALL_DECL *)(S&),R& (BOOST_TEST_CALL_DECL *)(S&)>
  92. operator<<( lazy_ostream_impl<PrevPrevType,TPrev> const& prev, R& (BOOST_TEST_CALL_DECL *man)(S&) )
  93. {
  94. typedef R& (BOOST_TEST_CALL_DECL * ManipType)(S&);
  95. return lazy_ostream_impl<lazy_ostream_impl<PrevPrevType,TPrev>,ManipType,ManipType>( prev, man );
  96. }
  97. //____________________________________________________________________________//
  98. #endif
  99. #define BOOST_TEST_LAZY_MSG( M ) (::boost::unit_test::lazy_ostream::instance() << M)
  100. } // namespace unit_test
  101. } // namespace boost
  102. #include <boost/test/detail/enable_warnings.hpp>
  103. #endif // BOOST_TEST_UTILS_LAZY_OSTREAM_HPP