diff --git a/CHANGELOG.md b/CHANGELOG.md index 62648465..bf3f4179 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ * Documentation tidying * is_invocable works with move-only types +* Use std::function and reference wrapper WebSocket diff --git a/include/beast/http/impl/parser.ipp b/include/beast/http/impl/parser.ipp index dab34891..1e40a277 100644 --- a/include/beast/http/impl/parser.ipp +++ b/include/beast/http/impl/parser.ipp @@ -14,16 +14,6 @@ namespace beast { namespace http { -template -parser:: -~parser() -{ - if(cb_h_) - cb_h_->~cb_h_t(); - if(cb_b_) - cb_b_->~cb_b_t(); -} - template parser:: parser() @@ -31,22 +21,6 @@ parser() { } -template -parser:: -parser(parser&& other) - : base_type(std::move(other)) - , m_(other.m_) - , wr_(other.wr_) - , wr_inited_(other.wr_inited_) -{ - if(other.cb_h_) - cb_h_ = other.cb_h_->move( - &cb_h_buf_); - if(other.cb_b_) - cb_b_ = other.cb_h_->move( - &cb_b_buf_); -} - template template parser:: @@ -71,42 +45,6 @@ parser(parser&& other, "moved-from parser has a body"}); } -template -template -void -parser:: -on_chunk_header(Callback& cb) -{ - // Callback may not be constant, caller is responsible for - // managing the lifetime of the callback. Copies are not made. - BOOST_STATIC_ASSERT(! std::is_const::value); - - // Can't set the callback after receiving any chunk data! - BOOST_ASSERT(! wr_inited_); - - if(cb_h_) - cb_h_->~cb_h_t(); - cb_h_ = new(&cb_h_buf_) cb_h_t_impl(cb); -} - -template -template -void -parser:: -on_chunk_body(Callback& cb) -{ - // Callback may not be constant, caller is responsible for - // managing the lifetime of the callback. Copies are not made. - BOOST_STATIC_ASSERT(! std::is_const::value); - - // Can't set the callback after receiving any chunk data! - BOOST_ASSERT(! wr_inited_); - - if(cb_b_) - cb_b_->~cb_b_t(); - cb_b_ = new(&cb_b_buf_) cb_b_t_impl(cb); -} - } // http } // beast diff --git a/include/beast/http/parser.hpp b/include/beast/http/parser.hpp index 0d164c36..fff3c00f 100644 --- a/include/beast/http/parser.hpp +++ b/include/beast/http/parser.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -55,96 +56,6 @@ class parser template friend class parser; - struct cb_h_exemplar - { - void - operator()(std::uint64_t, string_view, error_code&); - }; - - struct cb_h_t - { - virtual ~cb_h_t() = default; - virtual cb_h_t* move(void* dest) = 0; - virtual void operator()( - std::uint64_t size, - string_view extensions, - error_code& ec) = 0; - }; - - template - struct cb_h_t_impl : cb_h_t - { - Callback& cb_; - - explicit - cb_h_t_impl(Callback& cb) - : cb_(cb) - { - } - - cb_h_t* - move(void* dest) override - { - new(dest) cb_h_t_impl< - Callback>(std::move(*this)); - return this; - } - - void - operator()( - std::uint64_t size, - string_view extensions, - error_code& ec) override - { - cb_(size, extensions, ec); - } - }; - - struct cb_b_exemplar - { - std::size_t - operator()(std::uint64_t, string_view, error_code&); - }; - - struct cb_b_t - { - virtual ~cb_b_t() = default; - virtual cb_b_t* move(void* dest) = 0; - virtual std::size_t operator()( - std::uint64_t remain, - string_view body, - error_code& ec) = 0; - }; - - template - struct cb_b_t_impl : cb_b_t - { - Callback& cb_; - - explicit - cb_b_t_impl(Callback& cb) - : cb_(cb) - { - } - - cb_b_t* - move(void* dest) override - { - new(dest) cb_b_t_impl< - Callback>(std::move(*this)); - return this; - } - - std::size_t - operator()( - std::uint64_t remain, - string_view body, - error_code& ec) override - { - return cb_(remain, body, ec); - } - }; - using base_type = basic_parser>; @@ -152,13 +63,15 @@ class parser typename Body::writer wr_; bool wr_inited_ = false; - cb_h_t* cb_h_ = nullptr; - typename std::aligned_storage< - sizeof(cb_h_t_impl)>::type cb_h_buf_; + std::function cb_h_; - cb_b_t* cb_b_ = nullptr; - typename std::aligned_storage< - sizeof(cb_b_t_impl)>::type cb_b_buf_; + std::function cb_b_; public: /// The type of message returned by the parser @@ -166,7 +79,7 @@ public: message>; /// Destructor - ~parser(); + ~parser() = default; /// Constructor parser(); @@ -182,7 +95,7 @@ public: After the move, the only valid operation on the moved-from object is destruction. */ - parser(parser&& other); + parser(parser&& other) = default; /** Constructor @@ -324,7 +237,17 @@ public: */ template void - on_chunk_header(Callback& cb); + on_chunk_header(Callback& cb) + { + // Callback may not be constant, caller is responsible for + // managing the lifetime of the callback. Copies are not made. + BOOST_STATIC_ASSERT(! std::is_const::value); + + // Can't set the callback after receiving any chunk data! + BOOST_ASSERT(! wr_inited_); + + cb_h_ = std::ref(cb); + } /** Set a callback to be invoked on chunk body data @@ -362,7 +285,17 @@ public: */ template void - on_chunk_body(Callback& cb); + on_chunk_body(Callback& cb) + { + // Callback may not be constant, caller is responsible for + // managing the lifetime of the callback. Copies are not made. + BOOST_STATIC_ASSERT(! std::is_const::value); + + // Can't set the callback after receiving any chunk data! + BOOST_ASSERT(! wr_inited_); + + cb_b_ = std::ref(cb); + } private: friend class basic_parser; @@ -460,7 +393,7 @@ private: error_code& ec) { if(cb_h_) - return (*cb_h_)(size, extensions, ec); + return cb_h_(size, extensions, ec); ec.assign(0, ec.category()); } @@ -471,7 +404,7 @@ private: error_code& ec) { if(cb_b_) - return (*cb_b_)(remain, body, ec); + return cb_b_(remain, body, ec); return wr_.put(boost::asio::buffer( body.data(), body.size()), ec); }