我有一个的boost ::集装箱类型的集装箱间。但我喜欢用我的进程内存中的类也正常类。谁能帮我写一个构造函数,不带任何参数有类我目前的进程内存初始化。
的#include&LT;升压/间/集装箱/ vector.hpp&GT;#包括LT&;升压/间/分配器/ allocator.hpp&GT;#包括LT&;升压/间/ managed_shared_memory.hpp&GT;#包括LT&;升压/存档/ xml_oarchive.hpp&GT;#包括LT&;升压/存档/ xml_iarchive.hpp&GT;#包括LT&; shmfw /系列化/ interprocess_vector.hpp&GT;#包括LT&;&stdlib.h中GT; / *函数srand,兰特* /#包括LT&;&time.h中GT; /* 时间 */使用空间boost ::进程间;//别名整数的STL样分配,从该段分配整数typedef的分配器&LT; INT,managed_shared_memory :: segment_manager&GT; ShmemAllocator;//别名,它使用previous STL般分配器矢量typedef的矢量&lt;整型,ShmemAllocator&GT; MyVector;typedef的分配器&LT;无效,managed_shared_memory :: segment_manager&GT; void_allocator;类MYSTRUCT {上市: MyVector myVector; //由于void_allocator可以转换为任何其他分配器&LT;吨&gt;中我们可以简化 //初始化服用只是一个分配器为所有内部容器。 MYSTRUCT(常量void_allocator&安培; void_alloc) :myVector(void_alloc) {} //那是什么我喜欢 // MYSTRUCT() //:myVector(??) // {}};诠释主(){ //我想有类似的东西的工作和也低于SHM东西 // MYSTRUCT X; managed_shared_memory段; //一个管理的共享内存,我们可以构造对象 //与C字符串相关 尝试{ 段= managed_shared_memory(create_only,MySharedMemory,65536); }赶上(...){ 段= managed_shared_memory(open_onlyMySharedMemory); } //初始化STL般分配器 常量ShmemAllocator alloc_inst(segment.get_segment_manager()); MYSTRUCT * myStruct_src = segment.find_or_construct&LT;&MYSTRUCT GT; (MYSTRUCT)(alloc_inst); 函数srand(时间(NULL)); myStruct_src-&GT; myVector.push_back(RAND()); MYSTRUCT * myStruct_des = segment.find_or_construct&LT;&MYSTRUCT GT; (MYSTRUCT)(alloc_inst); 用于(为size_t我= 0; I&LT; myStruct_src-&GT; myVector.size();我++){ 性病::法院LT&;&LT; I&LT;&LT; :&所述;&下; myStruct_src-&GT; myVector [1] - ;&下; =&所述;&下; myStruct_des-&GT; myVector [1] - ;&下;的std :: ENDL; 如果(myStruct_src-&GT; myVector [i] = myStruct_des-&GT;!myVector [i]){ 性病::法院LT&;&LT; 出了些问题! &LT;&LT;的std :: ENDL; } } //segment.destroy<MyVector> (MyVector); 返回0;}解决方案
如果您更改分配器类型,你改变容器(比如,编译时模板实例化的本质)。
从技术上讲,你可以设计一个类型擦除分配器(点菜的std ::功能或的boost :: any_iterator ),但这可能会导致糟糕的性能。此外,它仍然需要所有的分配器中的所有静态已知的属性对应,减少了灵活性。
在现实中,我建议只templatizing MYSTRUCT 在分配器键入要用于任何嵌入式容器。然后特意采取这种分配器在构造函数中:
//变种堆使用:使用HeapStruct = MYSTRUCT&LT;的std ::分配器取代;//变到共享内存中使用:使用ShmemStruct = MYSTRUCT&LT; BoundShmemAllocator取代;演示程序:
的#include&LT;升压/间/集装箱/ vector.hpp&GT;#包括LT&;升压/间/分配器/ allocator.hpp&GT;#包括LT&;升压/间/ managed_shared_memory.hpp&GT;#包括LT&;升压/范围/ algorithm.hpp&GT;#包括LT&;&iostream的GT;#包括LT&;&了cassert GT;命名空间BIP =的boost ::进程间;模板&LT; typename的T&GT; 使用BoundShmemAllocator = BIP ::分配器&LT; T,BIP :: managed_shared_memory :: segment_manager取代;////////////////////////////////////////////////// ///////////////你MYSTRUCT,模板化的一个分配器类模板模板&LT;模板&LT; typename的...&GT;类分配器&GT;类MYSTRUCT {上市: BIP ::矢量&lt; INT,分配器&LT; INT&GT; &GT;整型; BIP ::矢量&lt;双,分配器&LT;双&GT; &GT;双打; MYSTRUCT(常量分配器&LT;无效&GT;&安培; void_alloc = {}) :整型(void_alloc) 双打(void_alloc) {}};//变堆中使用:使用HeapStruct = MYSTRUCT&LT;的std ::分配器取代;//变到共享内存中使用:使用ShmemStruct = MYSTRUCT&LT; BoundShmemAllocator取代;//////////////////////////////////////////////////// /////////////诠释主(){ 函数srand(时间(NULL)); //你可以有这样的事情工作: HeapStruct X; //也是SHM下面的东西 的std :: generate_n(性病:: back_inserter(x.ints),20,&安培;的std ::兰特); 的std :: generate_n(性病:: back_inserter(x.doubles),20,&安培;的std ::兰特); //一个管理的共享内存,我们可以构造对象 BIP :: managed_shared_memory段= BIP :: managed_shared_memory(BIP :: open_or_create,MySharedMemory,65536); BoundShmemAllocator&LT; INT&GT;常量shmem_alloc(segment.get_segment_manager()); 自动SRC = segment.find_or_construct&所述; ShmemStruct&GT(MYSTRUCT)(shmem_alloc); src-&GT; ints.insert(src-&GT; ints.end(),x.ints.begin(),x.ints.end()); src-&GT; doubles.insert(src-&GT; doubles.end(),x.doubles.begin(),x.doubles.end()); 汽车DES = segment.find_or_construct&LT; ShmemStruct&GT;(MYSTRUCT)(shmem_alloc); 性病::法院LT&;&LT; -------------------------; 提高::复制(src-&GT;整型,性病:: ostream_iterator&LT; INT&GT;(STD ::法院LT&;&LT;\\ NSRC整型,)); 提高::复制(沙漠 - &GT;整型,性病:: ostream_iterator&LT; INT&GT;(STD ::法院LT&;&LT;\\濒死体验整型,)); 性病::法院LT&;&LT; \\ n ------------------------- 提高::复制(src-&GT;双打的std :: ostream_iterator&LT;双&GT;(STD ::法院LT&;&LT;\\ NSRC双打,)); 提高::复制(沙漠 - &GT;双打的std :: ostream_iterator&LT;双&GT;(STD ::法院LT&;&LT;\\濒死体验双打,)); 断言(src-&GT; ints.size()==沙漠&GT; ints.size()); 断言(src-&GT; doubles.size()==沙漠&GT; doubles.size()); 断言(升压::不匹配(src-&GT;整型,沙漠 - &GT;整型)==的std :: make_pair(src-&GT; ints.end(),沙漠 - &GT; ints.end())); 断言(升压::不匹配(src-&GT;双打,沙漠 - &GT;双打)==的std :: make_pair(src-&GT; doubles.end(),沙漠 - &GT; doubles.end())); segment.destroy&所述; ShmemStruct&GT(MYSTRUCT);}I have the example demo program with a boost::interprocess Containers of containers type. But I like to use the class also a normal class within my process memory. Can someone help me to write a constructor which takes no arguments to have the class initialized in my current process memory.
#include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <shmfw/serialization/interprocess_vector.hpp> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ using namespace boost::interprocess; //Alias an STL-like allocator of ints that allocates ints from the segment typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator; //Alias a vector that uses the previous STL-like allocator typedef vector<int, ShmemAllocator> MyVector; typedef allocator<void, managed_shared_memory::segment_manager > void_allocator; class MyStruct { public: MyVector myVector; //Since void_allocator is convertible to any other allocator<T>, we can simplify //the initialization taking just one allocator for all inner containers. MyStruct ( const void_allocator &void_alloc ) : myVector ( void_alloc ) {} // Thats what I like to have //MyStruct () // : myVector ( ?? ) //{} }; int main () { // I would like to have something like that working and also the shm stuff below // MyStruct x; managed_shared_memory segment; //A managed shared memory where we can construct objects //associated with a c-string try { segment = managed_shared_memory( create_only, "MySharedMemory", 65536 ); } catch (...){ segment = managed_shared_memory( open_only, "MySharedMemory" ); } //Initialize the STL-like allocator const ShmemAllocator alloc_inst ( segment.get_segment_manager() ); MyStruct *myStruct_src = segment.find_or_construct<MyStruct> ( "MyStruct" ) ( alloc_inst ); srand (time(NULL)); myStruct_src->myVector.push_back ( rand() ); MyStruct *myStruct_des = segment.find_or_construct<MyStruct> ( "MyStruct" ) ( alloc_inst ); for ( size_t i = 0; i < myStruct_src->myVector.size(); i++ ) { std::cout << i << ": " << myStruct_src->myVector[i] << " = " << myStruct_des->myVector[i] << std::endl; if(myStruct_src->myVector[i] != myStruct_des->myVector[i]) { std::cout << "Something went wrong!" << std::endl; } } //segment.destroy<MyVector> ( "MyVector" ); return 0; }解决方案
If you change the allocator type, you change the container (such is the nature of compile-time template instantiation).
Technically, you could devise a type-erased allocator (à la std::function or boost::any_iterator) but this would probably result in abysmal performance. Also, it would still require all the allocators to correspond in all the statically known properties, reducing flexibility.
In reality, I suggest just templatizing MyStruct on the Allocator type to be used for any embedded containers. Then specifically take such an allocator in the constructor:
// Variant to use on the heap: using HeapStruct = MyStruct<std::allocator>; // Variant to use in shared memory: using ShmemStruct = MyStruct<BoundShmemAllocator>;Demo Program:
#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> namespace bip = boost::interprocess; template <typename T> using BoundShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>; /////////////////////////////////////////////////////////////// // Your MyStruct, templatized for an Allocator class template template <template<typename...> class Allocator> class MyStruct { public: bip::vector<int, Allocator<int> > ints; bip::vector<double, Allocator<double> > doubles; MyStruct(const Allocator<void>& void_alloc = {}) : ints(void_alloc), doubles(void_alloc) {} }; // Variant to use on the heap: using HeapStruct = MyStruct<std::allocator>; // Variant to use in shared memory: using ShmemStruct = MyStruct<BoundShmemAllocator>; // /////////////////////////////////////////////////////////////// int main() { srand(time(NULL)); // You can have something like this working: HeapStruct x; // and also the shm stuff below std::generate_n(std::back_inserter(x.ints), 20, &std::rand); std::generate_n(std::back_inserter(x.doubles), 20, &std::rand); // A managed shared memory where we can construct objects bip::managed_shared_memory segment = bip::managed_shared_memory(bip::open_or_create, "MySharedMemory", 65536); BoundShmemAllocator<int> const shmem_alloc(segment.get_segment_manager()); auto src = segment.find_or_construct<ShmemStruct>("MyStruct")(shmem_alloc); src->ints.insert(src->ints.end(), x.ints.begin(), x.ints.end()); src->doubles.insert(src->doubles.end(), x.doubles.begin(), x.doubles.end()); auto des = segment.find_or_construct<ShmemStruct>("MyStruct")(shmem_alloc); std::cout << "-------------------------"; boost::copy(src->ints, std::ostream_iterator<int>(std::cout << "\nsrc ints: ", "; ")); boost::copy(des->ints, std::ostream_iterator<int>(std::cout << "\ndes ints: ", "; ")); std::cout << "\n-------------------------"; boost::copy(src->doubles, std::ostream_iterator<double>(std::cout << "\nsrc doubles: ", "; ")); boost::copy(des->doubles, std::ostream_iterator<double>(std::cout << "\ndes doubles: ", "; ")); assert(src->ints.size() == des->ints.size()); assert(src->doubles.size() == des->doubles.size()); assert(boost::mismatch(src->ints, des->ints) == std::make_pair(src->ints.end(), des->ints.end())); assert(boost::mismatch(src->doubles, des->doubles) == std::make_pair(src->doubles.end(), des->doubles.end())); segment.destroy<ShmemStruct>("MyStruct"); }
更多推荐
提高::集装箱的集装箱间不共享内存
发布评论