write.hpp 4.5 KB

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