Skip to content

Commit 4f423e0

Browse files
authored
addon: add const overloads for multipart_t front/back (#682)
* addon: add const overloads for multipart_t front/back Add const-qualified overloads for multipart_t::front() and back() in `zmq_addon.hpp`. Note on API shape: - In a container-style API, non-const overloads would typically return `message_t&`. - This change intentionally preserves existing non-const return types (`const message_t&`) for backward-compatibility and to avoid broadening mutability semantics in this PR. Also extend `tests/multipart.cpp` with compile-time and runtime checks for const/non-const access paths. No behavioral change intended. * fix: applied clang-format to fix style. No functional changes.
1 parent 6a8dc38 commit 4f423e0

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

tests/multipart.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <catch2/catch_all.hpp>
22
#include <zmq_addon.hpp>
33

4+
#include <utility>
5+
46
#ifdef ZMQ_HAS_RVALUE_REFS
57

68
#ifdef ZMQ_CPP17
@@ -17,6 +19,19 @@ static_assert(std::is_invocable<decltype(&zmq::multipart_t::recv),
1719
#endif
1820
static_assert(std::is_constructible<zmq::multipart_t, zmq::socket_ref>::value,
1921
"Can't construct with socket_ref");
22+
static_assert(std::is_same<decltype(std::declval<zmq::multipart_t &>().front()),
23+
const zmq::message_t &>::value,
24+
"multipart_t::front() should keep returning const message_t&");
25+
static_assert(
26+
std::is_same<decltype(std::declval<const zmq::multipart_t &>().front()),
27+
const zmq::message_t &>::value,
28+
"multipart_t::front() const should return const message_t&");
29+
static_assert(std::is_same<decltype(std::declval<zmq::multipart_t &>().back()),
30+
const zmq::message_t &>::value,
31+
"multipart_t::back() should keep returning const message_t&");
32+
static_assert(std::is_same<decltype(std::declval<const zmq::multipart_t &>().back()),
33+
const zmq::message_t &>::value,
34+
"multipart_t::back() const should return const message_t&");
2035

2136
/// \todo split this up into separate test cases
2237
///
@@ -106,11 +121,18 @@ TEST_CASE("multipart legacy test", "[multipart]")
106121
multipart.pushmem("Frame0", 6);
107122
assert(multipart.size() == 10);
108123

109-
const message_t &front_msg = multipart.front();
124+
const message_t &front_msg_nonconst = multipart.front();
125+
assert(&front_msg_nonconst == &multipart[0]);
126+
127+
const multipart_t &const_multipart = multipart;
128+
const message_t &front_msg = const_multipart.front();
110129
assert(multipart.size() == 10);
111130
assert(std::string(front_msg.data<char>(), front_msg.size()) == "Frame0");
112131

113-
const message_t &back_msg = multipart.back();
132+
const message_t &back_msg_nonconst = multipart.back();
133+
assert(&back_msg_nonconst == &multipart[multipart.size() - 1]);
134+
135+
const message_t &back_msg = const_multipart.back();
114136
assert(multipart.size() == 10);
115137
assert(std::string(back_msg.data<char>(), back_msg.size()) == "Frame9");
116138

zmq_addon.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,15 @@ class multipart_t
594594
// get message part from front
595595
const message_t &front() { return m_parts.front(); }
596596

597+
// get message part from front
598+
const message_t &front() const { return m_parts.front(); }
599+
597600
// get message part from back
598601
const message_t &back() { return m_parts.back(); }
599602

603+
// get message part from back
604+
const message_t &back() const { return m_parts.back(); }
605+
600606
// Get pointer to a specific message part
601607
const message_t *peek(size_t index) const { return &m_parts[index]; }
602608

0 commit comments

Comments
 (0)