buffer.hpp 82 KB

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