doc_complex_map.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_complex_map
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/allocators/allocator.hpp>
  15. #include <boost/interprocess/containers/map.hpp>
  16. #include <boost/interprocess/containers/vector.hpp>
  17. #include <boost/interprocess/containers/string.hpp>
  18. //<-
  19. #include "../test/get_process_id_name.hpp"
  20. //->
  21. using namespace boost::interprocess;
  22. //Typedefs of allocators and containers
  23. typedef managed_shared_memory::segment_manager segment_manager_t;
  24. typedef allocator<void, segment_manager_t> void_allocator;
  25. typedef allocator<int, segment_manager_t> int_allocator;
  26. typedef vector<int, int_allocator> int_vector;
  27. typedef allocator<int_vector, segment_manager_t> int_vector_allocator;
  28. typedef vector<int_vector, int_vector_allocator> int_vector_vector;
  29. typedef allocator<char, segment_manager_t> char_allocator;
  30. typedef basic_string<char, std::char_traits<char>, char_allocator> char_string;
  31. class complex_data
  32. {
  33. int id_;
  34. char_string char_string_;
  35. int_vector_vector int_vector_vector_;
  36. public:
  37. //Since void_allocator is convertible to any other allocator<T>, we can simplify
  38. //the initialization taking just one allocator for all inner containers.
  39. complex_data(int id, const char *name, const void_allocator &void_alloc)
  40. : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc)
  41. {}
  42. //Other members...
  43. //<-
  44. int get_id() { return id_; };
  45. char_string get_char_string() { return char_string_; };
  46. int_vector_vector get_int_vector_vector() { return int_vector_vector_; };
  47. //->
  48. };
  49. //Definition of the map holding a string as key and complex_data as mapped type
  50. typedef std::pair<const char_string, complex_data> map_value_type;
  51. typedef std::pair<char_string, complex_data> movable_to_map_value_type;
  52. typedef allocator<map_value_type, segment_manager_t> map_value_type_allocator;
  53. typedef map< char_string, complex_data
  54. , std::less<char_string>, map_value_type_allocator> complex_map_type;
  55. int main ()
  56. {
  57. //Remove shared memory on construction and destruction
  58. struct shm_remove
  59. {
  60. //<-
  61. #if 1
  62. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  63. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  64. #else
  65. //->
  66. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  67. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  68. //<-
  69. #endif
  70. //->
  71. } remover;
  72. //<-
  73. (void)remover;
  74. //->
  75. //Create shared memory
  76. //<-
  77. #if 1
  78. managed_shared_memory segment(create_only,test::get_process_id_name(), 65536);
  79. #else
  80. //->
  81. managed_shared_memory segment(create_only,"MySharedMemory", 65536);
  82. //<-
  83. #endif
  84. //->
  85. //An allocator convertible to any allocator<T, segment_manager_t> type
  86. void_allocator alloc_inst (segment.get_segment_manager());
  87. //Construct the shared memory map and fill it
  88. complex_map_type *mymap = segment.construct<complex_map_type>
  89. //(object name), (first ctor parameter, second ctor parameter)
  90. ("MyMap")(std::less<char_string>(), alloc_inst);
  91. for(int i = 0; i < 100; ++i){
  92. //Both key(string) and value(complex_data) need an allocator in their constructors
  93. char_string key_object(alloc_inst);
  94. complex_data mapped_object(i, "default_name", alloc_inst);
  95. map_value_type value(key_object, mapped_object);
  96. //Modify values and insert them in the map
  97. mymap->insert(value);
  98. }
  99. return 0;
  100. }
  101. //]
  102. #include <boost/interprocess/detail/config_end.hpp>