Boost message_queue: just the constructor lets me configure it, no other member functions available
This is a classic Boost message_queue example:
#include <boost/interprocess/ipc/message_queue.hpp>
using namespace boost::interprocess;
struct Data { 开发者_JAVA百科... };
int main() {
Data data1;
message_queue::remove("message_queue");
message_queue mq(create_only, "message_queue", 100, sizeof(Data));
mq.send(&data1, sizeof(Data), 0);
}
Now I would like to put the mq
message_queue object inside a class as a member variable, with the lifetime of this class' object:
#include <boost/interprocess/ipc/message_queue.hpp>
using namespace boost::interprocess;
struct Data { ... };
class DataManager
{
message_queue mq;
public:
DataManager()
: mq(create_only, "message_queue", 100, sizeof(Data)) // ok
{
mq.Open(create_only, "message_queue", 100, sizeof(Data)); // Open does not exist
}
};
It seems I can only initialise the mq
object in the member initialisation list, since message_queue
does not provide the member functions to set its parameters at a later time.
Am I wrong? Is there another way to do it?
I would like to be able, e.g., to let an object use a message queue whose name is passed as a parameter to one of its member functions.
Thank you.
How about this :
class QueueManager
{
boost::scoped_ptr<message_queue> mq;
// ctor
QueueManager(string msgqname)
{
mq.reset(new message_queue(create_only, msgqname, 100, sizeof(Data));
}
};
Just to give you an idea that at least some parameters can be passed to class constructor. Since message queue uses shared memory underneath, I think most parameters can not be changed after construction.
精彩评论