Files
interprocess/example/doc_multi_index.cpp
Ion Gaztañaga 3af7cdba54 Intrusive:
*  Added `linear<>` and `cache_last<>` options to singly linked lists.
*  Added `optimize_multikey<>` option to unordered container hooks.
*  Optimized unordered containers when `store_hash` option is used in the hook.
*  Implementation changed to be exception agnostic so that it can be used
   in environments without exceptions.
*  Added `container_from_iterator` function to tree-based containers.

Interprocess:

*  Added anonymous shared memory for UNIX systems.
*  Fixed file lock compilation errors

[SVN r44819]
2008-04-27 15:03:06 +00:00

92 lines
2.9 KiB
C++

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2007. 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/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
//[doc_multi_index
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
using namespace boost::interprocess;
namespace bmi = boost::multi_index;
typedef managed_shared_memory::allocator<char>::type char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>shm_string;
//Data to insert in shared memory
struct employee
{
int id;
int age;
shm_string name;
employee( int id_
, int age_
, const char *name_
, const char_allocator &a)
: id(id_), age(age_), name(name_, a)
{}
};
//Tags
struct id{};
struct age{};
struct name{};
// Define a multi_index_container of employees with following indices:
// - a unique index sorted by employee::int,
// - a non-unique index sorted by employee::name,
// - a non-unique index sorted by employee::age.
typedef bmi::multi_index_container<
employee,
bmi::indexed_by<
bmi::ordered_unique
<bmi::tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
bmi::ordered_non_unique<
bmi::tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,shm_string,name)>,
bmi::ordered_non_unique
<bmi::tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >,
managed_shared_memory::allocator<employee>::type
> employee_set;
int main ()
{
//Erase previous shared memory with the name
shared_memory_object::remove("MySharedMemory");
try{
//Create shared memory
managed_shared_memory segment(create_only,"MySharedMemory", 65536);
//Construct the multi_index in shared memory
employee_set *es = segment.construct<employee_set>
("My MultiIndex Container") //Container's name in shared memory
( employee_set::ctor_args_list()
, segment.get_allocator<employee>()); //Ctor parameters
//Now insert elements
char_allocator ca(segment.get_allocator<char>());
es->insert(employee(0,31, "Joe", ca));
es->insert(employee(1,27, "Robert", ca));
es->insert(employee(2,40, "John", ca));
}
catch(...){
shared_memory_object::remove("MySharedMemory");
throw;
}
shared_memory_object::remove("MySharedMemory");
return 0;
}
//]
#include <boost/interprocess/detail/config_end.hpp>