restrict_impl.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.(See accompanying
  3. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  4. *
  5. * See http://www.boost.org/libs/iostreams for documentation.
  6. * File: boost/iostreams/detail/restrict_impl.hpp
  7. * Date: Sun Jan 06 12:57:30 MST 2008
  8. * Copyright: 2007-2008 CodeRage, LLC
  9. * Author: Jonathan Turkanis
  10. * Contact: turkanis at coderage dot com
  11. *
  12. * If included with the macro BOOST_IOSTREAMS_RESTRICT undefined, defines the
  13. * class template boost::iostreams::restriction. If included with the macro
  14. * BOOST_IOSTREAMS_RESTRICT defined as an identifier, defines the overloaded
  15. * function template boost::iostreams::BOOST_IOSTREAMS_RESTRICT, and object
  16. * generator for boost::iostreams::restriction.
  17. *
  18. * This design allows <boost/iostreams/restrict.hpp> and
  19. * <boost/iostreams/slice.hpp> to share an implementation.
  20. */
  21. #if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) && \
  22. !defined(BOOST_IOSTREAMS_RESTRICT)
  23. # define BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED
  24. //------------------Implementation of restriction-----------------------------//
  25. # include <algorithm> // min.
  26. # include <utility> // pair.
  27. # include <boost/cstdint.hpp> // intmax_t.
  28. # include <boost/config.hpp> // DEDUCED_TYPENAME.
  29. # include <boost/iostreams/categories.hpp>
  30. # include <boost/iostreams/char_traits.hpp>
  31. # include <boost/iostreams/detail/adapter/device_adapter.hpp>
  32. # include <boost/iostreams/detail/adapter/filter_adapter.hpp>
  33. # include <boost/iostreams/detail/call_traits.hpp>
  34. # include <boost/iostreams/detail/enable_if_stream.hpp>
  35. # include <boost/iostreams/detail/error.hpp>
  36. # include <boost/iostreams/detail/ios.hpp> // failure.
  37. # include <boost/iostreams/detail/select.hpp>
  38. # include <boost/iostreams/operations.hpp>
  39. # include <boost/iostreams/skip.hpp>
  40. # include <boost/iostreams/traits.hpp> // mode_of, is_direct.
  41. # include <boost/mpl/bool.hpp>
  42. # include <boost/static_assert.hpp>
  43. # include <boost/throw_exception.hpp>
  44. # include <boost/type_traits/is_convertible.hpp>
  45. # include <boost/iostreams/detail/config/disable_warnings.hpp>
  46. namespace boost { namespace iostreams {
  47. namespace detail {
  48. //
  49. // Template name: restricted_indirect_device.
  50. // Description: Provides an restricted view of an indirect Device.
  51. // Template parameters:
  52. // Device - An indirect model of Device that models either Source or
  53. // SeekableDevice.
  54. //
  55. template<typename Device>
  56. class restricted_indirect_device : public device_adapter<Device> {
  57. private:
  58. typedef typename detail::param_type<Device>::type param_type;
  59. public:
  60. typedef typename char_type_of<Device>::type char_type;
  61. typedef typename mode_of<Device>::type mode;
  62. BOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
  63. struct category
  64. : mode,
  65. device_tag,
  66. closable_tag,
  67. flushable_tag,
  68. localizable_tag,
  69. optimally_buffered_tag
  70. { };
  71. restricted_indirect_device( param_type dev, stream_offset off,
  72. stream_offset len = -1 );
  73. std::streamsize read(char_type* s, std::streamsize n);
  74. std::streamsize write(const char_type* s, std::streamsize n);
  75. std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
  76. private:
  77. stream_offset beg_, pos_, end_;
  78. };
  79. //
  80. // Template name: restricted_direct_device.
  81. // Description: Provides an restricted view of a Direct Device.
  82. // Template parameters:
  83. // Device - A model of Direct and Device.
  84. //
  85. template<typename Device>
  86. class restricted_direct_device : public device_adapter<Device> {
  87. public:
  88. typedef typename char_type_of<Device>::type char_type;
  89. typedef std::pair<char_type*, char_type*> pair_type;
  90. typedef typename mode_of<Device>::type mode;
  91. BOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
  92. struct category
  93. : mode_of<Device>::type,
  94. device_tag,
  95. direct_tag,
  96. closable_tag,
  97. localizable_tag
  98. { };
  99. restricted_direct_device( const Device& dev, stream_offset off,
  100. stream_offset len = -1 );
  101. pair_type input_sequence();
  102. pair_type output_sequence();
  103. private:
  104. pair_type sequence(mpl::true_);
  105. pair_type sequence(mpl::false_);
  106. char_type *beg_, *end_;
  107. };
  108. //
  109. // Template name: restricted_filter.
  110. // Description: Provides an restricted view of a Filter.
  111. // Template parameters:
  112. // Filter - An indirect model of Filter.
  113. //
  114. template<typename Filter>
  115. class restricted_filter : public filter_adapter<Filter> {
  116. public:
  117. typedef typename char_type_of<Filter>::type char_type;
  118. typedef typename mode_of<Filter>::type mode;
  119. BOOST_STATIC_ASSERT(!(is_convertible<mode, detail::two_sequence>::value));
  120. struct category
  121. : mode,
  122. filter_tag,
  123. multichar_tag,
  124. closable_tag,
  125. localizable_tag,
  126. optimally_buffered_tag
  127. { };
  128. restricted_filter( const Filter& flt, stream_offset off,
  129. stream_offset len = -1 );
  130. template<typename Source>
  131. std::streamsize read(Source& src, char_type* s, std::streamsize n)
  132. {
  133. using namespace std;
  134. if (!open_)
  135. open(src, BOOST_IOS::in);
  136. std::streamsize amt =
  137. end_ != -1 ?
  138. (std::min) (n, static_cast<std::streamsize>(end_ - pos_)) :
  139. n;
  140. std::streamsize result =
  141. iostreams::read(this->component(), src, s, amt);
  142. if (result != -1)
  143. pos_ += result;
  144. return result;
  145. }
  146. template<typename Sink>
  147. std::streamsize write(Sink& snk, const char_type* s, std::streamsize n)
  148. {
  149. if (!open_)
  150. open(snk, BOOST_IOS::out);
  151. if (end_ != -1 && pos_ + n >= end_) {
  152. if(pos_ < end_)
  153. pos_ += iostreams::write(this->component(),
  154. snk, s, end_ - pos_);
  155. boost::throw_exception(bad_write());
  156. }
  157. std::streamsize result =
  158. iostreams::write(this->component(), snk, s, n);
  159. pos_ += result;
  160. return result;
  161. }
  162. template<typename Device>
  163. std::streampos seek(Device& dev, stream_offset off, BOOST_IOS::seekdir way)
  164. {
  165. stream_offset next;
  166. if (way == BOOST_IOS::beg) {
  167. next = beg_ + off;
  168. } else if (way == BOOST_IOS::cur) {
  169. next = pos_ + off;
  170. } else if (end_ != -1) {
  171. next = end_ + off;
  172. } else {
  173. // Restriction is half-open; seek relative to the actual end.
  174. pos_ = this->component().seek(dev, off, BOOST_IOS::end);
  175. if (pos_ < beg_)
  176. boost::throw_exception(bad_seek());
  177. return offset_to_position(pos_ - beg_);
  178. }
  179. if (next < beg_ || (end_ != -1 && next >= end_))
  180. boost::throw_exception(bad_seek());
  181. pos_ = this->component().seek(dev, next, BOOST_IOS::cur);
  182. return offset_to_position(pos_ - beg_);
  183. }
  184. template<typename Device>
  185. void close(Device& dev)
  186. {
  187. open_ = false;
  188. detail::close_all(this->component(), dev);
  189. }
  190. template<typename Device>
  191. void close(Device& dev, BOOST_IOS::openmode which)
  192. {
  193. open_ = false;
  194. iostreams::close(this->component(), dev, which);
  195. }
  196. private:
  197. template<typename Device>
  198. void open(Device& dev, BOOST_IOS::openmode which)
  199. {
  200. typedef typename is_convertible<mode, dual_use>::type is_dual_use;
  201. open_ = true;
  202. which = is_dual_use() ? which : (BOOST_IOS::in | BOOST_IOS::out);
  203. iostreams::skip(this->component(), dev, beg_, which);
  204. }
  205. stream_offset beg_, pos_, end_;
  206. bool open_;
  207. };
  208. template<typename T>
  209. struct restriction_traits
  210. : iostreams::select< // Disambiguation for Tru64.
  211. is_filter<T>, restricted_filter<T>,
  212. is_direct<T>, restricted_direct_device<T>,
  213. else_, restricted_indirect_device<T>
  214. >
  215. { };
  216. } // End namespace detail.
  217. template<typename T>
  218. struct restriction : public detail::restriction_traits<T>::type {
  219. typedef typename detail::param_type<T>::type param_type;
  220. typedef typename detail::restriction_traits<T>::type base_type;
  221. restriction(param_type t, stream_offset off, stream_offset len = -1)
  222. : base_type(t, off, len)
  223. { }
  224. };
  225. namespace detail {
  226. //--------------Implementation of restricted_indirect_device------------------//
  227. template<typename Device>
  228. restricted_indirect_device<Device>::restricted_indirect_device
  229. (param_type dev, stream_offset off, stream_offset len)
  230. : device_adapter<Device>(dev), beg_(off), pos_(off),
  231. end_(len != -1 ? off + len : -1)
  232. {
  233. if (len < -1 || off < 0)
  234. boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset"));
  235. iostreams::skip(this->component(), off);
  236. }
  237. template<typename Device>
  238. inline std::streamsize restricted_indirect_device<Device>::read
  239. (char_type* s, std::streamsize n)
  240. {
  241. using namespace std;
  242. std::streamsize amt =
  243. end_ != -1 ?
  244. (std::min) (n, static_cast<std::streamsize>(end_ - pos_)) :
  245. n;
  246. std::streamsize result = iostreams::read(this->component(), s, amt);
  247. if (result != -1)
  248. pos_ += result;
  249. return result;
  250. }
  251. template<typename Device>
  252. inline std::streamsize restricted_indirect_device<Device>::write
  253. (const char_type* s, std::streamsize n)
  254. {
  255. if (end_ != -1 && pos_ + n >= end_) {
  256. if(pos_ < end_)
  257. pos_ += iostreams::write(this->component(), s, end_ - pos_);
  258. boost::throw_exception(bad_write());
  259. }
  260. std::streamsize result = iostreams::write(this->component(), s, n);
  261. pos_ += result;
  262. return result;
  263. }
  264. template<typename Device>
  265. std::streampos restricted_indirect_device<Device>::seek
  266. (stream_offset off, BOOST_IOS::seekdir way)
  267. {
  268. stream_offset next;
  269. if (way == BOOST_IOS::beg) {
  270. next = beg_ + off;
  271. } else if (way == BOOST_IOS::cur) {
  272. next = pos_ + off;
  273. } else if (end_ != -1) {
  274. next = end_ + off;
  275. } else {
  276. // Restriction is half-open; seek relative to the actual end.
  277. pos_ = iostreams::seek(this->component(), off, BOOST_IOS::end);
  278. if (pos_ < beg_)
  279. boost::throw_exception(bad_seek());
  280. return offset_to_position(pos_ - beg_);
  281. }
  282. if (next < beg_ || (end_ != -1 && next > end_))
  283. boost::throw_exception(bad_seek());
  284. pos_ = iostreams::seek(this->component(), next - pos_, BOOST_IOS::cur);
  285. return offset_to_position(pos_ - beg_);
  286. }
  287. //--------------Implementation of restricted_direct_device--------------------//
  288. template<typename Device>
  289. restricted_direct_device<Device>::restricted_direct_device
  290. (const Device& dev, stream_offset off, stream_offset len)
  291. : device_adapter<Device>(dev), beg_(0), end_(0)
  292. {
  293. std::pair<char_type*, char_type*> seq =
  294. sequence(is_convertible<category, input>());
  295. if ( off < 0 || len < -1 ||
  296. (len != -1 && off + len > seq.second - seq.first) )
  297. {
  298. boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset"));
  299. }
  300. beg_ = seq.first + off;
  301. end_ = len != -1 ?
  302. seq.first + off + len :
  303. seq.second;
  304. }
  305. template<typename Device>
  306. typename restricted_direct_device<Device>::pair_type
  307. restricted_direct_device<Device>::input_sequence()
  308. {
  309. BOOST_STATIC_ASSERT((is_convertible<category, input>::value));
  310. return std::make_pair(beg_, end_);
  311. }
  312. template<typename Device>
  313. typename restricted_direct_device<Device>::pair_type
  314. restricted_direct_device<Device>::output_sequence()
  315. {
  316. BOOST_STATIC_ASSERT((is_convertible<category, output>::value));
  317. return std::make_pair(beg_, end_);
  318. }
  319. template<typename Device>
  320. typename restricted_direct_device<Device>::pair_type
  321. restricted_direct_device<Device>::sequence(mpl::true_)
  322. { return iostreams::input_sequence(this->component()); }
  323. template<typename Device>
  324. typename restricted_direct_device<Device>::pair_type
  325. restricted_direct_device<Device>::sequence(mpl::false_)
  326. { return iostreams::output_sequence(this->component()); }
  327. //--------------Implementation of restricted_filter---------------------------//
  328. template<typename Filter>
  329. restricted_filter<Filter>::restricted_filter
  330. (const Filter& flt, stream_offset off, stream_offset len)
  331. : filter_adapter<Filter>(flt), beg_(off),
  332. pos_(off), end_(len != -1 ? off + len : -1), open_(false)
  333. {
  334. if (len < -1 || off < 0)
  335. boost::throw_exception(BOOST_IOSTREAMS_FAILURE("bad offset"));
  336. }
  337. } // End namespace detail.
  338. } } // End namespaces iostreams, boost.
  339. #elif defined(BOOST_IOSTREAMS_RESTRICT)
  340. namespace boost { namespace iostreams {
  341. //--------------Implementation of restrict/slice------------------------------//
  342. // Note: The following workarounds are patterned after resolve.hpp. It has not
  343. // yet been confirmed that they are necessary.
  344. # ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //------------------------//
  345. # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //------------------------------//
  346. template<typename T>
  347. restriction<T>
  348. BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
  349. BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
  350. { return restriction<T>(t, off, len); }
  351. template<typename Ch, typename Tr>
  352. restriction< std::basic_streambuf<Ch, Tr> >
  353. BOOST_IOSTREAMS_RESTRICT( std::basic_streambuf<Ch, Tr>& sb, stream_offset off,
  354. stream_offset len = -1 )
  355. { return restriction< std::basic_streambuf<Ch, Tr> >(sb, off, len); }
  356. template<typename Ch, typename Tr>
  357. restriction< std::basic_istream<Ch, Tr> >
  358. BOOST_IOSTREAMS_RESTRICT
  359. (std::basic_istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
  360. { return restriction< std::basic_istream<Ch, Tr> >(is, off, len); }
  361. template<typename Ch, typename Tr>
  362. restriction< std::basic_ostream<Ch, Tr> >
  363. BOOST_IOSTREAMS_RESTRICT
  364. (std::basic_ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
  365. { return restriction< std::basic_ostream<Ch, Tr> >(os, off, len); }
  366. template<typename Ch, typename Tr>
  367. restriction< std::basic_iostream<Ch, Tr> >
  368. BOOST_IOSTREAMS_RESTRICT
  369. (std::basic_iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
  370. { return restriction< std::basic_iostream<Ch, Tr> >(io, off, len); }
  371. # else // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //--------------------//
  372. template<typename T>
  373. restriction<T>
  374. BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
  375. BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
  376. { return restriction<T>(t, off, len); }
  377. restriction<std::streambuf>
  378. BOOST_IOSTREAMS_RESTRICT
  379. (std::streambuf& sb, stream_offset off, stream_offset len = -1)
  380. { return restriction<std::streambuf>(sb, off, len); }
  381. restriction<std::istream>
  382. BOOST_IOSTREAMS_RESTRICT
  383. (std::istream<Ch, Tr>& is, stream_offset off, stream_offset len = -1)
  384. { return restriction<std::istream>(is, off, len); }
  385. restriction<std::ostream>
  386. BOOST_IOSTREAMS_RESTRICT
  387. (std::ostream<Ch, Tr>& os, stream_offset off, stream_offset len = -1)
  388. { return restriction<std::ostream>(os, off, len); }
  389. restriction<std::iostream>
  390. BOOST_IOSTREAMS_RESTRICT
  391. (std::iostream<Ch, Tr>& io, stream_offset off, stream_offset len = -1)
  392. { return restriction<std::iostream>(io, off, len); }
  393. # endif // # ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES //-------------------//
  394. # else // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //---------------//
  395. template<typename T>
  396. restriction<T>
  397. BOOST_IOSTREAMS_RESTRICT
  398. (const T& t, stream_offset off, stream_offset len, mpl::true_)
  399. { // Bad overload resolution.
  400. return restriction<T>(const_cast<T&>(t, off, len));
  401. }
  402. template<typename T>
  403. restriction<T>
  404. BOOST_IOSTREAMS_RESTRICT
  405. (const T& t, stream_offset off, stream_offset len, mpl::false_)
  406. { return restriction<T>(t, off, len); }
  407. template<typename T>
  408. restriction<T>
  409. BOOST_IOSTREAMS_RESTRICT( const T& t, stream_offset off, stream_offset len = -1
  410. BOOST_IOSTREAMS_DISABLE_IF_STREAM(T) )
  411. { return BOOST_IOSTREAMS_RESTRICT(t, off, len, is_std_io<T>()); }
  412. # if !BOOST_WORKAROUND(__BORLANDC__, < 0x600) && \
  413. !defined(__GNUC__) // ---------------------------------------------------//
  414. template<typename T>
  415. restriction<T>
  416. BOOST_IOSTREAMS_RESTRICT(T& t, stream_offset off, stream_offset len = -1)
  417. { return restriction<T>(t, off, len); }
  418. # endif // Borland 5.x or GCC //-------------------------------//
  419. # endif // #ifndef BOOST_IOSTREAMS_BROKEN_OVERLOAD_RESOLUTION //--------------//
  420. } } // End namespaces iostreams, boost.
  421. # include <boost/iostreams/detail/config/enable_warnings.hpp>
  422. #endif // #if !defined(BOOST_IOSTREAMS_RESTRICT_IMPL_HPP_INCLUDED) ...