[zeromq-dev] The CRTP, or should zmq::message_t be templated?

Matt Weinstein mattweinstein at gmail.com
Thu Jun 17 18:31:50 CEST 2010


Here's the story:

I'm receiving a bunch of messages (until more == false) then  
processing them.

Sticking them into an stl container doesn't really work due to  
message_t being non-copyable.

So I'm probably going to use boost::intrusive_ptr, and write a wrapper  
class:

class message_wrapper_t : public zmq::message_t {
...
};

In order to do this properly I'm going to write three constructors and  
forward them to the corresponding message_t constructors.

Now I have to track the message_t constructors if I want this to be  
generally reusable.

It's not bad, but the thought is to use, e.g. the basic_string model  
for message_t, and instead define zmq::basic_message_t:

template <typename _T>
class basic_message_t : public _T {
};

class empty_base_t {
};

typedef basic_message_t<empty_base_t> message_t;

That way users can stick e.g. intrusive_bases in there:

class intrusive_message_base_t {
	int count_;
};

typedef basic_message_t<zmq::basic_message_t> counted_message_t;

void intrusive_ptr_add_ref(counted_message_t* pm) { ++pm->count_; }
...

Admittedly you may have to add a templated copy constructor for the  
base class, so you can initialize like e.g.:

zmq_message_t<more_complex_base_t> msg(more_complex_base_t("argument"));

I haven't really thought beyond this point... are there other uses for  
templated bases for message_t?  Not sure, but food for thought.

Right now I'm going to wrap the message_t class and implement the  
constructors. :-)

Thanks,

Best,

Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100617/f3066285/attachment.html>


More information about the zeromq-dev mailing list