Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:11

0001 //
0002 // registered_buffer.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_REGISTERED_BUFFER_HPP
0012 #define BOOST_ASIO_REGISTERED_BUFFER_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/buffer.hpp>
0020 
0021 #include <boost/asio/detail/push_options.hpp>
0022 
0023 namespace boost {
0024 namespace asio {
0025 namespace detail {
0026 
0027 class buffer_registration_base;
0028 
0029 } // namespace detail
0030 
0031 class const_registered_buffer;
0032 
0033 /// Type used to identify a registered buffer.
0034 class registered_buffer_id
0035 {
0036 public:
0037   /// The native buffer identifier type.
0038   typedef int native_handle_type;
0039 
0040   /// Default constructor creates an invalid registered buffer identifier.
0041   registered_buffer_id() noexcept
0042     : scope_(0),
0043       index_(-1)
0044   {
0045   }
0046 
0047   /// Get the native buffer identifier type.
0048   native_handle_type native_handle() const noexcept
0049   {
0050     return index_;
0051   }
0052 
0053   /// Compare two IDs for equality.
0054   friend bool operator==(const registered_buffer_id& lhs,
0055       const registered_buffer_id& rhs) noexcept
0056   {
0057     return lhs.scope_ == rhs.scope_ && lhs.index_ == rhs.index_;
0058   }
0059 
0060   /// Compare two IDs for equality.
0061   friend bool operator!=(const registered_buffer_id& lhs,
0062       const registered_buffer_id& rhs) noexcept
0063   {
0064     return lhs.scope_ != rhs.scope_ || lhs.index_ != rhs.index_;
0065   }
0066 
0067 private:
0068   friend class detail::buffer_registration_base;
0069 
0070   // Hidden constructor used by buffer registration.
0071   registered_buffer_id(const void* scope, int index) noexcept
0072     : scope_(scope),
0073       index_(index)
0074   {
0075   }
0076 
0077   const void* scope_;
0078   int index_;
0079 };
0080 
0081 /// Holds a registered buffer over modifiable data.
0082 /** 
0083  * Satisfies the @c MutableBufferSequence type requirements.
0084  */
0085 class mutable_registered_buffer
0086 {
0087 public:
0088   /// Default constructor creates an invalid registered buffer.
0089   mutable_registered_buffer() noexcept
0090     : buffer_(),
0091       id_()
0092   {
0093   }
0094 
0095   /// Get the underlying mutable buffer.
0096   const mutable_buffer& buffer() const noexcept
0097   {
0098     return buffer_;
0099   }
0100 
0101   /// Get a pointer to the beginning of the memory range.
0102   /**
0103    * @returns <tt>buffer().data()</tt>.
0104    */
0105   void* data() const noexcept
0106   {
0107     return buffer_.data();
0108   }
0109 
0110   /// Get the size of the memory range.
0111   /**
0112    * @returns <tt>buffer().size()</tt>.
0113    */
0114   std::size_t size() const noexcept
0115   {
0116     return buffer_.size();
0117   }
0118 
0119   /// Get the registered buffer identifier.
0120   const registered_buffer_id& id() const noexcept
0121   {
0122     return id_;
0123   }
0124 
0125   /// Move the start of the buffer by the specified number of bytes.
0126   mutable_registered_buffer& operator+=(std::size_t n) noexcept
0127   {
0128     buffer_ += n;
0129     return *this;
0130   }
0131 
0132 private:
0133   friend class detail::buffer_registration_base;
0134 
0135   // Hidden constructor used by buffer registration and operators.
0136   mutable_registered_buffer(const mutable_buffer& b,
0137       const registered_buffer_id& i) noexcept
0138     : buffer_(b),
0139       id_(i)
0140   {
0141   }
0142 
0143 #if !defined(GENERATING_DOCUMENTATION)
0144   friend mutable_registered_buffer buffer(
0145       const mutable_registered_buffer& b, std::size_t n) noexcept;
0146 #endif // !defined(GENERATING_DOCUMENTATION)
0147 
0148   mutable_buffer buffer_;
0149   registered_buffer_id id_;
0150 };
0151 
0152 /// Holds a registered buffer over non-modifiable data.
0153 /** 
0154  * Satisfies the @c ConstBufferSequence type requirements.
0155  */
0156 class const_registered_buffer
0157 {
0158 public:
0159   /// Default constructor creates an invalid registered buffer.
0160   const_registered_buffer() noexcept
0161     : buffer_(),
0162       id_()
0163   {
0164   }
0165 
0166   /// Construct a non-modifiable buffer from a modifiable one.
0167   const_registered_buffer(
0168       const mutable_registered_buffer& b) noexcept
0169     : buffer_(b.buffer()),
0170       id_(b.id())
0171   {
0172   }
0173 
0174   /// Get the underlying constant buffer.
0175   const const_buffer& buffer() const noexcept
0176   {
0177     return buffer_;
0178   }
0179 
0180   /// Get a pointer to the beginning of the memory range.
0181   /**
0182    * @returns <tt>buffer().data()</tt>.
0183    */
0184   const void* data() const noexcept
0185   {
0186     return buffer_.data();
0187   }
0188 
0189   /// Get the size of the memory range.
0190   /**
0191    * @returns <tt>buffer().size()</tt>.
0192    */
0193   std::size_t size() const noexcept
0194   {
0195     return buffer_.size();
0196   }
0197 
0198   /// Get the registered buffer identifier.
0199   const registered_buffer_id& id() const noexcept
0200   {
0201     return id_;
0202   }
0203 
0204   /// Move the start of the buffer by the specified number of bytes.
0205   const_registered_buffer& operator+=(std::size_t n) noexcept
0206   {
0207     buffer_ += n;
0208     return *this;
0209   }
0210 
0211 private:
0212   // Hidden constructor used by operators.
0213   const_registered_buffer(const const_buffer& b,
0214       const registered_buffer_id& i) noexcept
0215     : buffer_(b),
0216       id_(i)
0217   {
0218   }
0219 
0220 #if !defined(GENERATING_DOCUMENTATION)
0221   friend const_registered_buffer buffer(
0222       const const_registered_buffer& b, std::size_t n) noexcept;
0223 #endif // !defined(GENERATING_DOCUMENTATION)
0224 
0225   const_buffer buffer_;
0226   registered_buffer_id id_;
0227 };
0228 
0229 /** @addtogroup buffer_sequence_begin */
0230 
0231 /// Get an iterator to the first element in a buffer sequence.
0232 inline const mutable_buffer* buffer_sequence_begin(
0233     const mutable_registered_buffer& b) noexcept
0234 {
0235   return &b.buffer();
0236 }
0237 
0238 /// Get an iterator to the first element in a buffer sequence.
0239 inline const const_buffer* buffer_sequence_begin(
0240     const const_registered_buffer& b) noexcept
0241 {
0242   return &b.buffer();
0243 }
0244 
0245 /** @} */
0246 /** @addtogroup buffer_sequence_end */
0247 
0248 /// Get an iterator to one past the end element in a buffer sequence.
0249 inline const mutable_buffer* buffer_sequence_end(
0250     const mutable_registered_buffer& b) noexcept
0251 {
0252   return &b.buffer() + 1;
0253 }
0254 
0255 /// Get an iterator to one past the end element in a buffer sequence.
0256 inline const const_buffer* buffer_sequence_end(
0257     const const_registered_buffer& b) noexcept
0258 {
0259   return &b.buffer() + 1;
0260 }
0261 
0262 /** @} */
0263 /** @addtogroup buffer */
0264 
0265 /// Obtain a buffer representing the entire registered buffer.
0266 inline mutable_registered_buffer buffer(
0267     const mutable_registered_buffer& b) noexcept
0268 {
0269   return b;
0270 }
0271 
0272 /// Obtain a buffer representing the entire registered buffer.
0273 inline const_registered_buffer buffer(
0274     const const_registered_buffer& b) noexcept
0275 {
0276   return b;
0277 }
0278 
0279 /// Obtain a buffer representing part of a registered buffer.
0280 inline mutable_registered_buffer buffer(
0281     const mutable_registered_buffer& b, std::size_t n) noexcept
0282 {
0283   return mutable_registered_buffer(buffer(b.buffer_, n), b.id_);
0284 }
0285 
0286 /// Obtain a buffer representing part of a registered buffer.
0287 inline const_registered_buffer buffer(
0288     const const_registered_buffer& b, std::size_t n) noexcept
0289 {
0290   return const_registered_buffer(buffer(b.buffer_, n), b.id_);
0291 }
0292 
0293 /** @} */
0294 
0295 /// Create a new modifiable registered buffer that is offset from the start of
0296 /// another.
0297 /**
0298  * @relates mutable_registered_buffer
0299  */
0300 inline mutable_registered_buffer operator+(
0301     const mutable_registered_buffer& b, std::size_t n) noexcept
0302 {
0303   mutable_registered_buffer tmp(b);
0304   tmp += n;
0305   return tmp;
0306 }
0307 
0308 /// Create a new modifiable buffer that is offset from the start of another.
0309 /**
0310  * @relates mutable_registered_buffer
0311  */
0312 inline mutable_registered_buffer operator+(std::size_t n,
0313     const mutable_registered_buffer& b) noexcept
0314 {
0315   return b + n;
0316 }
0317 
0318 /// Create a new non-modifiable registered buffer that is offset from the start
0319 /// of another.
0320 /**
0321  * @relates const_registered_buffer
0322  */
0323 inline const_registered_buffer operator+(const const_registered_buffer& b,
0324     std::size_t n) noexcept
0325 {
0326   const_registered_buffer tmp(b);
0327   tmp += n;
0328   return tmp;
0329 }
0330 
0331 /// Create a new non-modifiable buffer that is offset from the start of another.
0332 /**
0333  * @relates const_registered_buffer
0334  */
0335 inline const_registered_buffer operator+(std::size_t n,
0336     const const_registered_buffer& b) noexcept
0337 {
0338   return b + n;
0339 }
0340 
0341 } // namespace asio
0342 } // namespace boost
0343 
0344 #include <boost/asio/detail/pop_options.hpp>
0345 
0346 #endif // BOOST_ASIO_REGISTERED_BUFFER_HPP