/* Boost.MultiIndex example of use of Boost.Interprocess allocators. * * Copyright 2003-2008 Joaquin M Lopez Munoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/libs/multi_index for library home page. */ #include "stdafx.h" using boost::multi_index_container; using namespace boost::multi_index; namespace bip = boost::interprocess; typedef bip::managed_windows_shared_memory ManagedSharedMemory; typedef ManagedSharedMemory::segment_manager SegmentManager; typedef bip::allocator CharAllocator; typedef bip::allocator VoidAllocator; /* shared_string is a string type placeable in shared memory, * courtesy of Boost.Interprocess. */ typedef bip::basic_string< char, std::char_traits, CharAllocator > shared_string; struct book { shared_string name; shared_string author; int id; int ownerid; int64_t brand; book(const shared_string::allocator_type& al) : name(al), author(al), id(), ownerid(), brand(0) {} friend std::ostream& operator<<(std::ostream& os, const book& b) { os << b.author << ": \"" << b.name << "\", I$" << b.id << ", O$" << b.ownerid << ", " << b.brand << " brand\n"; return os; } }; typedef bip::offset_ptr bookPtr; /* partial_str_less allows for partial searches taking into account * only the first n chars of the strings compared against. See * Tutorial: Basics: Special lookup operations for more info on this * type of comparison functors. */ /* partial_string is a mere string holder used to differentiate from * a plain string. */ struct partial_string { partial_string(const shared_string& str) :str(str){} shared_string str; }; struct partial_str_less { bool operator()(const shared_string& x, const shared_string& y)const { return x, hashed_unique < BOOST_MULTI_INDEX_MEMBER(book, shared_string, author) >, hashed_non_unique < BOOST_MULTI_INDEX_MEMBER(book, shared_string, name) >, ordered_unique < BOOST_MULTI_INDEX_MEMBER(book, int, id) >, ordered_non_unique < BOOST_MULTI_INDEX_MEMBER(book, int, ownerid) >, ordered_non_unique < BOOST_MULTI_INDEX_MEMBER(book, int64_t, brand) > >, ManagedSharedMemory::allocator::type > book_container; typedef book_container::nth_index<0>::type BOOK_BY_RANDOM_ACCESS_TYPE; class BookRepository { public: book_container m_BookContainer; BookRepository(const VoidAllocator& va) : m_BookContainer(va) { } BookRepository(const BookRepository& k) : m_BookContainer(k.m_BookContainer) { } }; /* A small utility to get data entered via std::cin */ template void enter(const char* msg, T& t) { std::cout << msg; std::string str; std::getline(std::cin, str); std::istringstream iss(str); iss >> t; } void enter(const char* msg, std::string& str) { std::cout << msg; std::getline(std::cin, str); } void enter(const char* msg, shared_string& str) { std::cout << msg; std::string stdstr; std::getline(std::cin, stdstr); str = stdstr.c_str(); } int _tmain(int argc, _TCHAR* argv[]) { /* Create (or open) the memory mapped file where the book container * is stored, along with a mutex for synchronized access. */ bip::permissions perm; perm.set_unrestricted(); ManagedSharedMemory *pseg= new ManagedSharedMemory ( bip::open_or_create, "./book_container.db", 65536, 0, perm ); ManagedSharedMemory& seg(*pseg); bip::named_mutex mutex( bip::open_or_create, "7FD6D7E8-320B-11DC-82CF-F0B655D89593"); /* // create or open the book container in shared memory book_container* pbc = seg.find_or_construct("book container")( book_container::ctor_args_list(), book_container::allocator_type(seg.get_segment_manager())); */ VoidAllocator *va = new VoidAllocator(seg.get_segment_manager()); BookRepository rep(*va); book_container* pbc=&rep.m_BookContainer; std::string command_info = "1. list books by author\n" "2. list all books by prize\n" "3. insert a book\n" "4. delete a book\n" "0. exit\n"; std::cout << command_info; /* main loop */ for (bool exit = false; !exit;){ int command = -1; enter("command: ", command); switch (command){ case 0:{ /* exit */ exit = true; break; } case 1:{ /* list books by author */ break; } case 2:{ /* list all books by prize */ break; } case 3:{ /* insert a book */ break; } default:{ std::cout << "select one option:\n" << command_info; break; } } } return 0; }