registered_buffer.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // registered_buffer.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_REGISTERED_BUFFER_HPP
  11. #define BOOST_ASIO_REGISTERED_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 <boost/asio/buffer.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. class buffer_registration_base;
  22. } // namespace detail
  23. class const_registered_buffer;
  24. /// Type used to identify a registered buffer.
  25. class registered_buffer_id
  26. {
  27. public:
  28. /// The native buffer identifier type.
  29. typedef int native_handle_type;
  30. /// Default constructor creates an invalid registered buffer identifier.
  31. registered_buffer_id() BOOST_ASIO_NOEXCEPT
  32. : scope_(0),
  33. index_(-1)
  34. {
  35. }
  36. /// Get the native buffer identifier type.
  37. native_handle_type native_handle() const BOOST_ASIO_NOEXCEPT
  38. {
  39. return index_;
  40. }
  41. /// Compare two IDs for equality.
  42. friend bool operator==(const registered_buffer_id& lhs,
  43. const registered_buffer_id& rhs) BOOST_ASIO_NOEXCEPT
  44. {
  45. return lhs.scope_ == rhs.scope_ && lhs.index_ == rhs.index_;
  46. }
  47. /// Compare two IDs for equality.
  48. friend bool operator!=(const registered_buffer_id& lhs,
  49. const registered_buffer_id& rhs) BOOST_ASIO_NOEXCEPT
  50. {
  51. return lhs.scope_ != rhs.scope_ || lhs.index_ != rhs.index_;
  52. }
  53. private:
  54. friend class detail::buffer_registration_base;
  55. // Hidden constructor used by buffer registration.
  56. registered_buffer_id(const void* scope, int index) BOOST_ASIO_NOEXCEPT
  57. : scope_(scope),
  58. index_(index)
  59. {
  60. }
  61. const void* scope_;
  62. int index_;
  63. };
  64. /// Holds a registered buffer over modifiable data.
  65. /**
  66. * Satisfies the @c MutableBufferSequence type requirements.
  67. */
  68. class mutable_registered_buffer
  69. {
  70. public:
  71. #if !defined(BOOST_ASIO_HAS_DECLTYPE) \
  72. && !defined(GENERATING_DOCUMENTATION)
  73. typedef mutable_buffer value_type;
  74. #endif // !defined(BOOST_ASIO_HAS_DECLTYPE)
  75. // && !defined(GENERATING_DOCUMENTATION)
  76. /// Default constructor creates an invalid registered buffer.
  77. mutable_registered_buffer() BOOST_ASIO_NOEXCEPT
  78. : buffer_(),
  79. id_()
  80. {
  81. }
  82. /// Get the underlying mutable buffer.
  83. const mutable_buffer& buffer() const BOOST_ASIO_NOEXCEPT
  84. {
  85. return buffer_;
  86. }
  87. /// Get a pointer to the beginning of the memory range.
  88. /**
  89. * @returns <tt>buffer().data()</tt>.
  90. */
  91. void* data() const BOOST_ASIO_NOEXCEPT
  92. {
  93. return buffer_.data();
  94. }
  95. /// Get the size of the memory range.
  96. /**
  97. * @returns <tt>buffer().size()</tt>.
  98. */
  99. std::size_t size() const BOOST_ASIO_NOEXCEPT
  100. {
  101. return buffer_.size();
  102. }
  103. /// Get the registered buffer identifier.
  104. const registered_buffer_id& id() const BOOST_ASIO_NOEXCEPT
  105. {
  106. return id_;
  107. }
  108. /// Move the start of the buffer by the specified number of bytes.
  109. mutable_registered_buffer& operator+=(std::size_t n) BOOST_ASIO_NOEXCEPT
  110. {
  111. buffer_ += n;
  112. return *this;
  113. }
  114. private:
  115. friend class detail::buffer_registration_base;
  116. // Hidden constructor used by buffer registration and operators.
  117. mutable_registered_buffer(const mutable_buffer& b,
  118. const registered_buffer_id& i) BOOST_ASIO_NOEXCEPT
  119. : buffer_(b),
  120. id_(i)
  121. {
  122. }
  123. #if !defined(GENERATING_DOCUMENTATION)
  124. friend mutable_registered_buffer buffer(
  125. const mutable_registered_buffer& b, std::size_t n) BOOST_ASIO_NOEXCEPT;
  126. #endif // !defined(GENERATING_DOCUMENTATION)
  127. mutable_buffer buffer_;
  128. registered_buffer_id id_;
  129. };
  130. /// Holds a registered buffer over non-modifiable data.
  131. /**
  132. * Satisfies the @c ConstBufferSequence type requirements.
  133. */
  134. class const_registered_buffer
  135. {
  136. public:
  137. #if !defined(BOOST_ASIO_HAS_DECLTYPE) \
  138. && !defined(GENERATING_DOCUMENTATION)
  139. typedef const_buffer value_type;
  140. #endif // !defined(BOOST_ASIO_HAS_DECLTYPE)
  141. // && !defined(GENERATING_DOCUMENTATION)
  142. /// Default constructor creates an invalid registered buffer.
  143. const_registered_buffer() BOOST_ASIO_NOEXCEPT
  144. : buffer_(),
  145. id_()
  146. {
  147. }
  148. /// Construct a non-modifiable buffer from a modifiable one.
  149. const_registered_buffer(
  150. const mutable_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  151. : buffer_(b.buffer()),
  152. id_(b.id())
  153. {
  154. }
  155. /// Get the underlying constant buffer.
  156. const const_buffer& buffer() const BOOST_ASIO_NOEXCEPT
  157. {
  158. return buffer_;
  159. }
  160. /// Get a pointer to the beginning of the memory range.
  161. /**
  162. * @returns <tt>buffer().data()</tt>.
  163. */
  164. const void* data() const BOOST_ASIO_NOEXCEPT
  165. {
  166. return buffer_.data();
  167. }
  168. /// Get the size of the memory range.
  169. /**
  170. * @returns <tt>buffer().size()</tt>.
  171. */
  172. std::size_t size() const BOOST_ASIO_NOEXCEPT
  173. {
  174. return buffer_.size();
  175. }
  176. /// Get the registered buffer identifier.
  177. const registered_buffer_id& id() const BOOST_ASIO_NOEXCEPT
  178. {
  179. return id_;
  180. }
  181. /// Move the start of the buffer by the specified number of bytes.
  182. const_registered_buffer& operator+=(std::size_t n) BOOST_ASIO_NOEXCEPT
  183. {
  184. buffer_ += n;
  185. return *this;
  186. }
  187. private:
  188. // Hidden constructor used by operators.
  189. const_registered_buffer(const const_buffer& b,
  190. const registered_buffer_id& i) BOOST_ASIO_NOEXCEPT
  191. : buffer_(b),
  192. id_(i)
  193. {
  194. }
  195. #if !defined(GENERATING_DOCUMENTATION)
  196. friend const_registered_buffer buffer(
  197. const const_registered_buffer& b, std::size_t n) BOOST_ASIO_NOEXCEPT;
  198. #endif // !defined(GENERATING_DOCUMENTATION)
  199. const_buffer buffer_;
  200. registered_buffer_id id_;
  201. };
  202. /** @addtogroup buffer_sequence_begin */
  203. /// Get an iterator to the first element in a buffer sequence.
  204. inline const mutable_buffer* buffer_sequence_begin(
  205. const mutable_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  206. {
  207. return &b.buffer();
  208. }
  209. /// Get an iterator to the first element in a buffer sequence.
  210. inline const const_buffer* buffer_sequence_begin(
  211. const const_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  212. {
  213. return &b.buffer();
  214. }
  215. /** @} */
  216. /** @addtogroup buffer_sequence_end */
  217. /// Get an iterator to one past the end element in a buffer sequence.
  218. inline const mutable_buffer* buffer_sequence_end(
  219. const mutable_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  220. {
  221. return &b.buffer() + 1;
  222. }
  223. /// Get an iterator to one past the end element in a buffer sequence.
  224. inline const const_buffer* buffer_sequence_end(
  225. const const_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  226. {
  227. return &b.buffer() + 1;
  228. }
  229. /** @} */
  230. /** @addtogroup buffer */
  231. /// Obtain a buffer representing the entire registered buffer.
  232. inline mutable_registered_buffer buffer(
  233. const mutable_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  234. {
  235. return b;
  236. }
  237. /// Obtain a buffer representing the entire registered buffer.
  238. inline const_registered_buffer buffer(
  239. const const_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  240. {
  241. return b;
  242. }
  243. /// Obtain a buffer representing part of a registered buffer.
  244. inline mutable_registered_buffer buffer(
  245. const mutable_registered_buffer& b, std::size_t n) BOOST_ASIO_NOEXCEPT
  246. {
  247. return mutable_registered_buffer(buffer(b.buffer_, n), b.id_);
  248. }
  249. /// Obtain a buffer representing part of a registered buffer.
  250. inline const_registered_buffer buffer(
  251. const const_registered_buffer& b, std::size_t n) BOOST_ASIO_NOEXCEPT
  252. {
  253. return const_registered_buffer(buffer(b.buffer_, n), b.id_);
  254. }
  255. /** @} */
  256. /// Create a new modifiable registered buffer that is offset from the start of
  257. /// another.
  258. /**
  259. * @relates mutable_registered_buffer
  260. */
  261. inline mutable_registered_buffer operator+(
  262. const mutable_registered_buffer& b, std::size_t n) BOOST_ASIO_NOEXCEPT
  263. {
  264. mutable_registered_buffer tmp(b);
  265. tmp += n;
  266. return tmp;
  267. }
  268. /// Create a new modifiable buffer that is offset from the start of another.
  269. /**
  270. * @relates mutable_registered_buffer
  271. */
  272. inline mutable_registered_buffer operator+(std::size_t n,
  273. const mutable_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  274. {
  275. return b + n;
  276. }
  277. /// Create a new non-modifiable registered buffer that is offset from the start
  278. /// of another.
  279. /**
  280. * @relates const_registered_buffer
  281. */
  282. inline const_registered_buffer operator+(const const_registered_buffer& b,
  283. std::size_t n) BOOST_ASIO_NOEXCEPT
  284. {
  285. const_registered_buffer tmp(b);
  286. tmp += n;
  287. return tmp;
  288. }
  289. /// Create a new non-modifiable buffer that is offset from the start of another.
  290. /**
  291. * @relates const_registered_buffer
  292. */
  293. inline const_registered_buffer operator+(std::size_t n,
  294. const const_registered_buffer& b) BOOST_ASIO_NOEXCEPT
  295. {
  296. return b + n;
  297. }
  298. } // namespace asio
  299. } // namespace boost
  300. #include <boost/asio/detail/pop_options.hpp>
  301. #endif // BOOST_ASIO_REGISTERED_BUFFER_HPP