Advertisement
Guest User

sample code

a guest
Oct 25th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <CGAL/Simple_cartesian.h>
  2. #include <CGAL/Polyhedron_3.h>
  3.  
  4. #include <boost/interprocess/managed_shared_memory.hpp>
  5. #include <boost/interprocess/allocators/allocator.hpp>
  6. #include <iostream>
  7.  
  8. using namespace boost::interprocess;
  9.  
  10. struct shm_remove {
  11.  
  12.     shm_remove() {
  13.         shared_memory_object::remove("MySharedMemory");
  14.     }
  15.  
  16.     ~shm_remove() {
  17.         shared_memory_object::remove("MySharedMemory");
  18.     }
  19. } remover;
  20.  
  21. managed_shared_memory segment(create_only,
  22.         "MySharedMemory", //segment name
  23.         65536);
  24.  
  25. template <typename _Tp>
  26. class MyStdAllocator : public std::allocator<_Tp> {};
  27.  
  28. template<class T, class SegmentManager>
  29. class MyBoostAllocator : public allocator<T, SegmentManager> {
  30. public:
  31.     typedef SegmentManager segment_manager;
  32.     typedef typename SegmentManager::void_pointer void_pointer;
  33.  
  34.     MyBoostAllocator(segment_manager *segment_mngr)
  35.     : allocator<T, SegmentManager>(segment_mngr) {
  36.     }
  37.  
  38.     MyBoostAllocator()
  39.     : allocator<T, SegmentManager>(segment.get_segment_manager()) {
  40.     }
  41.    
  42.     MyBoostAllocator(const allocator<T, SegmentManager> &other)
  43.     : allocator<T, SegmentManager>(other) {
  44.     }
  45.  
  46.     template<class T2>
  47.     MyBoostAllocator(const allocator<T2, SegmentManager> &other)
  48.     : allocator<T, SegmentManager>(other) {
  49.     }
  50. };
  51.  
  52. typedef CGAL::Simple_cartesian<double> Kernel;
  53. typedef std::allocator<int> StdAllocator;
  54. typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_3, CGAL::HalfedgeDS_default, StdAllocator> PolyhedronStd;
  55. typedef allocator <int, managed_shared_memory::segment_manager> ShMemAllocator;
  56. typedef MyBoostAllocator<int, managed_shared_memory::segment_manager> MyShMemAllocator;
  57. typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_3, CGAL::HalfedgeDS_default, ShMemAllocator > PolyhedronShMem;
  58. typedef CGAL::Polyhedron_3<Kernel, CGAL::Polyhedron_items_3, CGAL::HalfedgeDS_default, MyShMemAllocator > MyPolyhedronShMem;
  59.  
  60. int main(int argc, char** argv) {
  61.     struct shm_remove {
  62.         shm_remove() {
  63.             shared_memory_object::remove("MySharedMemory");
  64.         }
  65.         ~shm_remove() {
  66.             shared_memory_object::remove("MySharedMemory");
  67.         }
  68.     } remover;
  69.  
  70.    
  71.     PolyhedronStd p0;             // it is working
  72.     //PolyhedronShMem p1;         // compilation error - impossible to pass constructor arguments
  73.     //MyPolyhedronShMem p2;       // compilation error
  74.     //PolyhedronShMem p3(allocator_instance); //unfortunatelly no such constructor
  75.  
  76.     return 0;
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement