1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
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>
19 #include "../test/get_process_id_name.hpp"
24 using namespace boost::interprocess
;
27 typedef managed_shared_memory::segment_manager SegmentManager
;
28 typedef allocator
<char, SegmentManager
> CharAllocator
;
29 typedef basic_string
<char, std::char_traits
<char>
30 ,CharAllocator
> MyShmString
;
31 typedef allocator
<MyShmString
, SegmentManager
> StringAllocator
;
32 typedef vector
<MyShmString
, StringAllocator
> MyShmStringVector
;
34 //Remove shared memory on construction and destruction
39 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
40 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
43 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
44 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
55 managed_shared_memory
shm(create_only
, test::get_process_id_name(), 65536);
58 managed_shared_memory
shm(create_only
, "MySharedMemory", 10000);
64 CharAllocator
charallocator (shm
.get_segment_manager());
65 StringAllocator
stringallocator(shm
.get_segment_manager());
67 //Create a vector of strings in shared memory.
68 MyShmStringVector
*myshmvector
=
69 shm
.construct
<MyShmStringVector
>("myshmvector")(stringallocator
);
71 //Insert 50 strings in shared memory. The strings will be allocated
72 //only once and no string copy-constructor will be called when inserting
73 //strings, leading to a great performance.
74 MyShmString
string_to_compare(charallocator
);
75 string_to_compare
= "this is a long, long, long, long, long, long, string...";
77 myshmvector
->reserve(50);
78 for(int i
= 0; i
< 50; ++i
){
79 MyShmString
move_me(string_to_compare
);
80 //In the following line, no string copy-constructor will be called.
81 //"move_me"'s contents will be transferred to the string created in
83 myshmvector
->push_back(boost::move(move_me
));
85 //The source string is in default constructed state
86 assert(move_me
.empty());
88 //The newly created string will be equal to the "move_me"'s old contents
89 assert(myshmvector
->back() == string_to_compare
);
92 //Now erase a string...
93 myshmvector
->pop_back();
95 //...And insert one in the first position.
96 //No string copy-constructor or assignments will be called, but
97 //move constructors and move-assignments. No memory allocation
98 //function will be called in this operations!!
99 myshmvector
->insert(myshmvector
->begin(), boost::move(string_to_compare
));
101 //Destroy vector. This will free all strings that the vector contains
102 shm
.destroy_ptr(myshmvector
);
106 #include <boost/interprocess/detail/config_end.hpp>