diff --git a/CHANGELOG.md b/CHANGELOG.md index d6edf790..faf23487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 195: * net is a namespace alias for boost::asio +* Simplify multi_buffer and static_buffer sequences -------------------------------------------------------------------------------- diff --git a/include/boost/beast/core/impl/multi_buffer.ipp b/include/boost/beast/core/impl/multi_buffer.ipp index d1e0214f..b548fd18 100644 --- a/include/boost/beast/core/impl/multi_buffer.ipp +++ b/include/boost/beast/core/impl/multi_buffer.ipp @@ -149,19 +149,28 @@ public: class const_iterator; readable_bytes() = delete; + readable_bytes(readable_bytes const&) = default; readable_bytes& operator=(readable_bytes const&) = default; template< - bool OtherIsMutable, - bool ThisIsMutable = IsMutable> + bool IsMutable_ = IsMutable, + class = typename std::enable_if::type> readable_bytes( - readable_bytes const& other, - typename std::enable_if< - ! ThisIsMutable || OtherIsMutable>::type* = 0) + readable_bytes const& other) noexcept : b_(other.b_) { } + template< + bool IsMutable_ = IsMutable, + class = typename std::enable_if::type> + readable_bytes& operator=( + readable_bytes const& other) noexcept + { + b_ = other.b_; + return *this; + } + const_iterator begin() const noexcept; const_iterator end() const noexcept; diff --git a/include/boost/beast/core/static_buffer.hpp b/include/boost/beast/core/static_buffer.hpp index 40703b96..a98d5c70 100644 --- a/include/boost/beast/core/static_buffer.hpp +++ b/include/boost/beast/core/static_buffer.hpp @@ -21,6 +21,74 @@ namespace boost { namespace beast { +namespace detail { + +template +class buffers_pair +{ + template + friend class buffers_pair; + +public: + // VFALCO: This type is public otherwise + // asio::buffers_iterator won't compile. + using value_type = typename + std::conditional::type; + + using const_iterator = + value_type const*; + + buffers_pair() = default; + buffers_pair(buffers_pair const&) = default; + buffers_pair& operator=(buffers_pair const&) = default; + + template< + bool IsMutable_ = IsMutable, + class = typename std::enable_if::type> + buffers_pair(buffers_pair const& other) noexcept + : b_{other.b_[0], other.b_[1]} + { + } + + template< + bool IsMutable_ = IsMutable, + class = typename std::enable_if::type> + buffers_pair& operator=( + buffers_pair const& other) noexcept + { + b_ = {other.b_[0], other.b_[1]}; + return *this; + } + + value_type& + operator[](int i) noexcept + { + BOOST_ASSERT(i >= 0 && i < 2); + return b_[i]; + } + + const_iterator + begin() const noexcept + { + return &b_[0]; + } + + const_iterator + end() const noexcept + { + if(b_[1].size() > 0) + return &b_[2]; + else + return &b_[1]; + } +private: + value_type b_[2]; +}; + +} // detail + /** A dynamic buffer providing a fixed, circular buffer. A dynamic buffer encapsulates memory storage that may be @@ -60,94 +128,6 @@ class static_buffer_base std::size_t out_size_ = 0; std::size_t capacity_; - class const_buffer_pair; - - class mutable_buffer_pair - { - net::mutable_buffer b_[2]; - - friend class const_buffer_pair; - - public: - using const_iterator = - net::mutable_buffer const*; - - // workaround for buffers_iterator bug - using value_type = - net::mutable_buffer; - - mutable_buffer_pair() = default; - mutable_buffer_pair( - mutable_buffer_pair const&) = default; - - net::mutable_buffer& - operator[](int i) noexcept - { - BOOST_ASSERT(i >= 0 && i < 2); - return b_[i]; - } - - const_iterator - begin() const noexcept - { - return &b_[0]; - } - - const_iterator - end() const noexcept - { - if(b_[1].size() > 0) - return &b_[2]; - else - return &b_[1]; - } - }; - - class const_buffer_pair - { - net::const_buffer b_[2]; - - public: - using const_iterator = - net::const_buffer const*; - - // workaround for buffers_iterator bug - using value_type = - net::const_buffer; - - const_buffer_pair() = default; - const_buffer_pair( - const_buffer_pair const&) = default; - - const_buffer_pair( - mutable_buffer_pair const& other) - : b_{other.b_[0], other.b_[1]} - { - } - - net::const_buffer& - operator[](int i) noexcept - { - BOOST_ASSERT(i >= 0 && i < 2); - return b_[i]; - } - - const_iterator - begin() const noexcept - { - return &b_[0]; - } - - const_iterator - end() const noexcept - { - if(b_[1].size() > 0) - return &b_[2]; - else - return &b_[1]; - } - }; - static_buffer_base(static_buffer_base const& other) = delete; static_buffer_base& operator=(static_buffer_base const&) = delete; @@ -174,9 +154,9 @@ public: /// The MutableBufferSequence used to represent the writable bytes. using mutable_buffers_type = __implementation_defined__; #else - using const_buffers_type = const_buffer_pair; - using mutable_data_type = mutable_buffer_pair; - using mutable_buffers_type = mutable_buffer_pair; + using const_buffers_type = detail::buffers_pair; + using mutable_data_type = detail::buffers_pair; + using mutable_buffers_type = detail::buffers_pair; #endif /// Returns the number of readable bytes. diff --git a/test/beast/core/multi_buffer.cpp b/test/beast/core/multi_buffer.cpp index ebe5dd40..af56030c 100644 --- a/test/beast/core/multi_buffer.cpp +++ b/test/beast/core/multi_buffer.cpp @@ -46,6 +46,12 @@ public: BOOST_STATIC_ASSERT(std::is_convertible< multi_buffer::mutable_data_type, multi_buffer::const_buffers_type>::value); +#if ! defined( BOOST_LIBSTDCXX_VERSION ) || BOOST_LIBSTDCXX_VERSION >= 50000 + BOOST_STATIC_ASSERT(std::is_trivially_copyable< + multi_buffer::const_buffers_type>::value); + BOOST_STATIC_ASSERT(std::is_trivially_copyable< + multi_buffer::mutable_data_type>::value); +#endif template static diff --git a/test/beast/core/static_buffer.cpp b/test/beast/core/static_buffer.cpp index 1d4d3ef1..df424b90 100644 --- a/test/beast/core/static_buffer.cpp +++ b/test/beast/core/static_buffer.cpp @@ -44,6 +44,14 @@ public: BOOST_STATIC_ASSERT(std::is_convertible< static_buffer_base::mutable_data_type, static_buffer_base::const_buffers_type>::value); +#if ! defined( BOOST_LIBSTDCXX_VERSION ) || BOOST_LIBSTDCXX_VERSION >= 50000 +# ifndef BOOST_ASIO_ENABLE_BUFFER_DEBUGGING + BOOST_STATIC_ASSERT(std::is_trivially_copyable< + static_buffer_base::const_buffers_type>::value); + BOOST_STATIC_ASSERT(std::is_trivially_copyable< + static_buffer_base::mutable_data_type>::value); +# endif +#endif template void