Ion Gaztañaga writes:
El 20/07/2011 1:55, Nathan Lewis escribió:
typedef vector
char_string_vector_vector;
ERROR: you must define a vector with a char_string_allocator, not a
char_allocator, since you are storing char_string. Attached a working
exmple and differences
Best,
ion
Thank you for your response. So I made the changes and started the debugger to
see the data in the vector and it turns out it isn't putting the data in the
vector. I assume it is creating it on the heap instead even though it appears to
me I am using the allocator. I don't know if it is because of the Complex_Data
object is constructed with a void allocator or what. Do you have any thoughts on
what is going on here? I notice that the size of the vector is increasing when
calling the add method but the data is not there
#include
#include
#include
#include
#include
using namespace boost::interprocess;
using std::cout;
using std::endl;
//Typedefs of allocators and containers
typedef managed_shared_memory::segment_manager segment_manager_t;
typedef allocator void_allocator;
typedef allocator int_allocator;
typedef vector int_vector;
typedef allocatorint_vector_allocator;
typedef vectorint_vector_vector;
typedef allocator char_allocator;
typedef basic_string char_string;
typedef allocator char_string_allocator;
typedef vectorchar_string_vector_vector;
typedef char_string_vector_vector::iterator char_string_vector_iterator;
class complex_data
{
public: //Obviously making the variables of complex_data public isn't a good
idea I am just playing here for the moment
int id_;
char_string char_string_;
char_string_vector_vector char_string_vector_vector_;
double price_;
//Since void_allocator is convertible to any other allocator<T>, we simplify
//the initialization taking just one allocator for all inner containers.
complex_data(int id, const char *name, double prce, const void_allocator
&void_alloc)
: id_(id), char_string_(name, void_alloc),
char_string_vector_vector_(void_alloc), price_(prce)
{}
// add a string --> Error is on the next line
//void addStringItem(char_string* s)
//{char_string_vector_vector_.push_back(*s);}
void addStringItem(const char* s)
{
//Every time you build from a raw string you need an allocator
//in the constructor
char_allocator alloc(char_string_vector_vector_.get_allocator());
char_string_vector_vector_.push_back(char_string(s, alloc));
}
};
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 type
void_allocator alloc_inst (segment.get_segment_manager());
char_string tempStr("whatever", alloc_inst);
cout << "tempStr = " << tempStr << endl;
//Construct the shared memory map and fill it
complex_data *mymap = segment.construct("Complex_Data")(7,
"hi", 7.0, alloc_inst);
mymap->addStringItem("hello");
cout << "Size of char_string_vector_vector = " <<
mymap->char_string_vector_vector_.size();
char_string_vector_iterator it = mymap->char_string_vector_vector_.begin();
while (it != mymap->char_string_vector_vector_.end())
{
cout << "Inserted String is: " << endl;
//cout << "Inserted String is: " << it << endl;
}
//cout << "
//
return 0;
}