3

I like to create a boost interprocess vector of classes containing a interprocess container. The following code works until the resize function call and of course because my class has not default constructor. How to I resolve this problem? The example is based on the boost Containers of containers example

Thanks Markus

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>

using namespace boost::interprocess;

//Typedefs of allocators and containers
typedef managed_shared_memory::segment_manager                       segment_manager_t;
typedef allocator<void, segment_manager_t>                           void_allocator;
typedef allocator<int, segment_manager_t>                            int_allocator;
typedef vector<int, int_allocator>                                   int_vector;
typedef allocator<char, segment_manager_t>                           char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>   char_string;

class complex_data
{
public:
   int               id_;
   char_string       char_string_;
   int_vector int_vector_;

   //Since void_allocator is convertible to any other allocator<T>, we can simplify
   //the initialization taking just one allocator for all inner containers.
   complex_data(const void_allocator &void_alloc)
      : id_(-1), char_string_(void_alloc), int_vector_(void_alloc)
   {}
   //Other members...
};


typedef allocator<complex_data, segment_manager_t>     complex_data_allocator;
typedef vector<complex_data, complex_data_allocator>   complex_data_vector;

int main ()
{
   //Remove shared memory on construction and destruction
   struct shm_remove
   {
      shm_remove() { shared_memory_object::remove("MySharedMemory"); }
      ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
   } remover;

   //Create shared memory
   managed_shared_memory segment(create_only,"MySharedMemory", 65536);

   //An allocator convertible to any allocator<T, segment_manager_t> type
   void_allocator alloc_inst (segment.get_segment_manager());

   //Construct the shared memory map and fill it
   complex_data *complex_data0_ = segment.construct<complex_data> ("MyCompexData")(alloc_inst);

   complex_data0_->char_string_ = "Hello Wold";
   complex_data0_->int_vector_.push_back(3);

   complex_data *complex_data1_ = segment.find_or_construct<complex_data> ("MyCompexData")(alloc_inst);
   complex_data1_->int_vector_.push_back(6);

   std::cout << complex_data1_->id_ << ", " << complex_data0_->char_string_;
   for(size_t i = 0; i < complex_data1_->int_vector_.size(); i++) std::cout << ", " << complex_data1_->int_vector_[i];


   complex_data_vector *complex_data_vector0 = segment.construct<complex_data_vector> ("MyCompexDataVector")(alloc_inst);

   /**
    * Problem
    * How to I resize or add new elements?
    **/
   complex_data_vector0->resize(3);  


   return 0;
}
Max
  • 340
  • 2
  • 15

1 Answers1

6

REWRITE

As Boost Containers support the scoped allocator pattern, you can just use a scoped allocator, and the container will automatically pass it on internal construction (or emplacing) of new elements, when that element has the uses_allocator<> trait specialized positively for it:

Live On Coliru

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>

using namespace boost::interprocess;

//Typedefs of allocators and containers
typedef managed_shared_memory::segment_manager                       segment_manager_t;
typedef boost::container::scoped_allocator_adaptor<allocator<void, segment_manager_t> >
                                                                     void_allocator;
typedef void_allocator::rebind<int>::other                           int_allocator;
typedef vector<int, int_allocator>                                   int_vector;

typedef void_allocator::rebind<char>::other                          char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>   char_string;

class complex_data
{
public:
   int               id_;
   char_string       char_string_;
   int_vector int_vector_;

   //Since void_allocator is convertible to any other allocator<T>, we can simplify
   //the initialization taking just one allocator for all inner containers.
   typedef void_allocator allocator_type;

   complex_data(complex_data const& other, const allocator_type &void_alloc)
       : id_(other.id_), char_string_(other.char_string_, void_alloc), int_vector_(other.int_vector_, void_alloc)
   {}
   complex_data(const allocator_type &void_alloc)
      : id_(-1), char_string_(void_alloc), int_vector_(void_alloc)
   {}
   //Other members...
   //
};

typedef void_allocator::rebind<complex_data>::other    complex_data_allocator;
typedef vector<complex_data, complex_data_allocator>   complex_data_vector;

int main ()
{
   //Remove shared memory on construction and destruction
   struct shm_remove
   {
      shm_remove() { shared_memory_object::remove("MySharedMemory"); }
      ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
   } remover;

   //Create shared memory
   managed_shared_memory segment(create_only,"MySharedMemory", 65536);

   //An allocator convertible to any allocator<T, segment_manager_t> type
   void_allocator alloc_inst (segment.get_segment_manager());

   //Construct the shared memory map and fill it
   complex_data *complex_data0_ = segment.construct<complex_data> ("MyCompexData")(alloc_inst);

   complex_data0_->char_string_ = "Hello Wold";
   complex_data0_->int_vector_.push_back(3);

   complex_data *complex_data1_ = segment.find_or_construct<complex_data> ("MyCompexData")(alloc_inst);
   complex_data1_->int_vector_.push_back(6);

   std::cout << complex_data1_->id_ << ", " << complex_data0_->char_string_;
   for(size_t i = 0; i < complex_data1_->int_vector_.size(); i++) std::cout << ", " << complex_data1_->int_vector_[i];


   complex_data_vector *complex_data_vector0 = segment.construct<complex_data_vector> ("MyCompexDataVector")(alloc_inst);

   complex_data_vector0->resize(3);
   complex_data_vector0->emplace_back();
}

Note how I made the allocator definitions rebinds of the void_allocator type, making it much easier to change the 'root allocator' implementation for your code.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you for your suggestion but it is not working. I tried insert as you suggested. **/usr/include/c++/4.8/bits/stl_algobase.h:739:11: error: no match for ‘operator=’ (operand types are ‘complex_data’ and ‘const complex_data’)** – Max Oct 10 '14 at 11:54
  • This means your element type (complex_data) isn't copyable/movable. See here: **[T must meet the requirements of CopyAssignable and CopyConstructible.](http://en.cppreference.com/w/cpp/container/vector)** – sehe Oct 10 '14 at 12:18
  • Thats exacly my point, how can solve this problem with a my proposed class? – Max Oct 11 '14 at 17:20
  • @moresun Well, I had time to look at this on a GCC machine, and here are all the approaches nicely lined up **[Live On Coliru](http://coliru.stacked-crooked.com/a/3d6582c2d59015d2)**. The solution if your `complex_data` is different from your question code, make it copyable! See e.g. [Rule Of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). If you get stuck doing that, you could post a question about that – sehe Oct 11 '14 at 19:54
  • @moresun Just think about it: You'd need to somehow provide the allocator instance to the constructor of your element type. You can't, therefore. Unless you take a global reference to your allocator instance (which would be a phenomenally bad idea). – sehe Oct 13 '14 at 08:41