From a4eb96fcb93703356f0049d590703ecda5083cb1 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 18 Feb 2018 18:46:12 -0800 Subject: [PATCH] Don't use typeid --- CHANGELOG.md | 6 +++ .../beast/websocket/detail/stream_base.hpp | 47 +++++++++---------- include/boost/beast/websocket/impl/close.ipp | 2 + include/boost/beast/websocket/impl/ping.ipp | 2 + include/boost/beast/websocket/impl/read.ipp | 2 + include/boost/beast/websocket/impl/write.ipp | 2 + include/boost/beast/websocket/stream.hpp | 6 +-- test/beast/websocket/ping.cpp | 11 +---- 8 files changed, 40 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a630283d..cd1098e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +Version 156: + +* Don't use typeid + +-------------------------------------------------------------------------------- + Version 155: * Fix memory leak in advanced server examples diff --git a/include/boost/beast/websocket/detail/stream_base.hpp b/include/boost/beast/websocket/detail/stream_base.hpp index e8591870..2e93cce3 100644 --- a/include/boost/beast/websocket/detail/stream_base.hpp +++ b/include/boost/beast/websocket/detail/stream_base.hpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include @@ -27,57 +26,57 @@ namespace websocket { namespace detail { // used to order reads and writes -class type_mutex +class soft_mutex { - boost::typeindex::type_index ti_ = typeid(type_mutex); + int id_ = 0; public: - type_mutex() = default; - type_mutex(type_mutex const&) = delete; - type_mutex& operator=(type_mutex const&) = delete; + soft_mutex() = default; + soft_mutex(soft_mutex const&) = delete; + soft_mutex& operator=(soft_mutex const&) = delete; - type_mutex(type_mutex&& other) noexcept - : ti_(other.ti_) + soft_mutex(soft_mutex&& other) noexcept + : id_(other.id_) { - other.ti_ = boost::typeindex::type_id(); + other.id_ = 0; } - type_mutex& operator=(type_mutex&& other) noexcept + soft_mutex& operator=(soft_mutex&& other) noexcept { - ti_ = other.ti_; - other.ti_ = boost::typeindex::type_id(); + id_ = other.id_; + other.id_ = 0; return *this; } // VFALCO I'm not too happy that this function is needed void reset() { - ti_ = typeid(void); + id_ = 0; } bool is_locked() const { - return ti_ != boost::typeindex::type_id(); + return id_ != 0; } template bool is_locked(T const*) const { - return ti_ == boost::typeindex::type_id(); + return id_ == T::id; } template void lock(T const*) { - BOOST_ASSERT(ti_ == boost::typeindex::type_id()); - ti_ = typeid(T); + BOOST_ASSERT(id_ == 0); + id_ = T::id; } template void unlock(T const*) { - BOOST_ASSERT(ti_ == boost::typeindex::type_id()); - ti_ = typeid(void); + BOOST_ASSERT(id_ == T::id); + id_ = 0; } template @@ -89,19 +88,19 @@ public: // for an async_read to complete before performing another // async_read. // - BOOST_ASSERT(ti_ != boost::typeindex::type_id()); - if(ti_ != boost::typeindex::type_id()) + BOOST_ASSERT(id_ != T::id); + if(id_ != 0) return false; - ti_ = typeid(T); + id_ = T::id; return true; } template bool try_unlock(T const*) { - if(ti_ != boost::typeindex::type_id()) + if(id_ != T::id) return false; - ti_ = boost::typeindex::type_id(); + id_ = 0; return true; } }; diff --git a/include/boost/beast/websocket/impl/close.ipp b/include/boost/beast/websocket/impl/close.ipp index 9ebb92d7..c8796cb3 100644 --- a/include/boost/beast/websocket/impl/close.ipp +++ b/include/boost/beast/websocket/impl/close.ipp @@ -61,6 +61,8 @@ class stream::close_op handler_ptr d_; public: + static constexpr int id = 4; // for soft_mutex + close_op(close_op&&) = default; close_op(close_op const&) = delete; diff --git a/include/boost/beast/websocket/impl/ping.ipp b/include/boost/beast/websocket/impl/ping.ipp index 0fea6dd5..07abcedd 100644 --- a/include/boost/beast/websocket/impl/ping.ipp +++ b/include/boost/beast/websocket/impl/ping.ipp @@ -59,6 +59,8 @@ class stream::ping_op handler_ptr d_; public: + static constexpr int id = 3; // for soft_mutex + ping_op(ping_op&&) = default; ping_op(ping_op const&) = delete; diff --git a/include/boost/beast/websocket/impl/read.ipp b/include/boost/beast/websocket/impl/read.ipp index fc5d7b24..387b765a 100644 --- a/include/boost/beast/websocket/impl/read.ipp +++ b/include/boost/beast/websocket/impl/read.ipp @@ -90,6 +90,8 @@ class stream::read_some_op bool cont_ = false; public: + static constexpr int id = 1; // for soft_mutex + read_some_op(read_some_op&&) = default; read_some_op(read_some_op const&) = delete; diff --git a/include/boost/beast/websocket/impl/write.ipp b/include/boost/beast/websocket/impl/write.ipp index f96858cc..a34f66e9 100644 --- a/include/boost/beast/websocket/impl/write.ipp +++ b/include/boost/beast/websocket/impl/write.ipp @@ -152,6 +152,8 @@ class stream::write_some_op bool cont_ = false; public: + static constexpr int id = 2; // for soft_mutex + write_some_op(write_some_op&&) = default; write_some_op(write_some_op const&) = delete; diff --git a/include/boost/beast/websocket/stream.hpp b/include/boost/beast/websocket/stream.hpp index 21eac816..dcf54211 100644 --- a/include/boost/beast/websocket/stream.hpp +++ b/include/boost/beast/websocket/stream.hpp @@ -180,14 +180,14 @@ class stream = true; bool rd_close_ // did we read a close frame? = false; - detail::type_mutex rd_block_; // op currenly reading + detail::soft_mutex rd_block_; // op currently reading role_type role_ // server or client = role_type::client; status status_ = status::closed; - detail::type_mutex wr_block_; // op currenly writing + detail::soft_mutex wr_block_; // op currently writing bool wr_close_ // did we write a close frame? = false; bool wr_cont_ // next write is a continuation @@ -3331,10 +3331,8 @@ public: private: template class accept_op; template class close_op; - template class fail_op; template class handshake_op; template class ping_op; - template class read_fh_op; template class read_some_op; template class read_op; template class response_op; diff --git a/test/beast/websocket/ping.cpp b/test/beast/websocket/ping.cpp index f8bc4640..25e0a09f 100644 --- a/test/beast/websocket/ping.cpp +++ b/test/beast/websocket/ping.cpp @@ -384,15 +384,6 @@ public: ++count; BEAST_EXPECTS(ec == error::closed, ec.message()); - // Pings after a close are aborted - ws.async_ping("", - [&](error_code ec) - { - ++count; - BEAST_EXPECTS(ec == boost::asio:: - error::operation_aborted, - ec.message()); - }); }); if(! BEAST_EXPECT(run_until(ioc, 100, [&]{ return ws.wr_close_; }))) @@ -420,7 +411,7 @@ public: std::size_t n; for(n = 0; n < limit; ++n) { - if(count >= 4) + if(count >= 3) break; ioc.run_one(); }