buffer.hpp 88 KB

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