lazy_ostream.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 BOOST_TEST_DECL lazy_ostream {
  23. public:
  24. virtual ~lazy_ostream() {}
  25. static lazy_ostream& instance() { 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. static lazy_ostream inst;
  37. };
  38. //____________________________________________________________________________//
  39. template<typename PrevType, typename T, typename StorageT=T const&>
  40. class lazy_ostream_impl : public lazy_ostream {
  41. public:
  42. lazy_ostream_impl( PrevType const& prev, T const& value )
  43. : lazy_ostream( false )
  44. , m_prev( prev )
  45. , m_value( value )
  46. {
  47. }
  48. virtual std::ostream& operator()( std::ostream& ostr ) const
  49. {
  50. return m_prev(ostr) << m_value;
  51. }
  52. private:
  53. // Data members
  54. PrevType const& m_prev;
  55. StorageT m_value;
  56. };
  57. //____________________________________________________________________________//
  58. template<typename T>
  59. inline lazy_ostream_impl<lazy_ostream,T>
  60. operator<<( lazy_ostream const& prev, T const& v )
  61. {
  62. return lazy_ostream_impl<lazy_ostream,T>( prev, v );
  63. }
  64. //____________________________________________________________________________//
  65. template<typename PrevPrevType, typename TPrev, typename T>
  66. inline lazy_ostream_impl<lazy_ostream_impl<PrevPrevType,TPrev>,T>
  67. operator<<( lazy_ostream_impl<PrevPrevType,TPrev> const& prev, T const& v )
  68. {
  69. typedef lazy_ostream_impl<PrevPrevType,TPrev> PrevType;
  70. return lazy_ostream_impl<PrevType,T>( prev, v );
  71. }
  72. //____________________________________________________________________________//
  73. #if BOOST_TEST_USE_STD_LOCALE
  74. template<typename R,typename S>
  75. inline lazy_ostream_impl<lazy_ostream,R& (BOOST_TEST_CALL_DECL *)(S&),R& (BOOST_TEST_CALL_DECL *)(S&)>
  76. operator<<( lazy_ostream const& prev, R& (BOOST_TEST_CALL_DECL *man)(S&) )
  77. {
  78. typedef R& (BOOST_TEST_CALL_DECL * ManipType)(S&);
  79. return lazy_ostream_impl<lazy_ostream,ManipType,ManipType>( prev, man );
  80. }
  81. //____________________________________________________________________________//
  82. template<typename PrevPrevType, typename TPrev,typename R,typename S>
  83. inline lazy_ostream_impl<lazy_ostream_impl<PrevPrevType,TPrev>,R& (BOOST_TEST_CALL_DECL *)(S&),R& (BOOST_TEST_CALL_DECL *)(S&)>
  84. operator<<( lazy_ostream_impl<PrevPrevType,TPrev> 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_impl<PrevPrevType,TPrev>,ManipType,ManipType>( prev, man );
  88. }
  89. //____________________________________________________________________________//
  90. #endif
  91. #define BOOST_TEST_LAZY_MSG( M ) (::boost::unit_test::lazy_ostream::instance() << M)
  92. } // namespace unit_test
  93. } // namespace boost
  94. #include <boost/test/detail/enable_warnings.hpp>
  95. #endif // BOOST_TEST_UTILS_LAZY_OSTREAM_HPP