| 1 | /* Boost.MultiIndex example of use of Boost.Interprocess allocators.
|
|---|
| 2 | *
|
|---|
| 3 | * Copyright 2003-2008 Joaquin M Lopez Munoz.
|
|---|
| 4 | * Distributed under the Boost Software License, Version 1.0.
|
|---|
| 5 | * (See accompanying file LICENSE_1_0.txt or copy at
|
|---|
| 6 | * http://www.boost.org/LICENSE_1_0.txt)
|
|---|
| 7 | *
|
|---|
| 8 | * See http://www.boost.org/libs/multi_index for library home page.
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #include "stdafx.h"
|
|---|
| 12 |
|
|---|
| 13 | using boost::multi_index_container;
|
|---|
| 14 | using namespace boost::multi_index;
|
|---|
| 15 | namespace bip = boost::interprocess;
|
|---|
| 16 |
|
|---|
| 17 | typedef bip::managed_windows_shared_memory ManagedSharedMemory;
|
|---|
| 18 | typedef ManagedSharedMemory::segment_manager SegmentManager;
|
|---|
| 19 | typedef bip::allocator<char, SegmentManager> CharAllocator;
|
|---|
| 20 | typedef bip::allocator<void, SegmentManager> VoidAllocator;
|
|---|
| 21 |
|
|---|
| 22 | /* shared_string is a string type placeable in shared memory,
|
|---|
| 23 | * courtesy of Boost.Interprocess.
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | typedef bip::basic_string<
|
|---|
| 27 | char, std::char_traits<char>,
|
|---|
| 28 | CharAllocator
|
|---|
| 29 | > shared_string;
|
|---|
| 30 |
|
|---|
| 31 | struct book
|
|---|
| 32 | {
|
|---|
| 33 | shared_string name;
|
|---|
| 34 | shared_string author;
|
|---|
| 35 | int id;
|
|---|
| 36 | int ownerid;
|
|---|
| 37 | int64_t brand;
|
|---|
| 38 |
|
|---|
| 39 | book(const shared_string::allocator_type& al) :
|
|---|
| 40 | name(al), author(al), id(), ownerid(), brand(0)
|
|---|
| 41 | {}
|
|---|
| 42 |
|
|---|
| 43 | friend std::ostream& operator<<(std::ostream& os, const book& b)
|
|---|
| 44 | {
|
|---|
| 45 | os << b.author << ": \"" << b.name << "\", I$" << b.id << ", O$" << b.ownerid << ", " << b.brand << " brand\n";
|
|---|
| 46 | return os;
|
|---|
| 47 | }
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | typedef bip::offset_ptr<book> bookPtr;
|
|---|
| 51 |
|
|---|
| 52 | /* partial_str_less allows for partial searches taking into account
|
|---|
| 53 | * only the first n chars of the strings compared against. See
|
|---|
| 54 | * Tutorial: Basics: Special lookup operations for more info on this
|
|---|
| 55 | * type of comparison functors.
|
|---|
| 56 | */
|
|---|
| 57 |
|
|---|
| 58 | /* partial_string is a mere string holder used to differentiate from
|
|---|
| 59 | * a plain string.
|
|---|
| 60 | */
|
|---|
| 61 |
|
|---|
| 62 | struct partial_string
|
|---|
| 63 | {
|
|---|
| 64 | partial_string(const shared_string& str) :str(str){}
|
|---|
| 65 | shared_string str;
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | struct partial_str_less
|
|---|
| 69 | {
|
|---|
| 70 | bool operator()(const shared_string& x, const shared_string& y)const
|
|---|
| 71 | {
|
|---|
| 72 | return x<y;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | bool operator()(const shared_string& x, const partial_string& y)const
|
|---|
| 76 | {
|
|---|
| 77 | return x.substr(0, y.str.size())<y.str;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | bool operator()(const partial_string& x, const shared_string& y)const
|
|---|
| 81 | {
|
|---|
| 82 | return x.str<y.substr(0, x.str.size());
|
|---|
| 83 | }
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | /* Define a multi_index_container of book records with indices on
|
|---|
| 87 | * author, name and prize. The index on names allows for partial
|
|---|
| 88 | * searches. This container can be placed in shared memory because:
|
|---|
| 89 | * * book can be placed in shared memory.
|
|---|
| 90 | * * We are using a Boost.Interprocess specific allocator.
|
|---|
| 91 | */
|
|---|
| 92 |
|
|---|
| 93 | /* see Compiler specifics: Use of member_offset for info on
|
|---|
| 94 | * BOOST_MULTI_INDEX_MEMBER
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | typedef multi_index_container
|
|---|
| 98 | <
|
|---|
| 99 | bookPtr,
|
|---|
| 100 | indexed_by
|
|---|
| 101 | <
|
|---|
| 102 | random_access<>,
|
|---|
| 103 | hashed_unique
|
|---|
| 104 | <
|
|---|
| 105 | BOOST_MULTI_INDEX_MEMBER(book, shared_string, author)
|
|---|
| 106 | >,
|
|---|
| 107 | hashed_non_unique
|
|---|
| 108 | <
|
|---|
| 109 | BOOST_MULTI_INDEX_MEMBER(book, shared_string, name)
|
|---|
| 110 | >,
|
|---|
| 111 | ordered_unique
|
|---|
| 112 | <
|
|---|
| 113 | BOOST_MULTI_INDEX_MEMBER(book, int, id)
|
|---|
| 114 | >,
|
|---|
| 115 | ordered_non_unique
|
|---|
| 116 | <
|
|---|
| 117 | BOOST_MULTI_INDEX_MEMBER(book, int, ownerid)
|
|---|
| 118 | >,
|
|---|
| 119 | ordered_non_unique
|
|---|
| 120 | <
|
|---|
| 121 | BOOST_MULTI_INDEX_MEMBER(book, int64_t, brand)
|
|---|
| 122 | >
|
|---|
| 123 | >,
|
|---|
| 124 |
|
|---|
| 125 | ManagedSharedMemory::allocator<bookPtr>::type
|
|---|
| 126 |
|
|---|
| 127 | > book_container;
|
|---|
| 128 |
|
|---|
| 129 | typedef book_container::nth_index<0>::type BOOK_BY_RANDOM_ACCESS_TYPE;
|
|---|
| 130 |
|
|---|
| 131 | class BookRepository
|
|---|
| 132 | {
|
|---|
| 133 | public:
|
|---|
| 134 |
|
|---|
| 135 | book_container m_BookContainer;
|
|---|
| 136 |
|
|---|
| 137 | BookRepository(const VoidAllocator& va)
|
|---|
| 138 | :
|
|---|
| 139 | m_BookContainer(va)
|
|---|
| 140 | {
|
|---|
| 141 |
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | BookRepository(const BookRepository& k)
|
|---|
| 145 | :
|
|---|
| 146 | m_BookContainer(k.m_BookContainer)
|
|---|
| 147 | {
|
|---|
| 148 |
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 | /* A small utility to get data entered via std::cin */
|
|---|
| 154 |
|
|---|
| 155 | template<typename T>
|
|---|
| 156 | void enter(const char* msg, T& t)
|
|---|
| 157 | {
|
|---|
| 158 | std::cout << msg;
|
|---|
| 159 | std::string str;
|
|---|
| 160 | std::getline(std::cin, str);
|
|---|
| 161 | std::istringstream iss(str);
|
|---|
| 162 | iss >> t;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | void enter(const char* msg, std::string& str)
|
|---|
| 166 | {
|
|---|
| 167 | std::cout << msg;
|
|---|
| 168 | std::getline(std::cin, str);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | void enter(const char* msg, shared_string& str)
|
|---|
| 172 | {
|
|---|
| 173 | std::cout << msg;
|
|---|
| 174 | std::string stdstr;
|
|---|
| 175 | std::getline(std::cin, stdstr);
|
|---|
| 176 | str = stdstr.c_str();
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | int _tmain(int argc, _TCHAR* argv[])
|
|---|
| 180 | {
|
|---|
| 181 | /* Create (or open) the memory mapped file where the book container
|
|---|
| 182 | * is stored, along with a mutex for synchronized access.
|
|---|
| 183 | */
|
|---|
| 184 |
|
|---|
| 185 | bip::permissions perm;
|
|---|
| 186 |
|
|---|
| 187 | perm.set_unrestricted();
|
|---|
| 188 |
|
|---|
| 189 | ManagedSharedMemory *pseg=
|
|---|
| 190 | new ManagedSharedMemory
|
|---|
| 191 | (
|
|---|
| 192 | bip::open_or_create,
|
|---|
| 193 | "./book_container.db",
|
|---|
| 194 | 65536,
|
|---|
| 195 | 0,
|
|---|
| 196 | perm
|
|---|
| 197 | );
|
|---|
| 198 |
|
|---|
| 199 | ManagedSharedMemory& seg(*pseg);
|
|---|
| 200 |
|
|---|
| 201 | bip::named_mutex mutex(
|
|---|
| 202 | bip::open_or_create, "7FD6D7E8-320B-11DC-82CF-F0B655D89593");
|
|---|
| 203 |
|
|---|
| 204 | /*
|
|---|
| 205 | // create or open the book container in shared memory
|
|---|
| 206 |
|
|---|
| 207 | book_container* pbc = seg.find_or_construct<book_container>("book container")(
|
|---|
| 208 | book_container::ctor_args_list(),
|
|---|
| 209 | book_container::allocator_type(seg.get_segment_manager()));
|
|---|
| 210 | */
|
|---|
| 211 |
|
|---|
| 212 | VoidAllocator *va = new VoidAllocator(seg.get_segment_manager());
|
|---|
| 213 |
|
|---|
| 214 | BookRepository rep(*va);
|
|---|
| 215 |
|
|---|
| 216 | book_container* pbc=&rep.m_BookContainer;
|
|---|
| 217 |
|
|---|
| 218 | std::string command_info =
|
|---|
| 219 | "1. list books by author\n"
|
|---|
| 220 | "2. list all books by prize\n"
|
|---|
| 221 | "3. insert a book\n"
|
|---|
| 222 | "4. delete a book\n"
|
|---|
| 223 | "0. exit\n";
|
|---|
| 224 |
|
|---|
| 225 | std::cout << command_info;
|
|---|
| 226 |
|
|---|
| 227 | /* main loop */
|
|---|
| 228 |
|
|---|
| 229 | for (bool exit = false; !exit;){
|
|---|
| 230 | int command = -1;
|
|---|
| 231 | enter("command: ", command);
|
|---|
| 232 |
|
|---|
| 233 | switch (command){
|
|---|
| 234 | case 0:{ /* exit */
|
|---|
| 235 | exit = true;
|
|---|
| 236 | break;
|
|---|
| 237 | }
|
|---|
| 238 | case 1:{ /* list books by author */
|
|---|
| 239 |
|
|---|
| 240 | break;
|
|---|
| 241 | }
|
|---|
| 242 | case 2:{ /* list all books by prize */
|
|---|
| 243 | break;
|
|---|
| 244 | }
|
|---|
| 245 | case 3:{ /* insert a book */
|
|---|
| 246 | break;
|
|---|
| 247 | }
|
|---|
| 248 | default:{
|
|---|
| 249 | std::cout << "select one option:\n" << command_info;
|
|---|
| 250 | break;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | return 0;
|
|---|
| 256 | }
|
|---|