From 90f7b9c73263aec64198ac4b9799cf429bc0125a Mon Sep 17 00:00:00 2001 From: Dainis Jonitis Date: Tue, 18 Aug 2015 14:50:47 +0300 Subject: [PATCH 1/3] Add move semantics to sbuffer --- include/msgpack/sbuffer.hpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/include/msgpack/sbuffer.hpp b/include/msgpack/sbuffer.hpp index 2db671a0..420903c4 100644 --- a/include/msgpack/sbuffer.hpp +++ b/include/msgpack/sbuffer.hpp @@ -51,7 +51,30 @@ public: ::free(m_data); } -public: +#if !defined(MSGPACK_USE_CPP03) + sbuffer(const sbuffer&) = delete; + sbuffer& operator=(const sbuffer&) = delete; + + sbuffer(sbuffer&& other) : + m_size(other.m_size), m_alloc(other.m_alloc), m_data(other.m_data) + { + other.m_size = other.m_alloc = 0; + other.m_data = nullptr; + } + + sbuffer& operator=(sbuffer&& other) + { + ::free(m_data); + + m_size = other.m_size; + m_alloc = other.m_alloc; + m_data = other.m_data; + + other.m_size = other.m_alloc = 0; + other.m_data = nullptr; + } +#endif // !defined(MSGPACK_USE_CPP03) + void write(const char* buf, size_t len) { if(m_alloc - m_size < len) { @@ -118,10 +141,7 @@ private: private: sbuffer(const sbuffer&); sbuffer& operator=(const sbuffer&); -#else // defined(MSGPACK_USE_CPP03) - sbuffer(const sbuffer&) = delete; - sbuffer& operator=(const sbuffer&) = delete; -#endif // defined(MSGPACK_USE_CPP03) +#endif // defined(MSGPACK_USE_CPP03) private: size_t m_size; From c61446b98832c01715858d3a75a0e3037c24550f Mon Sep 17 00:00:00 2001 From: Dainis Jonitis Date: Tue, 18 Aug 2015 16:56:35 +0300 Subject: [PATCH 2/3] operator = should return reference to self --- include/msgpack/sbuffer.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/msgpack/sbuffer.hpp b/include/msgpack/sbuffer.hpp index 420903c4..ec476414 100644 --- a/include/msgpack/sbuffer.hpp +++ b/include/msgpack/sbuffer.hpp @@ -72,6 +72,8 @@ public: other.m_size = other.m_alloc = 0; other.m_data = nullptr; + + return *this; } #endif // !defined(MSGPACK_USE_CPP03) From e9eac32238c38553a68672055d593827aec15897 Mon Sep 17 00:00:00 2001 From: Dainis Jonitis Date: Tue, 18 Aug 2015 17:32:45 +0300 Subject: [PATCH 3/3] Fix member initialization order --- include/msgpack/sbuffer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/msgpack/sbuffer.hpp b/include/msgpack/sbuffer.hpp index ec476414..74909526 100644 --- a/include/msgpack/sbuffer.hpp +++ b/include/msgpack/sbuffer.hpp @@ -56,7 +56,7 @@ public: sbuffer& operator=(const sbuffer&) = delete; sbuffer(sbuffer&& other) : - m_size(other.m_size), m_alloc(other.m_alloc), m_data(other.m_data) + m_size(other.m_size), m_data(other.m_data), m_alloc(other.m_alloc) { other.m_size = other.m_alloc = 0; other.m_data = nullptr;