我正在使用boost :: icl :: interval_map,它工作得很好,但我希望这个容器存储在共享内存中。 boost是否支持在共享内存中存储boost :: icl容器
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 i = {"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;
it = party.find(42101035898);
if (it != party.end()) {
interval<double>::type when = it->first;
guests who = (*it++).second;
cout << who.size() << endl;
for (auto it2 : who) {
cout << when << ": " << it2.destinationGroup<< endl;
}
}
}
int main() {
boost_party();
return 0;
}
给我以下预期输出 现在我首先尝试在共享内存中放置一个简单的map_map,但我的代码永远不会编译
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;
提供以下错误
错误:模板参数列表中参数8的类型/值不匹配'模板类比较,模板类组合,模板类节,类间隔,模板类别Alloc&gt; class boost :: icl :: interval_map' typedef boost :: icl :: interval_map,ShmemAllocator&gt; MyMap中;
错误:期望一个类模板,得到'ShmemAllocator {aka boost :: interprocess :: allocator,boost :: interprocess :: segment_manager,boost :: interprocess :: iset_index&gt; &GT;}”
错误:';'标记
之前的声明中的无效类型答案 0 :(得分:1)
目前,编译器错误表明您正在传递类型作为模板参数,其中需要(可变参数)模板模板参数。
从技术上讲,您可以使用C ++ 11模板别名¹:
实现这一目标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
>;
然而,它不会导致我在我的livecoding会话中找到的工作代码。问题是Boost ICL目前不支持有状态分配器。
这意味着并非所有构造函数都将分配器实例传递到所需的状态。
可悲的是,这意味着只有一个hacky自定义分配器可以理解,其中自定义分配器将通过全局引用引用您的共享内存段。
这是一个演示,它显示了这样一个全局段分配器的概念验证:
<强> 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 the nested typedefs only
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;
for (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";
}
}
我使用managed_mapped_file
而不是maneged_shared_memory
,因为后者不支持在线编译器。
¹或在c ++ 03中使用“类型函数”