From 40463fb4c5e188392a9865f9339eb1e8c67ef4ac Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 18 Jan 2020 18:22:28 +0100 Subject: Simplified string() functions, and streambuf is no longer consumed when InMessage::string() is called --- client_ws.hpp | 21 +++------------------ server_ws.hpp | 21 +++------------------ 2 files changed, 6 insertions(+), 36 deletions(-) diff --git a/client_ws.hpp b/client_ws.hpp index 2d97717..1c82571 100644 --- a/client_ws.hpp +++ b/client_ws.hpp @@ -30,24 +30,9 @@ namespace SimpleWeb { return length; } - /// Convenience function to return std::string. The stream buffer is consumed. - /// Successive calls will return the same string. - const std::string &string() noexcept { - if(cached_string) - return *cached_string; - - cached_string = std::unique_ptr(new std::string()); - - try { - auto size = streambuf.size(); - cached_string->resize(size); - read(&(*cached_string)[0], static_cast(size)); - return *cached_string; - } - catch(...) { - cached_string->clear(); - return *cached_string; - } + /// Convenience function to return std::string. + std::string string() noexcept { + return std::string(asio::buffers_begin(streambuf.data()), asio::buffers_end(streambuf.data())); } private: diff --git a/server_ws.hpp b/server_ws.hpp index 8ec029f..dcce130 100644 --- a/server_ws.hpp +++ b/server_ws.hpp @@ -43,24 +43,9 @@ namespace SimpleWeb { return length; } - /// Convenience function to return std::string. The stream buffer is consumed. - /// Successive calls will return the same string. - const std::string &string() noexcept { - if(cached_string) - return *cached_string; - - cached_string = std::unique_ptr(new std::string()); - - try { - auto size = streambuf.size(); - cached_string->resize(size); - read(&(*cached_string)[0], static_cast(size)); - return *cached_string; - } - catch(...) { - cached_string->clear(); - return *cached_string; - } + /// Convenience function to return std::string. + std::string string() noexcept { + return std::string(asio::buffers_begin(streambuf.data()), asio::buffers_end(streambuf.data())); } private: -- cgit v1.2.3