Skip to content

Commit ea6eac2

Browse files
committed
Add more documentation and examples uses_allocator usage.
1 parent 12108f7 commit ea6eac2

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

doc/interprocess.qbk

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5167,7 +5167,9 @@ To place any of these containers in managed memory segments, we must
51675167
define the allocator template parameter with a [*Boost.Interprocess] allocator
51685168
so that the container allocates the values in the managed memory segment.
51695169
To place the container itself in shared memory, we construct it
5170-
in the managed memory segment just like any other object with [*Boost.Interprocess]:
5170+
in the managed memory segment just like any other object with [*Boost.Interprocess].
5171+
Note how these containers also support the uses-allocator protocol that simplifies
5172+
usage due to the implicit allocator passing protocol:
51715173

51725174
[import ../example/doc_cont.cpp]
51735175
[doc_cont]
@@ -5263,7 +5265,9 @@ and those strings need to be placed in shared memory. Shared memory strings requ
52635265
an allocator in their constructors so this usually makes object insertion a bit more
52645266
complicated.
52655267

5266-
Here is an example that shows how to put a multi index container in shared memory:
5268+
Here is an example that shows how to put a multi index container in shared memory. Note
5269+
also how MultiIndex supports the [link interprocess.managed_memory_segment_object_construction.uses_allocator uses-allocator construction]
5270+
so that allocator arguments can be automatically propagated into internal allocator-aware components:
52675271

52685272
[import ../example/doc_multi_index.cpp]
52695273
[doc_multi_index]
@@ -6853,7 +6857,8 @@ thank them:
68536857

68546858
[section:release_notes_boost_1_91_00 Boost 1.91 Release]
68556859

6856-
* Adds uses-allocator-construction (for types where `boost::container::uses_allocator<T>::value == true`), in the following utilities:
6860+
* Adds [link interprocess.managed_memory_segment_object_construction.uses_allocator uses-allocator construction]
6861+
(for types where `boost::container::uses_allocator<T>::value == true`), in the following utilities:
68576862
* Segment managers in their `construct` methods
68586863
* Allocators
68596864

example/doc_complex_map_uses_allocator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/container/map.hpp>
1616
#include <boost/container/vector.hpp>
1717
#include <boost/container/string.hpp>
18+
//=#include <functional>
1819
//<-
1920
#include "../test/get_process_id_name.hpp"
2021
//->

example/doc_cont.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ int main ()
6262
// . . .
6363
//When done, destroy and delete vector from the segment
6464
segment.destroy<MyVector>("MyVector");
65+
66+
//Note that you can take advantage of the uses-allocator protocol and avoid
67+
//explicitly passing the allocator parameter ("construct" will detect the container
68+
//is compatible with the protocol and initialize the allocator accordingly:
69+
myvector = segment.construct<MyVector>("MyVector") (begVal,endVal);
70+
71+
//Alternative destroy function that takes the pointer instead of the name
72+
segment.destroy_ptr(myvector);
73+
6574
return 0;
6675
}
6776
//]

example/doc_multi_index.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ typedef boost::container::basic_string<char, std::char_traits<char>, char_alloca
3838
//Data to insert in shared memory
3939
struct employee
4040
{
41+
typedef char_allocator allocator_type; //enables uses-allocator protocol
42+
4143
int id;
4244
int age;
4345
shm_string name;
@@ -85,7 +87,7 @@ int main ()
8587
//Create shared memory
8688
managed_shared_memory segment(create_only,test::get_process_id_name(), 65536);
8789

88-
//Construct the multi_index in shared memory
90+
//Construct the multi_index in shared memory (classic construction)
8991
employee_set *es = segment.construct<employee_set>
9092
("My MultiIndex Container") //Container's name in shared memory
9193
( employee_set::ctor_args_list()
@@ -96,6 +98,19 @@ int main ()
9698
es->insert(employee(0,31, "Joe", ca));
9799
es->insert(employee(1,27, "Robert", ca));
98100
es->insert(employee(2,40, "John", ca));
101+
segment.destroy_ptr(es);
102+
103+
//Now re-construct it using the uses-allocator protocol
104+
es = segment.construct<employee_set>
105+
("My MultiIndex Container") //Container's name in shared memory
106+
( employee_set::ctor_args_list() ); //Allocator parameters is implicit
107+
108+
//Now emplace elements (more natural, the allocator is implicitly propagated)
109+
es->emplace(0,31, "Joe");
110+
es->emplace(1,27, "Robert");
111+
es->emplace(2,40, "John");
112+
segment.destroy_ptr(es);
113+
99114
return 0;
100115
}
101116
//]

0 commit comments

Comments
 (0)