traits.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. //
  7. // Contains metafunctions char_type_of, category_of and mode_of used for
  8. // deducing the i/o category and i/o mode of a model of Filter or Device.
  9. //
  10. // Also contains several utility metafunctions, functions and macros.
  11. //
  12. #ifndef BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED
  13. #define BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED
  14. #if defined(_MSC_VER)
  15. # pragma once
  16. #endif
  17. #include <iosfwd> // stream types, char_traits.
  18. #include <boost/config.hpp> // partial spec, deduced typename.
  19. #include <boost/detail/workaround.hpp>
  20. #include <boost/iostreams/categories.hpp>
  21. #include <boost/iostreams/detail/bool_trait_def.hpp>
  22. #include <boost/iostreams/detail/config/wide_streams.hpp>
  23. #include <boost/iostreams/detail/is_iterator_range.hpp>
  24. #include <boost/iostreams/detail/select.hpp>
  25. #include <boost/iostreams/detail/select_by_size.hpp>
  26. #include <boost/iostreams/detail/wrap_unwrap.hpp>
  27. #include <boost/iostreams/traits_fwd.hpp>
  28. #include <boost/mpl/bool.hpp>
  29. #include <boost/mpl/eval_if.hpp>
  30. #include <boost/mpl/identity.hpp>
  31. #include <boost/mpl/int.hpp>
  32. #include <boost/mpl/or.hpp>
  33. #include <boost/range/iterator_range.hpp>
  34. #include <boost/range/value_type.hpp>
  35. #include <boost/ref.hpp>
  36. #include <boost/type_traits/is_convertible.hpp>
  37. // Must come last.
  38. #include <boost/iostreams/detail/config/disable_warnings.hpp>
  39. namespace boost { namespace iostreams {
  40. //----------Definitions of predicates for streams and stream buffers----------//
  41. #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------------------//
  42. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istream, std::basic_istream, 2)
  43. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostream, std::basic_ostream, 2)
  44. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_iostream, std::basic_iostream, 2)
  45. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_streambuf, std::basic_streambuf, 2)
  46. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ifstream, std::basic_ifstream, 2)
  47. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ofstream, std::basic_ofstream, 2)
  48. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_fstream, std::basic_fstream, 2)
  49. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_filebuf, std::basic_filebuf, 2)
  50. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istringstream, std::basic_istringstream, 3)
  51. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostringstream, std::basic_ostringstream, 3)
  52. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_stringstream, std::basic_stringstream, 3)
  53. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_stringbuf, std::basic_stringbuf, 3)
  54. #else // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-----------------------//
  55. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_istream, std::istream, 0)
  56. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_ostream, std::ostream, 0)
  57. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_iostream, std::iostream, 0)
  58. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_streambuf, std::streambuf, 0)
  59. #endif // #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //----------------------//
  60. template<typename T>
  61. struct is_std_io
  62. : mpl::or_< is_istream<T>, is_ostream<T>, is_streambuf<T> >
  63. { };
  64. template<typename T>
  65. struct is_std_file_device
  66. : mpl::or_<
  67. is_ifstream<T>,
  68. is_ofstream<T>,
  69. is_fstream<T>,
  70. is_filebuf<T>
  71. >
  72. { };
  73. template<typename T>
  74. struct is_std_string_device
  75. : mpl::or_<
  76. is_istringstream<T>,
  77. is_ostringstream<T>,
  78. is_stringstream<T>,
  79. is_stringbuf<T>
  80. >
  81. { };
  82. template<typename Device, typename Tr, typename Alloc>
  83. struct stream;
  84. template<typename T, typename Tr, typename Alloc, typename Mode>
  85. class stream_buffer;
  86. template< typename Mode, typename Ch, typename Tr,
  87. typename Alloc, typename Access >
  88. class filtering_stream;
  89. template< typename Mode, typename Ch, typename Tr,
  90. typename Alloc, typename Access >
  91. class wfiltering_stream;
  92. template< typename Mode, typename Ch, typename Tr,
  93. typename Alloc, typename Access >
  94. class filtering_streambuf;
  95. template< typename Mode, typename Ch, typename Tr,
  96. typename Alloc, typename Access >
  97. class filtering_wstreambuf;
  98. namespace detail {
  99. template<typename T, typename Tr>
  100. class linked_streambuf;
  101. BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream,
  102. boost::iostreams::stream,
  103. 3 )
  104. BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_boost_stream_buffer,
  105. boost::iostreams::stream_buffer,
  106. 4 )
  107. BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_stream_impl,
  108. boost::iostreams::filtering_stream,
  109. 5 )
  110. BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstream_impl,
  111. boost::iostreams::wfiltering_stream,
  112. 5 )
  113. BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_streambuf_impl,
  114. boost::iostreams::filtering_streambuf,
  115. 5 )
  116. BOOST_IOSTREAMS_BOOL_TRAIT_DEF( is_filtering_wstreambuf_impl,
  117. boost::iostreams::filtering_wstreambuf,
  118. 5 )
  119. BOOST_IOSTREAMS_BOOL_TRAIT_DEF(is_linked, linked_streambuf, 2)
  120. template<typename T>
  121. struct is_filtering_stream
  122. : mpl::or_<
  123. is_filtering_stream_impl<T>,
  124. is_filtering_wstream_impl<T>
  125. >
  126. { };
  127. template<typename T>
  128. struct is_filtering_streambuf
  129. : mpl::or_<
  130. is_filtering_streambuf_impl<T>,
  131. is_filtering_wstreambuf_impl<T>
  132. >
  133. { };
  134. template<typename T>
  135. struct is_boost
  136. : mpl::or_<
  137. is_boost_stream<T>,
  138. is_boost_stream_buffer<T>,
  139. is_filtering_stream<T>,
  140. is_filtering_streambuf<T>
  141. >
  142. { };
  143. } // End namespace detail.
  144. //------------------Definitions of char_type_of-------------------------------//
  145. namespace detail {
  146. template<typename T>
  147. struct member_char_type { typedef typename T::char_type type; };
  148. } // End namespace detail.
  149. # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------------------//
  150. template<typename T>
  151. struct char_type_of
  152. : detail::member_char_type<
  153. typename detail::unwrapped_type<T>::type
  154. >
  155. { };
  156. # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //---------------------//
  157. template<typename T>
  158. struct char_type_of {
  159. typedef typename detail::unwrapped_type<T>::type U;
  160. typedef typename
  161. mpl::eval_if<
  162. is_std_io<U>,
  163. mpl::identity<char>,
  164. detail::member_char_type<U>
  165. >::type type;
  166. };
  167. # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
  168. template<typename Iter>
  169. struct char_type_of< iterator_range<Iter> > {
  170. typedef typename iterator_value<Iter>::type type;
  171. };
  172. //------------------Definitions of category_of--------------------------------//
  173. namespace detail {
  174. template<typename T>
  175. struct member_category { typedef typename T::category type; };
  176. } // End namespace detail.
  177. template<typename T>
  178. struct category_of {
  179. template<typename U>
  180. struct member_category {
  181. typedef typename U::category type;
  182. };
  183. typedef typename detail::unwrapped_type<T>::type U;
  184. typedef typename
  185. mpl::eval_if<
  186. mpl::and_<
  187. is_std_io<U>,
  188. mpl::not_< detail::is_boost<U> >
  189. >,
  190. iostreams::select< // Disambiguation for Tru64
  191. is_filebuf<U>, filebuf_tag,
  192. is_ifstream<U>, ifstream_tag,
  193. is_ofstream<U>, ofstream_tag,
  194. is_fstream<U>, fstream_tag,
  195. is_stringbuf<U>, stringbuf_tag,
  196. is_istringstream<U>, istringstream_tag,
  197. is_ostringstream<U>, ostringstream_tag,
  198. is_stringstream<U>, stringstream_tag,
  199. is_streambuf<U>, generic_streambuf_tag,
  200. is_iostream<U>, generic_iostream_tag,
  201. is_istream<U>, generic_istream_tag,
  202. is_ostream<U>, generic_ostream_tag
  203. >,
  204. detail::member_category<U>
  205. >::type type;
  206. };
  207. // Partial specialization for reference wrappers
  208. template<typename T>
  209. struct category_of< reference_wrapper<T> >
  210. : category_of<T>
  211. { };
  212. //------------------Definition of get_category--------------------------------//
  213. //
  214. // Returns an object of type category_of<T>::type.
  215. //
  216. template<typename T>
  217. inline typename category_of<T>::type get_category(const T&)
  218. { typedef typename category_of<T>::type category; return category(); }
  219. //------------------Definition of int_type_of---------------------------------//
  220. template<typename T>
  221. struct int_type_of {
  222. #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
  223. typedef std::char_traits<
  224. BOOST_DEDUCED_TYPENAME char_type_of<T>::type
  225. > traits_type;
  226. typedef typename traits_type::int_type type;
  227. #else
  228. typedef int type;
  229. #endif
  230. };
  231. //------------------Definition of mode_of-------------------------------------//
  232. namespace detail {
  233. template<int N> struct io_mode_impl;
  234. #define BOOST_IOSTREAMS_MODE_HELPER(tag_, id_) \
  235. case_<id_> io_mode_impl_helper(tag_); \
  236. template<> struct io_mode_impl<id_> { typedef tag_ type; }; \
  237. /**/
  238. BOOST_IOSTREAMS_MODE_HELPER(input, 1)
  239. BOOST_IOSTREAMS_MODE_HELPER(output, 2)
  240. BOOST_IOSTREAMS_MODE_HELPER(bidirectional, 3)
  241. BOOST_IOSTREAMS_MODE_HELPER(input_seekable, 4)
  242. BOOST_IOSTREAMS_MODE_HELPER(output_seekable, 5)
  243. BOOST_IOSTREAMS_MODE_HELPER(seekable, 6)
  244. BOOST_IOSTREAMS_MODE_HELPER(dual_seekable, 7)
  245. BOOST_IOSTREAMS_MODE_HELPER(bidirectional_seekable, 8)
  246. BOOST_IOSTREAMS_MODE_HELPER(dual_use, 9)
  247. #undef BOOST_IOSTREAMS_MODE_HELPER
  248. template<typename T>
  249. struct io_mode_id {
  250. typedef typename category_of<T>::type category;
  251. BOOST_SELECT_BY_SIZE(int, value, detail::io_mode_impl_helper(category()));
  252. };
  253. } // End namespace detail.
  254. template<typename T> // Borland 5.6.4 requires this circumlocution.
  255. struct mode_of : detail::io_mode_impl< detail::io_mode_id<T>::value > { };
  256. // Partial specialization for reference wrappers
  257. template<typename T>
  258. struct mode_of< reference_wrapper<T> >
  259. : mode_of<T>
  260. { };
  261. //------------------Definition of is_device, is_filter and is_direct----------//
  262. namespace detail {
  263. template<typename T, typename Tag>
  264. struct has_trait_impl {
  265. typedef typename category_of<T>::type category;
  266. BOOST_STATIC_CONSTANT(bool, value = (is_convertible<category, Tag>::value));
  267. };
  268. template<typename T, typename Tag>
  269. struct has_trait
  270. : mpl::bool_<has_trait_impl<T, Tag>::value>
  271. { };
  272. } // End namespace detail.
  273. template<typename T>
  274. struct is_device : detail::has_trait<T, device_tag> { };
  275. template<typename T>
  276. struct is_filter : detail::has_trait<T, filter_tag> { };
  277. template<typename T>
  278. struct is_direct : detail::has_trait<T, direct_tag> { };
  279. //------------------Definition of BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS----------//
  280. #define BOOST_IOSTREAMS_STREAMBUF_TYPEDEFS(Tr) \
  281. typedef Tr traits_type; \
  282. typedef typename traits_type::int_type int_type; \
  283. typedef typename traits_type::off_type off_type; \
  284. typedef typename traits_type::pos_type pos_type; \
  285. /**/
  286. } } // End namespaces iostreams, boost.
  287. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  288. #endif // #ifndef BOOST_IOSTREAMS_IO_TRAITS_HPP_INCLUDED