lazy_ostream.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // (C) Copyright Gennadiy Rozental 2008.
  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: 49312 $
  10. //
  11. // Description : contains definition for all test tools in test toolbox
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_LAZY_OSTREAM_HPP_070708GER
  14. #define BOOST_TEST_LAZY_OSTREAM_HPP_070708GER
  15. // Boost.Test
  16. #include <boost/test/detail/config.hpp>
  17. // STL
  18. #include <iosfwd>
  19. #include <boost/test/detail/suppress_warnings.hpp>
  20. //____________________________________________________________________________//
  21. // ************************************************************************** //
  22. // ************** lazy_ostream ************** //
  23. // ************************************************************************** //
  24. namespace boost {
  25. namespace unit_test {
  26. class lazy_ostream {
  27. public:
  28. static lazy_ostream& instance() { static lazy_ostream inst; return inst; }
  29. friend std::ostream& operator<<( std::ostream& ostr, lazy_ostream const& o ) { return o( ostr ); }
  30. // access method
  31. bool empty() const { return m_empty; }
  32. // actual printing interface; to be accessed only by this class and children
  33. virtual std::ostream& operator()( std::ostream& ostr ) const { return ostr; }
  34. protected:
  35. explicit lazy_ostream( bool empty = true ) : m_empty( empty ) {}
  36. // protected destructor to make sure right one is called
  37. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  38. public:
  39. #endif
  40. BOOST_TEST_PROTECTED_VIRTUAL ~lazy_ostream() {}
  41. private:
  42. // Data members
  43. bool m_empty;
  44. };
  45. //____________________________________________________________________________//
  46. template<typename T>
  47. class lazy_ostream_impl : public lazy_ostream {
  48. public:
  49. lazy_ostream_impl( lazy_ostream const& prev, T value )
  50. : lazy_ostream( false )
  51. , m_prev( prev )
  52. , m_value( value )
  53. {}
  54. private:
  55. virtual std::ostream& operator()( std::ostream& ostr ) const
  56. {
  57. return m_prev(ostr) << m_value;
  58. }
  59. // Data members
  60. lazy_ostream const& m_prev;
  61. T m_value;
  62. };
  63. //____________________________________________________________________________//
  64. template<typename T>
  65. inline lazy_ostream_impl<T const&>
  66. operator<<( lazy_ostream const& prev, T const& v )
  67. {
  68. return lazy_ostream_impl<T const&>( prev, v );
  69. }
  70. //____________________________________________________________________________//
  71. #if BOOST_TEST_USE_STD_LOCALE
  72. template<typename R,typename S>
  73. inline lazy_ostream_impl<R& (BOOST_TEST_CALL_DECL *)(S&)>
  74. operator<<( lazy_ostream const& prev, R& (BOOST_TEST_CALL_DECL *man)(S&) )
  75. {
  76. return lazy_ostream_impl<R& (BOOST_TEST_CALL_DECL *)(S&)>( prev, man );
  77. }
  78. //____________________________________________________________________________//
  79. #endif
  80. } // namespace unit_test
  81. } // namespace boost
  82. //____________________________________________________________________________//
  83. #include <boost/test/detail/enable_warnings.hpp>
  84. #endif // BOOST_TEST_LAZY_OSTREAM_HPP_070708GER