range_adapter.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_RANGE_ADAPTER_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_RANGE_ADAPTER_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <algorithm> // min.
  12. #include <boost/assert.hpp>
  13. #include <cstddef> // ptrdiff_t.
  14. #include <iosfwd> // streamsize, streamoff.
  15. #include <iterator> // iterator_traits.
  16. #include <boost/iostreams/categories.hpp>
  17. #include <boost/iostreams/detail/error.hpp>
  18. #include <boost/iostreams/positioning.hpp>
  19. #include <boost/mpl/if.hpp>
  20. #include <boost/throw_exception.hpp>
  21. #include <boost/type_traits/is_convertible.hpp>
  22. #include <boost/core/enable_if.hpp>
  23. // Must come last.
  24. #include <boost/iostreams/detail/config/disable_warnings.hpp> // MSVC.
  25. namespace boost { namespace iostreams { namespace detail {
  26. // Used for simulated tag dispatch.
  27. template<typename Traversal> struct range_adapter_impl;
  28. //
  29. // Template name: range_adapter
  30. // Description: Device based on an instance of boost::iterator_range.
  31. // Template parameters:
  32. // Mode - A mode tag.
  33. // Range - An instance of iterator_range.
  34. //
  35. template<typename Mode, typename Range>
  36. class range_adapter {
  37. private:
  38. typedef typename Range::iterator iterator;
  39. typedef std::iterator_traits<iterator> iter_traits;
  40. typedef typename iter_traits::iterator_category iter_cat;
  41. public:
  42. typedef typename Range::value_type char_type;
  43. struct category : Mode, device_tag { };
  44. typedef typename
  45. mpl::if_<
  46. is_convertible<
  47. iter_cat,
  48. std::random_access_iterator_tag
  49. >,
  50. std::random_access_iterator_tag,
  51. std::forward_iterator_tag
  52. >::type tag;
  53. typedef range_adapter_impl<tag> impl;
  54. explicit range_adapter(const Range& rng);
  55. range_adapter(iterator first, iterator last);
  56. std::streamsize read(char_type* s, std::streamsize n);
  57. std::streamsize write(const char_type* s, std::streamsize n);
  58. std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
  59. private:
  60. iterator first_, cur_, last_;
  61. };
  62. //------------------Implementation of range_adapter---------------------------//
  63. template<typename Mode, typename Range>
  64. range_adapter<Mode, Range>::range_adapter(const Range& rng)
  65. : first_(rng.begin()), cur_(rng.begin()), last_(rng.end()) { }
  66. template<typename Mode, typename Range>
  67. range_adapter<Mode, Range>::range_adapter(iterator first, iterator last)
  68. : first_(first), cur_(first), last_(last) { }
  69. template<typename Mode, typename Range>
  70. inline std::streamsize range_adapter<Mode, Range>::read
  71. (char_type* s, std::streamsize n)
  72. { return impl::read(cur_, last_, s, n); }
  73. template<typename Mode, typename Range>
  74. inline std::streamsize range_adapter<Mode, Range>::write
  75. (const char_type* s, std::streamsize n)
  76. { return impl::write(cur_, last_, s, n); }
  77. template<typename Mode, typename Range>
  78. std::streampos range_adapter<Mode, Range>::seek
  79. (stream_offset off, BOOST_IOS::seekdir way)
  80. {
  81. impl::seek(first_, cur_, last_, off, way);
  82. return offset_to_position(cur_ - first_);
  83. }
  84. //------------------Implementation of range_adapter_impl----------------------//
  85. template<>
  86. struct range_adapter_impl<std::forward_iterator_tag> {
  87. template<typename Iter, typename Ch>
  88. static std::streamsize read
  89. (Iter& cur, Iter& last, Ch* s,std::streamsize n)
  90. {
  91. std::streamsize rem = n; // No. of chars remaining.
  92. while (cur != last && rem-- > 0) *s++ = *cur++;
  93. return n - rem != 0 ? n - rem : -1;
  94. }
  95. template<typename Iter, typename Ch>
  96. static std::streamsize write
  97. (Iter& cur, Iter& last, const Ch* s, std::streamsize n)
  98. {
  99. while (cur != last && n-- > 0) *cur++ = *s++;
  100. if (cur == last && n > 0)
  101. boost::throw_exception(write_area_exhausted());
  102. return n;
  103. }
  104. };
  105. template<>
  106. struct range_adapter_impl<std::random_access_iterator_tag> {
  107. template<typename Iter, typename Ch>
  108. static std::streamsize read
  109. (Iter& cur, Iter& last, Ch* s,std::streamsize n)
  110. {
  111. std::streamsize result =
  112. (std::min)(static_cast<std::streamsize>(last - cur), n);
  113. if (result)
  114. std::copy(cur, cur + result, s);
  115. cur += result;
  116. return result != 0 ? result : -1;
  117. }
  118. template<typename Iter, typename Ch>
  119. static std::streamsize write
  120. (Iter& cur, Iter& last, const Ch* s, std::streamsize n)
  121. {
  122. std::streamsize count =
  123. (std::min)(static_cast<std::streamsize>(last - cur), n);
  124. std::copy(s, s + count, cur);
  125. cur += count;
  126. if (count < n)
  127. boost::throw_exception(write_area_exhausted());
  128. return n;
  129. }
  130. template<typename Iter>
  131. static void seek
  132. ( Iter& first, Iter& cur, Iter& last, stream_offset off,
  133. BOOST_IOS::seekdir way )
  134. {
  135. using namespace std;
  136. switch (way) {
  137. case BOOST_IOS::beg:
  138. if (off > last - first || off < 0)
  139. boost::throw_exception(bad_seek());
  140. cur = first + off;
  141. break;
  142. case BOOST_IOS::cur:
  143. {
  144. std::ptrdiff_t newoff = cur - first + off;
  145. if (newoff > last - first || newoff < 0)
  146. boost::throw_exception(bad_seek());
  147. cur += off;
  148. break;
  149. }
  150. case BOOST_IOS::end:
  151. if (last - first + off < 0 || off > 0)
  152. boost::throw_exception(bad_seek());
  153. cur = last + off;
  154. break;
  155. default:
  156. BOOST_ASSERT(0);
  157. }
  158. }
  159. };
  160. } } } // End namespaces detail, iostreams, boost.
  161. #include <boost/iostreams/detail/config/enable_warnings.hpp> // MSVC.
  162. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_RANGE_ADAPTER_HPP_INCLUDED //---------------//