close.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_CLOSE_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_CLOSE_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/flush.hpp>
  15. #include <boost/iostreams/detail/adapter/non_blocking_adapter.hpp>
  16. #include <boost/iostreams/detail/ios.hpp> // BOOST_IOS
  17. #include <boost/iostreams/detail/select.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/identity.hpp>
  22. #include <boost/mpl/if.hpp>
  23. #include <boost/type_traits/is_convertible.hpp>
  24. #include <boost/type_traits/is_integral.hpp>
  25. #include <boost/type_traits/remove_cv.hpp>
  26. #include <boost/type_traits/remove_reference.hpp>
  27. // Must come last.
  28. #include <boost/iostreams/detail/config/disable_warnings.hpp>
  29. namespace boost { namespace iostreams {
  30. template<typename T>
  31. void close(T& t);
  32. template<typename T>
  33. void close(T& t, BOOST_IOS::openmode which);
  34. template<typename T, typename Sink>
  35. void close(T& t, Sink& snk, BOOST_IOS::openmode which);
  36. namespace detail {
  37. template<typename T>
  38. void close_all(T& t)
  39. {
  40. try {
  41. boost::iostreams::close(t, BOOST_IOS::in);
  42. } catch (...) {
  43. try {
  44. boost::iostreams::close(t, BOOST_IOS::out);
  45. } catch (...) { }
  46. throw;
  47. }
  48. boost::iostreams::close(t, BOOST_IOS::out);
  49. }
  50. template<typename T, typename Sink>
  51. void close_all(T& t, Sink& snk)
  52. {
  53. try {
  54. boost::iostreams::close(t, snk, BOOST_IOS::in);
  55. } catch (...) {
  56. try {
  57. boost::iostreams::close(t, snk, BOOST_IOS::out);
  58. } catch (...) { }
  59. throw;
  60. }
  61. boost::iostreams::close(t, snk, BOOST_IOS::out);
  62. }
  63. } // End namespace detail.
  64. } } // End namespaces iostreams, boost.
  65. namespace boost { namespace iostreams {
  66. namespace detail {
  67. template<typename T>
  68. struct close_impl;
  69. } // End namespace detail.
  70. template<typename T>
  71. void close(T& t) { detail::close_all(t); }
  72. template<typename T>
  73. void close(T& t, BOOST_IOS::openmode which)
  74. {
  75. #ifdef BOOST_IOSTREAMS_STRICT
  76. BOOST_ASSERT(which == BOOST_IOS::in || which == BOOST_IOS::out);
  77. #else
  78. if (which == (BOOST_IOS::in | BOOST_IOS::out)) {
  79. detail::close_all(t);
  80. return;
  81. }
  82. #endif
  83. detail::close_impl<T>::close(detail::unwrap(t), which);
  84. }
  85. template<typename T, typename Sink>
  86. void close(T& t, Sink& snk, BOOST_IOS::openmode which)
  87. {
  88. #ifdef BOOST_IOSTREAMS_STRICT
  89. BOOST_ASSERT(which == BOOST_IOS::in || which == BOOST_IOS::out);
  90. #else
  91. if (which == (BOOST_IOS::in | BOOST_IOS::out)) {
  92. detail::close_all(t, snk);
  93. return;
  94. }
  95. #endif
  96. detail::close_impl<T>::close(detail::unwrap(t), snk, which);
  97. }
  98. namespace detail {
  99. //------------------Definition of close_impl----------------------------------//
  100. struct close_boost_stream { };
  101. struct close_filtering_stream { };
  102. template<typename T>
  103. struct close_tag {
  104. typedef typename category_of<T>::type category;
  105. typedef typename detail::unwrapped_type<T>::type unwrapped;
  106. typedef typename
  107. iostreams::select<
  108. mpl::not_< is_convertible<category, closable_tag> >,
  109. any_tag,
  110. mpl::or_<
  111. is_boost_stream<unwrapped>,
  112. is_boost_stream_buffer<unwrapped>
  113. >,
  114. close_boost_stream,
  115. mpl::or_<
  116. is_filtering_stream<unwrapped>,
  117. is_filtering_streambuf<unwrapped>
  118. >,
  119. close_filtering_stream,
  120. mpl::or_<
  121. is_convertible<category, two_sequence>,
  122. is_convertible<category, dual_use>
  123. >,
  124. two_sequence,
  125. else_,
  126. closable_tag
  127. >::type type;
  128. };
  129. template<typename T>
  130. struct close_impl
  131. : mpl::if_<
  132. is_custom<T>,
  133. operations<T>,
  134. close_impl<BOOST_DEDUCED_TYPENAME close_tag<T>::type>
  135. >::type
  136. { };
  137. template<>
  138. struct close_impl<any_tag> {
  139. template<typename T>
  140. static void close(T& t, BOOST_IOS::openmode which)
  141. {
  142. if (which == BOOST_IOS::out)
  143. iostreams::flush(t);
  144. }
  145. template<typename T, typename Sink>
  146. static void close(T& t, Sink& snk, BOOST_IOS::openmode which)
  147. {
  148. if (which == BOOST_IOS::out) {
  149. non_blocking_adapter<Sink> nb(snk);
  150. iostreams::flush(t, nb);
  151. }
  152. }
  153. };
  154. template<>
  155. struct close_impl<close_boost_stream> {
  156. template<typename T>
  157. static void close(T& t)
  158. {
  159. t.close();
  160. }
  161. template<typename T>
  162. static void close(T& t, BOOST_IOS::openmode which)
  163. {
  164. if (which == BOOST_IOS::out)
  165. t.close();
  166. }
  167. };
  168. template<>
  169. struct close_impl<close_filtering_stream> {
  170. template<typename T>
  171. static void close(T& t, BOOST_IOS::openmode which)
  172. {
  173. typedef typename category_of<T>::type category;
  174. const bool in = is_convertible<category, input>::value &&
  175. !is_convertible<category, output>::value;
  176. if (in == (which == BOOST_IOS::in) && t.is_complete())
  177. t.pop();
  178. }
  179. };
  180. template<>
  181. struct close_impl<closable_tag> {
  182. template<typename T>
  183. static void close(T& t, BOOST_IOS::openmode which)
  184. {
  185. typedef typename category_of<T>::type category;
  186. const bool in = is_convertible<category, input>::value &&
  187. !is_convertible<category, output>::value;
  188. if (in == (which == BOOST_IOS::in))
  189. t.close();
  190. }
  191. template<typename T, typename Sink>
  192. static void close(T& t, Sink& snk, BOOST_IOS::openmode which)
  193. {
  194. typedef typename category_of<T>::type category;
  195. const bool in = is_convertible<category, input>::value &&
  196. !is_convertible<category, output>::value;
  197. if (in == (which == BOOST_IOS::in)) {
  198. non_blocking_adapter<Sink> nb(snk);
  199. t.close(nb);
  200. }
  201. }
  202. };
  203. template<>
  204. struct close_impl<two_sequence> {
  205. template<typename T>
  206. static void close(T& t, BOOST_IOS::openmode which) { t.close(which); }
  207. template<typename T, typename Sink>
  208. static void close(T& t, Sink& snk, BOOST_IOS::openmode which)
  209. {
  210. non_blocking_adapter<Sink> nb(snk);
  211. t.close(nb, which);
  212. }
  213. };
  214. } // End namespace detail.
  215. } } // End namespaces iostreams, boost.
  216. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  217. #endif // #ifndef BOOST_IOSTREAMS_CLOSE_HPP_INCLUDED