1//////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2008-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/managed_shared_memory.hpp>
11// intrusive/detail
12#include <boost/intrusive/detail/minimal_pair_header.hpp>
13
14typedef std::pair<double, int> simple_pair;
15
16using namespace boost::interprocess;
17
18struct array_pair : public simple_pair
19{
20 array_pair(double d, int i)
21 : simple_pair(d, i) {}
22};
23
24struct array_it_pair : public array_pair
25{
26 array_it_pair(double d, int i)
27 : array_pair(d, i) {}
28};
29
30struct named_name_generator
31{
32 static const bool searchable = true;
33
34 typedef simple_pair simple_type;
35 typedef array_pair array_type;
36 typedef array_it_pair array_it_type;
37 static const char *get_simple_name()
38 { return "MyType instance"; }
39 static const char *get_array_name()
40 { return "MyType array"; }
41 static const char *get_array_it_name()
42 { return "MyType array from it"; }
43};
44
45struct unique_name_generator
46{
47 static const bool searchable = true;
48
49 typedef simple_pair simple_type;
50 typedef array_pair array_type;
51 typedef array_it_pair array_it_type;
52 static const ipcdetail::unique_instance_t *get_simple_name()
53 { return 0; }
54 static const ipcdetail::unique_instance_t *get_array_name()
55 { return 0; }
56 static const ipcdetail::unique_instance_t *get_array_it_name()
57 { return 0; }
58};
59
60struct anonymous_name_generator
61{
62 static const bool searchable = false;
63
64 typedef simple_pair simple_type;
65 typedef array_pair array_type;
66 typedef array_it_pair array_it_type;
67 static const ipcdetail::anonymous_instance_t *get_simple_name()
68 { return 0; }
69 static const ipcdetail::anonymous_instance_t *get_array_name()
70 { return 0; }
71 static const ipcdetail::anonymous_instance_t *get_array_it_name()
72 { return 0; }
73};
74
75
76template<class NameGenerator>
77int construct_test()
78{
79 typedef typename NameGenerator::simple_type simple_type;
80 typedef typename NameGenerator::array_type array_type;
81 typedef typename NameGenerator::array_it_type array_it_type;
82
83 remove_shared_memory_on_destroy remover("MySharedMemory");
84 shared_memory_object::remove(filename: "MySharedMemory");
85 {
86 //A special shared memory where we can
87 //construct objects associated with a name.
88 //First remove any old shared memory of the same name, create
89 //the shared memory segment and initialize needed resources
90 managed_shared_memory segment
91 //create segment name segment size
92 (create_only, "MySharedMemory", 65536);
93
94 //Create an object of MyType initialized to {0.0, 0}
95 simple_type *s = segment.construct<simple_type>
96 (NameGenerator::get_simple_name())//name of the object
97 (1.0, 2); //ctor first argument
98 assert(s->first == 1.0 && s->second == 2);
99 if(!(s->first == 1.0 && s->second == 2))
100 return 1;
101
102 //Create an array of 10 elements of MyType initialized to {0.0, 0}
103 array_type *a = segment.construct<array_type>
104 (NameGenerator::get_array_name()) //name of the object
105 [10] //number of elements
106 (3.0, 4); //Same two ctor arguments for all objects
107 assert(a->first == 3.0 && a->second == 4);
108 if(!(a->first == 3.0 && a->second == 4))
109 return 1;
110
111 //Create an array of 3 elements of MyType initializing each one
112 //to a different value {0.0, 3}, {1.0, 4}, {2.0, 5}...
113 float float_initializer[3] = { 0.0, 1.0, 2.0 };
114 int int_initializer[3] = { 3, 4, 5 };
115
116 array_it_type *a_it = segment.construct_it<array_it_type>
117 (NameGenerator::get_array_it_name()) //name of the object
118 [3] //number of elements
119 ( &float_initializer[0] //Iterator for the 1st ctor argument
120 , &int_initializer[0]); //Iterator for the 2nd ctor argument
121 {
122 const array_it_type *a_it_ptr = a_it;
123 for(unsigned int i = 0, max = 3; i != max; ++i, ++a_it_ptr){
124 assert(a_it_ptr->first == float_initializer[i]);
125 if(a_it_ptr->first != float_initializer[i]){
126 return 1;
127 }
128 assert(a_it_ptr->second == int_initializer[i]);
129 if(a_it_ptr->second != int_initializer[i]){
130 return 1;
131 }
132 }
133 }
134
135 if(NameGenerator::searchable){
136 {
137 std::pair<simple_type*, managed_shared_memory::size_type> res;
138 //Find the object
139 res = segment.find<simple_type> (NameGenerator::get_simple_name());
140 //Length should be 1
141 assert(res.second == 1);
142 if(res.second != 1)
143 return 1;
144 assert(res.first == s);
145 if(res.first != s)
146 return 1;
147 }
148 {
149 std::pair<array_type*, managed_shared_memory::size_type> res;
150
151 //Find the array
152 res = segment.find<array_type> (NameGenerator::get_array_name());
153 //Length should be 10
154 assert(res.second == 10);
155 if(res.second != 10)
156 return 1;
157 assert(res.first == a);
158 if(res.first != a)
159 return 1;
160 }
161 {
162 std::pair<array_it_type*, managed_shared_memory::size_type> res;
163 //Find the array constructed from iterators
164 res = segment.find<array_it_type> (NameGenerator::get_array_it_name());
165 //Length should be 3
166 assert(res.second == 3);
167 if(res.second != 3)
168 return 1;
169 assert(res.first == a_it);
170 if(res.first != a_it)
171 return 1;
172 }
173 }
174 //We're done, delete all the objects
175 segment.destroy_ptr<simple_type>(s);
176 segment.destroy_ptr<array_type>(a);
177 segment.destroy_ptr<array_it_type>(a_it);
178 }
179 return 0;
180}
181
182int main ()
183{
184 if(0 != construct_test<named_name_generator>())
185 return 1;
186 if(0 != construct_test<unique_name_generator>())
187 return 1;
188 if(0 != construct_test<anonymous_name_generator>())
189 return 1;
190 return 0;
191}
192
193//]
194

source code of boost/libs/interprocess/test/named_construct_test.cpp