buffer.hpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. //
  2. // buffer.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_BUFFER_HPP
  11. #define BOOST_ASIO_BUFFER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include <boost/array.hpp>
  20. #include <boost/type_traits/is_const.hpp>
  21. #include <string>
  22. #include <vector>
  23. #include <boost/asio/detail/pop_options.hpp>
  24. #if defined(BOOST_MSVC)
  25. # if defined(_HAS_ITERATOR_DEBUGGING) && (_HAS_ITERATOR_DEBUGGING != 0)
  26. # if !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING)
  27. # define BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  28. # endif // !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING)
  29. # endif // defined(_HAS_ITERATOR_DEBUGGING)
  30. #endif // defined(BOOST_MSVC)
  31. #if defined(__GNUC__)
  32. # if defined(_GLIBCXX_DEBUG)
  33. # if !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING)
  34. # define BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  35. # endif // !defined(BOOST_ASIO_DISABLE_BUFFER_DEBUGGING)
  36. # endif // defined(_GLIBCXX_DEBUG)
  37. #endif // defined(__GNUC__)
  38. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  39. # include <boost/asio/detail/push_options.hpp>
  40. # include <boost/function.hpp>
  41. # include <boost/asio/detail/pop_options.hpp>
  42. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  43. namespace boost {
  44. namespace asio {
  45. class mutable_buffer;
  46. class const_buffer;
  47. namespace detail {
  48. void* buffer_cast_helper(const mutable_buffer&);
  49. const void* buffer_cast_helper(const const_buffer&);
  50. std::size_t buffer_size_helper(const mutable_buffer&);
  51. std::size_t buffer_size_helper(const const_buffer&);
  52. } // namespace detail
  53. /// Holds a buffer that can be modified.
  54. /**
  55. * The mutable_buffer class provides a safe representation of a buffer that can
  56. * be modified. It does not own the underlying data, and so is cheap to copy or
  57. * assign.
  58. */
  59. class mutable_buffer
  60. {
  61. public:
  62. /// Construct an empty buffer.
  63. mutable_buffer()
  64. : data_(0),
  65. size_(0)
  66. {
  67. }
  68. /// Construct a buffer to represent a given memory range.
  69. mutable_buffer(void* data, std::size_t size)
  70. : data_(data),
  71. size_(size)
  72. {
  73. }
  74. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  75. mutable_buffer(void* data, std::size_t size,
  76. boost::function<void()> debug_check)
  77. : data_(data),
  78. size_(size),
  79. debug_check_(debug_check)
  80. {
  81. }
  82. const boost::function<void()>& get_debug_check() const
  83. {
  84. return debug_check_;
  85. }
  86. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  87. private:
  88. friend void* boost::asio::detail::buffer_cast_helper(
  89. const mutable_buffer& b);
  90. friend std::size_t boost::asio::detail::buffer_size_helper(
  91. const mutable_buffer& b);
  92. void* data_;
  93. std::size_t size_;
  94. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  95. boost::function<void()> debug_check_;
  96. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  97. };
  98. namespace detail {
  99. inline void* buffer_cast_helper(const mutable_buffer& b)
  100. {
  101. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  102. if (b.size_ && b.debug_check_)
  103. b.debug_check_();
  104. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  105. return b.data_;
  106. }
  107. inline std::size_t buffer_size_helper(const mutable_buffer& b)
  108. {
  109. return b.size_;
  110. }
  111. } // namespace detail
  112. /// Cast a non-modifiable buffer to a specified pointer to POD type.
  113. /**
  114. * @relates mutable_buffer
  115. */
  116. template <typename PointerToPodType>
  117. inline PointerToPodType buffer_cast(const mutable_buffer& b)
  118. {
  119. return static_cast<PointerToPodType>(detail::buffer_cast_helper(b));
  120. }
  121. /// Get the number of bytes in a non-modifiable buffer.
  122. /**
  123. * @relates mutable_buffer
  124. */
  125. inline std::size_t buffer_size(const mutable_buffer& b)
  126. {
  127. return detail::buffer_size_helper(b);
  128. }
  129. /// Create a new modifiable buffer that is offset from the start of another.
  130. /**
  131. * @relates mutable_buffer
  132. */
  133. inline mutable_buffer operator+(const mutable_buffer& b, std::size_t start)
  134. {
  135. if (start > buffer_size(b))
  136. return mutable_buffer();
  137. char* new_data = buffer_cast<char*>(b) + start;
  138. std::size_t new_size = buffer_size(b) - start;
  139. return mutable_buffer(new_data, new_size
  140. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  141. , b.get_debug_check()
  142. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  143. );
  144. }
  145. /// Create a new modifiable buffer that is offset from the start of another.
  146. /**
  147. * @relates mutable_buffer
  148. */
  149. inline mutable_buffer operator+(std::size_t start, const mutable_buffer& b)
  150. {
  151. if (start > buffer_size(b))
  152. return mutable_buffer();
  153. char* new_data = buffer_cast<char*>(b) + start;
  154. std::size_t new_size = buffer_size(b) - start;
  155. return mutable_buffer(new_data, new_size
  156. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  157. , b.get_debug_check()
  158. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  159. );
  160. }
  161. /// Adapts a single modifiable buffer so that it meets the requirements of the
  162. /// MutableBufferSequence concept.
  163. class mutable_buffers_1
  164. : public mutable_buffer
  165. {
  166. public:
  167. /// The type for each element in the list of buffers.
  168. typedef mutable_buffer value_type;
  169. /// A random-access iterator type that may be used to read elements.
  170. typedef const mutable_buffer* const_iterator;
  171. /// Construct to represent a given memory range.
  172. mutable_buffers_1(void* data, std::size_t size)
  173. : mutable_buffer(data, size)
  174. {
  175. }
  176. /// Construct to represent a single modifiable buffer.
  177. explicit mutable_buffers_1(const mutable_buffer& b)
  178. : mutable_buffer(b)
  179. {
  180. }
  181. /// Get a random-access iterator to the first element.
  182. const_iterator begin() const
  183. {
  184. return this;
  185. }
  186. /// Get a random-access iterator for one past the last element.
  187. const_iterator end() const
  188. {
  189. return begin() + 1;
  190. }
  191. };
  192. /// Holds a buffer that cannot be modified.
  193. /**
  194. * The const_buffer class provides a safe representation of a buffer that cannot
  195. * be modified. It does not own the underlying data, and so is cheap to copy or
  196. * assign.
  197. */
  198. class const_buffer
  199. {
  200. public:
  201. /// Construct an empty buffer.
  202. const_buffer()
  203. : data_(0),
  204. size_(0)
  205. {
  206. }
  207. /// Construct a buffer to represent a given memory range.
  208. const_buffer(const void* data, std::size_t size)
  209. : data_(data),
  210. size_(size)
  211. {
  212. }
  213. /// Construct a non-modifiable buffer from a modifiable one.
  214. const_buffer(const mutable_buffer& b)
  215. : data_(boost::asio::detail::buffer_cast_helper(b)),
  216. size_(boost::asio::detail::buffer_size_helper(b))
  217. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  218. , debug_check_(b.get_debug_check())
  219. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  220. {
  221. }
  222. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  223. const_buffer(const void* data, std::size_t size,
  224. boost::function<void()> debug_check)
  225. : data_(data),
  226. size_(size),
  227. debug_check_(debug_check)
  228. {
  229. }
  230. const boost::function<void()>& get_debug_check() const
  231. {
  232. return debug_check_;
  233. }
  234. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  235. private:
  236. friend const void* boost::asio::detail::buffer_cast_helper(
  237. const const_buffer& b);
  238. friend std::size_t boost::asio::detail::buffer_size_helper(
  239. const const_buffer& b);
  240. const void* data_;
  241. std::size_t size_;
  242. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  243. boost::function<void()> debug_check_;
  244. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  245. };
  246. namespace detail {
  247. inline const void* buffer_cast_helper(const const_buffer& b)
  248. {
  249. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  250. if (b.size_ && b.debug_check_)
  251. b.debug_check_();
  252. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  253. return b.data_;
  254. }
  255. inline std::size_t buffer_size_helper(const const_buffer& b)
  256. {
  257. return b.size_;
  258. }
  259. } // namespace detail
  260. /// Cast a non-modifiable buffer to a specified pointer to POD type.
  261. /**
  262. * @relates const_buffer
  263. */
  264. template <typename PointerToPodType>
  265. inline PointerToPodType buffer_cast(const const_buffer& b)
  266. {
  267. return static_cast<PointerToPodType>(detail::buffer_cast_helper(b));
  268. }
  269. /// Get the number of bytes in a non-modifiable buffer.
  270. /**
  271. * @relates const_buffer
  272. */
  273. inline std::size_t buffer_size(const const_buffer& b)
  274. {
  275. return detail::buffer_size_helper(b);
  276. }
  277. /// Create a new non-modifiable buffer that is offset from the start of another.
  278. /**
  279. * @relates const_buffer
  280. */
  281. inline const_buffer operator+(const const_buffer& b, std::size_t start)
  282. {
  283. if (start > buffer_size(b))
  284. return const_buffer();
  285. const char* new_data = buffer_cast<const char*>(b) + start;
  286. std::size_t new_size = buffer_size(b) - start;
  287. return const_buffer(new_data, new_size
  288. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  289. , b.get_debug_check()
  290. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  291. );
  292. }
  293. /// Create a new non-modifiable buffer that is offset from the start of another.
  294. /**
  295. * @relates const_buffer
  296. */
  297. inline const_buffer operator+(std::size_t start, const const_buffer& b)
  298. {
  299. if (start > buffer_size(b))
  300. return const_buffer();
  301. const char* new_data = buffer_cast<const char*>(b) + start;
  302. std::size_t new_size = buffer_size(b) - start;
  303. return const_buffer(new_data, new_size
  304. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  305. , b.get_debug_check()
  306. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  307. );
  308. }
  309. /// Adapts a single non-modifiable buffer so that it meets the requirements of
  310. /// the ConstBufferSequence concept.
  311. class const_buffers_1
  312. : public const_buffer
  313. {
  314. public:
  315. /// The type for each element in the list of buffers.
  316. typedef const_buffer value_type;
  317. /// A random-access iterator type that may be used to read elements.
  318. typedef const const_buffer* const_iterator;
  319. /// Construct to represent a given memory range.
  320. const_buffers_1(const void* data, std::size_t size)
  321. : const_buffer(data, size)
  322. {
  323. }
  324. /// Construct to represent a single non-modifiable buffer.
  325. explicit const_buffers_1(const const_buffer& b)
  326. : const_buffer(b)
  327. {
  328. }
  329. /// Get a random-access iterator to the first element.
  330. const_iterator begin() const
  331. {
  332. return this;
  333. }
  334. /// Get a random-access iterator for one past the last element.
  335. const_iterator end() const
  336. {
  337. return begin() + 1;
  338. }
  339. };
  340. /// An implementation of both the ConstBufferSequence and MutableBufferSequence
  341. /// concepts to represent a null buffer sequence.
  342. class null_buffers
  343. {
  344. public:
  345. /// The type for each element in the list of buffers.
  346. typedef mutable_buffer value_type;
  347. /// A random-access iterator type that may be used to read elements.
  348. typedef const mutable_buffer* const_iterator;
  349. /// Get a random-access iterator to the first element.
  350. const_iterator begin() const
  351. {
  352. return &buf_;
  353. }
  354. /// Get a random-access iterator for one past the last element.
  355. const_iterator end() const
  356. {
  357. return &buf_;
  358. }
  359. private:
  360. mutable_buffer buf_;
  361. };
  362. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  363. namespace detail {
  364. template <typename Iterator>
  365. class buffer_debug_check
  366. {
  367. public:
  368. buffer_debug_check(Iterator iter)
  369. : iter_(iter)
  370. {
  371. }
  372. ~buffer_debug_check()
  373. {
  374. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  375. // MSVC's string iterator checking may crash in a std::string::iterator
  376. // object's destructor when the iterator points to an already-destroyed
  377. // std::string object, unless the iterator is cleared first.
  378. iter_ = Iterator();
  379. #endif // BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  380. }
  381. void operator()()
  382. {
  383. *iter_;
  384. }
  385. private:
  386. Iterator iter_;
  387. };
  388. } // namespace detail
  389. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  390. /** @defgroup buffer boost::asio::buffer
  391. *
  392. * @brief The boost::asio::buffer function is used to create a buffer object to
  393. * represent raw memory, an array of POD elements, a vector of POD elements,
  394. * or a std::string.
  395. *
  396. * A buffer object represents a contiguous region of memory as a 2-tuple
  397. * consisting of a pointer and size in bytes. A tuple of the form <tt>{void*,
  398. * size_t}</tt> specifies a mutable (modifiable) region of memory. Similarly, a
  399. * tuple of the form <tt>{const void*, size_t}</tt> specifies a const
  400. * (non-modifiable) region of memory. These two forms correspond to the classes
  401. * mutable_buffer and const_buffer, respectively. To mirror C++'s conversion
  402. * rules, a mutable_buffer is implicitly convertible to a const_buffer, and the
  403. * opposite conversion is not permitted.
  404. *
  405. * The simplest use case involves reading or writing a single buffer of a
  406. * specified size:
  407. *
  408. * @code sock.send(boost::asio::buffer(data, size)); @endcode
  409. *
  410. * In the above example, the return value of boost::asio::buffer meets the
  411. * requirements of the ConstBufferSequence concept so that it may be directly
  412. * passed to the socket's write function. A buffer created for modifiable
  413. * memory also meets the requirements of the MutableBufferSequence concept.
  414. *
  415. * An individual buffer may be created from a builtin array, std::vector or
  416. * boost::array of POD elements. This helps prevent buffer overruns by
  417. * automatically determining the size of the buffer:
  418. *
  419. * @code char d1[128];
  420. * size_t bytes_transferred = sock.receive(boost::asio::buffer(d1));
  421. *
  422. * std::vector<char> d2(128);
  423. * bytes_transferred = sock.receive(boost::asio::buffer(d2));
  424. *
  425. * boost::array<char, 128> d3;
  426. * bytes_transferred = sock.receive(boost::asio::buffer(d3)); @endcode
  427. *
  428. * In all three cases above, the buffers created are exactly 128 bytes long.
  429. * Note that a vector is @e never automatically resized when creating or using
  430. * a buffer. The buffer size is determined using the vector's <tt>size()</tt>
  431. * member function, and not its capacity.
  432. *
  433. * @par Accessing Buffer Contents
  434. *
  435. * The contents of a buffer may be accessed using the boost::asio::buffer_size
  436. * and boost::asio::buffer_cast functions:
  437. *
  438. * @code boost::asio::mutable_buffer b1 = ...;
  439. * std::size_t s1 = boost::asio::buffer_size(b1);
  440. * unsigned char* p1 = boost::asio::buffer_cast<unsigned char*>(b1);
  441. *
  442. * boost::asio::const_buffer b2 = ...;
  443. * std::size_t s2 = boost::asio::buffer_size(b2);
  444. * const void* p2 = boost::asio::buffer_cast<const void*>(b2); @endcode
  445. *
  446. * The boost::asio::buffer_cast function permits violations of type safety, so
  447. * uses of it in application code should be carefully considered.
  448. *
  449. * @par Buffer Invalidation
  450. *
  451. * A buffer object does not have any ownership of the memory it refers to. It
  452. * is the responsibility of the application to ensure the memory region remains
  453. * valid until it is no longer required for an I/O operation. When the memory
  454. * is no longer available, the buffer is said to have been invalidated.
  455. *
  456. * For the boost::asio::buffer overloads that accept an argument of type
  457. * std::vector, the buffer objects returned are invalidated by any vector
  458. * operation that also invalidates all references, pointers and iterators
  459. * referring to the elements in the sequence (C++ Std, 23.2.4)
  460. *
  461. * For the boost::asio::buffer overloads that accept an argument of type
  462. * std::string, the buffer objects returned are invalidated according to the
  463. * rules defined for invalidation of references, pointers and iterators
  464. * referring to elements of the sequence (C++ Std, 21.3).
  465. *
  466. * @par Buffer Arithmetic
  467. *
  468. * Buffer objects may be manipulated using simple arithmetic in a safe way
  469. * which helps prevent buffer overruns. Consider an array initialised as
  470. * follows:
  471. *
  472. * @code boost::array<char, 6> a = { 'a', 'b', 'c', 'd', 'e' }; @endcode
  473. *
  474. * A buffer object @c b1 created using:
  475. *
  476. * @code b1 = boost::asio::buffer(a); @endcode
  477. *
  478. * represents the entire array, <tt>{ 'a', 'b', 'c', 'd', 'e' }</tt>. An
  479. * optional second argument to the boost::asio::buffer function may be used to
  480. * limit the size, in bytes, of the buffer:
  481. *
  482. * @code b2 = boost::asio::buffer(a, 3); @endcode
  483. *
  484. * such that @c b2 represents the data <tt>{ 'a', 'b', 'c' }</tt>. Even if the
  485. * size argument exceeds the actual size of the array, the size of the buffer
  486. * object created will be limited to the array size.
  487. *
  488. * An offset may be applied to an existing buffer to create a new one:
  489. *
  490. * @code b3 = b1 + 2; @endcode
  491. *
  492. * where @c b3 will set to represent <tt>{ 'c', 'd', 'e' }</tt>. If the offset
  493. * exceeds the size of the existing buffer, the newly created buffer will be
  494. * empty.
  495. *
  496. * Both an offset and size may be specified to create a buffer that corresponds
  497. * to a specific range of bytes within an existing buffer:
  498. *
  499. * @code b4 = boost::asio::buffer(b1 + 1, 3); @endcode
  500. *
  501. * so that @c b4 will refer to the bytes <tt>{ 'b', 'c', 'd' }</tt>.
  502. *
  503. * @par Buffers and Scatter-Gather I/O
  504. *
  505. * To read or write using multiple buffers (i.e. scatter-gather I/O), multiple
  506. * buffer objects may be assigned into a container that supports the
  507. * MutableBufferSequence (for read) or ConstBufferSequence (for write) concepts:
  508. *
  509. * @code
  510. * char d1[128];
  511. * std::vector<char> d2(128);
  512. * boost::array<char, 128> d3;
  513. *
  514. * boost::array<mutable_buffer, 3> bufs1 = {
  515. * boost::asio::buffer(d1),
  516. * boost::asio::buffer(d2),
  517. * boost::asio::buffer(d3) };
  518. * bytes_transferred = sock.receive(bufs1);
  519. *
  520. * std::vector<const_buffer> bufs2;
  521. * bufs2.push_back(boost::asio::buffer(d1));
  522. * bufs2.push_back(boost::asio::buffer(d2));
  523. * bufs2.push_back(boost::asio::buffer(d3));
  524. * bytes_transferred = sock.send(bufs2); @endcode
  525. */
  526. /*@{*/
  527. /// Create a new modifiable buffer from an existing buffer.
  528. /**
  529. * @returns <tt>mutable_buffers_1(b)</tt>.
  530. */
  531. inline mutable_buffers_1 buffer(const mutable_buffer& b)
  532. {
  533. return mutable_buffers_1(b);
  534. }
  535. /// Create a new modifiable buffer from an existing buffer.
  536. /**
  537. * @returns A mutable_buffers_1 value equivalent to:
  538. * @code mutable_buffers_1(
  539. * buffer_cast<void*>(b),
  540. * min(buffer_size(b), max_size_in_bytes)); @endcode
  541. */
  542. inline mutable_buffers_1 buffer(const mutable_buffer& b,
  543. std::size_t max_size_in_bytes)
  544. {
  545. return mutable_buffers_1(
  546. mutable_buffer(buffer_cast<void*>(b),
  547. buffer_size(b) < max_size_in_bytes
  548. ? buffer_size(b) : max_size_in_bytes
  549. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  550. , b.get_debug_check()
  551. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  552. ));
  553. }
  554. /// Create a new non-modifiable buffer from an existing buffer.
  555. /**
  556. * @returns <tt>const_buffers_1(b)</tt>.
  557. */
  558. inline const_buffers_1 buffer(const const_buffer& b)
  559. {
  560. return const_buffers_1(b);
  561. }
  562. /// Create a new non-modifiable buffer from an existing buffer.
  563. /**
  564. * @returns A const_buffers_1 value equivalent to:
  565. * @code const_buffers_1(
  566. * buffer_cast<const void*>(b),
  567. * min(buffer_size(b), max_size_in_bytes)); @endcode
  568. */
  569. inline const_buffers_1 buffer(const const_buffer& b,
  570. std::size_t max_size_in_bytes)
  571. {
  572. return const_buffers_1(
  573. const_buffer(buffer_cast<const void*>(b),
  574. buffer_size(b) < max_size_in_bytes
  575. ? buffer_size(b) : max_size_in_bytes
  576. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  577. , b.get_debug_check()
  578. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  579. ));
  580. }
  581. /// Create a new modifiable buffer that represents the given memory range.
  582. /**
  583. * @returns <tt>mutable_buffers_1(data, size_in_bytes)</tt>.
  584. */
  585. inline mutable_buffers_1 buffer(void* data, std::size_t size_in_bytes)
  586. {
  587. return mutable_buffers_1(mutable_buffer(data, size_in_bytes));
  588. }
  589. /// Create a new non-modifiable buffer that represents the given memory range.
  590. /**
  591. * @returns <tt>const_buffers_1(data, size_in_bytes)</tt>.
  592. */
  593. inline const_buffers_1 buffer(const void* data,
  594. std::size_t size_in_bytes)
  595. {
  596. return const_buffers_1(const_buffer(data, size_in_bytes));
  597. }
  598. /// Create a new modifiable buffer that represents the given POD array.
  599. /**
  600. * @returns A mutable_buffers_1 value equivalent to:
  601. * @code mutable_buffers_1(
  602. * static_cast<void*>(data),
  603. * N * sizeof(PodType)); @endcode
  604. */
  605. template <typename PodType, std::size_t N>
  606. inline mutable_buffers_1 buffer(PodType (&data)[N])
  607. {
  608. return mutable_buffers_1(mutable_buffer(data, N * sizeof(PodType)));
  609. }
  610. /// Create a new modifiable buffer that represents the given POD array.
  611. /**
  612. * @returns A mutable_buffers_1 value equivalent to:
  613. * @code mutable_buffers_1(
  614. * static_cast<void*>(data),
  615. * min(N * sizeof(PodType), max_size_in_bytes)); @endcode
  616. */
  617. template <typename PodType, std::size_t N>
  618. inline mutable_buffers_1 buffer(PodType (&data)[N],
  619. std::size_t max_size_in_bytes)
  620. {
  621. return mutable_buffers_1(
  622. mutable_buffer(data,
  623. N * sizeof(PodType) < max_size_in_bytes
  624. ? N * sizeof(PodType) : max_size_in_bytes));
  625. }
  626. /// Create a new non-modifiable buffer that represents the given POD array.
  627. /**
  628. * @returns A const_buffers_1 value equivalent to:
  629. * @code const_buffers_1(
  630. * static_cast<const void*>(data),
  631. * N * sizeof(PodType)); @endcode
  632. */
  633. template <typename PodType, std::size_t N>
  634. inline const_buffers_1 buffer(const PodType (&data)[N])
  635. {
  636. return const_buffers_1(const_buffer(data, N * sizeof(PodType)));
  637. }
  638. /// Create a new non-modifiable buffer that represents the given POD array.
  639. /**
  640. * @returns A const_buffers_1 value equivalent to:
  641. * @code const_buffers_1(
  642. * static_cast<const void*>(data),
  643. * min(N * sizeof(PodType), max_size_in_bytes)); @endcode
  644. */
  645. template <typename PodType, std::size_t N>
  646. inline const_buffers_1 buffer(const PodType (&data)[N],
  647. std::size_t max_size_in_bytes)
  648. {
  649. return const_buffers_1(
  650. const_buffer(data,
  651. N * sizeof(PodType) < max_size_in_bytes
  652. ? N * sizeof(PodType) : max_size_in_bytes));
  653. }
  654. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) \
  655. || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  656. // Borland C++ and Sun Studio think the overloads:
  657. //
  658. // unspecified buffer(boost::array<PodType, N>& array ...);
  659. //
  660. // and
  661. //
  662. // unspecified buffer(boost::array<const PodType, N>& array ...);
  663. //
  664. // are ambiguous. This will be worked around by using a buffer_types traits
  665. // class that contains typedefs for the appropriate buffer and container
  666. // classes, based on whether PodType is const or non-const.
  667. namespace detail {
  668. template <bool IsConst>
  669. struct buffer_types_base;
  670. template <>
  671. struct buffer_types_base<false>
  672. {
  673. typedef mutable_buffer buffer_type;
  674. typedef mutable_buffers_1 container_type;
  675. };
  676. template <>
  677. struct buffer_types_base<true>
  678. {
  679. typedef const_buffer buffer_type;
  680. typedef const_buffers_1 container_type;
  681. };
  682. template <typename PodType>
  683. struct buffer_types
  684. : public buffer_types_base<boost::is_const<PodType>::value>
  685. {
  686. };
  687. } // namespace detail
  688. template <typename PodType, std::size_t N>
  689. inline typename detail::buffer_types<PodType>::container_type
  690. buffer(boost::array<PodType, N>& data)
  691. {
  692. typedef typename boost::asio::detail::buffer_types<PodType>::buffer_type
  693. buffer_type;
  694. typedef typename boost::asio::detail::buffer_types<PodType>::container_type
  695. container_type;
  696. return container_type(
  697. buffer_type(data.c_array(), data.size() * sizeof(PodType)));
  698. }
  699. template <typename PodType, std::size_t N>
  700. inline typename detail::buffer_types<PodType>::container_type
  701. buffer(boost::array<PodType, N>& data, std::size_t max_size_in_bytes)
  702. {
  703. typedef typename boost::asio::detail::buffer_types<PodType>::buffer_type
  704. buffer_type;
  705. typedef typename boost::asio::detail::buffer_types<PodType>::container_type
  706. container_type;
  707. return container_type(
  708. buffer_type(data.c_array(),
  709. data.size() * sizeof(PodType) < max_size_in_bytes
  710. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  711. }
  712. #else // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  713. // || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  714. /// Create a new modifiable buffer that represents the given POD array.
  715. /**
  716. * @returns A mutable_buffers_1 value equivalent to:
  717. * @code mutable_buffers_1(
  718. * data.data(),
  719. * data.size() * sizeof(PodType)); @endcode
  720. */
  721. template <typename PodType, std::size_t N>
  722. inline mutable_buffers_1 buffer(boost::array<PodType, N>& data)
  723. {
  724. return mutable_buffers_1(
  725. mutable_buffer(data.c_array(), data.size() * sizeof(PodType)));
  726. }
  727. /// Create a new modifiable buffer that represents the given POD array.
  728. /**
  729. * @returns A mutable_buffers_1 value equivalent to:
  730. * @code mutable_buffers_1(
  731. * data.data(),
  732. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  733. */
  734. template <typename PodType, std::size_t N>
  735. inline mutable_buffers_1 buffer(boost::array<PodType, N>& data,
  736. std::size_t max_size_in_bytes)
  737. {
  738. return mutable_buffers_1(
  739. mutable_buffer(data.c_array(),
  740. data.size() * sizeof(PodType) < max_size_in_bytes
  741. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  742. }
  743. /// Create a new non-modifiable buffer that represents the given POD array.
  744. /**
  745. * @returns A const_buffers_1 value equivalent to:
  746. * @code const_buffers_1(
  747. * data.data(),
  748. * data.size() * sizeof(PodType)); @endcode
  749. */
  750. template <typename PodType, std::size_t N>
  751. inline const_buffers_1 buffer(boost::array<const PodType, N>& data)
  752. {
  753. return const_buffers_1(
  754. const_buffer(data.data(), data.size() * sizeof(PodType)));
  755. }
  756. /// Create a new non-modifiable buffer that represents the given POD array.
  757. /**
  758. * @returns A const_buffers_1 value equivalent to:
  759. * @code const_buffers_1(
  760. * data.data(),
  761. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  762. */
  763. template <typename PodType, std::size_t N>
  764. inline const_buffers_1 buffer(boost::array<const PodType, N>& data,
  765. std::size_t max_size_in_bytes)
  766. {
  767. return const_buffers_1(
  768. const_buffer(data.data(),
  769. data.size() * sizeof(PodType) < max_size_in_bytes
  770. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  771. }
  772. #endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  773. // || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  774. /// Create a new non-modifiable buffer that represents the given POD array.
  775. /**
  776. * @returns A const_buffers_1 value equivalent to:
  777. * @code const_buffers_1(
  778. * data.data(),
  779. * data.size() * sizeof(PodType)); @endcode
  780. */
  781. template <typename PodType, std::size_t N>
  782. inline const_buffers_1 buffer(const boost::array<PodType, N>& data)
  783. {
  784. return const_buffers_1(
  785. const_buffer(data.data(), data.size() * sizeof(PodType)));
  786. }
  787. /// Create a new non-modifiable buffer that represents the given POD array.
  788. /**
  789. * @returns A const_buffers_1 value equivalent to:
  790. * @code const_buffers_1(
  791. * data.data(),
  792. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  793. */
  794. template <typename PodType, std::size_t N>
  795. inline const_buffers_1 buffer(const boost::array<PodType, N>& data,
  796. std::size_t max_size_in_bytes)
  797. {
  798. return const_buffers_1(
  799. const_buffer(data.data(),
  800. data.size() * sizeof(PodType) < max_size_in_bytes
  801. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  802. }
  803. /// Create a new modifiable buffer that represents the given POD vector.
  804. /**
  805. * @returns A mutable_buffers_1 value equivalent to:
  806. * @code mutable_buffers_1(
  807. * data.size() ? &data[0] : 0,
  808. * data.size() * sizeof(PodType)); @endcode
  809. *
  810. * @note The buffer is invalidated by any vector operation that would also
  811. * invalidate iterators.
  812. */
  813. template <typename PodType, typename Allocator>
  814. inline mutable_buffers_1 buffer(std::vector<PodType, Allocator>& data)
  815. {
  816. return mutable_buffers_1(
  817. mutable_buffer(data.size() ? &data[0] : 0, data.size() * sizeof(PodType)
  818. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  819. , detail::buffer_debug_check<
  820. typename std::vector<PodType, Allocator>::iterator
  821. >(data.begin())
  822. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  823. ));
  824. }
  825. /// Create a new modifiable buffer that represents the given POD vector.
  826. /**
  827. * @returns A mutable_buffers_1 value equivalent to:
  828. * @code mutable_buffers_1(
  829. * data.size() ? &data[0] : 0,
  830. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  831. *
  832. * @note The buffer is invalidated by any vector operation that would also
  833. * invalidate iterators.
  834. */
  835. template <typename PodType, typename Allocator>
  836. inline mutable_buffers_1 buffer(std::vector<PodType, Allocator>& data,
  837. std::size_t max_size_in_bytes)
  838. {
  839. return mutable_buffers_1(
  840. mutable_buffer(data.size() ? &data[0] : 0,
  841. data.size() * sizeof(PodType) < max_size_in_bytes
  842. ? data.size() * sizeof(PodType) : max_size_in_bytes
  843. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  844. , detail::buffer_debug_check<
  845. typename std::vector<PodType, Allocator>::iterator
  846. >(data.begin())
  847. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  848. ));
  849. }
  850. /// Create a new non-modifiable buffer that represents the given POD vector.
  851. /**
  852. * @returns A const_buffers_1 value equivalent to:
  853. * @code const_buffers_1(
  854. * data.size() ? &data[0] : 0,
  855. * data.size() * sizeof(PodType)); @endcode
  856. *
  857. * @note The buffer is invalidated by any vector operation that would also
  858. * invalidate iterators.
  859. */
  860. template <typename PodType, typename Allocator>
  861. inline const_buffers_1 buffer(
  862. const std::vector<PodType, Allocator>& data)
  863. {
  864. return const_buffers_1(
  865. const_buffer(data.size() ? &data[0] : 0, data.size() * sizeof(PodType)
  866. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  867. , detail::buffer_debug_check<
  868. typename std::vector<PodType, Allocator>::const_iterator
  869. >(data.begin())
  870. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  871. ));
  872. }
  873. /// Create a new non-modifiable buffer that represents the given POD vector.
  874. /**
  875. * @returns A const_buffers_1 value equivalent to:
  876. * @code const_buffers_1(
  877. * data.size() ? &data[0] : 0,
  878. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  879. *
  880. * @note The buffer is invalidated by any vector operation that would also
  881. * invalidate iterators.
  882. */
  883. template <typename PodType, typename Allocator>
  884. inline const_buffers_1 buffer(
  885. const std::vector<PodType, Allocator>& data, std::size_t max_size_in_bytes)
  886. {
  887. return const_buffers_1(
  888. const_buffer(data.size() ? &data[0] : 0,
  889. data.size() * sizeof(PodType) < max_size_in_bytes
  890. ? data.size() * sizeof(PodType) : max_size_in_bytes
  891. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  892. , detail::buffer_debug_check<
  893. typename std::vector<PodType, Allocator>::const_iterator
  894. >(data.begin())
  895. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  896. ));
  897. }
  898. /// Create a new non-modifiable buffer that represents the given string.
  899. /**
  900. * @returns <tt>const_buffers_1(data.data(), data.size())</tt>.
  901. *
  902. * @note The buffer is invalidated by any non-const operation called on the
  903. * given string object.
  904. */
  905. inline const_buffers_1 buffer(const std::string& data)
  906. {
  907. return const_buffers_1(const_buffer(data.data(), data.size()
  908. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  909. , detail::buffer_debug_check<std::string::const_iterator>(data.begin())
  910. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  911. ));
  912. }
  913. /// Create a new non-modifiable buffer that represents the given string.
  914. /**
  915. * @returns A const_buffers_1 value equivalent to:
  916. * @code const_buffers_1(
  917. * data.data(),
  918. * min(data.size(), max_size_in_bytes)); @endcode
  919. *
  920. * @note The buffer is invalidated by any non-const operation called on the
  921. * given string object.
  922. */
  923. inline const_buffers_1 buffer(const std::string& data,
  924. std::size_t max_size_in_bytes)
  925. {
  926. return const_buffers_1(
  927. const_buffer(data.data(),
  928. data.size() < max_size_in_bytes
  929. ? data.size() : max_size_in_bytes
  930. #if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
  931. , detail::buffer_debug_check<std::string::const_iterator>(data.begin())
  932. #endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
  933. ));
  934. }
  935. /*@}*/
  936. } // namespace asio
  937. } // namespace boost
  938. #include <boost/asio/detail/pop_options.hpp>
  939. #endif // BOOST_ASIO_BUFFER_HPP