zeromq / cppzmq

Header-only C++ binding for libzmq
http://www.zeromq.org
MIT License
1.94k stars 757 forks source link

Add operator== to zmq::multipart_t #512

Closed geirhei closed 3 years ago

geirhei commented 3 years ago

zmq::message_t already has

bool operator==(const message_t &other) const ZMQ_NOTHROW
    {
        const size_t my_size = size();
        return my_size == other.size() && 0 == memcmp(data(), other.data(), my_size);
    }

    bool operator!=(const message_t &other) const ZMQ_NOTHROW
    {
        return !(*this == other);
    }

but there is no similar one for zmq::multipart_t. It only has an equal-function taking in a pointer:

// Check if equal to other multipart
    bool equal(const multipart_t *other) const
    {
        if (size() != other->size())
            return false;
        for (size_t i = 0; i < size(); i++)
            if (*peek(i) != *other->peek(i))
                return false;
        return true;
    }

Having the operator== implemented for multipart_t as well would be very handy when writing tests.

gummif commented 3 years ago

You are welcome to add that as a PR. This class however has a very strange API and I would recommend to just use a vector of message_t and send/recv with zmq::send_multipart and zmq::recv_multipart.

geirhei commented 3 years ago

I'll have a go at it. I think the class seems quite convenient. Any specific reason why you recommend not using it?

gummif commented 3 years ago

Just looks like a C API ported directly to C++ and looks like there are some footguns when using the *typ functions.