diff options
author | eidheim <eidheim@gmail.com> | 2019-11-01 15:57:40 +0300 |
---|---|---|
committer | eidheim <eidheim@gmail.com> | 2019-11-01 15:57:40 +0300 |
commit | 1834e513dd1055e87d112659429fb988a16d20b2 (patch) | |
tree | 1e98e52701b74fde99706cf299602087ace92287 /client_ws.hpp | |
parent | 0669d51bc51e8da4b2673cb6c73a247a1e43dbc2 (diff) |
Fixes #127 : InMessage::string() now stores a string cache and returns the same string on successive calls
Diffstat (limited to 'client_ws.hpp')
-rw-r--r-- | client_ws.hpp | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/client_ws.hpp b/client_ws.hpp index 7f5a02e..05a5e5b 100644 --- a/client_ws.hpp +++ b/client_ws.hpp @@ -31,16 +31,22 @@ namespace SimpleWeb { } /// Convenience function to return std::string. The stream buffer is consumed. - std::string string() noexcept { + /// Successive calls will return the same string. + const std::string &string() noexcept { + if(cached_string) + return *cached_string; + + cached_string = std::unique_ptr<std::string>(new std::string()); + try { - std::string str; auto size = streambuf.size(); - str.resize(size); - read(&str[0], static_cast<std::streamsize>(size)); - return str; + cached_string->resize(size); + read(&(*cached_string)[0], static_cast<std::streamsize>(size)); + return *cached_string; } catch(...) { - return std::string(); + cached_string->clear(); + return *cached_string; } } @@ -49,6 +55,7 @@ namespace SimpleWeb { InMessage(unsigned char fin_rsv_opcode, std::size_t length) noexcept : std::istream(&streambuf), fin_rsv_opcode(fin_rsv_opcode), length(length) {} std::size_t length; asio::streambuf streambuf; + std::unique_ptr<std::string> cached_string; }; /// The buffer is consumed during send operations. |