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