doc_map.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include <boost/interprocess/detail/workaround.hpp>
  12. //[doc_map
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/containers/map.hpp>
  15. #include <boost/interprocess/allocators/allocator.hpp>
  16. #include <functional>
  17. #include <utility>
  18. //<-
  19. #include "../test/get_process_id_name.hpp"
  20. //->
  21. int main ()
  22. {
  23. using namespace boost::interprocess;
  24. //Remove shared memory on construction and destruction
  25. struct shm_remove
  26. {
  27. //<-
  28. #if 1
  29. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  30. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  31. #else
  32. //->
  33. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  34. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  35. //<-
  36. #endif
  37. //->
  38. } remover;
  39. //<-
  40. (void)remover;
  41. //->
  42. //Shared memory front-end that is able to construct objects
  43. //associated with a c-string. Erase previous shared memory with the name
  44. //to be used and create the memory segment at the specified address and initialize resources
  45. //<-
  46. #if 1
  47. managed_shared_memory segment(create_only,test::get_process_id_name(), 65536);
  48. #else
  49. //->
  50. managed_shared_memory segment
  51. (create_only
  52. ,"MySharedMemory" //segment name
  53. ,65536); //segment size in bytes
  54. //<-
  55. #endif
  56. //->
  57. //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
  58. //so the allocator must allocate that pair.
  59. typedef int KeyType;
  60. typedef float MappedType;
  61. typedef std::pair<const int, float> ValueType;
  62. //Alias an STL compatible allocator of for the map.
  63. //This allocator will allow to place containers
  64. //in managed shared memory segments
  65. typedef allocator<ValueType, managed_shared_memory::segment_manager>
  66. ShmemAllocator;
  67. //Alias a map of ints that uses the previous STL-like allocator.
  68. //Note that the third parameter argument is the ordering function
  69. //of the map, just like with std::map, used to compare the keys.
  70. typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap;
  71. //Initialize the shared memory STL-compatible allocator
  72. ShmemAllocator alloc_inst (segment.get_segment_manager());
  73. //Construct a shared memory map.
  74. //Note that the first parameter is the comparison function,
  75. //and the second one the allocator.
  76. //This the same signature as std::map's constructor taking an allocator
  77. MyMap *mymap =
  78. segment.construct<MyMap>("MyMap") //object name
  79. (std::less<int>() //first ctor parameter
  80. ,alloc_inst); //second ctor parameter
  81. //Insert data in the map
  82. for(int i = 0; i < 100; ++i){
  83. mymap->insert(std::pair<const int, float>(i, (float)i));
  84. }
  85. return 0;
  86. }
  87. //]
  88. #include <boost/interprocess/detail/config_end.hpp>