0

I want to put in shared memory a struct1 containing a vector of struct2 containing a wstring. So:

  • I made two allocators, one for wchar, one for struct2.
  • Then defined my containers wstring and vector.

I want to keep the wchar and struct2 allocators as members of the class, so that I don't have to keep a reference to the shm (since I don't have to create them using shm.get_manager() each time I want to manipulate data). But whatever configuration I use, it fails.

Now, I'm getting the following error at:

SharedData(InnerDataAllocator eAlloc):data(eAlloc) { 

Error C2664:
'boost::container::vector,void>::vector(const boost::container::vector,void> &)': cannot convert argument 1 from 'InnerDataAllocator' to 'const boost::container::new_allocator &'

header.h

typedef bip::allocator<wchar_t, bip::managed_shared_memory::segment_manager> CharAllocator;
typedef bip::basic_string<wchar_t, std::char_traits<wchar_t>, CharAllocator> MyShmString;

class InnerData {
public:
    uint64_t id;
    MyShmString name;
    uint8_t status;
    static const uint8_t defaultStatus = 3;

    InnerData(CharAllocator cAlloc):name(cAlloc) {

    }
};

typedef bip::allocator<InnerData, bip::managed_shared_memory::segment_manager> InnerDataAllocator;
class SharedData {
public:
    int a;
    bool newRequestUntreated = false;
    bool newReplyUntreated = false;

    bip::vector<InnerData> data; 
    SharedData(InnerDataAllocator eAlloc):data(eAlloc) {

    }
};

UsualInnerData{//like InnerData but with normal std::wstring
public:
    uint64_t id;
    std::wstring name;
    uint8_t status;
    static const uint8_t status Default = 3;
};

class Zone {
public:
    SharedData * sharedData;

    CharAllocator     charAllocator;
    InnerDataAllocator   innerDataAllocator;

    void putInShm(const std::vector<UsualInnerData> &in);
    void initializeZone(std::string &_name, std::string &_nameSeg);
    static bool alreadyCreated;
};

code.cpp

void Zone::createNewCheckingZone(std::string &_name, std::string &_nameSeg) {
    if (!alreadyCreated) {
        int size = getOverallSize();
        bip::managed_shared_memory shm(bip::create_only, _name.c_str(), 10000 + size);
        CharAllocator scharAllocator(shm.get_segment_manager());
        swap(charAllocator, scharAllocator); //use of swap because = operator says Not assignable from other allocator

        InnerDataAllocator sinnerDataAllocator(shm.get_segment_manager());
        swap(innerDataAllocator, sinnerDataAllocator);
        sharedData = shm.construct<SharedData>(_nameSeg)(innerDataAllocator);
        alreadyCreated = true;
    }
    return ;
}
void Zone::putInShm(const std::vector<UsualInnerData> &in){
    InnerData newInnerData(charAllocator);
    for (auto a : in) {
        newInnerData.id = a.id;
        newInnerData.name = a.xml.c_str();
        newInnerData.status = InnerData::defaultStatus;
        sharedData->data.push_back(newInnerData);
    }
    return;
}

As I can with no problem initialize the InnerData.name with the charAllocator of Zone, why can't I do the same, i.e. initialize the SharedData.data with the innerDataAllocator of Zone ?

Bonjour123
  • 1,421
  • 12
  • 13

1 Answers1

0

Actually, I made one mistake, (finished my coffee bag yesterday, begin to feel it now), I defined:

typedef  bip::vector<InnerData, InnderDataAllocator> InnerDataVector;

but didn't used it to define SharedData.data. Here is how I changed it:

class SharedData {
public:
    int a;
    bool newRequestUntreated = false;
    bool newReplyUntreated = false;
    InnerDataVector data; 
    SharedData(InnerDataAllocator eAlloc):data(eAlloc) {}
};

And now it compiles.

Bonjour123
  • 1,421
  • 12
  • 13