Skip to content

Commit e0314c9

Browse files
authored
Merge pull request #513 from geirhei/geirhei/multipart-equality-operator
Add == and != operators for multipart_t
2 parents 267d300 + 14f304f commit e0314c9

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

tests/multipart.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ TEST_CASE("multipart legacy test", "[multipart]")
7575
assert(ok);
7676
assert(copy.equal(&multipart));
7777

78+
// Test equality operators
79+
assert(copy == multipart);
80+
assert(multipart == copy);
81+
82+
multipart.pop();
83+
84+
assert(copy != multipart);
85+
assert(multipart != copy);
86+
87+
multipart_t emptyMessage1 {};
88+
multipart_t emptyMessage2 {};
89+
90+
assert(emptyMessage1 == emptyMessage2);
91+
assert(emptyMessage2 == emptyMessage1);
92+
7893
multipart.clear();
7994
assert(multipart.empty());
8095

zmq_addon.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,16 +612,26 @@ class multipart_t
612612
}
613613

614614
// Check if equal to other multipart
615-
bool equal(const multipart_t *other) const
615+
bool equal(const multipart_t *other) const ZMQ_NOTHROW
616616
{
617-
if (size() != other->size())
617+
return *this == *other;
618+
}
619+
620+
bool operator==(const multipart_t &other) const ZMQ_NOTHROW
621+
{
622+
if (size() != other.size())
618623
return false;
619624
for (size_t i = 0; i < size(); i++)
620-
if (*peek(i) != *other->peek(i))
625+
if (at(i) != other.at(i))
621626
return false;
622627
return true;
623628
}
624629

630+
bool operator!=(const multipart_t &other) const ZMQ_NOTHROW
631+
{
632+
return !(*this == other);
633+
}
634+
625635
#ifdef ZMQ_CPP11
626636

627637
// Return single part message_t encoded from this multipart_t.

0 commit comments

Comments
 (0)