doc_move_containers.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_move_containers
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/containers/vector.hpp>
  15. #include <boost/interprocess/containers/string.hpp>
  16. #include <boost/interprocess/allocators/allocator.hpp>
  17. #include <cassert>
  18. //<-
  19. #include "../test/get_process_id_name.hpp"
  20. //->
  21. int main ()
  22. {
  23. using namespace boost::interprocess;
  24. //Typedefs
  25. typedef managed_shared_memory::segment_manager SegmentManager;
  26. typedef allocator<char, SegmentManager> CharAllocator;
  27. typedef basic_string<char, std::char_traits<char>
  28. ,CharAllocator> MyShmString;
  29. typedef allocator<MyShmString, SegmentManager> StringAllocator;
  30. typedef vector<MyShmString, StringAllocator> MyShmStringVector;
  31. //Remove shared memory on construction and destruction
  32. struct shm_remove
  33. {
  34. //<-
  35. #if 1
  36. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  37. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  38. #else
  39. //->
  40. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  41. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  42. //<-
  43. #endif
  44. //->
  45. } remover;
  46. //<-
  47. (void)remover;
  48. //->
  49. //<-
  50. #if 1
  51. managed_shared_memory shm(create_only, test::get_process_id_name(), 65536);
  52. #else
  53. //->
  54. managed_shared_memory shm(create_only, "MySharedMemory", 10000);
  55. //<-
  56. #endif
  57. //->
  58. //Create allocators
  59. CharAllocator charallocator (shm.get_segment_manager());
  60. StringAllocator stringallocator(shm.get_segment_manager());
  61. //Create a vector of strings in shared memory.
  62. MyShmStringVector *myshmvector =
  63. shm.construct<MyShmStringVector>("myshmvector")(stringallocator);
  64. //Insert 50 strings in shared memory. The strings will be allocated
  65. //only once and no string copy-constructor will be called when inserting
  66. //strings, leading to a great performance.
  67. MyShmString string_to_compare(charallocator);
  68. string_to_compare = "this is a long, long, long, long, long, long, string...";
  69. myshmvector->reserve(50);
  70. for(int i = 0; i < 50; ++i){
  71. MyShmString move_me(string_to_compare);
  72. //In the following line, no string copy-constructor will be called.
  73. //"move_me"'s contents will be transferred to the string created in
  74. //the vector
  75. myshmvector->push_back(boost::move(move_me));
  76. //The source string is in default constructed state
  77. assert(move_me.empty());
  78. //The newly created string will be equal to the "move_me"'s old contents
  79. assert(myshmvector->back() == string_to_compare);
  80. }
  81. //Now erase a string...
  82. myshmvector->pop_back();
  83. //...And insert one in the first position.
  84. //No string copy-constructor or assignments will be called, but
  85. //move constructors and move-assignments. No memory allocation
  86. //function will be called in this operations!!
  87. myshmvector->insert(myshmvector->begin(), boost::move(string_to_compare));
  88. //Destroy vector. This will free all strings that the vector contains
  89. shm.destroy_ptr(myshmvector);
  90. return 0;
  91. }
  92. //]
  93. #include <boost/interprocess/detail/config_end.hpp>