buffer.hpp 68 KB

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