vectorstream.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. // This file comes from SGI's sstream file. Modified by Ion Gaztanaga 2005-2012.
  12. // Changed internal SGI string to a generic, templatized vector. Added efficient
  13. // internal buffer get/set/swap functions, so that we can obtain/establish the
  14. // internal buffer without any reallocation or copy. Kill those temporaries!
  15. ///////////////////////////////////////////////////////////////////////////////
  16. /*
  17. * Copyright (c) 1998
  18. * Silicon Graphics Computer Systems, Inc.
  19. *
  20. * Permission to use, copy, modify, distribute and sell this software
  21. * and its documentation for any purpose is hereby granted without fee,
  22. * provided that the above copyright notice appear in all copies and
  23. * that both that copyright notice and this permission notice appear
  24. * in supporting documentation. Silicon Graphics makes no
  25. * representations about the suitability of this software for any
  26. * purpose. It is provided "as is" without express or implied warranty.
  27. */
  28. //!\file
  29. //!This file defines basic_vectorbuf, basic_ivectorstream,
  30. //!basic_ovectorstream, and basic_vectorstreamclasses. These classes
  31. //!represent streamsbufs and streams whose sources or destinations are
  32. //!STL-like vectors that can be swapped with external vectors to avoid
  33. //!unnecessary allocations/copies.
  34. #ifndef BOOST_INTERPROCESS_VECTORSTREAM_HPP
  35. #define BOOST_INTERPROCESS_VECTORSTREAM_HPP
  36. #ifndef BOOST_CONFIG_HPP
  37. # include <boost/config.hpp>
  38. #endif
  39. #
  40. #if defined(BOOST_HAS_PRAGMA_ONCE)
  41. # pragma once
  42. #endif
  43. #include <boost/interprocess/detail/config_begin.hpp>
  44. #include <boost/interprocess/detail/workaround.hpp>
  45. #include <iosfwd>
  46. #include <ios>
  47. #include <istream>
  48. #include <ostream>
  49. #include <string> // char traits
  50. #include <cstddef> // ptrdiff_t
  51. #include <boost/interprocess/interprocess_fwd.hpp>
  52. #include <boost/assert.hpp>
  53. namespace boost { namespace interprocess {
  54. //!A streambuf class that controls the transmission of elements to and from
  55. //!a basic_ivectorstream, basic_ovectorstream or basic_vectorstream.
  56. //!It holds a character vector specified by CharVector template parameter
  57. //!as its formatting buffer. The vector must have contiguous storage, like
  58. //!std::vector, boost::interprocess::vector or boost::interprocess::basic_string
  59. template <class CharVector, class CharTraits>
  60. class basic_vectorbuf
  61. : public std::basic_streambuf<typename CharVector::value_type, CharTraits>
  62. {
  63. public:
  64. typedef CharVector vector_type;
  65. typedef typename CharVector::value_type char_type;
  66. typedef typename CharTraits::int_type int_type;
  67. typedef typename CharTraits::pos_type pos_type;
  68. typedef typename CharTraits::off_type off_type;
  69. typedef CharTraits traits_type;
  70. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  71. private:
  72. typedef std::basic_streambuf<char_type, traits_type> base_t;
  73. basic_vectorbuf(const basic_vectorbuf&);
  74. basic_vectorbuf & operator =(const basic_vectorbuf&);
  75. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  76. public:
  77. //!Constructor. Throws if vector_type default
  78. //!constructor throws.
  79. explicit basic_vectorbuf(std::ios_base::openmode mode
  80. = std::ios_base::in | std::ios_base::out)
  81. : base_t(), m_mode(mode)
  82. { this->initialize_pointers(); }
  83. //!Constructor. Throws if
  84. //!vector_type(const VectorParameter &param) throws.
  85. template<class VectorParameter>
  86. explicit basic_vectorbuf(const VectorParameter &param,
  87. std::ios_base::openmode mode
  88. = std::ios_base::in | std::ios_base::out)
  89. : base_t(), m_mode(mode), m_vect(param)
  90. { this->initialize_pointers(); }
  91. public:
  92. //!Swaps the underlying vector with the passed vector.
  93. //!This function resets the read/write position in the stream.
  94. //!Does not throw.
  95. void swap_vector(vector_type &vect)
  96. {
  97. if (this->m_mode & std::ios_base::out){
  98. //Update high water if necessary
  99. //And resize vector to remove extra size
  100. if (mp_high_water < base_t::pptr()){
  101. //Restore the vector's size if necessary
  102. mp_high_water = base_t::pptr();
  103. }
  104. //This does not reallocate
  105. m_vect.resize(mp_high_water - (m_vect.size() ? &m_vect[0] : 0));
  106. }
  107. //Now swap vector
  108. m_vect.swap(vect);
  109. this->initialize_pointers();
  110. }
  111. //!Returns a const reference to the internal vector.
  112. //!Does not throw.
  113. const vector_type &vector() const
  114. {
  115. if (this->m_mode & std::ios_base::out){
  116. if (mp_high_water < base_t::pptr()){
  117. //Restore the vector's size if necessary
  118. mp_high_water = base_t::pptr();
  119. }
  120. //This shouldn't reallocate
  121. typedef typename vector_type::size_type size_type;
  122. char_type *old_ptr = base_t::pbase();
  123. size_type high_pos = size_type(mp_high_water-old_ptr);
  124. if(m_vect.size() > high_pos){
  125. m_vect.resize(high_pos);
  126. //But we must update end write pointer because vector size is now shorter
  127. int old_pos = base_t::pptr() - base_t::pbase();
  128. const_cast<basic_vectorbuf*>(this)->base_t::setp(old_ptr, old_ptr + high_pos);
  129. const_cast<basic_vectorbuf*>(this)->base_t::pbump(old_pos);
  130. }
  131. }
  132. return m_vect;
  133. }
  134. //!Preallocates memory from the internal vector.
  135. //!Resets the stream to the first position.
  136. //!Throws if the internals vector's memory allocation throws.
  137. void reserve(typename vector_type::size_type size)
  138. {
  139. if (this->m_mode & std::ios_base::out && size > m_vect.size()){
  140. typename vector_type::difference_type write_pos = base_t::pptr() - base_t::pbase();
  141. typename vector_type::difference_type read_pos = base_t::gptr() - base_t::eback();
  142. //Now update pointer data
  143. m_vect.reserve(size);
  144. this->initialize_pointers();
  145. base_t::pbump((int)write_pos);
  146. if(this->m_mode & std::ios_base::in){
  147. base_t::gbump((int)read_pos);
  148. }
  149. }
  150. }
  151. //!Calls clear() method of the internal vector.
  152. //!Resets the stream to the first position.
  153. void clear()
  154. { m_vect.clear(); this->initialize_pointers(); }
  155. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  156. private:
  157. //Maximizes high watermark to the initial vector size,
  158. //initializes read and write iostream buffers to the capacity
  159. //and resets stream positions
  160. void initialize_pointers()
  161. {
  162. // The initial read position is the beginning of the vector.
  163. if(!(m_mode & std::ios_base::out)){
  164. if(m_vect.empty()){
  165. this->setg(0, 0, 0);
  166. }
  167. else{
  168. this->setg(&m_vect[0], &m_vect[0], &m_vect[0] + m_vect.size());
  169. }
  170. }
  171. // The initial write position is the beginning of the vector.
  172. if(m_mode & std::ios_base::out){
  173. //First get real size
  174. int real_size = (int)m_vect.size();
  175. //Then maximize size for high watermarking
  176. m_vect.resize(m_vect.capacity());
  177. BOOST_ASSERT(m_vect.size() == m_vect.capacity());
  178. //Set high watermarking with the expanded size
  179. mp_high_water = m_vect.size() ? (&m_vect[0] + real_size) : 0;
  180. //Now set formatting pointers
  181. if(m_vect.empty()){
  182. this->setp(0, 0);
  183. if(m_mode & std::ios_base::in)
  184. this->setg(0, 0, 0);
  185. }
  186. else{
  187. char_type *p = &m_vect[0];
  188. this->setp(p, p + m_vect.size());
  189. if(m_mode & std::ios_base::in)
  190. this->setg(p, p, p + real_size);
  191. }
  192. if (m_mode & (std::ios_base::app | std::ios_base::ate)){
  193. base_t::pbump((int)real_size);
  194. }
  195. }
  196. }
  197. protected:
  198. virtual int_type underflow()
  199. {
  200. if (base_t::gptr() == 0)
  201. return CharTraits::eof();
  202. if(m_mode & std::ios_base::out){
  203. if (mp_high_water < base_t::pptr())
  204. mp_high_water = base_t::pptr();
  205. if (base_t::egptr() < mp_high_water)
  206. base_t::setg(base_t::eback(), base_t::gptr(), mp_high_water);
  207. }
  208. if (base_t::gptr() < base_t::egptr())
  209. return CharTraits::to_int_type(*base_t::gptr());
  210. return CharTraits::eof();
  211. }
  212. virtual int_type pbackfail(int_type c = CharTraits::eof())
  213. {
  214. if(this->gptr() != this->eback()) {
  215. if(!CharTraits::eq_int_type(c, CharTraits::eof())) {
  216. if(CharTraits::eq(CharTraits::to_char_type(c), this->gptr()[-1])) {
  217. this->gbump(-1);
  218. return c;
  219. }
  220. else if(m_mode & std::ios_base::out) {
  221. this->gbump(-1);
  222. *this->gptr() = c;
  223. return c;
  224. }
  225. else
  226. return CharTraits::eof();
  227. }
  228. else {
  229. this->gbump(-1);
  230. return CharTraits::not_eof(c);
  231. }
  232. }
  233. else
  234. return CharTraits::eof();
  235. }
  236. virtual int_type overflow(int_type c = CharTraits::eof())
  237. {
  238. if(m_mode & std::ios_base::out) {
  239. if(!CharTraits::eq_int_type(c, CharTraits::eof())) {
  240. typedef typename vector_type::difference_type dif_t;
  241. //The new output position is the previous one plus one
  242. //because 'overflow' requires putting 'c' on the buffer
  243. dif_t new_outpos = base_t::pptr() - base_t::pbase() + 1;
  244. //Adjust high water if necessary
  245. dif_t hipos = mp_high_water - base_t::pbase();
  246. if (hipos < new_outpos)
  247. hipos = new_outpos;
  248. //Insert the new data
  249. m_vect.push_back(CharTraits::to_char_type(c));
  250. m_vect.resize(m_vect.capacity());
  251. BOOST_ASSERT(m_vect.size() == m_vect.capacity());
  252. char_type* p = const_cast<char_type*>(&m_vect[0]);
  253. //A reallocation might have happened, update pointers
  254. base_t::setp(p, p + (dif_t)m_vect.size());
  255. mp_high_water = p + hipos;
  256. if (m_mode & std::ios_base::in)
  257. base_t::setg(p, p + (base_t::gptr() - base_t::eback()), mp_high_water);
  258. //Update write position to the old position + 1
  259. base_t::pbump((int)new_outpos);
  260. return c;
  261. }
  262. else // c is EOF, so we don't have to do anything
  263. return CharTraits::not_eof(c);
  264. }
  265. else // Overflow always fails if it's read-only.
  266. return CharTraits::eof();
  267. }
  268. virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir,
  269. std::ios_base::openmode mode
  270. = std::ios_base::in | std::ios_base::out)
  271. {
  272. //Get seek mode
  273. bool in(0 != (mode & std::ios_base::in)), out(0 != (mode & std::ios_base::out));
  274. //Test for logic errors
  275. if(!in & !out)
  276. return pos_type(off_type(-1));
  277. else if((in && out) && (dir == std::ios_base::cur))
  278. return pos_type(off_type(-1));
  279. else if((in && (!(m_mode & std::ios_base::in) || (off != 0 && this->gptr() == 0) )) ||
  280. (out && (!(m_mode & std::ios_base::out) || (off != 0 && this->pptr() == 0))))
  281. return pos_type(off_type(-1));
  282. off_type newoff;
  283. //Just calculate the end of the stream. If the stream is read-only
  284. //the limit is the size of the vector. Otherwise, the high water mark
  285. //will mark the real size.
  286. off_type limit;
  287. if(m_mode & std::ios_base::out){
  288. //Update high water marking because pptr() is going to change and it might
  289. //have been updated since last overflow()
  290. if(mp_high_water < base_t::pptr())
  291. mp_high_water = base_t::pptr();
  292. //Update read limits in case high water mark was changed
  293. if(m_mode & std::ios_base::in){
  294. if (base_t::egptr() < mp_high_water)
  295. base_t::setg(base_t::eback(), base_t::gptr(), mp_high_water);
  296. }
  297. limit = static_cast<off_type>(mp_high_water - base_t::pbase());
  298. }
  299. else{
  300. limit = static_cast<off_type>(m_vect.size());
  301. }
  302. switch(dir) {
  303. case std::ios_base::beg:
  304. newoff = 0;
  305. break;
  306. case std::ios_base::end:
  307. newoff = limit;
  308. break;
  309. case std::ios_base::cur:
  310. newoff = in ? static_cast<std::streamoff>(this->gptr() - this->eback())
  311. : static_cast<std::streamoff>(this->pptr() - this->pbase());
  312. break;
  313. default:
  314. return pos_type(off_type(-1));
  315. }
  316. newoff += off;
  317. if (newoff < 0 || newoff > limit)
  318. return pos_type(-1);
  319. if (m_mode & std::ios_base::app && mode & std::ios_base::out && newoff != limit)
  320. return pos_type(-1);
  321. //This can reassign pointers
  322. //if(m_vect.size() != m_vect.capacity())
  323. //this->initialize_pointers();
  324. if (in)
  325. base_t::setg(base_t::eback(), base_t::eback() + newoff, base_t::egptr());
  326. if (out){
  327. base_t::setp(base_t::pbase(), base_t::epptr());
  328. base_t::pbump(newoff);
  329. }
  330. return pos_type(newoff);
  331. }
  332. virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode
  333. = std::ios_base::in | std::ios_base::out)
  334. { return seekoff(pos - pos_type(off_type(0)), std::ios_base::beg, mode); }
  335. private:
  336. std::ios_base::openmode m_mode;
  337. mutable vector_type m_vect;
  338. mutable char_type* mp_high_water;
  339. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  340. };
  341. //!A basic_istream class that holds a character vector specified by CharVector
  342. //!template parameter as its formatting buffer. The vector must have
  343. //!contiguous storage, like std::vector, boost::interprocess::vector or
  344. //!boost::interprocess::basic_string
  345. template <class CharVector, class CharTraits>
  346. class basic_ivectorstream
  347. : public std::basic_istream<typename CharVector::value_type, CharTraits>
  348. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  349. , private basic_vectorbuf<CharVector, CharTraits>
  350. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  351. {
  352. public:
  353. typedef CharVector vector_type;
  354. typedef typename std::basic_ios
  355. <typename CharVector::value_type, CharTraits>::char_type char_type;
  356. typedef typename std::basic_ios<char_type, CharTraits>::int_type int_type;
  357. typedef typename std::basic_ios<char_type, CharTraits>::pos_type pos_type;
  358. typedef typename std::basic_ios<char_type, CharTraits>::off_type off_type;
  359. typedef typename std::basic_ios<char_type, CharTraits>::traits_type traits_type;
  360. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  361. private:
  362. typedef basic_vectorbuf<CharVector, CharTraits> vectorbuf_t;
  363. typedef std::basic_ios<char_type, CharTraits> basic_ios_t;
  364. typedef std::basic_istream<char_type, CharTraits> base_t;
  365. vectorbuf_t & get_buf() { return *this; }
  366. const vectorbuf_t & get_buf() const{ return *this; }
  367. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  368. public:
  369. //!Constructor. Throws if vector_type default
  370. //!constructor throws.
  371. basic_ivectorstream(std::ios_base::openmode mode = std::ios_base::in)
  372. : base_t(0) //Initializes first the base class to safely init the virtual basic_ios base
  373. //(via basic_ios::init() call in base_t's constructor) without the risk of a
  374. //previous throwing vectorbuf constructor. Set the streambuf after risk has gone.
  375. , vectorbuf_t(mode | std::ios_base::in)
  376. { this->base_t::rdbuf(&get_buf()); }
  377. //!Constructor. Throws if vector_type(const VectorParameter &param)
  378. //!throws.
  379. template<class VectorParameter>
  380. basic_ivectorstream(const VectorParameter &param,
  381. std::ios_base::openmode mode = std::ios_base::in)
  382. : vectorbuf_t(param, mode | std::ios_base::in)
  383. //basic_ios_t() is constructed uninitialized as virtual base
  384. //and initialized inside base_t calling basic_ios::init()
  385. , base_t(&get_buf())
  386. {}
  387. public:
  388. //!Returns the address of the stored
  389. //!stream buffer.
  390. basic_vectorbuf<CharVector, CharTraits>* rdbuf() const
  391. { return const_cast<basic_vectorbuf<CharVector, CharTraits>*>(&get_buf()); }
  392. //!Swaps the underlying vector with the passed vector.
  393. //!This function resets the read position in the stream.
  394. //!Does not throw.
  395. void swap_vector(vector_type &vect)
  396. { get_buf().swap_vector(vect); }
  397. //!Returns a const reference to the internal vector.
  398. //!Does not throw.
  399. const vector_type &vector() const
  400. { return get_buf().vector(); }
  401. //!Calls reserve() method of the internal vector.
  402. //!Resets the stream to the first position.
  403. //!Throws if the internals vector's reserve throws.
  404. void reserve(typename vector_type::size_type size)
  405. { get_buf().reserve(size); }
  406. //!Calls clear() method of the internal vector.
  407. //!Resets the stream to the first position.
  408. void clear()
  409. { get_buf().clear(); }
  410. };
  411. //!A basic_ostream class that holds a character vector specified by CharVector
  412. //!template parameter as its formatting buffer. The vector must have
  413. //!contiguous storage, like std::vector, boost::interprocess::vector or
  414. //!boost::interprocess::basic_string
  415. template <class CharVector, class CharTraits>
  416. class basic_ovectorstream
  417. : public std::basic_ostream<typename CharVector::value_type, CharTraits>
  418. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  419. , private basic_vectorbuf<CharVector, CharTraits>
  420. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  421. {
  422. public:
  423. typedef CharVector vector_type;
  424. typedef typename std::basic_ios
  425. <typename CharVector::value_type, CharTraits>::char_type char_type;
  426. typedef typename std::basic_ios<char_type, CharTraits>::int_type int_type;
  427. typedef typename std::basic_ios<char_type, CharTraits>::pos_type pos_type;
  428. typedef typename std::basic_ios<char_type, CharTraits>::off_type off_type;
  429. typedef typename std::basic_ios<char_type, CharTraits>::traits_type traits_type;
  430. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  431. private:
  432. typedef basic_vectorbuf<CharVector, CharTraits> vectorbuf_t;
  433. typedef std::basic_ios<char_type, CharTraits> basic_ios_t;
  434. typedef std::basic_ostream<char_type, CharTraits> base_t;
  435. vectorbuf_t & get_buf() { return *this; }
  436. const vectorbuf_t & get_buf()const { return *this; }
  437. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  438. public:
  439. //!Constructor. Throws if vector_type default
  440. //!constructor throws.
  441. basic_ovectorstream(std::ios_base::openmode mode = std::ios_base::out)
  442. : base_t(0) //Initializes first the base class to safely init the virtual basic_ios base
  443. //(via basic_ios::init() call in base_t's constructor) without the risk of a
  444. //previous throwing vectorbuf constructor. Set the streambuf after risk has gone.
  445. , vectorbuf_t(mode | std::ios_base::out)
  446. { this->base_t::rdbuf(&get_buf()); }
  447. //!Constructor. Throws if vector_type(const VectorParameter &param)
  448. //!throws.
  449. template<class VectorParameter>
  450. basic_ovectorstream(const VectorParameter &param,
  451. std::ios_base::openmode mode = std::ios_base::out)
  452. : base_t(0) //Initializes first the base class to safely init the virtual basic_ios base
  453. //(via basic_ios::init() call in base_t's constructor) without the risk of a
  454. //previous throwing vectorbuf constructor. Set the streambuf after risk has gone.
  455. , vectorbuf_t(param, mode | std::ios_base::out)
  456. { this->base_t::rdbuf(&get_buf()); }
  457. public:
  458. //!Returns the address of the stored
  459. //!stream buffer.
  460. basic_vectorbuf<CharVector, CharTraits>* rdbuf() const
  461. { return const_cast<basic_vectorbuf<CharVector, CharTraits>*>(&get_buf()); }
  462. //!Swaps the underlying vector with the passed vector.
  463. //!This function resets the write position in the stream.
  464. //!Does not throw.
  465. void swap_vector(vector_type &vect)
  466. { get_buf().swap_vector(vect); }
  467. //!Returns a const reference to the internal vector.
  468. //!Does not throw.
  469. const vector_type &vector() const
  470. { return get_buf().vector(); }
  471. //!Calls reserve() method of the internal vector.
  472. //!Resets the stream to the first position.
  473. //!Throws if the internals vector's reserve throws.
  474. void reserve(typename vector_type::size_type size)
  475. { get_buf().reserve(size); }
  476. };
  477. //!A basic_iostream class that holds a character vector specified by CharVector
  478. //!template parameter as its formatting buffer. The vector must have
  479. //!contiguous storage, like std::vector, boost::interprocess::vector or
  480. //!boost::interprocess::basic_string
  481. template <class CharVector, class CharTraits>
  482. class basic_vectorstream
  483. : public std::basic_iostream<typename CharVector::value_type, CharTraits>
  484. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  485. , private basic_vectorbuf<CharVector, CharTraits>
  486. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  487. {
  488. public:
  489. typedef CharVector vector_type;
  490. typedef typename std::basic_ios
  491. <typename CharVector::value_type, CharTraits>::char_type char_type;
  492. typedef typename std::basic_ios<char_type, CharTraits>::int_type int_type;
  493. typedef typename std::basic_ios<char_type, CharTraits>::pos_type pos_type;
  494. typedef typename std::basic_ios<char_type, CharTraits>::off_type off_type;
  495. typedef typename std::basic_ios<char_type, CharTraits>::traits_type traits_type;
  496. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  497. private:
  498. typedef basic_vectorbuf<CharVector, CharTraits> vectorbuf_t;
  499. typedef std::basic_ios<char_type, CharTraits> basic_ios_t;
  500. typedef std::basic_iostream<char_type, CharTraits> base_t;
  501. vectorbuf_t & get_buf() { return *this; }
  502. const vectorbuf_t & get_buf() const{ return *this; }
  503. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  504. public:
  505. //!Constructor. Throws if vector_type default
  506. //!constructor throws.
  507. basic_vectorstream(std::ios_base::openmode mode
  508. = std::ios_base::in | std::ios_base::out)
  509. : base_t(0) //Initializes first the base class to safely init the virtual basic_ios base
  510. //(via basic_ios::init() call in base_t's constructor) without the risk of a
  511. //previous throwing vectorbuf constructor. Set the streambuf after risk has gone.
  512. , vectorbuf_t(mode)
  513. { this->base_t::rdbuf(&get_buf()); }
  514. //!Constructor. Throws if vector_type(const VectorParameter &param)
  515. //!throws.
  516. template<class VectorParameter>
  517. basic_vectorstream(const VectorParameter &param, std::ios_base::openmode mode
  518. = std::ios_base::in | std::ios_base::out)
  519. : base_t(0) //Initializes first the base class to safely init the virtual basic_ios base
  520. //(via basic_ios::init() call in base_t's constructor) without the risk of a
  521. //previous throwing vectorbuf constructor. Set the streambuf after risk has gone.
  522. , vectorbuf_t(param, mode)
  523. { this->base_t::rdbuf(&get_buf()); }
  524. public:
  525. //Returns the address of the stored stream buffer.
  526. basic_vectorbuf<CharVector, CharTraits>* rdbuf() const
  527. { return const_cast<basic_vectorbuf<CharVector, CharTraits>*>(&get_buf()); }
  528. //!Swaps the underlying vector with the passed vector.
  529. //!This function resets the read/write position in the stream.
  530. //!Does not throw.
  531. void swap_vector(vector_type &vect)
  532. { get_buf().swap_vector(vect); }
  533. //!Returns a const reference to the internal vector.
  534. //!Does not throw.
  535. const vector_type &vector() const
  536. { return get_buf().vector(); }
  537. //!Calls reserve() method of the internal vector.
  538. //!Resets the stream to the first position.
  539. //!Throws if the internals vector's reserve throws.
  540. void reserve(typename vector_type::size_type size)
  541. { get_buf().reserve(size); }
  542. //!Calls clear() method of the internal vector.
  543. //!Resets the stream to the first position.
  544. void clear()
  545. { get_buf().clear(); }
  546. };
  547. //Some typedefs to simplify usage
  548. //!
  549. //!typedef basic_vectorbuf<std::vector<char> > vectorbuf;
  550. //!typedef basic_vectorstream<std::vector<char> > vectorstream;
  551. //!typedef basic_ivectorstream<std::vector<char> > ivectorstream;
  552. //!typedef basic_ovectorstream<std::vector<char> > ovectorstream;
  553. //!
  554. //!typedef basic_vectorbuf<std::vector<wchar_t> > wvectorbuf;
  555. //!typedef basic_vectorstream<std::vector<wchar_t> > wvectorstream;
  556. //!typedef basic_ivectorstream<std::vector<wchar_t> > wivectorstream;
  557. //!typedef basic_ovectorstream<std::vector<wchar_t> > wovectorstream;
  558. }} //namespace boost { namespace interprocess {
  559. #include <boost/interprocess/detail/config_end.hpp>
  560. #endif /* BOOST_INTERPROCESS_VECTORSTREAM_HPP */