doc_unique_ptr.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/interprocess for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #include <boost/interprocess/detail/config_begin.hpp>
  12. #include <boost/interprocess/detail/workaround.hpp>
  13. //[doc_unique_ptr
  14. #include <boost/interprocess/managed_mapped_file.hpp>
  15. #include <boost/interprocess/smart_ptr/unique_ptr.hpp>
  16. #include <boost/interprocess/containers/vector.hpp>
  17. #include <boost/interprocess/containers/list.hpp>
  18. #include <boost/interprocess/allocators/allocator.hpp>
  19. #include <cassert>
  20. //<-
  21. #include "../test/get_process_id_name.hpp"
  22. //->
  23. using namespace boost::interprocess;
  24. //This is type of the object we'll allocate dynamically
  25. struct MyType
  26. {
  27. MyType(int number = 0)
  28. : number_(number)
  29. {}
  30. int number_;
  31. };
  32. //This is the type of a unique pointer to the previous type
  33. //that will be built in the mapped file
  34. typedef managed_unique_ptr<MyType, managed_mapped_file>::type unique_ptr_type;
  35. //Define containers of unique pointer. Unique pointer simplifies object management
  36. typedef vector
  37. < unique_ptr_type
  38. , allocator<unique_ptr_type, managed_mapped_file::segment_manager>
  39. > unique_ptr_vector_t;
  40. typedef list
  41. < unique_ptr_type
  42. , allocator<unique_ptr_type, managed_mapped_file::segment_manager>
  43. > unique_ptr_list_t;
  44. int main ()
  45. {
  46. //Define file names
  47. //<-
  48. #if 1
  49. std::string mapped_file(boost::interprocess::ipcdetail::get_temporary_path());
  50. mapped_file += "/"; mapped_file += test::get_process_id_name();
  51. const char *MappedFile = mapped_file.c_str();
  52. #else
  53. //->
  54. const char *MappedFile = "MyMappedFile";
  55. //<-
  56. #endif
  57. //->
  58. //Destroy any previous file with the name to be used.
  59. struct file_remove
  60. {
  61. file_remove(const char *MappedFile)
  62. : MappedFile_(MappedFile) { file_mapping::remove(MappedFile_); }
  63. ~file_remove(){ file_mapping::remove(MappedFile_); }
  64. const char *MappedFile_;
  65. } remover(MappedFile);
  66. {
  67. managed_mapped_file file(create_only, MappedFile, 65536);
  68. //Construct an object in the file and
  69. //pass ownership to this local unique pointer
  70. unique_ptr_type local_unique_ptr (make_managed_unique_ptr
  71. (file.construct<MyType>("unique object")(), file));
  72. assert(local_unique_ptr.get() != 0);
  73. //Reset the unique pointer. The object is automatically destroyed
  74. local_unique_ptr.reset();
  75. assert(file.find<MyType>("unique object").first == 0);
  76. //Now create a vector of unique pointers
  77. unique_ptr_vector_t *unique_vector =
  78. file.construct<unique_ptr_vector_t>("unique vector")(file.get_segment_manager());
  79. //Speed optimization
  80. unique_vector->reserve(100);
  81. //Now insert all values
  82. for(int i = 0; i < 100; ++i){
  83. unique_ptr_type p(make_managed_unique_ptr(file.construct<MyType>(anonymous_instance)(i), file));
  84. unique_vector->push_back(boost::move(p));
  85. assert(unique_vector->back()->number_ == i);
  86. }
  87. //Now create a list of unique pointers
  88. unique_ptr_list_t *unique_list =
  89. file.construct<unique_ptr_list_t>("unique list")(file.get_segment_manager());
  90. //Pass ownership of all values to the list
  91. for(int i = 99; !unique_vector->empty(); --i){
  92. unique_list->push_front(boost::move(unique_vector->back()));
  93. //The unique ptr of the vector is now empty...
  94. assert(unique_vector->back() == 0);
  95. unique_vector->pop_back();
  96. //...and the list has taken ownership of the value
  97. assert(unique_list->front() != 0);
  98. assert(unique_list->front()->number_ == i);
  99. }
  100. assert(unique_list->size() == 100);
  101. //Now destroy the empty vector.
  102. file.destroy_ptr(unique_vector);
  103. //The mapped file is unmapped here. Objects have been flushed to disk
  104. }
  105. {
  106. //Reopen the mapped file and find again the list
  107. managed_mapped_file file(open_only, MappedFile);
  108. unique_ptr_list_t *unique_list =
  109. file.find<unique_ptr_list_t>("unique list").first;
  110. assert(unique_list);
  111. assert(unique_list->size() == 100);
  112. unique_ptr_list_t::const_iterator list_it = unique_list->begin();
  113. for(int i = 0; i < 100; ++i, ++list_it){
  114. assert((*list_it)->number_ == i);
  115. }
  116. //Now destroy the list. All elements will be automatically deallocated.
  117. file.destroy_ptr(unique_list);
  118. }
  119. return 0;
  120. }
  121. //]
  122. #include <boost/interprocess/detail/config_end.hpp>