buffer.hpp 81 KB

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