buffer.hpp 81 KB

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