1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost 4 // Software License, Version 1.0. (See accompanying file 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // See http://www.boost.org/libs/interprocess for documentation. 8 // 9 ////////////////////////////////////////////////////////////////////////////// 10 #include <boost/interprocess/detail/config_begin.hpp> 11 //[doc_managed_construction_info 12 #include <boost/interprocess/managed_shared_memory.hpp> 13 #include <cassert> 14 #include <cstring> 15 //<- 16 #include "../test/get_process_id_name.hpp" 17 //-> 18 19 class my_class 20 { 21 //... 22 }; 23 main()24int main() 25 { 26 using namespace boost::interprocess; 27 28 //Remove shared memory on construction and destruction 29 struct shm_remove 30 { 31 //<- 32 #if 1 33 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } 34 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } 35 #else 36 //-> 37 shm_remove() { shared_memory_object::remove("MySharedMemory"); } 38 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } 39 //<- 40 #endif 41 //-> 42 } remover; 43 //<- 44 (void)remover; 45 //-> 46 47 //<- 48 #if 1 49 managed_shared_memory managed_shm(create_only, test::get_process_id_name(), 10000*sizeof(std::size_t)); 50 #else 51 //-> 52 managed_shared_memory managed_shm(create_only, "MySharedMemory", 10000*sizeof(std::size_t)); 53 //<- 54 #endif 55 //-> 56 57 //Construct objects 58 my_class *named_object = managed_shm.construct<my_class>("Object name")[1](); 59 my_class *unique_object = managed_shm.construct<my_class>(unique_instance)[2](); 60 my_class *anon_object = managed_shm.construct<my_class>(anonymous_instance)[3](); 61 62 //Now test "get_instance_name" function. 63 assert(0 == std::strcmp(managed_shared_memory::get_instance_name(named_object), "Object name")); 64 assert(0 == std::strcmp(managed_shared_memory::get_instance_name(unique_object), typeid(my_class).name())); 65 assert(0 == managed_shared_memory::get_instance_name(anon_object)); 66 67 //Now test "get_instance_type" function. 68 assert(named_type == managed_shared_memory::get_instance_type(named_object)); 69 assert(unique_type == managed_shared_memory::get_instance_type(unique_object)); 70 assert(anonymous_type == managed_shared_memory::get_instance_type(anon_object)); 71 72 //Now test "get_instance_length" function. 73 assert(1 == managed_shared_memory::get_instance_length(named_object)); 74 assert(2 == managed_shared_memory::get_instance_length(unique_object)); 75 assert(3 == managed_shared_memory::get_instance_length(anon_object)); 76 77 managed_shm.destroy_ptr(named_object); 78 managed_shm.destroy_ptr(unique_object); 79 managed_shm.destroy_ptr(anon_object); 80 return 0; 81 } 82 //] 83 #include <boost/interprocess/detail/config_end.hpp> 84