direct_adapter.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/config.hpp> // SFINAE, MSVC, put ptrdiff_t in std.
  12. #include <algorithm> // copy, min.
  13. #include <cstddef> // ptrdiff_t.
  14. #include <boost/detail/workaround.hpp>
  15. #include <boost/iostreams/categories.hpp>
  16. #include <boost/iostreams/detail/config/limits.hpp> // forwarding.
  17. #include <boost/iostreams/detail/config/wide_streams.hpp> // locale.
  18. #include <boost/iostreams/detail/double_object.hpp>
  19. #include <boost/iostreams/detail/error.hpp>
  20. #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
  21. #include <boost/iostreams/traits.hpp> // mode_of, is_direct.
  22. #include <boost/iostreams/operations.hpp>
  23. #include <boost/mpl/bool.hpp>
  24. #include <boost/mpl/or.hpp>
  25. #include <boost/preprocessor/iteration/local.hpp>
  26. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  27. #include <boost/preprocessor/repetition/enum_params.hpp>
  28. #include <boost/static_assert.hpp>
  29. #include <boost/throw_exception.hpp>
  30. #include <boost/type_traits/is_convertible.hpp>
  31. // Must come last.
  32. #include <boost/iostreams/detail/config/disable_warnings.hpp> // VC7.1
  33. namespace boost { namespace iostreams { namespace detail {
  34. //------------------Definition of direct_adapter_base-------------------------//
  35. // Put all initialization in base class to faciliate forwarding.
  36. template<typename Direct>
  37. class direct_adapter_base {
  38. public:
  39. typedef typename char_type_of<Direct>::type char_type;
  40. typedef typename mode_of<Direct>::type mode_type;
  41. struct category
  42. : mode_type,
  43. device_tag,
  44. closable_tag
  45. #ifndef BOOST_IOSTREAMS_NO_LOCALE
  46. , localizable_tag
  47. #endif
  48. { };
  49. protected:
  50. explicit direct_adapter_base(const Direct& d);
  51. typedef is_convertible<category, two_sequence> is_double;
  52. struct pointers {
  53. pointers() : beg(0), ptr(0), end(0) { }
  54. char_type *beg, *ptr, *end;
  55. };
  56. void init_input(mpl::true_);
  57. void init_input(mpl::false_) { }
  58. void init_output(mpl::true_);
  59. void init_output(mpl::false_) { }
  60. double_object<pointers, is_double> ptrs_;
  61. Direct d_;
  62. };
  63. template<typename Direct>
  64. class direct_adapter : private direct_adapter_base<Direct> {
  65. private:
  66. typedef direct_adapter_base<Direct> base_type;
  67. typedef typename base_type::pointers pointers;
  68. typedef typename base_type::is_double is_double;
  69. using base_type::ptrs_;
  70. using base_type::d_;
  71. public:
  72. typedef typename base_type::char_type char_type;
  73. typedef typename base_type::category category;
  74. // Constructors
  75. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  76. direct_adapter(const Direct& d) : base_type(d) { }
  77. direct_adapter(const direct_adapter& d) : base_type(d) { }
  78. # define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY)
  79. #else
  80. template<typename U>
  81. struct is_direct
  82. : mpl::or_<
  83. is_same<U, direct_adapter<Direct> >,
  84. is_same<U, Direct>
  85. >
  86. { };
  87. template<typename U>
  88. direct_adapter(const U& u)
  89. : base_type(forward(u, is_direct<U>()))
  90. { }
  91. # define BOOST_PP_LOCAL_LIMITS (2, BOOST_IOSTREAMS_MAX_FORWARDING_ARITY)
  92. #endif
  93. #define BOOST_PP_LOCAL_MACRO(n) \
  94. template<BOOST_PP_ENUM_PARAMS(n, typename P)> \
  95. direct_adapter(BOOST_PP_ENUM_BINARY_PARAMS(n, const P, &p)) \
  96. : base_type(Direct(BOOST_PP_ENUM_PARAMS(n, p))) \
  97. { } \
  98. /**/
  99. #include BOOST_PP_LOCAL_ITERATE()
  100. #undef BOOST_PP_LOCAL_MACRO
  101. // Device interface.
  102. std::streamsize read(char_type* s, std::streamsize n);
  103. std::streamsize write(const char_type* s, std::streamsize n);
  104. std::streampos seek( stream_offset, BOOST_IOS::seekdir,
  105. BOOST_IOS::openmode = BOOST_IOS::in | BOOST_IOS::out );
  106. void close();
  107. void close(BOOST_IOS::openmode which);
  108. #ifndef BOOST_IOSTREAMS_NO_LOCALE
  109. void imbue(const std::locale&);
  110. #endif
  111. // Direct device access.
  112. Direct& operator*() { return d_; }
  113. Direct* operator->() { return &d_; }
  114. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  115. private:
  116. template<typename U>
  117. static Direct forward(const U& u, mpl::true_) { return u; }
  118. template<typename U>
  119. static Direct forward(const U& u, mpl::false_) { return Direct(u); }
  120. #endif
  121. };
  122. //--------------Definition of wrap_direct and unwrap_direct-------------------//
  123. template<typename Device>
  124. struct wrap_direct_traits
  125. : mpl::if_<
  126. is_direct<Device>,
  127. direct_adapter<Device>,
  128. Device
  129. >
  130. { };
  131. template<typename Device>
  132. typename wrap_direct_traits<Device>::type
  133. inline wrap_direct(Device dev)
  134. {
  135. typedef typename wrap_direct_traits<Device>::type type;
  136. return type(dev);
  137. }
  138. template<typename Device>
  139. inline Device& unwrap_direct(Device& d) { return d; }
  140. template<typename Device>
  141. inline Device& unwrap_direct(direct_adapter<Device>& d) { return *d; }
  142. //--------------Implementation of direct_adapter_base-------------------------//
  143. template<typename Direct>
  144. direct_adapter_base<Direct>::direct_adapter_base(const Direct& d) : d_(d)
  145. {
  146. init_input(is_convertible<category, input>());
  147. init_output(is_convertible<category, output>());
  148. }
  149. template<typename Direct>
  150. void direct_adapter_base<Direct>::init_input(mpl::true_)
  151. {
  152. std::pair<char_type*, char_type*> seq = iostreams::input_sequence(d_);
  153. ptrs_.first().beg = seq.first;
  154. ptrs_.first().ptr = seq.first;
  155. ptrs_.first().end = seq.second;
  156. }
  157. template<typename Direct>
  158. void direct_adapter_base<Direct>::init_output(mpl::true_)
  159. {
  160. std::pair<char_type*, char_type*> seq = iostreams::output_sequence(d_);
  161. ptrs_.second().beg = seq.first;
  162. ptrs_.second().ptr = seq.first;
  163. ptrs_.second().end = seq.second;
  164. }
  165. //--------------Implementation of direct_adapter------------------------------//
  166. template<typename Direct>
  167. inline std::streamsize direct_adapter<Direct>::read
  168. (char_type* s, std::streamsize n)
  169. {
  170. using namespace std;
  171. pointers& get = ptrs_.first();
  172. std::streamsize avail =
  173. static_cast<std::streamsize>(get.end - get.ptr);
  174. std::streamsize result = (std::min)(n, avail);
  175. std::copy(get.ptr, get.ptr + result, s);
  176. get.ptr += result;
  177. return result != 0 ? result : -1;
  178. }
  179. template<typename Direct>
  180. inline std::streamsize direct_adapter<Direct>::write
  181. (const char_type* s, std::streamsize n)
  182. {
  183. using namespace std;
  184. pointers& put = ptrs_.second();
  185. if (n > static_cast<std::streamsize>(put.end - put.ptr))
  186. boost::throw_exception(write_area_exhausted());
  187. std::copy(s, s + n, put.ptr);
  188. put.ptr += n;
  189. return n;
  190. }
  191. template<typename Direct>
  192. inline std::streampos direct_adapter<Direct>::seek
  193. ( stream_offset off, BOOST_IOS::seekdir way,
  194. BOOST_IOS::openmode which )
  195. {
  196. using namespace std;
  197. pointers& get = ptrs_.first();
  198. pointers& put = ptrs_.second();
  199. if (way == BOOST_IOS::cur && get.ptr != put.ptr)
  200. boost::throw_exception(bad_seek());
  201. ptrdiff_t next = 0;
  202. if ((which & BOOST_IOS::in) || !is_double::value) {
  203. if (way == BOOST_IOS::beg)
  204. next = off;
  205. else if (way == BOOST_IOS::cur)
  206. next = get.ptr - get.beg + off;
  207. else
  208. next = get.end - get.beg + off;
  209. if (next >= 0 && next <= get.end - get.beg)
  210. get.ptr = get.beg + next;
  211. else
  212. boost::throw_exception(bad_seek());
  213. }
  214. if ((which & BOOST_IOS::out) && is_double::value) {
  215. if (way == BOOST_IOS::beg)
  216. next = off;
  217. else if (way == BOOST_IOS::cur)
  218. next = put.ptr - put.beg + off;
  219. else
  220. next = put.end - put.beg + off;
  221. if (next >= 0 && next <= put.end - put.beg)
  222. put.ptr = put.beg + next;
  223. else
  224. boost::throw_exception(bad_seek());
  225. }
  226. return offset_to_position(next);
  227. }
  228. template<typename Direct>
  229. void direct_adapter<Direct>::close()
  230. {
  231. BOOST_STATIC_ASSERT((!is_convertible<category, two_sequence>::value));
  232. detail::close_all(d_);
  233. }
  234. template<typename Direct>
  235. void direct_adapter<Direct>::close(BOOST_IOS::openmode which)
  236. {
  237. BOOST_STATIC_ASSERT((is_convertible<category, two_sequence>::value));
  238. boost::iostreams::close(d_, which);
  239. }
  240. #ifndef BOOST_IOSTREAMS_NO_LOCALE
  241. template<typename Direct>
  242. void direct_adapter<Direct>::imbue(const std::locale& loc)
  243. { boost::iostreams::imbue(d_, loc); }
  244. #endif
  245. } } } // End namespaces detail, iostreams, boost.
  246. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  247. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_DIRECT_ADAPTER_HPP_INCLUDED