[interprocess] Container

Hi, is there a possibility to put something like this into the shared_memory? class A { int a; double d; // ... boost::interprocess string s; } Thanks in advance.

AMDG Moritz wrote:
is there a possibility to put something like this into the shared_memory?
class A { int a; double d; // ... boost::interprocess string s; }
Thanks in advance.
Yes. You need to use an appropriate allocator, but other than that, it should be fine. In Christ, Steven Watanabe

is there a possibility to put something like this into the shared_memory?
class A { int a; double d; // ... boost::interprocess string s; }
I need to save classes in the managed_shared_memory that contain strings and vectors and all that stuff. So the general way to put a string into the shared_mem is like this: typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator; typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> string; string *s = managed_shm.find_or_construct<string>("String")("bla", managed_shm.get_segment_manager()); So I now want to save a class that contains a string. Therefore I wanted to use a class like this: class A { public: A(boost::interprocess::managed_shared_memory::segment_manager *seg) : calloc(seg), s(calloc) {}; CharAllocator calloc; string s; } and then I wanted to do something like this: A *a = managed_shm.find_or_construct<A>("name")(managed_shm.get_segment_manager()); //to save in the shared_mem BUT for some reason the = operator of allocator is private. The first question is now if there is a general way to save a class like this in the shared_memory. And if so, the second question is how I can create an instance of this class that does _not_ allocate memory from the shared_memory (for purposes of the process that writes to the shared_mem). To write to the shared_mem I just want to say: (*a) = anotherA; where anotherA is an instance from A that does not allocate memory from the shared memory. I hope it is not too confusing. Thanks in advance Best regards, Moritz

I tried a lot of things and it all comes down to the question below. First I'll give an overview about what I want to do. First some typedefs and then the class that should be written to the shared_memory: typedef boost::interprocess::managed_shared_memory::segment_manager segment_manager_t; typedef boost::interprocess::allocator<void, segment_manager_t> void_allocator; typedef boost::interprocess::allocator<char, segment_manager_t> char_allocator; typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> char_string; class A { A(const void_allocator &void_alloc); : str("", void_alloc) {}; char_string str; //... other datatypes ... }; const void_allocator * _void_alloc = _managed_shm->get_segment_manager(); ObjectType * pointer; One object of class A is now written to shared_mem via: template<typename ObjectType> void createObjWithAllocator(ObjectType & obj) { pointer = _managed_shm->construct<ObjectType>(boost::interprocess::unique_instance)(*_void_alloc); }; The real communication is something like this. And here is the real problem: template <typename ObjectType> void write_object(ObjectType & data) { (*pointer) = data; }; The problem now is that I cannot instantiate an object of class A without an Allocator void_allocator. So the final question of all my posts before is, if (and how) I can create an void_allocator that allocates memory from the heap and not from the shared_mem that fits the declaration of void_allocator. Any hints? Thanks in advance Best regards, Mortz
participants (2)
-
Moritz
-
Steven Watanabe