From cc98ac8aba4db20695313b2c0474d83878d705f8 Mon Sep 17 00:00:00 2001 From: Botond Ballo Date: Sat, 19 Nov 2011 14:02:16 +0100 Subject: [PATCH] Subject: [PATCH] C++11 move constructor/assignment operator for socket_t and context_t Added a C++11 move constructor and move assignment operator to zmq::socket_t and zmq::context_t. These functions are only enabled if the compiler supports C++11 rvalue references. Currently the code can detect rvalue reference support for the following compilers: GCC, MSVC, clang. Signed-off-by: Botond Ballo Copyrights and project name adjusted Signed-off-by: Martin Sustrik --- zmq.hpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/zmq.hpp b/zmq.hpp index 5aa6759..d2e89d3 100644 --- a/zmq.hpp +++ b/zmq.hpp @@ -1,15 +1,16 @@ /* - Copyright (c) 2007-2011 iMatix Corporation - Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file + Copyright (c) 2009-2011 250bpm s.r.o. + Copyright (c) 2011 Botond Ballo + Copyright (c) 2007-2009 iMatix Corporation - This file is part of 0MQ. + This file is part of cppzmq. - 0MQ is free software; you can redistribute it and/or modify it under + cppzmq is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. - 0MQ is distributed in the hope that it will be useful, + cppzmq is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. @@ -25,8 +26,24 @@ #include #include +#include #include +// Detect whether the compiler supports C++11 rvalue references. +#if (defined(__GNUC__) && (__GNUC__ > 4 || \ + (__GNUC__ == 4 && __GNUC_MINOR__ > 2)) && \ + defined(__GXX_EXPERIMENTAL_CXX0X__)) + #define ZMQ_HAS_RVALUE_REFS +#endif +#if (defined(__clang__)) + #if __has_feature(cxx_rvalue_references) + #define ZMQ_HAS_RVALUE_REFS + #endif +#endif +#if (defined(_MSC_VER) && (_MSC_VER >= 1600)) + #define ZMQ_HAS_RVALUE_REFS +#endif + namespace zmq { @@ -180,8 +197,22 @@ namespace zmq throw error_t (); } +#ifdef ZMQ_HAS_RVALUE_REFS + inline context_t (context_t &&rhs) : ptr (rhs.ptr) + { + rhs.ptr = NULL; + } + inline context_t &operator = (context_t &&rhs) + { + std::swap (ptr, rhs.ptr); + return *this; + } +#endif + inline ~context_t () { + if (ptr == NULL) + return; int rc = zmq_term (ptr); assert (rc == 0); } @@ -213,6 +244,18 @@ namespace zmq throw error_t (); } +#ifdef ZMQ_HAS_RVALUE_REFS + inline socket_t(socket_t&& rhs) : ptr(rhs.ptr) + { + rhs.ptr = NULL; + } + inline socket_t& operator=(socket_t&& rhs) + { + std::swap(ptr, rhs.ptr); + return *this; + } +#endif + inline ~socket_t () { close();