write.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_WRITE_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_WRITE_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/categories.hpp>
  14. #include <boost/iostreams/detail/char_traits.hpp>
  15. #include <boost/iostreams/detail/dispatch.hpp>
  16. #include <boost/iostreams/detail/ios.hpp> // streamsize.
  17. #include <boost/iostreams/detail/streambuf.hpp>
  18. #include <boost/iostreams/detail/wrap_unwrap.hpp>
  19. #include <boost/iostreams/operations_fwd.hpp>
  20. #include <boost/iostreams/traits.hpp>
  21. #include <boost/mpl/if.hpp>
  22. // Must come last.
  23. #include <boost/iostreams/detail/config/disable_warnings.hpp>
  24. namespace boost { namespace iostreams {
  25. namespace detail {
  26. template<typename T>
  27. struct write_device_impl;
  28. template<typename T>
  29. struct write_filter_impl;
  30. } // End namespace detail.
  31. template<typename T>
  32. bool put(T& t, typename char_type_of<T>::type c)
  33. { return detail::write_device_impl<T>::put(detail::unwrap(t), c); }
  34. template<typename T>
  35. inline std::streamsize write
  36. (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
  37. { return detail::write_device_impl<T>::write(detail::unwrap(t), s, n); }
  38. template<typename T, typename Sink>
  39. inline std::streamsize
  40. write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
  41. std::streamsize n )
  42. { return detail::write_filter_impl<T>::write(detail::unwrap(t), snk, s, n); }
  43. namespace detail {
  44. //------------------Definition of write_device_impl---------------------------//
  45. template<typename T>
  46. struct write_device_impl
  47. : mpl::if_<
  48. is_custom<T>,
  49. operations<T>,
  50. write_device_impl<
  51. BOOST_DEDUCED_TYPENAME
  52. dispatch<
  53. T, ostream_tag, streambuf_tag, output
  54. >::type
  55. >
  56. >::type
  57. { };
  58. template<>
  59. struct write_device_impl<ostream_tag> {
  60. template<typename T>
  61. static bool put(T& t, typename char_type_of<T>::type c)
  62. {
  63. typedef typename char_type_of<T>::type char_type;
  64. typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
  65. return !traits_type::eq_int_type( t.rdbuf()->sputc(c),
  66. traits_type::eof() );
  67. }
  68. template<typename T>
  69. static std::streamsize write
  70. (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
  71. { return t.rdbuf()->sputn(s, n); }
  72. };
  73. template<>
  74. struct write_device_impl<streambuf_tag> {
  75. template<typename T>
  76. static bool put(T& t, typename char_type_of<T>::type c)
  77. {
  78. typedef typename char_type_of<T>::type char_type;
  79. typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
  80. return !traits_type::eq_int_type(t.sputc(c), traits_type::eof());
  81. }
  82. template<typename T>
  83. static std::streamsize write
  84. (T& t, const typename char_type_of<T>::type* s, std::streamsize n)
  85. { return t.sputn(s, n); }
  86. };
  87. template<>
  88. struct write_device_impl<output> {
  89. template<typename T>
  90. static bool put(T& t, typename char_type_of<T>::type c)
  91. { return t.write(&c, 1) == 1; }
  92. template<typename T>
  93. static std::streamsize
  94. write(T& t, const typename char_type_of<T>::type* s, std::streamsize n)
  95. { return t.write(s, n); }
  96. };
  97. //------------------Definition of write_filter_impl---------------------------//
  98. template<typename T>
  99. struct write_filter_impl
  100. : mpl::if_<
  101. is_custom<T>,
  102. operations<T>,
  103. write_filter_impl<
  104. BOOST_DEDUCED_TYPENAME
  105. dispatch<
  106. T, multichar_tag, any_tag
  107. >::type
  108. >
  109. >::type
  110. { };
  111. template<>
  112. struct write_filter_impl<multichar_tag> {
  113. template<typename T, typename Sink>
  114. static std::streamsize
  115. write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
  116. std::streamsize n )
  117. { return t.write(snk, s, n); }
  118. };
  119. template<>
  120. struct write_filter_impl<any_tag> {
  121. template<typename T, typename Sink>
  122. static std::streamsize
  123. write( T& t, Sink& snk, const typename char_type_of<T>::type* s,
  124. std::streamsize n )
  125. {
  126. for (std::streamsize off = 0; off < n; ++off)
  127. if (!t.put(snk, s[off]))
  128. return off;
  129. return n;
  130. }
  131. };
  132. } // End namespace detail.
  133. } } // End namespaces iostreams, boost.
  134. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  135. #endif // #ifndef BOOST_IOSTREAMS_WRITE_HPP_INCLUDED