buffer.hpp 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  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_v2 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