boost::interprocess Containers of containers NOT i

2019-08-05 09:38发布

问题:

Based on a previous question boost::interprocess Containers of containers NOT in shared memory I am able to create objects in the shared memory and on the heap. What I want now is a template deep copy function to copy between heap an shm usable with all interprocess vectors independent to the allocator.

#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/range/algorithm.hpp>
#include <iostream>
#include <cassert>

template <typename T>
using BoundShmemAllocator = boost::interprocess::allocator<T, boost::interprocess::managed_shared_memory::segment_manager>;

class MyStruct {
public:
    MyStruct() {};
    MyStruct ( int i ) : i ( i ) {};
    int i;
};

template <template<typename...> class BoundShmemAllocator>
using MyStructVector = boost::interprocess::vector<MyStruct, BoundShmemAllocator<MyStruct> >;

// Variant to use on the heap:
using HeapMyStructVector  = MyStructVector<std::allocator>;
// Variant to use in shared memory:
using ShmemMyStructVector = MyStructVector<BoundShmemAllocator>;

// ----> Question how to I get this working, or way not?
//template <typename T, template<typename...> class AllocatorSrc, template<typename...> class AllocatorDes>
//void copy ( boost::interprocess::vector<T, AllocatorSrc<T> > &src, boost::interprocess::vector<T, AllocatorDes<T> > &des ) {
void copy ( const ShmemMyStructVector &src, HeapMyStructVector &des ) {
    des.resize ( src.size() );
    typename boost::interprocess::vector<T, AllocatorSrc<T> >::const_iterator itSrc;
    typename boost::interprocess::vector<T, AllocatorDes<T> >::iterator itDst;
    for(itSrc = src.begin(), itDst = des.begin(); itSrc != src.end(); itDst++, itSrc++){
      *itDst = *itSrc;
    }
}


//
///////////////////////////////////////////////////////////////

int main() {
    srand ( time ( NULL ) );

    // A managed shared memory where we can construct objects
    boost::interprocess::managed_shared_memory segment = boost::interprocess::managed_shared_memory ( boost::interprocess::open_or_create, "MySharedMemory", 65536 );
    BoundShmemAllocator<int> const shmem_alloc ( segment.get_segment_manager() );

    ShmemMyStructVector *src = segment.find_or_construct<ShmemMyStructVector> ( "MyStruct" ) ( shmem_alloc );   
    for ( size_t i = 0; i < 5; i++ )  src->push_back(rand());

    // You can have something like this working:
    HeapMyStructVector des; // and also the shm stuff below
    des.push_back(rand());


    copy(*src, des);
    std::cout << "Shmem: ";
    for ( size_t i = 0; i < src->size(); i++ ) std::cout << i << ": " << src->at(i).i << (i!=src->size()-1?", ":"\n");
    std::cout << "Heap: ";
    for ( size_t i = 0; i < des.size(); i++ ) std::cout << i << ": " << des.at(i).i << (i!=des.size()-1?", ":"\n");


    segment.destroy<ShmemMyStructVector> ( "MyStruct" );
}

回答1:

Why are you pushing argument type deduction so hard?

template <typename Src, typename Dest>
void copy(Src const &src, Dest &des) {
    des.assign(src.begin(), src.end());
}

Works like a charm for me: Live On Coliru

(Note on MSVC I needed the patch from https://svn.boost.org/trac/boost/ticket/9369)


To the comment:

The usual trick is to deduce the arguments in a function template, and dispatch to a function object which you specialize for different actual argument types.

In this case, I partially specialized for the vector case only:

namespace detail {
    template <typename Src, typename Dest> struct copier;

    template <typename T, typename A1, typename A2> struct copier<
        typename boost::interprocess::vector<T, A1>,
        typename boost::interprocess::vector<T, A2> >
    {
        template <typename Src, typename Dest>
        static void call(Src const &src, Dest &des) {
            des.assign(src.begin(), src.end());
        }
    };
}

template <typename Src, typename Dest>
void copy(Src const &src, Dest &des) {
    detail::copier<Src, Dest>::template call(src, des);
}