c++ boost icl containers in shared memory -


im working boost::icl::interval_map works want container stored in shared memory. boost provide support storing boost::icl containers in shared memory

    using namespace std;     using namespace boost::icl;      struct ifm {            std::string destinationgroup;            int priority;            ifm()            {                destinationgroup= "";                priority = 0;             }             ifm(const std::string& d, const int& p)                  {                      destinationgroup= d;                       priority = p;                   }              ifm& operator +=(const ifm& right)             {                 destinationgroup+= right.destinationgroup;                 priority += right.priority;                 return *this;             }           bool operator <(const ifm& left) const {             if(priority <  left.priority)                 return true;         }          bool operator ==(const ifm& left) const {             return destinationgroup== left.destinationgroup                     && priority == left.priority;          }      };      typedef std::set<ifm> guests;       void boost_party()     {         interval_map<double, guests> party;          ifm = {"123", 1};         ifm j = {"124", 1};         ifm k = {"126", 2,};         ifm l = {"128", 1};         ifm m = {"129", 1};         ifm n = {"130", 1};          guests ii;         ii.insert(i);          guests jj;         jj.insert(j);          guests kk;         kk.insert(k);          guests ll;         ll.insert(l);           guests mm;         mm.insert(m);          party.add(make_pair(interval<double>::closed(12345600000,12345699999), guests(ii)));         party.add(make_pair(interval<double>::closed(32100000000,32199999999), guests(jj)));         party.add(make_pair(interval<double>::closed(42000000000,42999999999), guests(ll)));         party.add(make_pair(interval<double>::closed(42101000000,42101099999), guests(kk)));         party.add(make_pair(interval<double>::closed(67000000000,67999999999), guests(mm)));          interval_map<double, guests>::const_iterator it;         = party.find(42101035898);          if (it != party.end()) {             interval<double>::type when = it->first;             guests = (*it++).second;             cout << who.size() << endl;             (auto it2 : who) {                     cout << when << ": " << it2.destinationgroup<< endl;             }         }     }  int main() {     boost_party();     return 0; } 

gives me following output expected m trying put simple map interval_map in shared memory first code never compiles

boost::interprocess::managed_shared_memory segment(      boost::interprocess::create_only, "mysharedmemory" //segment name      , 65536);       typedef int keytype;      typedef int mappedtype;      typedef pair<int,int> keyvalue;       typedef boost::interprocess::allocator<keyvalue, boost::interprocess::managed_shared_memory::segment_manager>      shmemallocator;       shmemallocator alloc_inst (segment.get_segment_manager());       typedef boost::icl::interval_map<int, int, boost::icl::partial_absorber, std::less, boost::icl::inplace_plus,boost::icl::inter_section, boost::icl::discrete_interval<int, std::less>, shmemallocator> mymap; 

give following error

error: type/value mismatch @ argument 8 in template parameter list ‘template class compare, template class combine, template class section, class interval, template class alloc> class boost::icl::interval_map’ typedef boost::icl::interval_map, shmemallocator> mymap;

error: expected class template, got ‘shmemallocator {aka boost::interprocess::allocator, boost::interprocess::segment_manager, boost::interprocess::iset_index> >}’

error: invalid type in declaration before ‘;’ token

for now, compiler error indicates you're passing type template argument, (variadic) template template argument expected.

you technically achieve using c++11 template aliasing¹:

template <typename t> using allocator = bip::allocator<t, smgr>; template<typename t>  using set       = std::set<t, allocator<t> >;  template <typename domain, typename codomain> using basic_map = icl::interval_map<domain, codomain,         icl::partial_absorber, std::less, icl::inplace_plus, icl::inter_section, icl::discrete_interval<int, std::less>,         allocator     >; 

however, not lead working code i've found in livecoding session. problem boost icl doesn't support stateful allocators.

this means not constructors take allocator instance pass in required state.

sadly, means hacky custom allocator conceivably work, custom allocator refer shared memory segment global reference.

demo

here's demo shows proof-of-concept such global-segment allocator:

live on coliru

#include <boost/icl/interval_map.hpp> #include <boost/interprocess/managed_mapped_file.hpp> #include <iostream> #include <vector> #include <set>  namespace bip = boost::interprocess; namespace icl = boost::icl;  namespace shared {     using segment = bip::managed_mapped_file;     using smgr    = segment::segment_manager; }  namespace {      static bip::managed_mapped_file global_mm(bip::open_or_create, "./demo.bin", 1ul<<20);     static bip::allocator<void, shared::smgr> global_alloc(global_mm.get_segment_manager());      template <class t> struct simpleallocator : std::allocator<t> { // inheriting nested typedefs         typedef t value_type;          simpleallocator() : _alloc(global_alloc) {}         template <class u>              simpleallocator(const simpleallocator<u> &other) : _alloc(other._alloc) {}          t* allocate(std::size_t n)           { return std::addressof(*_alloc.allocate(n)); }         void deallocate(t *p, std::size_t n) { _alloc.deallocate(p, n); }          // optionals         template <typename other> struct rebind { typedef simpleallocator<other> other; };          bip::allocator<t, shared::smgr> _alloc;     };      template <class t, class u> bool operator==(const simpleallocator<t> &, const simpleallocator<u> &) { return true;  }     template <class t, class u> bool operator!=(const simpleallocator<t> &, const simpleallocator<u> &) { return false; } }  namespace shared {      template <typename t> using allocator = simpleallocator<t>;     template<typename t>  using set       = std::set<t, std::less<t>, allocator<t> >;      template <typename domain, typename codomain>     using basic_map = icl::interval_map<domain, codomain,             icl::partial_absorber, std::less, icl::inplace_plus, icl::inter_section, icl::discrete_interval<int, std::less>,             allocator         >;      using map      = basic_map<int, set<int> >;     using interval = map::interval_type; }  #include <iostream>  int main() {      shared::map demo;     (auto&& element : {             shared::map::value_type { shared::interval::right_open(4, 5), { 1, 7, } },             shared::map::value_type { shared::interval::right_open(2, 6), { 1, 2, 3, } },         })     {         demo.add(element);         std::cout << "adding: " << element.first << ", result: " << demo << "\n";     } } 

i used managed_mapped_file instead of maneged_shared_memory because latter not support on online compiler.


¹ or using "type function" in c++03


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -