buffer.hpp 79 KB

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