#if ANKERL_UNORDERED_DENSE_HAS_BOOST # if __clang__ # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wold-style-cast" # endif # include # include # include # include # include # include # include # include // Alias an STL-like allocator of ints that allocates ints from the segment using shmem_allocator = boost::interprocess::allocator, boost::interprocess::managed_shared_memory::segment_manager>; using shmem_vector = boost::interprocess::vector, shmem_allocator>; // Remove shared memory on construction and destruction struct shm_remove { shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); } ~shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); } shm_remove(shm_remove const&) = delete; shm_remove(shm_remove&&) = delete; auto operator=(shm_remove const&) -> shm_remove = delete; auto operator=(shm_remove&&) -> shm_remove = delete; }; TYPE_TO_STRING_MAP(int, std::string, ankerl::unordered_dense::hash, std::equal_to, shmem_vector); // See https://www.boost.org/doc/libs/1_80_0/doc/html/interprocess/allocators_containers.html TEST_CASE_TEMPLATE( "boost_container_vector", map_t, ankerl::unordered_dense::map, std::equal_to, shmem_vector>, ankerl::unordered_dense:: segmented_map, std::equal_to, shmem_allocator>) { auto remover = shm_remove(); // Create shared memory auto segment = boost::interprocess::managed_shared_memory(boost::interprocess::create_only, "MySharedMemory", 1024 * 1024); auto map = map_t{shmem_allocator{segment.get_segment_manager()}}; int total = 10000; for (int i = 0; i < total; ++i) { map.try_emplace(i, std::to_string(i)); } REQUIRE(map.size() == static_cast(total)); for (int i = 0; i < total; ++i) { auto it = map.find(i); REQUIRE(it != map.end()); REQUIRE(it->first == i); REQUIRE(it->second == std::to_string(i)); } map.erase(total + 123); REQUIRE(map.size() == static_cast(total)); map.erase(29); REQUIRE(map.size() == static_cast(total - 1)); map.emplace(std::pair(9999999, "hello")); REQUIRE(map.size() == static_cast(total)); } #endif // ANKERL_UNORDERED_DENSE_HAS_BOOST