lazy_ostream.hpp 4.6 KB

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