Skip to content

Commit 324b11f

Browse files
gummifsigiesec
andauthored
Problem: Context lacks typesafe options
Solution: Define an enum class for the context options Co-Authored-By: Simon Giesecke <[email protected]> Co-authored-by: Simon Giesecke <[email protected]>
1 parent 4bd01bc commit 324b11f

File tree

2 files changed

+71
-20
lines changed

2 files changed

+71
-20
lines changed

tests/context.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,20 @@ TEST_CASE("context - use socket after shutdown", "[context]")
6363
REQUIRE(e.num() == ETERM);
6464
}
6565
}
66+
67+
TEST_CASE("context set/get options", "[context]")
68+
{
69+
zmq::context_t context;
70+
context.set(zmq::ctxopt::blocky, false);
71+
context.set(zmq::ctxopt::io_threads, 5);
72+
CHECK(context.get(zmq::ctxopt::io_threads) == 5);
73+
74+
CHECK_THROWS_AS(
75+
context.set(static_cast<zmq::ctxopt>(-42), 5),
76+
const zmq::error_t &);
77+
78+
CHECK_THROWS_AS(
79+
context.get(static_cast<zmq::ctxopt>(-42)),
80+
const zmq::error_t &);
81+
}
6682
#endif

zmq.hpp

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@
6464
#define ZMQ_NULLPTR nullptr
6565
#define ZMQ_CONSTEXPR_FN constexpr
6666
#define ZMQ_CONSTEXPR_VAR constexpr
67+
#define ZMQ_CPP11_DEPRECATED(msg) ZMQ_DEPRECATED(msg)
6768
#else
6869
#define ZMQ_NOTHROW throw()
6970
#define ZMQ_EXPLICIT
7071
#define ZMQ_OVERRIDE
7172
#define ZMQ_NULLPTR 0
7273
#define ZMQ_CONSTEXPR_FN
7374
#define ZMQ_CONSTEXPR_VAR const
75+
#define ZMQ_CPP11_DEPRECATED(msg)
7476
#endif
7577

7678
#include <zmq.h>
@@ -630,6 +632,27 @@ inline void swap(message_t &a, message_t &b) ZMQ_NOTHROW
630632
a.swap(b);
631633
}
632634

635+
#ifdef ZMQ_CPP11
636+
enum class ctxopt
637+
{
638+
blocky = ZMQ_BLOCKY,
639+
io_threads = ZMQ_IO_THREADS,
640+
thread_sched_policy = ZMQ_THREAD_SCHED_POLICY,
641+
thread_priority = ZMQ_THREAD_PRIORITY,
642+
thread_affinity_cpu_add = ZMQ_THREAD_AFFINITY_CPU_ADD,
643+
thread_affinity_cpu_remove = ZMQ_THREAD_AFFINITY_CPU_REMOVE,
644+
thread_name_prefix = ZMQ_THREAD_NAME_PREFIX,
645+
max_msgsz = ZMQ_MAX_MSGSZ,
646+
#ifdef ZMQ_ZERO_COPY_RECV
647+
zero_copy_recv = ZMQ_ZERO_COPY_RECV,
648+
#endif
649+
max_sockets = ZMQ_MAX_SOCKETS,
650+
socket_limit = ZMQ_SOCKET_LIMIT,
651+
ipv6 = ZMQ_IPV6,
652+
msg_t_size = ZMQ_MSG_T_SIZE
653+
};
654+
#endif
655+
633656
class context_t
634657
{
635658
public:
@@ -664,16 +687,38 @@ class context_t
664687
}
665688
#endif
666689

690+
~context_t() ZMQ_NOTHROW { close(); }
691+
692+
ZMQ_CPP11_DEPRECATED("from 4.7.0, use set taking zmq::ctxopt instead")
667693
int setctxopt(int option_, int optval_)
668694
{
669695
int rc = zmq_ctx_set(ptr, option_, optval_);
670696
ZMQ_ASSERT(rc == 0);
671697
return rc;
672698
}
673699

700+
ZMQ_CPP11_DEPRECATED("from 4.7.0, use get taking zmq::ctxopt instead")
674701
int getctxopt(int option_) { return zmq_ctx_get(ptr, option_); }
675702

676-
~context_t() ZMQ_NOTHROW { close(); }
703+
#ifdef ZMQ_CPP11
704+
void set(ctxopt option, int optval)
705+
{
706+
int rc = zmq_ctx_set(ptr, static_cast<int>(option), optval);
707+
if (rc == -1)
708+
throw error_t();
709+
}
710+
711+
ZMQ_NODISCARD int get(ctxopt option)
712+
{
713+
int rc = zmq_ctx_get(ptr, static_cast<int>(option));
714+
// some options have a default value of -1
715+
// which is unfortunate, and may result in errors
716+
// that don't make sense
717+
if (rc == -1)
718+
throw error_t();
719+
return rc;
720+
}
721+
#endif
677722

678723
// Terminates context (see also shutdown()).
679724
void close() ZMQ_NOTHROW
@@ -1266,9 +1311,7 @@ class socket_base
12661311

12671312
bool connected() const ZMQ_NOTHROW { return (_handle != ZMQ_NULLPTR); }
12681313

1269-
#ifdef ZMQ_CPP11
1270-
ZMQ_DEPRECATED("from 4.3.1, use send taking a const_buffer and send_flags")
1271-
#endif
1314+
ZMQ_CPP11_DEPRECATED("from 4.3.1, use send taking a const_buffer and send_flags")
12721315
size_t send(const void *buf_, size_t len_, int flags_ = 0)
12731316
{
12741317
int nbytes = zmq_send(_handle, buf_, len_, flags_);
@@ -1279,9 +1322,7 @@ class socket_base
12791322
throw error_t();
12801323
}
12811324

1282-
#ifdef ZMQ_CPP11
1283-
ZMQ_DEPRECATED("from 4.3.1, use send taking message_t and send_flags")
1284-
#endif
1325+
ZMQ_CPP11_DEPRECATED("from 4.3.1, use send taking message_t and send_flags")
12851326
bool send(message_t &msg_,
12861327
int flags_ = 0) // default until removed
12871328
{
@@ -1294,10 +1335,9 @@ class socket_base
12941335
}
12951336

12961337
template<typename T>
1297-
#ifdef ZMQ_CPP11
1298-
ZMQ_DEPRECATED("from 4.4.1, use send taking message_t or buffer (for contiguous "
1299-
"ranges), and send_flags")
1300-
#endif
1338+
ZMQ_CPP11_DEPRECATED(
1339+
"from 4.4.1, use send taking message_t or buffer (for contiguous "
1340+
"ranges), and send_flags")
13011341
bool send(T first, T last, int flags_ = 0)
13021342
{
13031343
zmq::message_t msg(first, last);
@@ -1310,9 +1350,7 @@ class socket_base
13101350
}
13111351

13121352
#ifdef ZMQ_HAS_RVALUE_REFS
1313-
#ifdef ZMQ_CPP11
1314-
ZMQ_DEPRECATED("from 4.3.1, use send taking message_t and send_flags")
1315-
#endif
1353+
ZMQ_CPP11_DEPRECATED("from 4.3.1, use send taking message_t and send_flags")
13161354
bool send(message_t &&msg_,
13171355
int flags_ = 0) // default until removed
13181356
{
@@ -1352,9 +1390,8 @@ class socket_base
13521390
}
13531391
#endif
13541392

1355-
#ifdef ZMQ_CPP11
1356-
ZMQ_DEPRECATED("from 4.3.1, use recv taking a mutable_buffer and recv_flags")
1357-
#endif
1393+
ZMQ_CPP11_DEPRECATED(
1394+
"from 4.3.1, use recv taking a mutable_buffer and recv_flags")
13581395
size_t recv(void *buf_, size_t len_, int flags_ = 0)
13591396
{
13601397
int nbytes = zmq_recv(_handle, buf_, len_, flags_);
@@ -1365,10 +1402,8 @@ class socket_base
13651402
throw error_t();
13661403
}
13671404

1368-
#ifdef ZMQ_CPP11
1369-
ZMQ_DEPRECATED(
1405+
ZMQ_CPP11_DEPRECATED(
13701406
"from 4.3.1, use recv taking a reference to message_t and recv_flags")
1371-
#endif
13721407
bool recv(message_t *msg_, int flags_ = 0)
13731408
{
13741409
int nbytes = zmq_msg_recv(msg_->handle(), _handle, flags_);

0 commit comments

Comments
 (0)