flush.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_FLUSH_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_FLUSH_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/config.hpp> // DEDUCED_TYPENAME, MSVC.
  12. #include <boost/detail/workaround.hpp>
  13. #include <boost/iostreams/detail/dispatch.hpp>
  14. #include <boost/iostreams/detail/streambuf.hpp>
  15. #include <boost/iostreams/detail/wrap_unwrap.hpp>
  16. #include <boost/iostreams/operations_fwd.hpp>
  17. #include <boost/iostreams/traits.hpp>
  18. #include <boost/mpl/if.hpp>
  19. // Must come last.
  20. #include <boost/iostreams/detail/config/disable_warnings.hpp>
  21. namespace boost { namespace iostreams {
  22. namespace detail {
  23. template<typename T>
  24. struct flush_device_impl;
  25. template<typename T>
  26. struct flush_filter_impl;
  27. } // End namespace detail.
  28. template<typename T>
  29. bool flush(T& t)
  30. { return detail::flush_device_impl<T>::flush(detail::unwrap(t)); }
  31. template<typename T, typename Sink>
  32. bool flush(T& t, Sink& snk)
  33. { return detail::flush_filter_impl<T>::flush(detail::unwrap(t), snk); }
  34. namespace detail {
  35. //------------------Definition of flush_device_impl---------------------------//
  36. template<typename T>
  37. struct flush_device_impl
  38. : mpl::if_<
  39. is_custom<T>,
  40. operations<T>,
  41. flush_device_impl<
  42. BOOST_DEDUCED_TYPENAME
  43. dispatch<
  44. T, ostream_tag, streambuf_tag, flushable_tag, any_tag
  45. >::type
  46. >
  47. >::type
  48. { };
  49. template<>
  50. struct flush_device_impl<ostream_tag> {
  51. template<typename T>
  52. static bool flush(T& t)
  53. { return t.rdbuf()->BOOST_IOSTREAMS_PUBSYNC() == 0; }
  54. };
  55. template<>
  56. struct flush_device_impl<streambuf_tag> {
  57. template<typename T>
  58. static bool flush(T& t)
  59. { return t.BOOST_IOSTREAMS_PUBSYNC() == 0; }
  60. };
  61. template<>
  62. struct flush_device_impl<flushable_tag> {
  63. template<typename T>
  64. static bool flush(T& t) { return t.flush(); }
  65. };
  66. template<>
  67. struct flush_device_impl<any_tag> {
  68. template<typename T>
  69. static bool flush(T&) { return true; }
  70. };
  71. //------------------Definition of flush_filter_impl---------------------------//
  72. template<typename T>
  73. struct flush_filter_impl
  74. : mpl::if_<
  75. is_custom<T>,
  76. operations<T>,
  77. flush_filter_impl<
  78. BOOST_DEDUCED_TYPENAME
  79. dispatch<
  80. T, flushable_tag, any_tag
  81. >::type
  82. >
  83. >::type
  84. { };
  85. template<>
  86. struct flush_filter_impl<flushable_tag> {
  87. template<typename T, typename Sink>
  88. static bool flush(T& t, Sink& snk) { return t.flush(snk); }
  89. };
  90. template<>
  91. struct flush_filter_impl<any_tag> {
  92. template<typename T, typename Sink>
  93. static bool flush(T&, Sink&) { return false; }
  94. };
  95. } // End namespace detail.
  96. } } // End namespaces iostreams, boost.
  97. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  98. #endif // #ifndef BOOST_IOSTREAMS_FLUSH_HPP_INCLUDED