buffer.hpp 65 KB

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