Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost Shared Memory Compilation Issue
From: Nathan Lewis (nathanlewissubscription_at_[hidden])
Date: 2011-07-21 22:51:36


Ion Gaztañaga <igaztanaga <at> gmail.com> writes:

>
> El 21/07/2011 19:40, Nathan Lewis escribió:
> > Ion Gaztañaga<igaztanaga<at> gmail.com> writes:
>
> What does "the data is not there" mean? You can't see it with the
> debugger because pointers for shared memory are not raw pointers so the
> debugger can't show values like with heap allocations.
>
> Best,
>
> Ion
> _______________________________________________
> Boost-users mailing list
> Boost-users <at> lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

Oh my, Ion I didn't realize that. One reason I was playing with the debugger was
because I was having trouble accessing the element in the vector via an
iterator. I believe that I've got the iterator set up correctly but can't figure
out why I can't print out the elements, what I am doing won't compile. I believe
the iterator is a char_string which is just a string that is allocated in the
shared memory. What I am trying to do is print it out in the loop below.
Ultimately I probably want to copy into regular strings on the other end when in
the application on the other end. I am pretty new to template parameters and
generic programming. If you don't mind correcting my thinking. Hopefully I'll be
able to stop bugging you. I can see my self using this part of boost a fair
amount just need to master it better. I've been pointing several other friends
the boost interprocess library and they are pretty impressed.

Regards,

This is the compile error I am getting:
error C2679: binary '<<' : no operator found which takes a right-hand operand of
type 'char_string_vector_iterator' (or there is no acceptable conversion)
ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator
<<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char
*)' [found using argument-dependent lookup]
with[_Elem=char,_Traits=std::char_traits<char>] ...

#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;
using std::cout;
using std::endl;

//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<int_vector, segment_manager_t> int_vector_allocator;
typedef vector<int_vector, int_vector_allocator> int_vector_vector;
typedef allocator<char, segment_manager_t> char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator> char_string;
typedef allocator<char_string, segment_manager_t> char_string_allocator;
typedef vector<char_string, char_string_allocator> char_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)
   {}

   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<T, segment_manager_t> 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>("Complex_Data")(7,
"hi", 7.0, alloc_inst);
   mymap->addStringItem("hello");
   mymap->addStringItem("how");
   mymap->addStringItem("are");
   mymap->addStringItem("you");

   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())
   {
      // error is on the line below
      cout << "Inserted String is: " << it << endl;
      it++;
   }

   //
   return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net