buffer.hpp 95 KB

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