c++ - template meta-programming how to specialize on a collection -
i have following utlsharedipcwrapper
template class created access user defined type placed in inter-process memory.
normally class used simple type example:
// construct faultreport - default no faults auto faultwrapper = managed_shm.construct< utlsharedipcwrapper<uint64_t>>("faultreport")(0);
and works well, had need use boost shared memory map collection (boost::interprocess::map
) template argument) in:
using char_allocator = boost::interprocess::managed_shared_memory::allocator<char>::type; using shm_string = boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator>; using keytype = shm_string; using valuetype = std::pair<const keytype, shm_string>; using shmemallocator = boost::interprocess::allocator<valuetype, boost::interprocess::managed_shared_memory::segment_manager>; using sharedmemorymap = boost::interprocess::map<shm_string, shm_string, std::less<keytype>, shmemallocator>; ... // create new shared memory segment 2k size managed_shared_memory managed_shm(open_only, "sharedmemname"); //initialize shared memory stl-compatible allocator shmemallocator alloc(managed_shm.get_segment_manager()); auto psharednvpairs = managed_shm.find<utlsharedipcwrapper< sharedmemorymap>>("namevaluepairs").first;
my question how can change template class definition below pass collection::value types's parameters instead of separately reading entire map 1 operation via psharednvpairs->getshareddata()
updating temporary map , writing shared memory again via psharednvpairs->setshareddata(*psharednvpairs)
. know different types not collections , thefore template metaprogramming magic have performed selective enable if etc,., have method added class along lines of
// don't know correct signature here void setshareddatavalue(const t::value_type& rshareddatavalue) { boost::interprocess::scoped_lock<upgradable_mutex_type> lock(mmutex); ... not sure here update collection } template<typename t> struct utlsharedipcwrapper { private: using upgradable_mutex_type = boost::interprocess::interprocess_upgradable_mutex; mutable upgradable_mutex_type mmutex; /*volatile*/ t mshareddata; public: // explicit constructor used initialize directly existing memory explicit utlsharedipcwrapper(const t& rinitialvalue) : mshareddata(rinitialvalue) {} t getshareddata() const { boost::interprocess::sharable_lock<upgradable_mutex_type> lock(mmutex); return mshareddata; } void setshareddata(const t& rshareddata) { boost::interprocess::scoped_lock<upgradable_mutex_type> lock(mmutex); // update shared data copy mapped - scoped locked used if exception thrown // bit lock guard use this->mshareddata = rshareddata; } };
why not just
// don't know correct signature here void setshareddatavalue(const typename t::value_type& rshareddatavalue) { boost::interprocess::scoped_lock<upgradable_mutex_type> lock(mmutex); mshareddata.insert(rshareddatavalue); }
Comments
Post a Comment