lazy_ostream.hpp 4.3 KB

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