Merge pull request #303 from gummif/gfa/swap

Problem: Missing swap functions
This commit is contained in:
Simon Giesecke 2019-04-04 14:51:10 +02:00 committed by GitHub
commit d1e7c538cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 108 additions and 9 deletions

View File

@ -1,6 +1,11 @@
#include <catch.hpp> #include <catch.hpp>
#include <zmq.hpp> #include <zmq.hpp>
#if (__cplusplus >= 201703L)
static_assert(std::is_nothrow_swappable<zmq::context_t>::value,
"context_t should be nothrow swappable");
#endif
TEST_CASE("context construct default and destroy", "[context]") TEST_CASE("context construct default and destroy", "[context]")
{ {
zmq::context_t context; zmq::context_t context;
@ -12,3 +17,13 @@ TEST_CASE("context create, close and destroy", "[context]")
context.close(); context.close();
CHECK(NULL == (void *) context); CHECK(NULL == (void *) context);
} }
#ifdef ZMQ_CPP11
TEST_CASE("context swap", "[context]")
{
zmq::context_t context1;
zmq::context_t context2;
using std::swap;
swap(context1, context2);
}
#endif

View File

@ -8,6 +8,10 @@ static_assert(!std::is_copy_constructible<zmq::message_t>::value,
static_assert(!std::is_copy_assignable<zmq::message_t>::value, static_assert(!std::is_copy_assignable<zmq::message_t>::value,
"message_t should not be copy-assignable"); "message_t should not be copy-assignable");
#endif #endif
#if (__cplusplus >= 201703L)
static_assert(std::is_nothrow_swappable<zmq::message_t>::value,
"message_t should be nothrow swappable");
#endif
TEST_CASE("message default constructed", "[message]") TEST_CASE("message default constructed", "[message]")
{ {
@ -15,6 +19,22 @@ TEST_CASE("message default constructed", "[message]")
CHECK(0u == message.size()); CHECK(0u == message.size());
} }
#ifdef ZMQ_CPP11
TEST_CASE("message swap", "[message]")
{
const std::string data = "foo";
zmq::message_t message1;
zmq::message_t message2(data.data(), data.size());
using std::swap;
swap(message1, message2);
CHECK(message1.size() == data.size());
CHECK(message2.size() == 0);
swap(message1, message2);
CHECK(message1.size() == 0);
CHECK(message2.size() == data.size());
}
#endif
namespace { namespace {
const char *const data = "Hi"; const char *const data = "Hi";
} }

View File

@ -4,6 +4,7 @@
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
#include <functional>
class mock_monitor_t : public zmq::monitor_t class mock_monitor_t : public zmq::monitor_t
{ {

View File

@ -5,6 +5,11 @@
#include <array> #include <array>
#include <memory> #include <memory>
#if (__cplusplus >= 201703L)
static_assert(std::is_nothrow_swappable<zmq::poller_t<>>::value,
"poller_t should be nothrow swappable");
#endif
TEST_CASE("poller create destroy", "[poller]") TEST_CASE("poller create destroy", "[poller]")
{ {
zmq::poller_t<> poller; zmq::poller_t<> poller;
@ -28,6 +33,14 @@ TEST_CASE("poller move assign empty", "[poller]")
b = std::move(a); b = std::move(a);
} }
TEST_CASE("poller swap", "[poller]")
{
zmq::poller_t<> a;
zmq::poller_t<> b;
using std::swap;
swap(a, b);
}
TEST_CASE("poller move construct non empty", "[poller]") TEST_CASE("poller move construct non empty", "[poller]")
{ {
zmq::context_t context; zmq::context_t context;

View File

@ -4,6 +4,11 @@
#include <future> #include <future>
#endif #endif
#if (__cplusplus >= 201703L)
static_assert(std::is_nothrow_swappable<zmq::socket_t>::value,
"socket_t should be nothrow swappable");
#endif
TEST_CASE("socket create destroy", "[socket]") TEST_CASE("socket create destroy", "[socket]")
{ {
zmq::context_t context; zmq::context_t context;
@ -16,6 +21,15 @@ TEST_CASE("socket create by enum and destroy", "[socket]")
zmq::context_t context; zmq::context_t context;
zmq::socket_t socket(context, zmq::socket_type::router); zmq::socket_t socket(context, zmq::socket_type::router);
} }
TEST_CASE("socket swap", "[socket]")
{
zmq::context_t context;
zmq::socket_t socket1(context, zmq::socket_type::router);
zmq::socket_t socket2(context, zmq::socket_type::dealer);
using std::swap;
swap(socket1, socket2);
}
#endif #endif
TEST_CASE("socket sends and receives const buffer", "[socket]") TEST_CASE("socket sends and receives const buffer", "[socket]")

44
zmq.hpp
View File

@ -73,8 +73,6 @@
#ifdef ZMQ_CPP11 #ifdef ZMQ_CPP11
#include <chrono> #include <chrono>
#include <tuple> #include <tuple>
#include <functional>
#include <unordered_map>
#include <memory> #include <memory>
#endif #endif
@ -492,6 +490,12 @@ class message_t
return os.str(); return os.str();
} }
void swap(message_t &other) ZMQ_NOTHROW
{
// this assumes zmq::msg_t from libzmq is trivially relocatable
std::swap(msg, other.msg);
}
private: private:
// The underlying message // The underlying message
zmq_msg_t msg; zmq_msg_t msg;
@ -502,6 +506,11 @@ class message_t
void operator=(const message_t &) ZMQ_DELETED_FUNCTION; void operator=(const message_t &) ZMQ_DELETED_FUNCTION;
}; };
inline void swap(message_t &a, message_t &b) ZMQ_NOTHROW
{
a.swap(b);
}
class context_t class context_t
{ {
public: public:
@ -570,6 +579,11 @@ class context_t
operator bool() const ZMQ_NOTHROW { return ptr != ZMQ_NULLPTR; } operator bool() const ZMQ_NOTHROW { return ptr != ZMQ_NULLPTR; }
void swap(context_t &other) ZMQ_NOTHROW
{
std::swap(ptr, other.ptr);
}
private: private:
void *ptr; void *ptr;
@ -577,6 +591,10 @@ class context_t
void operator=(const context_t &) ZMQ_DELETED_FUNCTION; void operator=(const context_t &) ZMQ_DELETED_FUNCTION;
}; };
inline void swap(context_t &a, context_t &b) ZMQ_NOTHROW {
a.swap(b);
}
#ifdef ZMQ_CPP11 #ifdef ZMQ_CPP11
enum class socket_type : int enum class socket_type : int
{ {
@ -783,6 +801,12 @@ class socket_t
} }
#endif #endif
void swap(socket_t &other) ZMQ_NOTHROW
{
std::swap(ptr, other.ptr);
std::swap(ctxptr, other.ctxptr);
}
private: private:
void *ptr; void *ptr;
void *ctxptr; void *ctxptr;
@ -791,6 +815,10 @@ class socket_t
void operator=(const socket_t &) ZMQ_DELETED_FUNCTION; void operator=(const socket_t &) ZMQ_DELETED_FUNCTION;
}; };
inline void swap(socket_t &a, socket_t &b) ZMQ_NOTHROW {
a.swap(b);
}
ZMQ_DEPRECATED("from 4.3.1, use proxy taking socket_t objects") ZMQ_DEPRECATED("from 4.3.1, use proxy taking socket_t objects")
inline void proxy(void *frontend, void *backend, void *capture) inline void proxy(void *frontend, void *backend, void *capture)
{ {
@ -1147,6 +1175,8 @@ class monitor_t
template<typename T = void> class poller_t template<typename T = void> class poller_t
{ {
public: public:
poller_t() = default;
void add(zmq::socket_t &socket, short events, T *user_data) void add(zmq::socket_t &socket, short events, T *user_data)
{ {
if (0 if (0
@ -1192,17 +1222,19 @@ template<typename T = void> class poller_t
} }
private: private:
std::unique_ptr<void, std::function<void(void *)>> poller_ptr{ std::unique_ptr<void, void(*)(void *)> poller_ptr{
[]() { []() {
auto poller_new = zmq_poller_new(); auto poller_new = zmq_poller_new();
if (poller_new) if (poller_new)
return poller_new; return poller_new;
throw error_t(); throw error_t();
}(), }(), &destroy_poller};
[](void *ptr) {
static void destroy_poller(void *ptr)
{
int rc = zmq_poller_destroy(&ptr); int rc = zmq_poller_destroy(&ptr);
ZMQ_ASSERT(rc == 0); ZMQ_ASSERT(rc == 0);
}}; }
}; };
#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER) #endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)

View File

@ -30,6 +30,10 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#ifdef ZMQ_CPP11
#include <functional>
#include <unordered_map>
#endif
namespace zmq namespace zmq
{ {