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
main()22 int main ()
23 {
24 using namespace boost::interprocess;
25
26 //Typedefs
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;
33
34 //Remove shared memory on construction and destruction
35 struct shm_remove
36 {
37 //<-
38 #if 1
39 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
40 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
41 #else
42 //->
43 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
44 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
45 //<-
46 #endif
47 //->
48 } remover;
49 //<-
50 (void)remover;
51 //->
52
53 //<-
54 #if 1
55 managed_shared_memory shm(create_only, test::get_process_id_name(), 65536);
56 #else
57 //->
58 managed_shared_memory shm(create_only, "MySharedMemory", 10000);
59 //<-
60 #endif
61 //->
62
63 //Create allocators
64 CharAllocator charallocator (shm.get_segment_manager());
65 StringAllocator stringallocator(shm.get_segment_manager());
66
67 //Create a vector of strings in shared memory.
68 MyShmStringVector *myshmvector =
69 shm.construct<MyShmStringVector>("myshmvector")(stringallocator);
70
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...";
76
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
82 //the vector
83 myshmvector->push_back(boost::move(move_me));
84
85 //The source string is in default constructed state
86 assert(move_me.empty());
87
88 //The newly created string will be equal to the "move_me"'s old contents
89 assert(myshmvector->back() == string_to_compare);
90 }
91
92 //Now erase a string...
93 myshmvector->pop_back();
94
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));
100
101 //Destroy vector. This will free all strings that the vector contains
102 shm.destroy_ptr(myshmvector);
103 return 0;
104 }
105 //]
106 #include <boost/interprocess/detail/config_end.hpp>
107
108