| 1 |
|
|---|
| 2 | #include <boost/interprocess/managed_mapped_file.hpp>
|
|---|
| 3 | #include <boost/interprocess/containers/string.hpp>
|
|---|
| 4 | #include <boost/interprocess/containers/vector.hpp>
|
|---|
| 5 | #include <boost/interprocess/allocators/allocator.hpp>
|
|---|
| 6 |
|
|---|
| 7 | #include <functional>
|
|---|
| 8 | #include <utility>
|
|---|
| 9 | #include <stddef.h>
|
|---|
| 10 | #include <iostream>
|
|---|
| 11 |
|
|---|
| 12 | int main()
|
|---|
| 13 | {
|
|---|
| 14 | using namespace boost::interprocess;
|
|---|
| 15 |
|
|---|
| 16 | const size_t mapped_segment_size = 0x1 << 20; // 1MB
|
|---|
| 17 |
|
|---|
| 18 | const char* test_strings[] = { "a1", "b2", "c3", "d4", "e5" };
|
|---|
| 19 |
|
|---|
| 20 | const size_t num_strings = sizeof(test_strings)/sizeof(const char*);
|
|---|
| 21 |
|
|---|
| 22 | typedef managed_mapped_file::segment_manager segment_manager_t;
|
|---|
| 23 |
|
|---|
| 24 | typedef allocator<char, segment_manager_t> shm_char_allocator_t;
|
|---|
| 25 |
|
|---|
| 26 | typedef basic_string<char, std::char_traits<char>, shm_char_allocator_t> shm_string_t;
|
|---|
| 27 |
|
|---|
| 28 | typedef allocator<shm_string_t, segment_manager_t> shm_string_allocator_t;
|
|---|
| 29 |
|
|---|
| 30 | typedef vector<shm_string_t, shm_string_allocator_t> shm_string_vector_t;
|
|---|
| 31 |
|
|---|
| 32 | managed_mapped_file segment(open_or_create, "TestMappedFile", mapped_segment_size);
|
|---|
| 33 |
|
|---|
| 34 | shm_char_allocator_t shm_char_allocator(segment.get_segment_manager());
|
|---|
| 35 |
|
|---|
| 36 | shm_string_allocator_t shm_string_allocator(segment.get_segment_manager());
|
|---|
| 37 |
|
|---|
| 38 | shm_string_vector_t* vec = segment.find<shm_string_vector_t>("TestStringVector").first;
|
|---|
| 39 | if (vec == 0) {
|
|---|
| 40 | // We didn't find it; create and populate it
|
|---|
| 41 | vec = segment.construct<shm_string_vector_t>("TestStringVector")(shm_string_allocator);
|
|---|
| 42 | for (size_t i = 0; i < num_strings; ++i) {
|
|---|
| 43 | vec->push_back(shm_string_t(test_strings[i], shm_char_allocator));
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | // Print out values
|
|---|
| 48 | std::cout << "Shared memory allocator: " << std::endl;
|
|---|
| 49 | for (shm_string_vector_t::iterator i = vec->begin(); i != vec->end(); ++i) {
|
|---|
| 50 | const shm_string_t& s = *i;
|
|---|
| 51 | std::cout << "String: '" << s << "' size: " << s.size() << " length: " << s.length()
|
|---|
| 52 | << " c_str(): '" << s.c_str() << "'"
|
|---|
| 53 | << std::endl;
|
|---|
| 54 | }
|
|---|
| 55 | std::cout << "Standard allocator: " << std::endl;
|
|---|
| 56 | vector<string> vec2;
|
|---|
| 57 | for (size_t j = 0; j < num_strings; ++j) {
|
|---|
| 58 | vec2.push_back(string(test_strings[j]));
|
|---|
| 59 | }
|
|---|
| 60 | for (vector<string>::iterator k = vec2.begin(); k != vec2.end(); ++k) {
|
|---|
| 61 | const string& s = *k;
|
|---|
| 62 | std::cout << "String: '" << s << "' size: " << s.size() << " length: " << s.length()
|
|---|
| 63 | << " c_str(): '" << s.c_str() << "'"
|
|---|
| 64 | << std::endl;
|
|---|
| 65 | }
|
|---|
| 66 | return 0;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|