doc_unordered_map.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_unordered_map
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/allocators/allocator.hpp>
  15. //<-
  16. //Shield against external warnings
  17. #include <boost/interprocess/detail/config_external_begin.hpp>
  18. //->
  19. #include <boost/unordered_map.hpp> //boost::unordered_map
  20. //<-
  21. #include <boost/interprocess/detail/config_external_end.hpp>
  22. #include "../test/get_process_id_name.hpp"
  23. //->
  24. #include <functional> //std::equal_to
  25. #include <boost/functional/hash.hpp> //boost::hash
  26. //<-
  27. #include "../test/get_process_id_name.hpp"
  28. //->
  29. int main ()
  30. {
  31. using namespace boost::interprocess;
  32. //Remove shared memory on construction and destruction
  33. struct shm_remove
  34. {
  35. //<-
  36. #if 1
  37. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  38. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  39. #else
  40. //->
  41. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  42. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  43. //<-
  44. #endif
  45. //->
  46. } remover;
  47. //<-
  48. (void)remover;
  49. //->
  50. //Create shared memory
  51. //<-
  52. #if 1
  53. managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
  54. #else
  55. //->
  56. managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  57. //<-
  58. #endif
  59. //->
  60. //Note that unordered_map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
  61. //so the allocator must allocate that pair.
  62. typedef int KeyType;
  63. typedef float MappedType;
  64. typedef std::pair<const int, float> ValueType;
  65. //Typedef the allocator
  66. typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
  67. //Alias an unordered_map of ints that uses the previous STL-like allocator.
  68. typedef boost::unordered_map
  69. < KeyType , MappedType
  70. , boost::hash<KeyType> ,std::equal_to<KeyType>
  71. , ShmemAllocator>
  72. MyHashMap;
  73. //Construct a shared memory hash map.
  74. //Note that the first parameter is the initial bucket count and
  75. //after that, the hash function, the equality function and the allocator
  76. MyHashMap *myhashmap = segment.construct<MyHashMap>("MyHashMap") //object name
  77. ( 3, boost::hash<int>(), std::equal_to<int>() //
  78. , segment.get_allocator<ValueType>()); //allocator instance
  79. //Insert data in the hash map
  80. for(int i = 0; i < 100; ++i){
  81. myhashmap->insert(ValueType(i, (float)i));
  82. }
  83. return 0;
  84. }
  85. //]
  86. #include <boost/interprocess/detail/config_end.hpp>