Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjhjstz/7b501d4b2327a01ec24c8c977b32dd10 to your computer and use it in GitHub Desktop.
Save yjhjstz/7b501d4b2327a01ec24c8c977b32dd10 to your computer and use it in GitHub Desktop.
boost::interprocess::managed_mapped_file + boost::multi_index::multi_index_containe
#include <iostream>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#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>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
typedef boost::interprocess::managed_shared_memory::allocator<char>::type char_allocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> shm_string;
struct employee
{
employee(int id_, int age_, const char *name_, const char_allocator &a)
: id(id_), age(age_), name(name_, a) {}
//private:
int id;
int age;
shm_string name;
};
struct raidx {};
struct sidx {};
struct id {};
struct age {};
struct name {};
typedef boost::multi_index::multi_index_container<
employee,
boost::multi_index::indexed_by<
boost::multi_index::random_access<boost::multi_index::tag<raidx>>,
boost::multi_index::sequenced<boost::multi_index::tag<sidx>>,
boost::multi_index::ordered_unique<
boost::multi_index::tag<id>, BOOST_MULTI_INDEX_MEMBER(employee, int, id)
>,
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<name>, BOOST_MULTI_INDEX_MEMBER(employee, shm_string, name)
>,
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<age>, BOOST_MULTI_INDEX_MEMBER(employee, int, age)
>
>,
boost::interprocess::managed_mapped_file::allocator<employee>::type
> employee_set;
int main(int, char**)
{
/*struct shm_remove
{
shm_remove() { boost::interprocess::shared_memory_object::remove("shm_test"); }
~shm_remove() { boost::interprocess::shared_memory_object::remove("shm_test"); }
} remover;*/
//boost::uuids::uuid random_uuid = boost::uuids::random_generator()();
//boost::uuids::uuid random_uuid = boost::uuids::nil_generator()();
//boost::uuids::uuid uuid = boost::uuids::name_generator(random_uuid)("test");
//std::string uuid_s = boost::uuids::to_string(uuid);
//std::cout << uuid << std::endl;
{
//boost::interprocess::managed_mapped_file segment(
// boost::interprocess::open_or_create, uuid_s.c_str(), 65536);
//boost::interprocess::managed_shared_memory segment(
// boost::interprocess::open_or_create, uuid_s.c_str(), 65536);
//segment.get_allocator<char>();
boost::interprocess::managed_mapped_file segment(
boost::interprocess::open_or_create, "employee_set_file", 65536);
char_allocator ca(segment.get_allocator<char>());
employee_set* e_set = segment.find_or_construct<employee_set>("employee_set")(
employee_set::ctor_args_list(), segment.get_allocator<employee>());
//std::cout << ((e_set->get<raidx>()).back().id()) << std::endl;
if (e_set->get<raidx>().size() > 0)
{
std::cout << e_set->get<raidx>().back().id << std::endl;
}
if (e_set->get<id>().size() > 1)
{
//std::cout << std::next(e_set->get<id>().begin())->id << std::endl;
std::cout << e_set->get<id>().begin()->id << std::endl;
std::cout << e_set->get<id>().rbegin()->id << std::endl;
}
auto e = e_set->get<id>().find(10);
if (e != e_set->get<id>().end())
{
std::cout << "found" << std::endl;
std::cout << e->name << std::endl;
}
else
{
std::cout << "not found" << std::endl;
e_set->get<sidx>().emplace_back(20, 75, "Test Person 4", ca);
e_set->push_back(employee(15, 50, "Test Person 1", ca));
e_set->push_back(employee(10, 25, "Test Person 2", ca));
e_set->push_back(employee( 5, 10, "Test Person 3", ca));
}
/*for (const auto& n :
boost::make_iterator_range(segment.named_begin(), segment.named_end()))
{
std::cout << n.name() << std::endl;
}
int* p = segment.find_or_construct<int>("int_wert")(0);
std::cout << ++(*p) << std::endl;
std::cout << segment.get_size() << std::endl;*/
}
//std::cout << boost::interprocess::managed_mapped_file::shrink_to_fit(uuid_s.c_str()) << std::endl;
//std::cout << segment.get_size() << std::endl;
//std::pair<int*, std::size_t> p = segment.find<int>("int_wert");
/*if (p.first)
{
std::cout << "found int_wert" << std::endl;
std::cout << ++(*p.first) << std::endl;
}
else
{
std::cout << "constructed int_wert" << std::endl;
int* p_c = segment.construct<int>("int_wert")(10);
std::cout << *p_c << std::endl;
}*/
std::cin.get();
//char_allocator alloc;
//shm_string s1("", )
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment