boost::interprocess scoped_allocator AND Containers of containers NOT in shared memory

荒凉一梦 提交于 2019-11-28 01:38:15

Well. I'm not sure what was the challenge, beyond... yes let's make the allocator a template argument, and define

namespace Shared {
    typedef bi::managed_shared_memory::segment_manager                             segment_manager_t;
    typedef bc::scoped_allocator_adaptor<bi::allocator<void, segment_manager_t> >  void_allocator;
    using Lists = common::Lists<void_allocator>;
}

namespace Heap {
    typedef std::allocator<void> void_allocator;
    using Lists = common::Lists<void_allocator>;
}

So, I went ahead and actually made converting constructors:

Live On Coliru

namespace common {
    template <typename Alloc>
    class CheckList {
        typedef typename Alloc::template rebind<double>::other double_allocator;
        typedef bc::vector<double, double_allocator> double_vector;

    public:
        double_vector values;

        typedef Alloc allocator_type;
        CheckList(const allocator_type& void_alloc = allocator_type()) : values(void_alloc) {}

        template <typename Alloc2>
        CheckList(CheckList<Alloc2> const& other, const allocator_type& void_alloc = allocator_type())
        : values(void_alloc)
        {
            for(auto& v : other.values) values.emplace_back(v);
        }

        friend std::ostream& operator<<(std::ostream& os, const CheckList& o) {
            for (size_t i = 0; i < o.values.size(); i++)
                os << (i?", ":"") << o.values[i];
            return os;
        }
    };

    template <typename Alloc>
    class Lists {
        typedef typename Alloc::template rebind<CheckList<Alloc> >::other checklist_allocator;

    public:
        typedef Alloc allocator_type;
        typedef bc::vector<CheckList<Alloc>, checklist_allocator> checklist_vector;

        template <typename Alloc2>
        Lists& operator=(Lists<Alloc2> const& other) {
            for(auto& cl : other.checklists) checklists.emplace_back(cl);
            return *this;
        }

        Lists(const Alloc& void_alloc = allocator_type()) : checklists(void_alloc) {}
        checklist_vector checklists;

        friend std::ostream& operator<<(std::ostream& os, const Lists& o) {
            for (size_t i = 0; i < o.checklists.size(); i++)
                os << o.checklists[i] << '\n';
            return os;
        }
    };
}

This means you can now have a function that returns heap-based collections and assign it to a shared-memory version of the same:

Heap::Lists generate_local() {
    Heap::Lists lists;
    Heap::Lists::checklist_vector::value_type v;

    for (int i=0; i<10; ++i) {
        v.values.emplace_back(i+1);
        lists.checklists.push_back(v);
    }

    return lists;
}

// later:

    Lists& lists = *segment.find_or_construct<Lists>("Lists")(alloc_inst);

    if (lists.checklists.size() != 10) {
        std::cout << "Create Data" << std::endl;
        auto x = generate_local();
        lists = std::move(x);
    } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!