Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2726 lines
84KB

  1. //
  2. // buffer.hpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_BUFFER_HPP
  11. #define ASIO_BUFFER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include <cstring>
  18. #include <limits>
  19. #include <stdexcept>
  20. #include <string>
  21. #include <vector>
  22. #include "asio/detail/array_fwd.hpp"
  23. #include "asio/detail/is_buffer_sequence.hpp"
  24. #include "asio/detail/throw_exception.hpp"
  25. #include "asio/detail/type_traits.hpp"
  26. #if defined(ASIO_MSVC)
  27. # if defined(_HAS_ITERATOR_DEBUGGING) && (_HAS_ITERATOR_DEBUGGING != 0)
  28. # if !defined(ASIO_DISABLE_BUFFER_DEBUGGING)
  29. # define ASIO_ENABLE_BUFFER_DEBUGGING
  30. # endif // !defined(ASIO_DISABLE_BUFFER_DEBUGGING)
  31. # endif // defined(_HAS_ITERATOR_DEBUGGING)
  32. #endif // defined(ASIO_MSVC)
  33. #if defined(__GNUC__)
  34. # if defined(_GLIBCXX_DEBUG)
  35. # if !defined(ASIO_DISABLE_BUFFER_DEBUGGING)
  36. # define ASIO_ENABLE_BUFFER_DEBUGGING
  37. # endif // !defined(ASIO_DISABLE_BUFFER_DEBUGGING)
  38. # endif // defined(_GLIBCXX_DEBUG)
  39. #endif // defined(__GNUC__)
  40. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  41. # include "asio/detail/functional.hpp"
  42. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  43. #if defined(ASIO_HAS_BOOST_WORKAROUND)
  44. # include <boost/detail/workaround.hpp>
  45. # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) \
  46. || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  47. # define ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND
  48. # endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  49. // || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  50. #endif // defined(ASIO_HAS_BOOST_WORKAROUND)
  51. #if defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND)
  52. # include "asio/detail/type_traits.hpp"
  53. #endif // defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND)
  54. #include "asio/detail/push_options.hpp"
  55. namespace asio {
  56. class mutable_buffer;
  57. class const_buffer;
  58. /// Holds a buffer that can be modified.
  59. /**
  60. * The mutable_buffer class provides a safe representation of a buffer that can
  61. * be modified. It does not own the underlying data, and so is cheap to copy or
  62. * assign.
  63. *
  64. * @par Accessing Buffer Contents
  65. *
  66. * The contents of a buffer may be accessed using the @c data() and @c size()
  67. * member functions:
  68. *
  69. * @code asio::mutable_buffer b1 = ...;
  70. * std::size_t s1 = b1.size();
  71. * unsigned char* p1 = static_cast<unsigned char*>(b1.data());
  72. * @endcode
  73. *
  74. * The @c data() member function permits violations of type safety, so uses of
  75. * it in application code should be carefully considered.
  76. */
  77. class mutable_buffer
  78. {
  79. public:
  80. /// Construct an empty buffer.
  81. mutable_buffer() ASIO_NOEXCEPT
  82. : data_(0),
  83. size_(0)
  84. {
  85. }
  86. /// Construct a buffer to represent a given memory range.
  87. mutable_buffer(void* data, std::size_t size) ASIO_NOEXCEPT
  88. : data_(data),
  89. size_(size)
  90. {
  91. }
  92. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  93. mutable_buffer(void* data, std::size_t size,
  94. asio::detail::function<void()> debug_check)
  95. : data_(data),
  96. size_(size),
  97. debug_check_(debug_check)
  98. {
  99. }
  100. const asio::detail::function<void()>& get_debug_check() const
  101. {
  102. return debug_check_;
  103. }
  104. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  105. /// Get a pointer to the beginning of the memory range.
  106. void* data() const ASIO_NOEXCEPT
  107. {
  108. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  109. if (size_ && debug_check_)
  110. debug_check_();
  111. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  112. return data_;
  113. }
  114. /// Get the size of the memory range.
  115. std::size_t size() const ASIO_NOEXCEPT
  116. {
  117. return size_;
  118. }
  119. private:
  120. void* data_;
  121. std::size_t size_;
  122. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  123. asio::detail::function<void()> debug_check_;
  124. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  125. };
  126. /// Adapts a single modifiable buffer so that it meets the requirements of the
  127. /// MutableBufferSequence concept.
  128. class mutable_buffers_1
  129. : public mutable_buffer
  130. {
  131. public:
  132. /// The type for each element in the list of buffers.
  133. typedef mutable_buffer value_type;
  134. /// A random-access iterator type that may be used to read elements.
  135. typedef const mutable_buffer* const_iterator;
  136. /// Construct to represent a given memory range.
  137. mutable_buffers_1(void* data, std::size_t size) ASIO_NOEXCEPT
  138. : mutable_buffer(data, size)
  139. {
  140. }
  141. /// Construct to represent a single modifiable buffer.
  142. explicit mutable_buffers_1(const mutable_buffer& b) ASIO_NOEXCEPT
  143. : mutable_buffer(b)
  144. {
  145. }
  146. /// Get a random-access iterator to the first element.
  147. const_iterator begin() const ASIO_NOEXCEPT
  148. {
  149. return this;
  150. }
  151. /// Get a random-access iterator for one past the last element.
  152. const_iterator end() const ASIO_NOEXCEPT
  153. {
  154. return begin() + 1;
  155. }
  156. };
  157. /// Holds a buffer that cannot be modified.
  158. /**
  159. * The const_buffer class provides a safe representation of a buffer that cannot
  160. * be modified. It does not own the underlying data, and so is cheap to copy or
  161. * assign.
  162. *
  163. * @par Accessing Buffer Contents
  164. *
  165. * The contents of a buffer may be accessed using the @c data() and @c size()
  166. * member functions:
  167. *
  168. * @code asio::const_buffer b1 = ...;
  169. * std::size_t s1 = b1.size();
  170. * const unsigned char* p1 = static_cast<const unsigned char*>(b1.data());
  171. * @endcode
  172. *
  173. * The @c data() member function permits violations of type safety, so uses of
  174. * it in application code should be carefully considered.
  175. */
  176. class const_buffer
  177. {
  178. public:
  179. /// Construct an empty buffer.
  180. const_buffer() ASIO_NOEXCEPT
  181. : data_(0),
  182. size_(0)
  183. {
  184. }
  185. /// Construct a buffer to represent a given memory range.
  186. const_buffer(const void* data, std::size_t size) ASIO_NOEXCEPT
  187. : data_(data),
  188. size_(size)
  189. {
  190. }
  191. /// Construct a non-modifiable buffer from a modifiable one.
  192. const_buffer(const mutable_buffer& b) ASIO_NOEXCEPT
  193. : data_(b.data()),
  194. size_(b.size())
  195. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  196. , debug_check_(b.get_debug_check())
  197. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  198. {
  199. }
  200. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  201. const_buffer(const void* data, std::size_t size,
  202. asio::detail::function<void()> debug_check)
  203. : data_(data),
  204. size_(size),
  205. debug_check_(debug_check)
  206. {
  207. }
  208. const asio::detail::function<void()>& get_debug_check() const
  209. {
  210. return debug_check_;
  211. }
  212. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  213. /// Get a pointer to the beginning of the memory range.
  214. const void* data() const ASIO_NOEXCEPT
  215. {
  216. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  217. if (size_ && debug_check_)
  218. debug_check_();
  219. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  220. return data_;
  221. }
  222. /// Get the size of the memory range.
  223. std::size_t size() const ASIO_NOEXCEPT
  224. {
  225. return size_;
  226. }
  227. private:
  228. const void* data_;
  229. std::size_t size_;
  230. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  231. asio::detail::function<void()> debug_check_;
  232. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  233. };
  234. /// Adapts a single non-modifiable buffer so that it meets the requirements of
  235. /// the ConstBufferSequence concept.
  236. class const_buffers_1
  237. : public const_buffer
  238. {
  239. public:
  240. /// The type for each element in the list of buffers.
  241. typedef const_buffer value_type;
  242. /// A random-access iterator type that may be used to read elements.
  243. typedef const const_buffer* const_iterator;
  244. /// Construct to represent a given memory range.
  245. const_buffers_1(const void* data, std::size_t size) ASIO_NOEXCEPT
  246. : const_buffer(data, size)
  247. {
  248. }
  249. /// Construct to represent a single non-modifiable buffer.
  250. explicit const_buffers_1(const const_buffer& b) ASIO_NOEXCEPT
  251. : const_buffer(b)
  252. {
  253. }
  254. /// Get a random-access iterator to the first element.
  255. const_iterator begin() const ASIO_NOEXCEPT
  256. {
  257. return this;
  258. }
  259. /// Get a random-access iterator for one past the last element.
  260. const_iterator end() const ASIO_NOEXCEPT
  261. {
  262. return begin() + 1;
  263. }
  264. };
  265. /// Trait to determine whether a type satisfies the MutableBufferSequence
  266. /// requirements.
  267. template <typename T>
  268. struct is_mutable_buffer_sequence
  269. #if defined(GENERATING_DOCUMENTATION)
  270. : integral_constant<bool, automatically_determined>
  271. #else // defined(GENERATING_DOCUMENTATION)
  272. : asio::detail::is_buffer_sequence<T, mutable_buffer>
  273. #endif // defined(GENERATING_DOCUMENTATION)
  274. {
  275. };
  276. /// Trait to determine whether a type satisfies the ConstBufferSequence
  277. /// requirements.
  278. template <typename T>
  279. struct is_const_buffer_sequence
  280. #if defined(GENERATING_DOCUMENTATION)
  281. : integral_constant<bool, automatically_determined>
  282. #else // defined(GENERATING_DOCUMENTATION)
  283. : asio::detail::is_buffer_sequence<T, const_buffer>
  284. #endif // defined(GENERATING_DOCUMENTATION)
  285. {
  286. };
  287. /// Trait to determine whether a type satisfies the DynamicBufferSequence
  288. /// requirements.
  289. template <typename T>
  290. struct is_dynamic_buffer_sequence
  291. #if defined(GENERATING_DOCUMENTATION)
  292. : integral_constant<bool, automatically_determined>
  293. #else // defined(GENERATING_DOCUMENTATION)
  294. : asio::detail::is_dynamic_buffer_sequence<T>
  295. #endif // defined(GENERATING_DOCUMENTATION)
  296. {
  297. };
  298. /// (Deprecated: Use the socket/descriptor wait() and async_wait() member
  299. /// functions.) An implementation of both the ConstBufferSequence and
  300. /// MutableBufferSequence concepts to represent a null buffer sequence.
  301. class null_buffers
  302. {
  303. public:
  304. /// The type for each element in the list of buffers.
  305. typedef mutable_buffer value_type;
  306. /// A random-access iterator type that may be used to read elements.
  307. typedef const mutable_buffer* const_iterator;
  308. /// Get a random-access iterator to the first element.
  309. const_iterator begin() const ASIO_NOEXCEPT
  310. {
  311. return &buf_;
  312. }
  313. /// Get a random-access iterator for one past the last element.
  314. const_iterator end() const ASIO_NOEXCEPT
  315. {
  316. return &buf_;
  317. }
  318. private:
  319. mutable_buffer buf_;
  320. };
  321. /** @defgroup buffer_size asio::buffer_size
  322. *
  323. * @brief The asio::buffer_size function determines the total number of
  324. * bytes in a buffer or buffer sequence.
  325. */
  326. /*@{*/
  327. /// Get the number of bytes in a modifiable buffer.
  328. inline std::size_t buffer_size(const mutable_buffer& b) ASIO_NOEXCEPT
  329. {
  330. return b.size();
  331. }
  332. /// Get the number of bytes in a modifiable buffer.
  333. inline std::size_t buffer_size(const mutable_buffers_1& b) ASIO_NOEXCEPT
  334. {
  335. return b.size();
  336. }
  337. /// Get the number of bytes in a non-modifiable buffer.
  338. inline std::size_t buffer_size(const const_buffer& b) ASIO_NOEXCEPT
  339. {
  340. return b.size();
  341. }
  342. /// Get the number of bytes in a non-modifiable buffer.
  343. inline std::size_t buffer_size(const const_buffers_1& b) ASIO_NOEXCEPT
  344. {
  345. return b.size();
  346. }
  347. /// Get the total number of bytes in a buffer sequence.
  348. /**
  349. * The @c BufferSequence template parameter may meet either of the @c
  350. * ConstBufferSequence or @c MutableBufferSequence type requirements.
  351. */
  352. template <typename BufferSequence>
  353. inline std::size_t buffer_size(const BufferSequence& b,
  354. typename enable_if<
  355. is_const_buffer_sequence<BufferSequence>::value
  356. >::type* = 0) ASIO_NOEXCEPT
  357. {
  358. std::size_t total_buffer_size = 0;
  359. typename BufferSequence::const_iterator iter = b.begin();
  360. typename BufferSequence::const_iterator end = b.end();
  361. for (; iter != end; ++iter)
  362. total_buffer_size += iter->size();
  363. return total_buffer_size;
  364. }
  365. /*@}*/
  366. #if !defined(ASIO_NO_DEPRECATED)
  367. /** @defgroup buffer_cast asio::buffer_cast
  368. *
  369. * @brief (Deprecated: Use the @c data() member function.) The
  370. * asio::buffer_cast function is used to obtain a pointer to the
  371. * underlying memory region associated with a buffer.
  372. *
  373. * @par Examples:
  374. *
  375. * To access the memory of a non-modifiable buffer, use:
  376. * @code asio::const_buffer b1 = ...;
  377. * const unsigned char* p1 = asio::buffer_cast<const unsigned char*>(b1);
  378. * @endcode
  379. *
  380. * To access the memory of a modifiable buffer, use:
  381. * @code asio::mutable_buffer b2 = ...;
  382. * unsigned char* p2 = asio::buffer_cast<unsigned char*>(b2);
  383. * @endcode
  384. *
  385. * The asio::buffer_cast function permits violations of type safety, so
  386. * uses of it in application code should be carefully considered.
  387. */
  388. /*@{*/
  389. /// Cast a non-modifiable buffer to a specified pointer to POD type.
  390. template <typename PointerToPodType>
  391. inline PointerToPodType buffer_cast(const mutable_buffer& b) ASIO_NOEXCEPT
  392. {
  393. return static_cast<PointerToPodType>(b.data());
  394. }
  395. /// Cast a non-modifiable buffer to a specified pointer to POD type.
  396. template <typename PointerToPodType>
  397. inline PointerToPodType buffer_cast(const const_buffer& b) ASIO_NOEXCEPT
  398. {
  399. return static_cast<PointerToPodType>(b.data());
  400. }
  401. /*@}*/
  402. #endif // !defined(ASIO_NO_DEPRECATED)
  403. /// Create a new modifiable buffer that is offset from the start of another.
  404. /**
  405. * @relates mutable_buffer
  406. */
  407. inline mutable_buffer operator+(const mutable_buffer& b,
  408. std::size_t start) ASIO_NOEXCEPT
  409. {
  410. if (start > b.size())
  411. return mutable_buffer();
  412. char* new_data = static_cast<char*>(b.data()) + start;
  413. std::size_t new_size = b.size() - start;
  414. return mutable_buffer(new_data, new_size
  415. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  416. , b.get_debug_check()
  417. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  418. );
  419. }
  420. /// Create a new modifiable buffer that is offset from the start of another.
  421. /**
  422. * @relates mutable_buffer
  423. */
  424. inline mutable_buffer operator+(std::size_t start,
  425. const mutable_buffer& b) ASIO_NOEXCEPT
  426. {
  427. return b + start;
  428. }
  429. /// Create a new non-modifiable buffer that is offset from the start of another.
  430. /**
  431. * @relates const_buffer
  432. */
  433. inline const_buffer operator+(const const_buffer& b,
  434. std::size_t start) ASIO_NOEXCEPT
  435. {
  436. if (start > b.size())
  437. return const_buffer();
  438. const char* new_data = static_cast<const char*>(b.data()) + start;
  439. std::size_t new_size = b.size() - start;
  440. return const_buffer(new_data, new_size
  441. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  442. , b.get_debug_check()
  443. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  444. );
  445. }
  446. /// Create a new non-modifiable buffer that is offset from the start of another.
  447. /**
  448. * @relates const_buffer
  449. */
  450. inline const_buffer operator+(std::size_t start,
  451. const const_buffer& b) ASIO_NOEXCEPT
  452. {
  453. return b + start;
  454. }
  455. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  456. namespace detail {
  457. template <typename Iterator>
  458. class buffer_debug_check
  459. {
  460. public:
  461. buffer_debug_check(Iterator iter)
  462. : iter_(iter)
  463. {
  464. }
  465. ~buffer_debug_check()
  466. {
  467. #if defined(ASIO_MSVC) && (ASIO_MSVC == 1400)
  468. // MSVC 8's string iterator checking may crash in a std::string::iterator
  469. // object's destructor when the iterator points to an already-destroyed
  470. // std::string object, unless the iterator is cleared first.
  471. iter_ = Iterator();
  472. #endif // defined(ASIO_MSVC) && (ASIO_MSVC == 1400)
  473. }
  474. void operator()()
  475. {
  476. *iter_;
  477. }
  478. private:
  479. Iterator iter_;
  480. };
  481. } // namespace detail
  482. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  483. /** @defgroup buffer asio::buffer
  484. *
  485. * @brief The asio::buffer function is used to create a buffer object to
  486. * represent raw memory, an array of POD elements, a vector of POD elements,
  487. * or a std::string.
  488. *
  489. * A buffer object represents a contiguous region of memory as a 2-tuple
  490. * consisting of a pointer and size in bytes. A tuple of the form <tt>{void*,
  491. * size_t}</tt> specifies a mutable (modifiable) region of memory. Similarly, a
  492. * tuple of the form <tt>{const void*, size_t}</tt> specifies a const
  493. * (non-modifiable) region of memory. These two forms correspond to the classes
  494. * mutable_buffer and const_buffer, respectively. To mirror C++'s conversion
  495. * rules, a mutable_buffer is implicitly convertible to a const_buffer, and the
  496. * opposite conversion is not permitted.
  497. *
  498. * The simplest use case involves reading or writing a single buffer of a
  499. * specified size:
  500. *
  501. * @code sock.send(asio::buffer(data, size)); @endcode
  502. *
  503. * In the above example, the return value of asio::buffer meets the
  504. * requirements of the ConstBufferSequence concept so that it may be directly
  505. * passed to the socket's write function. A buffer created for modifiable
  506. * memory also meets the requirements of the MutableBufferSequence concept.
  507. *
  508. * An individual buffer may be created from a builtin array, std::vector,
  509. * std::array or boost::array of POD elements. This helps prevent buffer
  510. * overruns by automatically determining the size of the buffer:
  511. *
  512. * @code char d1[128];
  513. * size_t bytes_transferred = sock.receive(asio::buffer(d1));
  514. *
  515. * std::vector<char> d2(128);
  516. * bytes_transferred = sock.receive(asio::buffer(d2));
  517. *
  518. * std::array<char, 128> d3;
  519. * bytes_transferred = sock.receive(asio::buffer(d3));
  520. *
  521. * boost::array<char, 128> d4;
  522. * bytes_transferred = sock.receive(asio::buffer(d4)); @endcode
  523. *
  524. * In all three cases above, the buffers created are exactly 128 bytes long.
  525. * Note that a vector is @e never automatically resized when creating or using
  526. * a buffer. The buffer size is determined using the vector's <tt>size()</tt>
  527. * member function, and not its capacity.
  528. *
  529. * @par Accessing Buffer Contents
  530. *
  531. * The contents of a buffer may be accessed using the @c data() and @c size()
  532. * member functions:
  533. *
  534. * @code asio::mutable_buffer b1 = ...;
  535. * std::size_t s1 = b1.size();
  536. * unsigned char* p1 = static_cast<unsigned char*>(b1.data());
  537. *
  538. * asio::const_buffer b2 = ...;
  539. * std::size_t s2 = b2.size();
  540. * const void* p2 = b2.data(); @endcode
  541. *
  542. * The @c data() member function permits violations of type safety, so
  543. * uses of it in application code should be carefully considered.
  544. *
  545. * For convenience, a @ref buffer_size function is provided that works with
  546. * both buffers and buffer sequences (that is, types meeting the
  547. * ConstBufferSequence or MutableBufferSequence type requirements). In this
  548. * case, the function returns the total size of all buffers in the sequence.
  549. *
  550. * @par Buffer Copying
  551. *
  552. * The @ref buffer_copy function may be used to copy raw bytes between
  553. * individual buffers and buffer sequences.
  554. *
  555. * In particular, when used with the @ref buffer_size, the @ref buffer_copy
  556. * function can be used to linearise a sequence of buffers. For example:
  557. *
  558. * @code vector<const_buffer> buffers = ...;
  559. *
  560. * vector<unsigned char> data(asio::buffer_size(buffers));
  561. * asio::buffer_copy(asio::buffer(data), buffers); @endcode
  562. *
  563. * Note that @ref buffer_copy is implemented in terms of @c memcpy, and
  564. * consequently it cannot be used to copy between overlapping memory regions.
  565. *
  566. * @par Buffer Invalidation
  567. *
  568. * A buffer object does not have any ownership of the memory it refers to. It
  569. * is the responsibility of the application to ensure the memory region remains
  570. * valid until it is no longer required for an I/O operation. When the memory
  571. * is no longer available, the buffer is said to have been invalidated.
  572. *
  573. * For the asio::buffer overloads that accept an argument of type
  574. * std::vector, the buffer objects returned are invalidated by any vector
  575. * operation that also invalidates all references, pointers and iterators
  576. * referring to the elements in the sequence (C++ Std, 23.2.4)
  577. *
  578. * For the asio::buffer overloads that accept an argument of type
  579. * std::basic_string, the buffer objects returned are invalidated according to
  580. * the rules defined for invalidation of references, pointers and iterators
  581. * referring to elements of the sequence (C++ Std, 21.3).
  582. *
  583. * @par Buffer Arithmetic
  584. *
  585. * Buffer objects may be manipulated using simple arithmetic in a safe way
  586. * which helps prevent buffer overruns. Consider an array initialised as
  587. * follows:
  588. *
  589. * @code boost::array<char, 6> a = { 'a', 'b', 'c', 'd', 'e' }; @endcode
  590. *
  591. * A buffer object @c b1 created using:
  592. *
  593. * @code b1 = asio::buffer(a); @endcode
  594. *
  595. * represents the entire array, <tt>{ 'a', 'b', 'c', 'd', 'e' }</tt>. An
  596. * optional second argument to the asio::buffer function may be used to
  597. * limit the size, in bytes, of the buffer:
  598. *
  599. * @code b2 = asio::buffer(a, 3); @endcode
  600. *
  601. * such that @c b2 represents the data <tt>{ 'a', 'b', 'c' }</tt>. Even if the
  602. * size argument exceeds the actual size of the array, the size of the buffer
  603. * object created will be limited to the array size.
  604. *
  605. * An offset may be applied to an existing buffer to create a new one:
  606. *
  607. * @code b3 = b1 + 2; @endcode
  608. *
  609. * where @c b3 will set to represent <tt>{ 'c', 'd', 'e' }</tt>. If the offset
  610. * exceeds the size of the existing buffer, the newly created buffer will be
  611. * empty.
  612. *
  613. * Both an offset and size may be specified to create a buffer that corresponds
  614. * to a specific range of bytes within an existing buffer:
  615. *
  616. * @code b4 = asio::buffer(b1 + 1, 3); @endcode
  617. *
  618. * so that @c b4 will refer to the bytes <tt>{ 'b', 'c', 'd' }</tt>.
  619. *
  620. * @par Buffers and Scatter-Gather I/O
  621. *
  622. * To read or write using multiple buffers (i.e. scatter-gather I/O), multiple
  623. * buffer objects may be assigned into a container that supports the
  624. * MutableBufferSequence (for read) or ConstBufferSequence (for write) concepts:
  625. *
  626. * @code
  627. * char d1[128];
  628. * std::vector<char> d2(128);
  629. * boost::array<char, 128> d3;
  630. *
  631. * boost::array<mutable_buffer, 3> bufs1 = {
  632. * asio::buffer(d1),
  633. * asio::buffer(d2),
  634. * asio::buffer(d3) };
  635. * bytes_transferred = sock.receive(bufs1);
  636. *
  637. * std::vector<const_buffer> bufs2;
  638. * bufs2.push_back(asio::buffer(d1));
  639. * bufs2.push_back(asio::buffer(d2));
  640. * bufs2.push_back(asio::buffer(d3));
  641. * bytes_transferred = sock.send(bufs2); @endcode
  642. */
  643. /*@{*/
  644. /// Create a new modifiable buffer from an existing buffer.
  645. /**
  646. * @returns <tt>mutable_buffers_1(b)</tt>.
  647. */
  648. inline mutable_buffers_1 buffer(const mutable_buffer& b) ASIO_NOEXCEPT
  649. {
  650. return mutable_buffers_1(b);
  651. }
  652. /// Create a new modifiable buffer from an existing buffer.
  653. /**
  654. * @returns A mutable_buffers_1 value equivalent to:
  655. * @code mutable_buffers_1(
  656. * b.data(),
  657. * min(b.size(), max_size_in_bytes)); @endcode
  658. */
  659. inline mutable_buffers_1 buffer(const mutable_buffer& b,
  660. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  661. {
  662. return mutable_buffers_1(
  663. mutable_buffer(b.data(),
  664. b.size() < max_size_in_bytes
  665. ? b.size() : max_size_in_bytes
  666. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  667. , b.get_debug_check()
  668. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  669. ));
  670. }
  671. /// Create a new non-modifiable buffer from an existing buffer.
  672. /**
  673. * @returns <tt>const_buffers_1(b)</tt>.
  674. */
  675. inline const_buffers_1 buffer(const const_buffer& b) ASIO_NOEXCEPT
  676. {
  677. return const_buffers_1(b);
  678. }
  679. /// Create a new non-modifiable buffer from an existing buffer.
  680. /**
  681. * @returns A const_buffers_1 value equivalent to:
  682. * @code const_buffers_1(
  683. * b.data(),
  684. * min(b.size(), max_size_in_bytes)); @endcode
  685. */
  686. inline const_buffers_1 buffer(const const_buffer& b,
  687. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  688. {
  689. return const_buffers_1(
  690. const_buffer(b.data(),
  691. b.size() < max_size_in_bytes
  692. ? b.size() : max_size_in_bytes
  693. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  694. , b.get_debug_check()
  695. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  696. ));
  697. }
  698. /// Create a new modifiable buffer that represents the given memory range.
  699. /**
  700. * @returns <tt>mutable_buffers_1(data, size_in_bytes)</tt>.
  701. */
  702. inline mutable_buffers_1 buffer(void* data,
  703. std::size_t size_in_bytes) ASIO_NOEXCEPT
  704. {
  705. return mutable_buffers_1(mutable_buffer(data, size_in_bytes));
  706. }
  707. /// Create a new non-modifiable buffer that represents the given memory range.
  708. /**
  709. * @returns <tt>const_buffers_1(data, size_in_bytes)</tt>.
  710. */
  711. inline const_buffers_1 buffer(const void* data,
  712. std::size_t size_in_bytes) ASIO_NOEXCEPT
  713. {
  714. return const_buffers_1(const_buffer(data, size_in_bytes));
  715. }
  716. /// Create a new modifiable buffer that represents the given POD array.
  717. /**
  718. * @returns A mutable_buffers_1 value equivalent to:
  719. * @code mutable_buffers_1(
  720. * static_cast<void*>(data),
  721. * N * sizeof(PodType)); @endcode
  722. */
  723. template <typename PodType, std::size_t N>
  724. inline mutable_buffers_1 buffer(PodType (&data)[N]) ASIO_NOEXCEPT
  725. {
  726. return mutable_buffers_1(mutable_buffer(data, N * sizeof(PodType)));
  727. }
  728. /// Create a new modifiable buffer that represents the given POD array.
  729. /**
  730. * @returns A mutable_buffers_1 value equivalent to:
  731. * @code mutable_buffers_1(
  732. * static_cast<void*>(data),
  733. * min(N * sizeof(PodType), max_size_in_bytes)); @endcode
  734. */
  735. template <typename PodType, std::size_t N>
  736. inline mutable_buffers_1 buffer(PodType (&data)[N],
  737. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  738. {
  739. return mutable_buffers_1(
  740. mutable_buffer(data,
  741. N * sizeof(PodType) < max_size_in_bytes
  742. ? N * sizeof(PodType) : max_size_in_bytes));
  743. }
  744. /// Create a new non-modifiable buffer that represents the given POD array.
  745. /**
  746. * @returns A const_buffers_1 value equivalent to:
  747. * @code const_buffers_1(
  748. * static_cast<const void*>(data),
  749. * N * sizeof(PodType)); @endcode
  750. */
  751. template <typename PodType, std::size_t N>
  752. inline const_buffers_1 buffer(const PodType (&data)[N]) ASIO_NOEXCEPT
  753. {
  754. return const_buffers_1(const_buffer(data, N * 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. * static_cast<const void*>(data),
  761. * min(N * sizeof(PodType), max_size_in_bytes)); @endcode
  762. */
  763. template <typename PodType, std::size_t N>
  764. inline const_buffers_1 buffer(const PodType (&data)[N],
  765. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  766. {
  767. return const_buffers_1(
  768. const_buffer(data,
  769. N * sizeof(PodType) < max_size_in_bytes
  770. ? N * sizeof(PodType) : max_size_in_bytes));
  771. }
  772. #if defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND)
  773. // Borland C++ and Sun Studio think the overloads:
  774. //
  775. // unspecified buffer(boost::array<PodType, N>& array ...);
  776. //
  777. // and
  778. //
  779. // unspecified buffer(boost::array<const PodType, N>& array ...);
  780. //
  781. // are ambiguous. This will be worked around by using a buffer_types traits
  782. // class that contains typedefs for the appropriate buffer and container
  783. // classes, based on whether PodType is const or non-const.
  784. namespace detail {
  785. template <bool IsConst>
  786. struct buffer_types_base;
  787. template <>
  788. struct buffer_types_base<false>
  789. {
  790. typedef mutable_buffer buffer_type;
  791. typedef mutable_buffers_1 container_type;
  792. };
  793. template <>
  794. struct buffer_types_base<true>
  795. {
  796. typedef const_buffer buffer_type;
  797. typedef const_buffers_1 container_type;
  798. };
  799. template <typename PodType>
  800. struct buffer_types
  801. : public buffer_types_base<is_const<PodType>::value>
  802. {
  803. };
  804. } // namespace detail
  805. template <typename PodType, std::size_t N>
  806. inline typename detail::buffer_types<PodType>::container_type
  807. buffer(boost::array<PodType, N>& data) ASIO_NOEXCEPT
  808. {
  809. typedef typename asio::detail::buffer_types<PodType>::buffer_type
  810. buffer_type;
  811. typedef typename asio::detail::buffer_types<PodType>::container_type
  812. container_type;
  813. return container_type(
  814. buffer_type(data.c_array(), data.size() * sizeof(PodType)));
  815. }
  816. template <typename PodType, std::size_t N>
  817. inline typename detail::buffer_types<PodType>::container_type
  818. buffer(boost::array<PodType, N>& data,
  819. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  820. {
  821. typedef typename asio::detail::buffer_types<PodType>::buffer_type
  822. buffer_type;
  823. typedef typename asio::detail::buffer_types<PodType>::container_type
  824. container_type;
  825. return container_type(
  826. buffer_type(data.c_array(),
  827. data.size() * sizeof(PodType) < max_size_in_bytes
  828. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  829. }
  830. #else // defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND)
  831. /// Create a new modifiable buffer that represents the given POD array.
  832. /**
  833. * @returns A mutable_buffers_1 value equivalent to:
  834. * @code mutable_buffers_1(
  835. * data.data(),
  836. * data.size() * sizeof(PodType)); @endcode
  837. */
  838. template <typename PodType, std::size_t N>
  839. inline mutable_buffers_1 buffer(
  840. boost::array<PodType, N>& data) ASIO_NOEXCEPT
  841. {
  842. return mutable_buffers_1(
  843. mutable_buffer(data.c_array(), data.size() * sizeof(PodType)));
  844. }
  845. /// Create a new modifiable buffer that represents the given POD array.
  846. /**
  847. * @returns A mutable_buffers_1 value equivalent to:
  848. * @code mutable_buffers_1(
  849. * data.data(),
  850. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  851. */
  852. template <typename PodType, std::size_t N>
  853. inline mutable_buffers_1 buffer(boost::array<PodType, N>& data,
  854. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  855. {
  856. return mutable_buffers_1(
  857. mutable_buffer(data.c_array(),
  858. data.size() * sizeof(PodType) < max_size_in_bytes
  859. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  860. }
  861. /// Create a new non-modifiable buffer that represents the given POD array.
  862. /**
  863. * @returns A const_buffers_1 value equivalent to:
  864. * @code const_buffers_1(
  865. * data.data(),
  866. * data.size() * sizeof(PodType)); @endcode
  867. */
  868. template <typename PodType, std::size_t N>
  869. inline const_buffers_1 buffer(
  870. boost::array<const PodType, N>& data) ASIO_NOEXCEPT
  871. {
  872. return const_buffers_1(
  873. const_buffer(data.data(), data.size() * sizeof(PodType)));
  874. }
  875. /// Create a new non-modifiable buffer that represents the given POD array.
  876. /**
  877. * @returns A const_buffers_1 value equivalent to:
  878. * @code const_buffers_1(
  879. * data.data(),
  880. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  881. */
  882. template <typename PodType, std::size_t N>
  883. inline const_buffers_1 buffer(boost::array<const PodType, N>& data,
  884. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  885. {
  886. return const_buffers_1(
  887. const_buffer(data.data(),
  888. data.size() * sizeof(PodType) < max_size_in_bytes
  889. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  890. }
  891. #endif // defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND)
  892. /// Create a new non-modifiable buffer that represents the given POD array.
  893. /**
  894. * @returns A const_buffers_1 value equivalent to:
  895. * @code const_buffers_1(
  896. * data.data(),
  897. * data.size() * sizeof(PodType)); @endcode
  898. */
  899. template <typename PodType, std::size_t N>
  900. inline const_buffers_1 buffer(
  901. const boost::array<PodType, N>& data) ASIO_NOEXCEPT
  902. {
  903. return const_buffers_1(
  904. const_buffer(data.data(), data.size() * sizeof(PodType)));
  905. }
  906. /// Create a new non-modifiable buffer that represents the given POD array.
  907. /**
  908. * @returns A const_buffers_1 value equivalent to:
  909. * @code const_buffers_1(
  910. * data.data(),
  911. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  912. */
  913. template <typename PodType, std::size_t N>
  914. inline const_buffers_1 buffer(const boost::array<PodType, N>& data,
  915. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  916. {
  917. return const_buffers_1(
  918. const_buffer(data.data(),
  919. data.size() * sizeof(PodType) < max_size_in_bytes
  920. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  921. }
  922. #if defined(ASIO_HAS_STD_ARRAY) || defined(GENERATING_DOCUMENTATION)
  923. /// Create a new modifiable buffer that represents the given POD array.
  924. /**
  925. * @returns A mutable_buffers_1 value equivalent to:
  926. * @code mutable_buffers_1(
  927. * data.data(),
  928. * data.size() * sizeof(PodType)); @endcode
  929. */
  930. template <typename PodType, std::size_t N>
  931. inline mutable_buffers_1 buffer(
  932. std::array<PodType, N>& data) ASIO_NOEXCEPT
  933. {
  934. return mutable_buffers_1(
  935. mutable_buffer(data.data(), data.size() * sizeof(PodType)));
  936. }
  937. /// Create a new modifiable buffer that represents the given POD array.
  938. /**
  939. * @returns A mutable_buffers_1 value equivalent to:
  940. * @code mutable_buffers_1(
  941. * data.data(),
  942. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  943. */
  944. template <typename PodType, std::size_t N>
  945. inline mutable_buffers_1 buffer(std::array<PodType, N>& data,
  946. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  947. {
  948. return mutable_buffers_1(
  949. mutable_buffer(data.data(),
  950. data.size() * sizeof(PodType) < max_size_in_bytes
  951. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  952. }
  953. /// Create a new non-modifiable buffer that represents the given POD array.
  954. /**
  955. * @returns A const_buffers_1 value equivalent to:
  956. * @code const_buffers_1(
  957. * data.data(),
  958. * data.size() * sizeof(PodType)); @endcode
  959. */
  960. template <typename PodType, std::size_t N>
  961. inline const_buffers_1 buffer(
  962. std::array<const PodType, N>& data) ASIO_NOEXCEPT
  963. {
  964. return const_buffers_1(
  965. const_buffer(data.data(), data.size() * sizeof(PodType)));
  966. }
  967. /// Create a new non-modifiable buffer that represents the given POD array.
  968. /**
  969. * @returns A const_buffers_1 value equivalent to:
  970. * @code const_buffers_1(
  971. * data.data(),
  972. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  973. */
  974. template <typename PodType, std::size_t N>
  975. inline const_buffers_1 buffer(std::array<const PodType, N>& data,
  976. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  977. {
  978. return const_buffers_1(
  979. const_buffer(data.data(),
  980. data.size() * sizeof(PodType) < max_size_in_bytes
  981. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  982. }
  983. /// Create a new non-modifiable buffer that represents the given POD array.
  984. /**
  985. * @returns A const_buffers_1 value equivalent to:
  986. * @code const_buffers_1(
  987. * data.data(),
  988. * data.size() * sizeof(PodType)); @endcode
  989. */
  990. template <typename PodType, std::size_t N>
  991. inline const_buffers_1 buffer(
  992. const std::array<PodType, N>& data) ASIO_NOEXCEPT
  993. {
  994. return const_buffers_1(
  995. const_buffer(data.data(), data.size() * sizeof(PodType)));
  996. }
  997. /// Create a new non-modifiable buffer that represents the given POD array.
  998. /**
  999. * @returns A const_buffers_1 value equivalent to:
  1000. * @code const_buffers_1(
  1001. * data.data(),
  1002. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  1003. */
  1004. template <typename PodType, std::size_t N>
  1005. inline const_buffers_1 buffer(const std::array<PodType, N>& data,
  1006. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  1007. {
  1008. return const_buffers_1(
  1009. const_buffer(data.data(),
  1010. data.size() * sizeof(PodType) < max_size_in_bytes
  1011. ? data.size() * sizeof(PodType) : max_size_in_bytes));
  1012. }
  1013. #endif // defined(ASIO_HAS_STD_ARRAY) || defined(GENERATING_DOCUMENTATION)
  1014. /// Create a new modifiable buffer that represents the given POD vector.
  1015. /**
  1016. * @returns A mutable_buffers_1 value equivalent to:
  1017. * @code mutable_buffers_1(
  1018. * data.size() ? &data[0] : 0,
  1019. * data.size() * sizeof(PodType)); @endcode
  1020. *
  1021. * @note The buffer is invalidated by any vector operation that would also
  1022. * invalidate iterators.
  1023. */
  1024. template <typename PodType, typename Allocator>
  1025. inline mutable_buffers_1 buffer(
  1026. std::vector<PodType, Allocator>& data) ASIO_NOEXCEPT
  1027. {
  1028. return mutable_buffers_1(
  1029. mutable_buffer(data.size() ? &data[0] : 0, data.size() * sizeof(PodType)
  1030. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1031. , detail::buffer_debug_check<
  1032. typename std::vector<PodType, Allocator>::iterator
  1033. >(data.begin())
  1034. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1035. ));
  1036. }
  1037. /// Create a new modifiable buffer that represents the given POD vector.
  1038. /**
  1039. * @returns A mutable_buffers_1 value equivalent to:
  1040. * @code mutable_buffers_1(
  1041. * data.size() ? &data[0] : 0,
  1042. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  1043. *
  1044. * @note The buffer is invalidated by any vector operation that would also
  1045. * invalidate iterators.
  1046. */
  1047. template <typename PodType, typename Allocator>
  1048. inline mutable_buffers_1 buffer(std::vector<PodType, Allocator>& data,
  1049. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  1050. {
  1051. return mutable_buffers_1(
  1052. mutable_buffer(data.size() ? &data[0] : 0,
  1053. data.size() * sizeof(PodType) < max_size_in_bytes
  1054. ? data.size() * sizeof(PodType) : max_size_in_bytes
  1055. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1056. , detail::buffer_debug_check<
  1057. typename std::vector<PodType, Allocator>::iterator
  1058. >(data.begin())
  1059. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1060. ));
  1061. }
  1062. /// Create a new non-modifiable buffer that represents the given POD vector.
  1063. /**
  1064. * @returns A const_buffers_1 value equivalent to:
  1065. * @code const_buffers_1(
  1066. * data.size() ? &data[0] : 0,
  1067. * data.size() * sizeof(PodType)); @endcode
  1068. *
  1069. * @note The buffer is invalidated by any vector operation that would also
  1070. * invalidate iterators.
  1071. */
  1072. template <typename PodType, typename Allocator>
  1073. inline const_buffers_1 buffer(
  1074. const std::vector<PodType, Allocator>& data) ASIO_NOEXCEPT
  1075. {
  1076. return const_buffers_1(
  1077. const_buffer(data.size() ? &data[0] : 0, data.size() * sizeof(PodType)
  1078. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1079. , detail::buffer_debug_check<
  1080. typename std::vector<PodType, Allocator>::const_iterator
  1081. >(data.begin())
  1082. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1083. ));
  1084. }
  1085. /// Create a new non-modifiable buffer that represents the given POD vector.
  1086. /**
  1087. * @returns A const_buffers_1 value equivalent to:
  1088. * @code const_buffers_1(
  1089. * data.size() ? &data[0] : 0,
  1090. * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
  1091. *
  1092. * @note The buffer is invalidated by any vector operation that would also
  1093. * invalidate iterators.
  1094. */
  1095. template <typename PodType, typename Allocator>
  1096. inline const_buffers_1 buffer(
  1097. const std::vector<PodType, Allocator>& data,
  1098. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  1099. {
  1100. return const_buffers_1(
  1101. const_buffer(data.size() ? &data[0] : 0,
  1102. data.size() * sizeof(PodType) < max_size_in_bytes
  1103. ? data.size() * sizeof(PodType) : max_size_in_bytes
  1104. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1105. , detail::buffer_debug_check<
  1106. typename std::vector<PodType, Allocator>::const_iterator
  1107. >(data.begin())
  1108. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1109. ));
  1110. }
  1111. /// Create a new modifiable buffer that represents the given string.
  1112. /**
  1113. * @returns <tt>mutable_buffers_1(data.size() ? &data[0] : 0,
  1114. * data.size() * sizeof(Elem))</tt>.
  1115. *
  1116. * @note The buffer is invalidated by any non-const operation called on the
  1117. * given string object.
  1118. */
  1119. template <typename Elem, typename Traits, typename Allocator>
  1120. inline mutable_buffers_1 buffer(
  1121. std::basic_string<Elem, Traits, Allocator>& data) ASIO_NOEXCEPT
  1122. {
  1123. return mutable_buffers_1(mutable_buffer(data.size() ? &data[0] : 0,
  1124. data.size() * sizeof(Elem)
  1125. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1126. , detail::buffer_debug_check<
  1127. typename std::basic_string<Elem, Traits, Allocator>::iterator
  1128. >(data.begin())
  1129. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1130. ));
  1131. }
  1132. /// Create a new non-modifiable buffer that represents the given string.
  1133. /**
  1134. * @returns A mutable_buffers_1 value equivalent to:
  1135. * @code mutable_buffers_1(
  1136. * data.size() ? &data[0] : 0,
  1137. * min(data.size() * sizeof(Elem), max_size_in_bytes)); @endcode
  1138. *
  1139. * @note The buffer is invalidated by any non-const operation called on the
  1140. * given string object.
  1141. */
  1142. template <typename Elem, typename Traits, typename Allocator>
  1143. inline mutable_buffers_1 buffer(
  1144. std::basic_string<Elem, Traits, Allocator>& data,
  1145. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  1146. {
  1147. return mutable_buffers_1(
  1148. mutable_buffer(data.size() ? &data[0] : 0,
  1149. data.size() * sizeof(Elem) < max_size_in_bytes
  1150. ? data.size() * sizeof(Elem) : max_size_in_bytes
  1151. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1152. , detail::buffer_debug_check<
  1153. typename std::basic_string<Elem, Traits, Allocator>::iterator
  1154. >(data.begin())
  1155. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1156. ));
  1157. }
  1158. /// Create a new non-modifiable buffer that represents the given string.
  1159. /**
  1160. * @returns <tt>const_buffers_1(data.data(), data.size() * sizeof(Elem))</tt>.
  1161. *
  1162. * @note The buffer is invalidated by any non-const operation called on the
  1163. * given string object.
  1164. */
  1165. template <typename Elem, typename Traits, typename Allocator>
  1166. inline const_buffers_1 buffer(
  1167. const std::basic_string<Elem, Traits, Allocator>& data) ASIO_NOEXCEPT
  1168. {
  1169. return const_buffers_1(const_buffer(data.data(), data.size() * sizeof(Elem)
  1170. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1171. , detail::buffer_debug_check<
  1172. typename std::basic_string<Elem, Traits, Allocator>::const_iterator
  1173. >(data.begin())
  1174. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1175. ));
  1176. }
  1177. /// Create a new non-modifiable buffer that represents the given string.
  1178. /**
  1179. * @returns A const_buffers_1 value equivalent to:
  1180. * @code const_buffers_1(
  1181. * data.data(),
  1182. * min(data.size() * sizeof(Elem), max_size_in_bytes)); @endcode
  1183. *
  1184. * @note The buffer is invalidated by any non-const operation called on the
  1185. * given string object.
  1186. */
  1187. template <typename Elem, typename Traits, typename Allocator>
  1188. inline const_buffers_1 buffer(
  1189. const std::basic_string<Elem, Traits, Allocator>& data,
  1190. std::size_t max_size_in_bytes) ASIO_NOEXCEPT
  1191. {
  1192. return const_buffers_1(
  1193. const_buffer(data.data(),
  1194. data.size() * sizeof(Elem) < max_size_in_bytes
  1195. ? data.size() * sizeof(Elem) : max_size_in_bytes
  1196. #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
  1197. , detail::buffer_debug_check<
  1198. typename std::basic_string<Elem, Traits, Allocator>::const_iterator
  1199. >(data.begin())
  1200. #endif // ASIO_ENABLE_BUFFER_DEBUGGING
  1201. ));
  1202. }
  1203. /*@}*/
  1204. /// Adapt a basic_string to the DynamicBufferSequence requirements.
  1205. /**
  1206. * Requires that <tt>sizeof(Elem) == 1</tt>.
  1207. */
  1208. template <typename Elem, typename Traits, typename Allocator>
  1209. class dynamic_string_buffer
  1210. {
  1211. public:
  1212. /// The type used to represent the input sequence as a list of buffers.
  1213. typedef const_buffers_1 const_buffers_type;
  1214. /// The type used to represent the output sequence as a list of buffers.
  1215. typedef mutable_buffers_1 mutable_buffers_type;
  1216. /// Construct a dynamic buffer from a string.
  1217. /**
  1218. * @param s The string to be used as backing storage for the dynamic buffer.
  1219. * Any existing data in the string is treated as the dynamic buffer's input
  1220. * sequence. The object stores a reference to the string and the user is
  1221. * responsible for ensuring that the string object remains valid until the
  1222. * dynamic_string_buffer object is destroyed.
  1223. *
  1224. * @param maximum_size Specifies a maximum size for the buffer, in bytes.
  1225. */
  1226. explicit dynamic_string_buffer(std::basic_string<Elem, Traits, Allocator>& s,
  1227. std::size_t maximum_size =
  1228. (std::numeric_limits<std::size_t>::max)()) ASIO_NOEXCEPT
  1229. : string_(s),
  1230. size_(string_.size()),
  1231. max_size_(maximum_size)
  1232. {
  1233. }
  1234. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  1235. /// Move construct a dynamic buffer.
  1236. dynamic_string_buffer(dynamic_string_buffer&& other) ASIO_NOEXCEPT
  1237. : string_(other.string_),
  1238. size_(other.size_),
  1239. max_size_(other.max_size_)
  1240. {
  1241. }
  1242. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  1243. /// Get the size of the input sequence.
  1244. std::size_t size() const ASIO_NOEXCEPT
  1245. {
  1246. return size_;
  1247. }
  1248. /// Get the maximum size of the dynamic buffer.
  1249. /**
  1250. * @returns The allowed maximum of the sum of the sizes of the input sequence
  1251. * and output sequence.
  1252. */
  1253. std::size_t max_size() const ASIO_NOEXCEPT
  1254. {
  1255. return max_size_;
  1256. }
  1257. /// Get the current capacity of the dynamic buffer.
  1258. /**
  1259. * @returns The current total capacity of the buffer, i.e. for both the input
  1260. * sequence and output sequence.
  1261. */
  1262. std::size_t capacity() const ASIO_NOEXCEPT
  1263. {
  1264. return string_.capacity();
  1265. }
  1266. /// Get a list of buffers that represents the input sequence.
  1267. /**
  1268. * @returns An object of type @c const_buffers_type that satisfies
  1269. * ConstBufferSequence requirements, representing the basic_string memory in
  1270. * input sequence.
  1271. *
  1272. * @note The returned object is invalidated by any @c dynamic_string_buffer
  1273. * or @c basic_string member function that modifies the input sequence or
  1274. * output sequence.
  1275. */
  1276. const_buffers_type data() const ASIO_NOEXCEPT
  1277. {
  1278. return asio::buffer(string_, size_);
  1279. }
  1280. /// Get a list of buffers that represents the output sequence, with the given
  1281. /// size.
  1282. /**
  1283. * Ensures that the output sequence can accommodate @c n bytes, resizing the
  1284. * basic_string object as necessary.
  1285. *
  1286. * @returns An object of type @c mutable_buffers_type that satisfies
  1287. * MutableBufferSequence requirements, representing basic_string memory
  1288. * at the start of the output sequence of size @c n.
  1289. *
  1290. * @throws std::length_error If <tt>size() + n > max_size()</tt>.
  1291. *
  1292. * @note The returned object is invalidated by any @c dynamic_string_buffer
  1293. * or @c basic_string member function that modifies the input sequence or
  1294. * output sequence.
  1295. */
  1296. mutable_buffers_type prepare(std::size_t n)
  1297. {
  1298. if (size () > max_size() || max_size() - size() < n)
  1299. {
  1300. std::length_error ex("dynamic_string_buffer too long");
  1301. asio::detail::throw_exception(ex);
  1302. }
  1303. string_.resize(size_ + n);
  1304. return asio::buffer(asio::buffer(string_) + size_, n);
  1305. }
  1306. /// Move bytes from the output sequence to the input sequence.
  1307. /**
  1308. * @param n The number of bytes to append from the start of the output
  1309. * sequence to the end of the input sequence. The remainder of the output
  1310. * sequence is discarded.
  1311. *
  1312. * Requires a preceding call <tt>prepare(x)</tt> where <tt>x >= n</tt>, and
  1313. * no intervening operations that modify the input or output sequence.
  1314. *
  1315. * @note If @c n is greater than the size of the output sequence, the entire
  1316. * output sequence is moved to the input sequence and no error is issued.
  1317. */
  1318. void commit(std::size_t n)
  1319. {
  1320. size_ += (std::min)(n, string_.size() - size_);
  1321. string_.resize(size_);
  1322. }
  1323. /// Remove characters from the input sequence.
  1324. /**
  1325. * Removes @c n characters from the beginning of the input sequence.
  1326. *
  1327. * @note If @c n is greater than the size of the input sequence, the entire
  1328. * input sequence is consumed and no error is issued.
  1329. */
  1330. void consume(std::size_t n)
  1331. {
  1332. std::size_t consume_length = (std::min)(n, size_);
  1333. string_.erase(consume_length);
  1334. size_ -= consume_length;
  1335. }
  1336. private:
  1337. std::basic_string<Elem, Traits, Allocator>& string_;
  1338. std::size_t size_;
  1339. const std::size_t max_size_;
  1340. };
  1341. /// Adapt a vector to the DynamicBufferSequence requirements.
  1342. /**
  1343. * Requires that <tt>sizeof(Elem) == 1</tt>.
  1344. */
  1345. template <typename Elem, typename Allocator>
  1346. class dynamic_vector_buffer
  1347. {
  1348. public:
  1349. /// The type used to represent the input sequence as a list of buffers.
  1350. typedef const_buffers_1 const_buffers_type;
  1351. /// The type used to represent the output sequence as a list of buffers.
  1352. typedef mutable_buffers_1 mutable_buffers_type;
  1353. /// Construct a dynamic buffer from a string.
  1354. /**
  1355. * @param v The vector to be used as backing storage for the dynamic buffer.
  1356. * Any existing data in the vector is treated as the dynamic buffer's input
  1357. * sequence. The object stores a reference to the vector and the user is
  1358. * responsible for ensuring that the vector object remains valid until the
  1359. * dynamic_vector_buffer object is destroyed.
  1360. *
  1361. * @param maximum_size Specifies a maximum size for the buffer, in bytes.
  1362. */
  1363. explicit dynamic_vector_buffer(std::vector<Elem, Allocator>& v,
  1364. std::size_t maximum_size =
  1365. (std::numeric_limits<std::size_t>::max)()) ASIO_NOEXCEPT
  1366. : vector_(v),
  1367. size_(vector_.size()),
  1368. max_size_(maximum_size)
  1369. {
  1370. }
  1371. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  1372. /// Move construct a dynamic buffer.
  1373. dynamic_vector_buffer(dynamic_vector_buffer&& other) ASIO_NOEXCEPT
  1374. : vector_(other.vector_),
  1375. size_(other.size_),
  1376. max_size_(other.max_size_)
  1377. {
  1378. }
  1379. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  1380. /// Get the size of the input sequence.
  1381. std::size_t size() const ASIO_NOEXCEPT
  1382. {
  1383. return size_;
  1384. }
  1385. /// Get the maximum size of the dynamic buffer.
  1386. /**
  1387. * @returns The allowed maximum of the sum of the sizes of the input sequence
  1388. * and output sequence.
  1389. */
  1390. std::size_t max_size() const ASIO_NOEXCEPT
  1391. {
  1392. return max_size_;
  1393. }
  1394. /// Get the current capacity of the dynamic buffer.
  1395. /**
  1396. * @returns The current total capacity of the buffer, i.e. for both the input
  1397. * sequence and output sequence.
  1398. */
  1399. std::size_t capacity() const ASIO_NOEXCEPT
  1400. {
  1401. return vector_.capacity();
  1402. }
  1403. /// Get a list of buffers that represents the input sequence.
  1404. /**
  1405. * @returns An object of type @c const_buffers_type that satisfies
  1406. * ConstBufferSequence requirements, representing the basic_string memory in
  1407. * input sequence.
  1408. *
  1409. * @note The returned object is invalidated by any @c dynamic_vector_buffer
  1410. * or @c basic_string member function that modifies the input sequence or
  1411. * output sequence.
  1412. */
  1413. const_buffers_type data() const ASIO_NOEXCEPT
  1414. {
  1415. return asio::buffer(vector_, size_);
  1416. }
  1417. /// Get a list of buffers that represents the output sequence, with the given
  1418. /// size.
  1419. /**
  1420. * Ensures that the output sequence can accommodate @c n bytes, resizing the
  1421. * basic_string object as necessary.
  1422. *
  1423. * @returns An object of type @c mutable_buffers_type that satisfies
  1424. * MutableBufferSequence requirements, representing basic_string memory
  1425. * at the start of the output sequence of size @c n.
  1426. *
  1427. * @throws std::length_error If <tt>size() + n > max_size()</tt>.
  1428. *
  1429. * @note The returned object is invalidated by any @c dynamic_vector_buffer
  1430. * or @c basic_string member function that modifies the input sequence or
  1431. * output sequence.
  1432. */
  1433. mutable_buffers_type prepare(std::size_t n)
  1434. {
  1435. if (size () > max_size() || max_size() - size() < n)
  1436. {
  1437. std::length_error ex("dynamic_vector_buffer too long");
  1438. asio::detail::throw_exception(ex);
  1439. }
  1440. vector_.resize(size_ + n);
  1441. return asio::buffer(asio::buffer(vector_) + size_, n);
  1442. }
  1443. /// Move bytes from the output sequence to the input sequence.
  1444. /**
  1445. * @param n The number of bytes to append from the start of the output
  1446. * sequence to the end of the input sequence. The remainder of the output
  1447. * sequence is discarded.
  1448. *
  1449. * Requires a preceding call <tt>prepare(x)</tt> where <tt>x >= n</tt>, and
  1450. * no intervening operations that modify the input or output sequence.
  1451. *
  1452. * @note If @c n is greater than the size of the output sequence, the entire
  1453. * output sequence is moved to the input sequence and no error is issued.
  1454. */
  1455. void commit(std::size_t n)
  1456. {
  1457. size_ += (std::min)(n, vector_.size() - size_);
  1458. vector_.resize(size_);
  1459. }
  1460. /// Remove characters from the input sequence.
  1461. /**
  1462. * Removes @c n characters from the beginning of the input sequence.
  1463. *
  1464. * @note If @c n is greater than the size of the input sequence, the entire
  1465. * input sequence is consumed and no error is issued.
  1466. */
  1467. void consume(std::size_t n)
  1468. {
  1469. std::size_t consume_length = (std::min)(n, size_);
  1470. vector_.erase(consume_length);
  1471. size_ -= consume_length;
  1472. }
  1473. private:
  1474. std::vector<Elem, Allocator>& vector_;
  1475. std::size_t size_;
  1476. const std::size_t max_size_;
  1477. };
  1478. /** @defgroup dynamic_buffer asio::dynamic_buffer
  1479. *
  1480. * @brief The asio::dynamic_buffer function is used to create a
  1481. * dynamically resized buffer from a @c std::basic_string or @c std::vector.
  1482. */
  1483. /*@{*/
  1484. /// Create a new dynamic buffer that represents the given string.
  1485. /**
  1486. * @returns <tt>dynamic_string_buffer<Elem, Traits, Allocator>(data)</tt>.
  1487. */
  1488. template <typename Elem, typename Traits, typename Allocator>
  1489. inline dynamic_string_buffer<Elem, Traits, Allocator> dynamic_buffer(
  1490. std::basic_string<Elem, Traits, Allocator>& data) ASIO_NOEXCEPT
  1491. {
  1492. return dynamic_string_buffer<Elem, Traits, Allocator>(data);
  1493. }
  1494. /// Create a new dynamic buffer that represents the given string.
  1495. /**
  1496. * @returns <tt>dynamic_string_buffer<Elem, Traits, Allocator>(data,
  1497. * max_size)</tt>.
  1498. */
  1499. template <typename Elem, typename Traits, typename Allocator>
  1500. inline dynamic_string_buffer<Elem, Traits, Allocator> dynamic_buffer(
  1501. std::basic_string<Elem, Traits, Allocator>& data,
  1502. std::size_t max_size) ASIO_NOEXCEPT
  1503. {
  1504. return dynamic_string_buffer<Elem, Traits, Allocator>(data, max_size);
  1505. }
  1506. /// Create a new dynamic buffer that represents the given vector.
  1507. /**
  1508. * @returns <tt>dynamic_vector_buffer<Elem, Allocator>(data)</tt>.
  1509. */
  1510. template <typename Elem, typename Allocator>
  1511. inline dynamic_vector_buffer<Elem, Allocator> dynamic_buffer(
  1512. std::vector<Elem, Allocator>& data) ASIO_NOEXCEPT
  1513. {
  1514. return dynamic_vector_buffer<Elem, Allocator>(data);
  1515. }
  1516. /// Create a new dynamic buffer that represents the given vector.
  1517. /**
  1518. * @returns <tt>dynamic_vector_buffer<Elem, Allocator>(data, max_size)</tt>.
  1519. */
  1520. template <typename Elem, typename Allocator>
  1521. inline dynamic_vector_buffer<Elem, Allocator> dynamic_buffer(
  1522. std::vector<Elem, Allocator>& data,
  1523. std::size_t max_size) ASIO_NOEXCEPT
  1524. {
  1525. return dynamic_vector_buffer<Elem, Allocator>(data, max_size);
  1526. }
  1527. /*@}*/
  1528. /** @defgroup buffer_copy asio::buffer_copy
  1529. *
  1530. * @brief The asio::buffer_copy function is used to copy bytes from a
  1531. * source buffer (or buffer sequence) to a target buffer (or buffer sequence).
  1532. *
  1533. * The @c buffer_copy function is available in two forms:
  1534. *
  1535. * @li A 2-argument form: @c buffer_copy(target, source)
  1536. *
  1537. * @li A 3-argument form: @c buffer_copy(target, source, max_bytes_to_copy)
  1538. *
  1539. * Both forms return the number of bytes actually copied. The number of bytes
  1540. * copied is the lesser of:
  1541. *
  1542. * @li @c buffer_size(target)
  1543. *
  1544. * @li @c buffer_size(source)
  1545. *
  1546. * @li @c If specified, @c max_bytes_to_copy.
  1547. *
  1548. * This prevents buffer overflow, regardless of the buffer sizes used in the
  1549. * copy operation.
  1550. *
  1551. * Note that @ref buffer_copy is implemented in terms of @c memcpy, and
  1552. * consequently it cannot be used to copy between overlapping memory regions.
  1553. */
  1554. /*@{*/
  1555. /// Copies bytes from a source buffer to a target buffer.
  1556. /**
  1557. * @param target A modifiable buffer representing the memory region to which
  1558. * the bytes will be copied.
  1559. *
  1560. * @param source A non-modifiable buffer representing the memory region from
  1561. * which the bytes will be copied.
  1562. *
  1563. * @returns The number of bytes copied.
  1564. *
  1565. * @note The number of bytes copied is the lesser of:
  1566. *
  1567. * @li @c buffer_size(target)
  1568. *
  1569. * @li @c buffer_size(source)
  1570. *
  1571. * This function is implemented in terms of @c memcpy, and consequently it
  1572. * cannot be used to copy between overlapping memory regions.
  1573. */
  1574. inline std::size_t buffer_copy(const mutable_buffer& target,
  1575. const const_buffer& source) ASIO_NOEXCEPT
  1576. {
  1577. using namespace std; // For memcpy.
  1578. std::size_t target_size = target.size();
  1579. std::size_t source_size = source.size();
  1580. std::size_t n = target_size < source_size ? target_size : source_size;
  1581. memcpy(target.data(), source.data(), n);
  1582. return n;
  1583. }
  1584. /// Copies bytes from a source buffer to a target buffer.
  1585. /**
  1586. * @param target A modifiable buffer representing the memory region to which
  1587. * the bytes will be copied.
  1588. *
  1589. * @param source A non-modifiable buffer representing the memory region from
  1590. * which the bytes will be copied.
  1591. *
  1592. * @returns The number of bytes copied.
  1593. *
  1594. * @note The number of bytes copied is the lesser of:
  1595. *
  1596. * @li @c buffer_size(target)
  1597. *
  1598. * @li @c buffer_size(source)
  1599. *
  1600. * This function is implemented in terms of @c memcpy, and consequently it
  1601. * cannot be used to copy between overlapping memory regions.
  1602. */
  1603. inline std::size_t buffer_copy(const mutable_buffer& target,
  1604. const const_buffers_1& source) ASIO_NOEXCEPT
  1605. {
  1606. return buffer_copy(target, static_cast<const const_buffer&>(source));
  1607. }
  1608. /// Copies bytes from a source buffer to a target buffer.
  1609. /**
  1610. * @param target A modifiable buffer representing the memory region to which
  1611. * the bytes will be copied.
  1612. *
  1613. * @param source A modifiable buffer representing the memory region from which
  1614. * the bytes will be copied. The contents of the source buffer will not be
  1615. * modified.
  1616. *
  1617. * @returns The number of bytes copied.
  1618. *
  1619. * @note The number of bytes copied is the lesser of:
  1620. *
  1621. * @li @c buffer_size(target)
  1622. *
  1623. * @li @c buffer_size(source)
  1624. *
  1625. * This function is implemented in terms of @c memcpy, and consequently it
  1626. * cannot be used to copy between overlapping memory regions.
  1627. */
  1628. inline std::size_t buffer_copy(const mutable_buffer& target,
  1629. const mutable_buffer& source) ASIO_NOEXCEPT
  1630. {
  1631. return buffer_copy(target, const_buffer(source));
  1632. }
  1633. /// Copies bytes from a source buffer to a target buffer.
  1634. /**
  1635. * @param target A modifiable buffer representing the memory region to which
  1636. * the bytes will be copied.
  1637. *
  1638. * @param source A modifiable buffer representing the memory region from which
  1639. * the bytes will be copied. The contents of the source buffer will not be
  1640. * modified.
  1641. *
  1642. * @returns The number of bytes copied.
  1643. *
  1644. * @note The number of bytes copied is the lesser of:
  1645. *
  1646. * @li @c buffer_size(target)
  1647. *
  1648. * @li @c buffer_size(source)
  1649. *
  1650. * This function is implemented in terms of @c memcpy, and consequently it
  1651. * cannot be used to copy between overlapping memory regions.
  1652. */
  1653. inline std::size_t buffer_copy(const mutable_buffer& target,
  1654. const mutable_buffers_1& source) ASIO_NOEXCEPT
  1655. {
  1656. return buffer_copy(target, const_buffer(source));
  1657. }
  1658. /// Copies bytes from a source buffer sequence to a target buffer.
  1659. /**
  1660. * @param target A modifiable buffer representing the memory region to which
  1661. * the bytes will be copied.
  1662. *
  1663. * @param source A non-modifiable buffer sequence representing the memory
  1664. * regions from which the bytes will be copied.
  1665. *
  1666. * @returns The number of bytes copied.
  1667. *
  1668. * @note The number of bytes copied is the lesser of:
  1669. *
  1670. * @li @c buffer_size(target)
  1671. *
  1672. * @li @c buffer_size(source)
  1673. *
  1674. * This function is implemented in terms of @c memcpy, and consequently it
  1675. * cannot be used to copy between overlapping memory regions.
  1676. */
  1677. template <typename ConstBufferSequence>
  1678. std::size_t buffer_copy(const mutable_buffer& target,
  1679. const ConstBufferSequence& source,
  1680. typename enable_if<
  1681. is_const_buffer_sequence<ConstBufferSequence>::value
  1682. >::type* = 0) ASIO_NOEXCEPT
  1683. {
  1684. std::size_t total_bytes_copied = 0;
  1685. typename ConstBufferSequence::const_iterator source_iter = source.begin();
  1686. typename ConstBufferSequence::const_iterator source_end = source.end();
  1687. for (mutable_buffer target_buffer(target);
  1688. target_buffer.size() && source_iter != source_end; ++source_iter)
  1689. {
  1690. const_buffer source_buffer(*source_iter);
  1691. std::size_t bytes_copied = buffer_copy(target_buffer, source_buffer);
  1692. total_bytes_copied += bytes_copied;
  1693. target_buffer = target_buffer + bytes_copied;
  1694. }
  1695. return total_bytes_copied;
  1696. }
  1697. /// Copies bytes from a source buffer to a target buffer.
  1698. /**
  1699. * @param target A modifiable buffer representing the memory region to which
  1700. * the bytes will be copied.
  1701. *
  1702. * @param source A non-modifiable buffer representing the memory region from
  1703. * which the bytes will be copied.
  1704. *
  1705. * @returns The number of bytes copied.
  1706. *
  1707. * @note The number of bytes copied is the lesser of:
  1708. *
  1709. * @li @c buffer_size(target)
  1710. *
  1711. * @li @c buffer_size(source)
  1712. *
  1713. * This function is implemented in terms of @c memcpy, and consequently it
  1714. * cannot be used to copy between overlapping memory regions.
  1715. */
  1716. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  1717. const const_buffer& source) ASIO_NOEXCEPT
  1718. {
  1719. return buffer_copy(static_cast<const mutable_buffer&>(target), source);
  1720. }
  1721. /// Copies bytes from a source buffer to a target buffer.
  1722. /**
  1723. * @param target A modifiable buffer representing the memory region to which
  1724. * the bytes will be copied.
  1725. *
  1726. * @param source A non-modifiable buffer representing the memory region from
  1727. * which the bytes will be copied.
  1728. *
  1729. * @returns The number of bytes copied.
  1730. *
  1731. * @note The number of bytes copied is the lesser of:
  1732. *
  1733. * @li @c buffer_size(target)
  1734. *
  1735. * @li @c buffer_size(source)
  1736. *
  1737. * This function is implemented in terms of @c memcpy, and consequently it
  1738. * cannot be used to copy between overlapping memory regions.
  1739. */
  1740. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  1741. const const_buffers_1& source) ASIO_NOEXCEPT
  1742. {
  1743. return buffer_copy(static_cast<const mutable_buffer&>(target),
  1744. static_cast<const const_buffer&>(source));
  1745. }
  1746. /// Copies bytes from a source buffer to a target buffer.
  1747. /**
  1748. * @param target A modifiable buffer representing the memory region to which
  1749. * the bytes will be copied.
  1750. *
  1751. * @param source A modifiable buffer representing the memory region from which
  1752. * the bytes will be copied. The contents of the source buffer will not be
  1753. * modified.
  1754. *
  1755. * @returns The number of bytes copied.
  1756. *
  1757. * @note The number of bytes copied is the lesser of:
  1758. *
  1759. * @li @c buffer_size(target)
  1760. *
  1761. * @li @c buffer_size(source)
  1762. *
  1763. * This function is implemented in terms of @c memcpy, and consequently it
  1764. * cannot be used to copy between overlapping memory regions.
  1765. */
  1766. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  1767. const mutable_buffer& source) ASIO_NOEXCEPT
  1768. {
  1769. return buffer_copy(static_cast<const mutable_buffer&>(target),
  1770. const_buffer(source));
  1771. }
  1772. /// Copies bytes from a source buffer to a target buffer.
  1773. /**
  1774. * @param target A modifiable buffer representing the memory region to which
  1775. * the bytes will be copied.
  1776. *
  1777. * @param source A modifiable buffer representing the memory region from which
  1778. * the bytes will be copied. The contents of the source buffer will not be
  1779. * modified.
  1780. *
  1781. * @returns The number of bytes copied.
  1782. *
  1783. * @note The number of bytes copied is the lesser of:
  1784. *
  1785. * @li @c buffer_size(target)
  1786. *
  1787. * @li @c buffer_size(source)
  1788. *
  1789. * This function is implemented in terms of @c memcpy, and consequently it
  1790. * cannot be used to copy between overlapping memory regions.
  1791. */
  1792. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  1793. const mutable_buffers_1& source) ASIO_NOEXCEPT
  1794. {
  1795. return buffer_copy(static_cast<const mutable_buffer&>(target),
  1796. const_buffer(source));
  1797. }
  1798. /// Copies bytes from a source buffer sequence to a target buffer.
  1799. /**
  1800. * @param target A modifiable buffer representing the memory region to which
  1801. * the bytes will be copied.
  1802. *
  1803. * @param source A non-modifiable buffer sequence representing the memory
  1804. * regions from which the bytes will be copied.
  1805. *
  1806. * @returns The number of bytes copied.
  1807. *
  1808. * @note The number of bytes copied is the lesser of:
  1809. *
  1810. * @li @c buffer_size(target)
  1811. *
  1812. * @li @c buffer_size(source)
  1813. *
  1814. * This function is implemented in terms of @c memcpy, and consequently it
  1815. * cannot be used to copy between overlapping memory regions.
  1816. */
  1817. template <typename ConstBufferSequence>
  1818. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  1819. const ConstBufferSequence& source,
  1820. typename enable_if<
  1821. is_const_buffer_sequence<ConstBufferSequence>::value
  1822. >::type* = 0) ASIO_NOEXCEPT
  1823. {
  1824. return buffer_copy(static_cast<const mutable_buffer&>(target), source);
  1825. }
  1826. /// Copies bytes from a source buffer to a target buffer sequence.
  1827. /**
  1828. * @param target A modifiable buffer sequence representing the memory regions to
  1829. * which the bytes will be copied.
  1830. *
  1831. * @param source A non-modifiable buffer representing the memory region from
  1832. * which the bytes will be copied.
  1833. *
  1834. * @returns The number of bytes copied.
  1835. *
  1836. * @note The number of bytes copied is the lesser of:
  1837. *
  1838. * @li @c buffer_size(target)
  1839. *
  1840. * @li @c buffer_size(source)
  1841. *
  1842. * This function is implemented in terms of @c memcpy, and consequently it
  1843. * cannot be used to copy between overlapping memory regions.
  1844. */
  1845. template <typename MutableBufferSequence>
  1846. std::size_t buffer_copy(const MutableBufferSequence& target,
  1847. const const_buffer& source,
  1848. typename enable_if<
  1849. is_mutable_buffer_sequence<MutableBufferSequence>::value
  1850. >::type* = 0) ASIO_NOEXCEPT
  1851. {
  1852. std::size_t total_bytes_copied = 0;
  1853. typename MutableBufferSequence::const_iterator target_iter = target.begin();
  1854. typename MutableBufferSequence::const_iterator target_end = target.end();
  1855. for (const_buffer source_buffer(source);
  1856. source_buffer.size() && target_iter != target_end; ++target_iter)
  1857. {
  1858. mutable_buffer target_buffer(*target_iter);
  1859. std::size_t bytes_copied = buffer_copy(target_buffer, source_buffer);
  1860. total_bytes_copied += bytes_copied;
  1861. source_buffer = source_buffer + bytes_copied;
  1862. }
  1863. return total_bytes_copied;
  1864. }
  1865. /// Copies bytes from a source buffer to a target buffer sequence.
  1866. /**
  1867. * @param target A modifiable buffer sequence representing the memory regions to
  1868. * which the bytes will be copied.
  1869. *
  1870. * @param source A non-modifiable buffer representing the memory region from
  1871. * which the bytes will be copied.
  1872. *
  1873. * @returns The number of bytes copied.
  1874. *
  1875. * @note The number of bytes copied is the lesser of:
  1876. *
  1877. * @li @c buffer_size(target)
  1878. *
  1879. * @li @c buffer_size(source)
  1880. *
  1881. * This function is implemented in terms of @c memcpy, and consequently it
  1882. * cannot be used to copy between overlapping memory regions.
  1883. */
  1884. template <typename MutableBufferSequence>
  1885. inline std::size_t buffer_copy(const MutableBufferSequence& target,
  1886. const const_buffers_1& source,
  1887. typename enable_if<
  1888. is_mutable_buffer_sequence<MutableBufferSequence>::value
  1889. >::type* = 0) ASIO_NOEXCEPT
  1890. {
  1891. return buffer_copy(target, static_cast<const const_buffer&>(source));
  1892. }
  1893. /// Copies bytes from a source buffer to a target buffer sequence.
  1894. /**
  1895. * @param target A modifiable buffer sequence representing the memory regions to
  1896. * which the bytes will be copied.
  1897. *
  1898. * @param source A modifiable buffer representing the memory region from which
  1899. * the bytes will be copied. The contents of the source buffer will not be
  1900. * modified.
  1901. *
  1902. * @returns The number of bytes copied.
  1903. *
  1904. * @note The number of bytes copied is the lesser of:
  1905. *
  1906. * @li @c buffer_size(target)
  1907. *
  1908. * @li @c buffer_size(source)
  1909. *
  1910. * This function is implemented in terms of @c memcpy, and consequently it
  1911. * cannot be used to copy between overlapping memory regions.
  1912. */
  1913. template <typename MutableBufferSequence>
  1914. inline std::size_t buffer_copy(const MutableBufferSequence& target,
  1915. const mutable_buffer& source,
  1916. typename enable_if<
  1917. is_mutable_buffer_sequence<MutableBufferSequence>::value
  1918. >::type* = 0) ASIO_NOEXCEPT
  1919. {
  1920. return buffer_copy(target, const_buffer(source));
  1921. }
  1922. /// Copies bytes from a source buffer to a target buffer sequence.
  1923. /**
  1924. * @param target A modifiable buffer sequence representing the memory regions to
  1925. * which the bytes will be copied.
  1926. *
  1927. * @param source A modifiable buffer representing the memory region from which
  1928. * the bytes will be copied. The contents of the source buffer will not be
  1929. * modified.
  1930. *
  1931. * @returns The number of bytes copied.
  1932. *
  1933. * @note The number of bytes copied is the lesser of:
  1934. *
  1935. * @li @c buffer_size(target)
  1936. *
  1937. * @li @c buffer_size(source)
  1938. *
  1939. * This function is implemented in terms of @c memcpy, and consequently it
  1940. * cannot be used to copy between overlapping memory regions.
  1941. */
  1942. template <typename MutableBufferSequence>
  1943. inline std::size_t buffer_copy(const MutableBufferSequence& target,
  1944. const mutable_buffers_1& source,
  1945. typename enable_if<
  1946. is_mutable_buffer_sequence<MutableBufferSequence>::value
  1947. >::type* = 0) ASIO_NOEXCEPT
  1948. {
  1949. return buffer_copy(target, const_buffer(source));
  1950. }
  1951. /// Copies bytes from a source buffer sequence to a target buffer sequence.
  1952. /**
  1953. * @param target A modifiable buffer sequence representing the memory regions to
  1954. * which the bytes will be copied.
  1955. *
  1956. * @param source A non-modifiable buffer sequence representing the memory
  1957. * regions from which the bytes will be copied.
  1958. *
  1959. * @returns The number of bytes copied.
  1960. *
  1961. * @note The number of bytes copied is the lesser of:
  1962. *
  1963. * @li @c buffer_size(target)
  1964. *
  1965. * @li @c buffer_size(source)
  1966. *
  1967. * This function is implemented in terms of @c memcpy, and consequently it
  1968. * cannot be used to copy between overlapping memory regions.
  1969. */
  1970. template <typename MutableBufferSequence, typename ConstBufferSequence>
  1971. std::size_t buffer_copy(const MutableBufferSequence& target,
  1972. const ConstBufferSequence& source,
  1973. typename enable_if<
  1974. is_mutable_buffer_sequence<MutableBufferSequence>::value &&
  1975. is_const_buffer_sequence<ConstBufferSequence>::value
  1976. >::type* = 0) ASIO_NOEXCEPT
  1977. {
  1978. std::size_t total_bytes_copied = 0;
  1979. typename MutableBufferSequence::const_iterator target_iter = target.begin();
  1980. typename MutableBufferSequence::const_iterator target_end = target.end();
  1981. std::size_t target_buffer_offset = 0;
  1982. typename ConstBufferSequence::const_iterator source_iter = source.begin();
  1983. typename ConstBufferSequence::const_iterator source_end = source.end();
  1984. std::size_t source_buffer_offset = 0;
  1985. while (target_iter != target_end && source_iter != source_end)
  1986. {
  1987. mutable_buffer target_buffer =
  1988. mutable_buffer(*target_iter) + target_buffer_offset;
  1989. const_buffer source_buffer =
  1990. const_buffer(*source_iter) + source_buffer_offset;
  1991. std::size_t bytes_copied = buffer_copy(target_buffer, source_buffer);
  1992. total_bytes_copied += bytes_copied;
  1993. if (bytes_copied == target_buffer.size())
  1994. {
  1995. ++target_iter;
  1996. target_buffer_offset = 0;
  1997. }
  1998. else
  1999. target_buffer_offset += bytes_copied;
  2000. if (bytes_copied == source_buffer.size())
  2001. {
  2002. ++source_iter;
  2003. source_buffer_offset = 0;
  2004. }
  2005. else
  2006. source_buffer_offset += bytes_copied;
  2007. }
  2008. return total_bytes_copied;
  2009. }
  2010. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2011. /**
  2012. * @param target A modifiable buffer representing the memory region to which
  2013. * the bytes will be copied.
  2014. *
  2015. * @param source A non-modifiable buffer representing the memory region from
  2016. * which the bytes will be copied.
  2017. *
  2018. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2019. *
  2020. * @returns The number of bytes copied.
  2021. *
  2022. * @note The number of bytes copied is the lesser of:
  2023. *
  2024. * @li @c buffer_size(target)
  2025. *
  2026. * @li @c buffer_size(source)
  2027. *
  2028. * @li @c max_bytes_to_copy
  2029. *
  2030. * This function is implemented in terms of @c memcpy, and consequently it
  2031. * cannot be used to copy between overlapping memory regions.
  2032. */
  2033. inline std::size_t buffer_copy(const mutable_buffer& target,
  2034. const const_buffer& source,
  2035. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2036. {
  2037. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2038. }
  2039. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2040. /**
  2041. * @param target A modifiable buffer representing the memory region to which
  2042. * the bytes will be copied.
  2043. *
  2044. * @param source A non-modifiable buffer representing the memory region from
  2045. * which the bytes will be copied.
  2046. *
  2047. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2048. *
  2049. * @returns The number of bytes copied.
  2050. *
  2051. * @note The number of bytes copied is the lesser of:
  2052. *
  2053. * @li @c buffer_size(target)
  2054. *
  2055. * @li @c buffer_size(source)
  2056. *
  2057. * @li @c max_bytes_to_copy
  2058. *
  2059. * This function is implemented in terms of @c memcpy, and consequently it
  2060. * cannot be used to copy between overlapping memory regions.
  2061. */
  2062. inline std::size_t buffer_copy(const mutable_buffer& target,
  2063. const const_buffers_1& source,
  2064. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2065. {
  2066. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2067. }
  2068. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2069. /**
  2070. * @param target A modifiable buffer representing the memory region to which
  2071. * the bytes will be copied.
  2072. *
  2073. * @param source A modifiable buffer representing the memory region from which
  2074. * the bytes will be copied. The contents of the source buffer will not be
  2075. * modified.
  2076. *
  2077. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2078. *
  2079. * @returns The number of bytes copied.
  2080. *
  2081. * @note The number of bytes copied is the lesser of:
  2082. *
  2083. * @li @c buffer_size(target)
  2084. *
  2085. * @li @c buffer_size(source)
  2086. *
  2087. * @li @c max_bytes_to_copy
  2088. *
  2089. * This function is implemented in terms of @c memcpy, and consequently it
  2090. * cannot be used to copy between overlapping memory regions.
  2091. */
  2092. inline std::size_t buffer_copy(const mutable_buffer& target,
  2093. const mutable_buffer& source,
  2094. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2095. {
  2096. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2097. }
  2098. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2099. /**
  2100. * @param target A modifiable buffer representing the memory region to which
  2101. * the bytes will be copied.
  2102. *
  2103. * @param source A modifiable buffer representing the memory region from which
  2104. * the bytes will be copied. The contents of the source buffer will not be
  2105. * modified.
  2106. *
  2107. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2108. *
  2109. * @returns The number of bytes copied.
  2110. *
  2111. * @note The number of bytes copied is the lesser of:
  2112. *
  2113. * @li @c buffer_size(target)
  2114. *
  2115. * @li @c buffer_size(source)
  2116. *
  2117. * @li @c max_bytes_to_copy
  2118. *
  2119. * This function is implemented in terms of @c memcpy, and consequently it
  2120. * cannot be used to copy between overlapping memory regions.
  2121. */
  2122. inline std::size_t buffer_copy(const mutable_buffer& target,
  2123. const mutable_buffers_1& source,
  2124. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2125. {
  2126. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2127. }
  2128. /// Copies a limited number of bytes from a source buffer sequence to a target
  2129. /// buffer.
  2130. /**
  2131. * @param target A modifiable buffer representing the memory region to which
  2132. * the bytes will be copied.
  2133. *
  2134. * @param source A non-modifiable buffer sequence representing the memory
  2135. * regions from which the bytes will be copied.
  2136. *
  2137. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2138. *
  2139. * @returns The number of bytes copied.
  2140. *
  2141. * @note The number of bytes copied is the lesser of:
  2142. *
  2143. * @li @c buffer_size(target)
  2144. *
  2145. * @li @c buffer_size(source)
  2146. *
  2147. * @li @c max_bytes_to_copy
  2148. *
  2149. * This function is implemented in terms of @c memcpy, and consequently it
  2150. * cannot be used to copy between overlapping memory regions.
  2151. */
  2152. template <typename ConstBufferSequence>
  2153. inline std::size_t buffer_copy(const mutable_buffer& target,
  2154. const ConstBufferSequence& source, std::size_t max_bytes_to_copy,
  2155. typename enable_if<
  2156. is_const_buffer_sequence<ConstBufferSequence>::value
  2157. >::type* = 0) ASIO_NOEXCEPT
  2158. {
  2159. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2160. }
  2161. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2162. /**
  2163. * @param target A modifiable buffer representing the memory region to which
  2164. * the bytes will be copied.
  2165. *
  2166. * @param source A non-modifiable buffer representing the memory region from
  2167. * which the bytes will be copied.
  2168. *
  2169. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2170. *
  2171. * @returns The number of bytes copied.
  2172. *
  2173. * @note The number of bytes copied is the lesser of:
  2174. *
  2175. * @li @c buffer_size(target)
  2176. *
  2177. * @li @c buffer_size(source)
  2178. *
  2179. * @li @c max_bytes_to_copy
  2180. *
  2181. * This function is implemented in terms of @c memcpy, and consequently it
  2182. * cannot be used to copy between overlapping memory regions.
  2183. */
  2184. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  2185. const const_buffer& source,
  2186. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2187. {
  2188. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2189. }
  2190. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2191. /**
  2192. * @param target A modifiable buffer representing the memory region to which
  2193. * the bytes will be copied.
  2194. *
  2195. * @param source A non-modifiable buffer representing the memory region from
  2196. * which the bytes will be copied.
  2197. *
  2198. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2199. *
  2200. * @returns The number of bytes copied.
  2201. *
  2202. * @note The number of bytes copied is the lesser of:
  2203. *
  2204. * @li @c buffer_size(target)
  2205. *
  2206. * @li @c buffer_size(source)
  2207. *
  2208. * @li @c max_bytes_to_copy
  2209. *
  2210. * This function is implemented in terms of @c memcpy, and consequently it
  2211. * cannot be used to copy between overlapping memory regions.
  2212. */
  2213. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  2214. const const_buffers_1& source,
  2215. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2216. {
  2217. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2218. }
  2219. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2220. /**
  2221. * @param target A modifiable buffer representing the memory region to which
  2222. * the bytes will be copied.
  2223. *
  2224. * @param source A modifiable buffer representing the memory region from which
  2225. * the bytes will be copied. The contents of the source buffer will not be
  2226. * modified.
  2227. *
  2228. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2229. *
  2230. * @returns The number of bytes copied.
  2231. *
  2232. * @note The number of bytes copied is the lesser of:
  2233. *
  2234. * @li @c buffer_size(target)
  2235. *
  2236. * @li @c buffer_size(source)
  2237. *
  2238. * @li @c max_bytes_to_copy
  2239. *
  2240. * This function is implemented in terms of @c memcpy, and consequently it
  2241. * cannot be used to copy between overlapping memory regions.
  2242. */
  2243. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  2244. const mutable_buffer& source,
  2245. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2246. {
  2247. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2248. }
  2249. /// Copies a limited number of bytes from a source buffer to a target buffer.
  2250. /**
  2251. * @param target A modifiable buffer representing the memory region to which
  2252. * the bytes will be copied.
  2253. *
  2254. * @param source A modifiable buffer representing the memory region from which
  2255. * the bytes will be copied. The contents of the source buffer will not be
  2256. * modified.
  2257. *
  2258. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2259. *
  2260. * @returns The number of bytes copied.
  2261. *
  2262. * @note The number of bytes copied is the lesser of:
  2263. *
  2264. * @li @c buffer_size(target)
  2265. *
  2266. * @li @c buffer_size(source)
  2267. *
  2268. * @li @c max_bytes_to_copy
  2269. *
  2270. * This function is implemented in terms of @c memcpy, and consequently it
  2271. * cannot be used to copy between overlapping memory regions.
  2272. */
  2273. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  2274. const mutable_buffers_1& source,
  2275. std::size_t max_bytes_to_copy) ASIO_NOEXCEPT
  2276. {
  2277. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2278. }
  2279. /// Copies a limited number of bytes from a source buffer sequence to a target
  2280. /// buffer.
  2281. /**
  2282. * @param target A modifiable buffer representing the memory region to which
  2283. * the bytes will be copied.
  2284. *
  2285. * @param source A non-modifiable buffer sequence representing the memory
  2286. * regions from which the bytes will be copied.
  2287. *
  2288. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2289. *
  2290. * @returns The number of bytes copied.
  2291. *
  2292. * @note The number of bytes copied is the lesser of:
  2293. *
  2294. * @li @c buffer_size(target)
  2295. *
  2296. * @li @c buffer_size(source)
  2297. *
  2298. * @li @c max_bytes_to_copy
  2299. *
  2300. * This function is implemented in terms of @c memcpy, and consequently it
  2301. * cannot be used to copy between overlapping memory regions.
  2302. */
  2303. template <typename ConstBufferSequence>
  2304. inline std::size_t buffer_copy(const mutable_buffers_1& target,
  2305. const ConstBufferSequence& source, std::size_t max_bytes_to_copy,
  2306. typename enable_if<
  2307. is_const_buffer_sequence<ConstBufferSequence>::value
  2308. >::type* = 0) ASIO_NOEXCEPT
  2309. {
  2310. return buffer_copy(buffer(target, max_bytes_to_copy), source);
  2311. }
  2312. /// Copies a limited number of bytes from a source buffer to a target buffer
  2313. /// sequence.
  2314. /**
  2315. * @param target A modifiable buffer sequence representing the memory regions to
  2316. * which the bytes will be copied.
  2317. *
  2318. * @param source A non-modifiable buffer representing the memory region from
  2319. * which the bytes will be copied.
  2320. *
  2321. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2322. *
  2323. * @returns The number of bytes copied.
  2324. *
  2325. * @note The number of bytes copied is the lesser of:
  2326. *
  2327. * @li @c buffer_size(target)
  2328. *
  2329. * @li @c buffer_size(source)
  2330. *
  2331. * @li @c max_bytes_to_copy
  2332. *
  2333. * This function is implemented in terms of @c memcpy, and consequently it
  2334. * cannot be used to copy between overlapping memory regions.
  2335. */
  2336. template <typename MutableBufferSequence>
  2337. inline std::size_t buffer_copy(const MutableBufferSequence& target,
  2338. const const_buffer& source, std::size_t max_bytes_to_copy,
  2339. typename enable_if<
  2340. is_mutable_buffer_sequence<MutableBufferSequence>::value
  2341. >::type* = 0) ASIO_NOEXCEPT
  2342. {
  2343. return buffer_copy(target, buffer(source, max_bytes_to_copy));
  2344. }
  2345. /// Copies a limited number of bytes from a source buffer to a target buffer
  2346. /// sequence.
  2347. /**
  2348. * @param target A modifiable buffer sequence representing the memory regions to
  2349. * which the bytes will be copied.
  2350. *
  2351. * @param source A non-modifiable buffer representing the memory region from
  2352. * which the bytes will be copied.
  2353. *
  2354. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2355. *
  2356. * @returns The number of bytes copied.
  2357. *
  2358. * @note The number of bytes copied is the lesser of:
  2359. *
  2360. * @li @c buffer_size(target)
  2361. *
  2362. * @li @c buffer_size(source)
  2363. *
  2364. * @li @c max_bytes_to_copy
  2365. *
  2366. * This function is implemented in terms of @c memcpy, and consequently it
  2367. * cannot be used to copy between overlapping memory regions.
  2368. */
  2369. template <typename MutableBufferSequence>
  2370. inline std::size_t buffer_copy(const MutableBufferSequence& target,
  2371. const const_buffers_1& source, std::size_t max_bytes_to_copy,
  2372. typename enable_if<
  2373. is_mutable_buffer_sequence<MutableBufferSequence>::value
  2374. >::type* = 0) ASIO_NOEXCEPT
  2375. {
  2376. return buffer_copy(target, buffer(source, max_bytes_to_copy));
  2377. }
  2378. /// Copies a limited number of bytes from a source buffer to a target buffer
  2379. /// sequence.
  2380. /**
  2381. * @param target A modifiable buffer sequence representing the memory regions to
  2382. * which the bytes will be copied.
  2383. *
  2384. * @param source A modifiable buffer representing the memory region from which
  2385. * the bytes will be copied. The contents of the source buffer will not be
  2386. * modified.
  2387. *
  2388. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2389. *
  2390. * @returns The number of bytes copied.
  2391. *
  2392. * @note The number of bytes copied is the lesser of:
  2393. *
  2394. * @li @c buffer_size(target)
  2395. *
  2396. * @li @c buffer_size(source)
  2397. *
  2398. * @li @c max_bytes_to_copy
  2399. *
  2400. * This function is implemented in terms of @c memcpy, and consequently it
  2401. * cannot be used to copy between overlapping memory regions.
  2402. */
  2403. template <typename MutableBufferSequence>
  2404. inline std::size_t buffer_copy(const MutableBufferSequence& target,
  2405. const mutable_buffer& source, std::size_t max_bytes_to_copy,
  2406. typename enable_if<
  2407. is_mutable_buffer_sequence<MutableBufferSequence>::value
  2408. >::type* = 0) ASIO_NOEXCEPT
  2409. {
  2410. return buffer_copy(target, buffer(source, max_bytes_to_copy));
  2411. }
  2412. /// Copies a limited number of bytes from a source buffer to a target buffer
  2413. /// sequence.
  2414. /**
  2415. * @param target A modifiable buffer sequence representing the memory regions to
  2416. * which the bytes will be copied.
  2417. *
  2418. * @param source A modifiable buffer representing the memory region from which
  2419. * the bytes will be copied. The contents of the source buffer will not be
  2420. * modified.
  2421. *
  2422. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2423. *
  2424. * @returns The number of bytes copied.
  2425. *
  2426. * @note The number of bytes copied is the lesser of:
  2427. *
  2428. * @li @c buffer_size(target)
  2429. *
  2430. * @li @c buffer_size(source)
  2431. *
  2432. * @li @c max_bytes_to_copy
  2433. *
  2434. * This function is implemented in terms of @c memcpy, and consequently it
  2435. * cannot be used to copy between overlapping memory regions.
  2436. */
  2437. template <typename MutableBufferSequence>
  2438. inline std::size_t buffer_copy(const MutableBufferSequence& target,
  2439. const mutable_buffers_1& source, std::size_t max_bytes_to_copy,
  2440. typename enable_if<
  2441. is_mutable_buffer_sequence<MutableBufferSequence>::value
  2442. >::type* = 0) ASIO_NOEXCEPT
  2443. {
  2444. return buffer_copy(target, buffer(source, max_bytes_to_copy));
  2445. }
  2446. /// Copies a limited number of bytes from a source buffer sequence to a target
  2447. /// buffer sequence.
  2448. /**
  2449. * @param target A modifiable buffer sequence representing the memory regions to
  2450. * which the bytes will be copied.
  2451. *
  2452. * @param source A non-modifiable buffer sequence representing the memory
  2453. * regions from which the bytes will be copied.
  2454. *
  2455. * @param max_bytes_to_copy The maximum number of bytes to be copied.
  2456. *
  2457. * @returns The number of bytes copied.
  2458. *
  2459. * @note The number of bytes copied is the lesser of:
  2460. *
  2461. * @li @c buffer_size(target)
  2462. *
  2463. * @li @c buffer_size(source)
  2464. *
  2465. * @li @c max_bytes_to_copy
  2466. *
  2467. * This function is implemented in terms of @c memcpy, and consequently it
  2468. * cannot be used to copy between overlapping memory regions.
  2469. */
  2470. template <typename MutableBufferSequence, typename ConstBufferSequence>
  2471. std::size_t buffer_copy(const MutableBufferSequence& target,
  2472. const ConstBufferSequence& source, std::size_t max_bytes_to_copy,
  2473. typename enable_if<
  2474. is_mutable_buffer_sequence<MutableBufferSequence>::value &&
  2475. is_const_buffer_sequence<ConstBufferSequence>::value
  2476. >::type* = 0) ASIO_NOEXCEPT
  2477. {
  2478. std::size_t total_bytes_copied = 0;
  2479. typename MutableBufferSequence::const_iterator target_iter = target.begin();
  2480. typename MutableBufferSequence::const_iterator target_end = target.end();
  2481. std::size_t target_buffer_offset = 0;
  2482. typename ConstBufferSequence::const_iterator source_iter = source.begin();
  2483. typename ConstBufferSequence::const_iterator source_end = source.end();
  2484. std::size_t source_buffer_offset = 0;
  2485. while (total_bytes_copied != max_bytes_to_copy
  2486. && target_iter != target_end && source_iter != source_end)
  2487. {
  2488. mutable_buffer target_buffer =
  2489. mutable_buffer(*target_iter) + target_buffer_offset;
  2490. const_buffer source_buffer =
  2491. const_buffer(*source_iter) + source_buffer_offset;
  2492. std::size_t bytes_copied = buffer_copy(target_buffer,
  2493. source_buffer, max_bytes_to_copy - total_bytes_copied);
  2494. total_bytes_copied += bytes_copied;
  2495. if (bytes_copied == target_buffer.size())
  2496. {
  2497. ++target_iter;
  2498. target_buffer_offset = 0;
  2499. }
  2500. else
  2501. target_buffer_offset += bytes_copied;
  2502. if (bytes_copied == source_buffer.size())
  2503. {
  2504. ++source_iter;
  2505. source_buffer_offset = 0;
  2506. }
  2507. else
  2508. source_buffer_offset += bytes_copied;
  2509. }
  2510. return total_bytes_copied;
  2511. }
  2512. /*@}*/
  2513. } // namespace asio
  2514. #include "asio/detail/pop_options.hpp"
  2515. #endif // ASIO_BUFFER_HPP