Skip to content

Commit c170aab

Browse files
authored
Merge pull request #523 from jasujm/master
Problem: `sock.get(zmq::sockopt::type)` is not typesafe
2 parents 33ed542 + cbe4499 commit c170aab

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

tests/socket.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,10 @@ TEST_CASE("socket check integral options", "[socket]")
406406
#endif
407407
#ifdef ZMQ_TYPE
408408
check_integral_opt_get<int>(zmq::sockopt::type, router, "type");
409-
#endif
409+
#ifdef ZMQ_CPP11
410+
check_integral_opt_get<zmq::socket_type>(zmq::sockopt::socket_type, router, "socket_type");
411+
#endif // ZMQ_CPP11
412+
#endif // ZMQ_TYPE
410413

411414
#ifdef ZMQ_HAVE_VMCI
412415
#ifdef ZMQ_VMCI_BUFFER_SIZE

zmq.hpp

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,39 @@ constexpr const_buffer operator"" _zbuf(const char32_t *str, size_t len) noexcep
13761376
}
13771377
}
13781378

1379+
#ifdef ZMQ_CPP11
1380+
enum class socket_type : int
1381+
{
1382+
req = ZMQ_REQ,
1383+
rep = ZMQ_REP,
1384+
dealer = ZMQ_DEALER,
1385+
router = ZMQ_ROUTER,
1386+
pub = ZMQ_PUB,
1387+
sub = ZMQ_SUB,
1388+
xpub = ZMQ_XPUB,
1389+
xsub = ZMQ_XSUB,
1390+
push = ZMQ_PUSH,
1391+
pull = ZMQ_PULL,
1392+
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
1393+
server = ZMQ_SERVER,
1394+
client = ZMQ_CLIENT,
1395+
radio = ZMQ_RADIO,
1396+
dish = ZMQ_DISH,
1397+
gather = ZMQ_GATHER,
1398+
scatter = ZMQ_SCATTER,
1399+
dgram = ZMQ_DGRAM,
1400+
#endif
1401+
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 3)
1402+
peer = ZMQ_PEER,
1403+
channel = ZMQ_CHANNEL,
1404+
#endif
1405+
#if ZMQ_VERSION_MAJOR >= 4
1406+
stream = ZMQ_STREAM,
1407+
#endif
1408+
pair = ZMQ_PAIR
1409+
};
1410+
#endif
1411+
13791412
namespace sockopt
13801413
{
13811414
// There are two types of options,
@@ -1615,7 +1648,10 @@ ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TOS, tos, int);
16151648
#endif
16161649
#ifdef ZMQ_TYPE
16171650
ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, type, int);
1618-
#endif
1651+
#ifdef ZMQ_CPP11
1652+
ZMQ_DEFINE_INTEGRAL_OPT(ZMQ_TYPE, socket_type, socket_type);
1653+
#endif // ZMQ_CPP11
1654+
#endif // ZMQ_TYPE
16191655
#ifdef ZMQ_UNSUBSCRIBE
16201656
ZMQ_DEFINE_ARRAY_OPT(ZMQ_UNSUBSCRIBE, unsubscribe);
16211657
#endif
@@ -1757,7 +1793,7 @@ class socket_base
17571793
template<int Opt, class T, bool BoolUnit>
17581794
ZMQ_NODISCARD T get(sockopt::integral_option<Opt, T, BoolUnit>) const
17591795
{
1760-
static_assert(std::is_integral<T>::value, "T must be integral");
1796+
static_assert(std::is_scalar<T>::value, "T must be scalar");
17611797
T val;
17621798
size_t size = sizeof val;
17631799
get_option(Opt, &val, &size);
@@ -2026,39 +2062,6 @@ class socket_base
20262062
};
20272063
} // namespace detail
20282064

2029-
#ifdef ZMQ_CPP11
2030-
enum class socket_type : int
2031-
{
2032-
req = ZMQ_REQ,
2033-
rep = ZMQ_REP,
2034-
dealer = ZMQ_DEALER,
2035-
router = ZMQ_ROUTER,
2036-
pub = ZMQ_PUB,
2037-
sub = ZMQ_SUB,
2038-
xpub = ZMQ_XPUB,
2039-
xsub = ZMQ_XSUB,
2040-
push = ZMQ_PUSH,
2041-
pull = ZMQ_PULL,
2042-
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 0)
2043-
server = ZMQ_SERVER,
2044-
client = ZMQ_CLIENT,
2045-
radio = ZMQ_RADIO,
2046-
dish = ZMQ_DISH,
2047-
gather = ZMQ_GATHER,
2048-
scatter = ZMQ_SCATTER,
2049-
dgram = ZMQ_DGRAM,
2050-
#endif
2051-
#if defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 3)
2052-
peer = ZMQ_PEER,
2053-
channel = ZMQ_CHANNEL,
2054-
#endif
2055-
#if ZMQ_VERSION_MAJOR >= 4
2056-
stream = ZMQ_STREAM,
2057-
#endif
2058-
pair = ZMQ_PAIR
2059-
};
2060-
#endif
2061-
20622065
struct from_handle_t
20632066
{
20642067
struct _private

0 commit comments

Comments
 (0)