doc_scoped_ptr.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <boost/interprocess/detail/workaround.hpp>
  12. //[doc_scoped_ptr
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <boost/interprocess/smart_ptr/scoped_ptr.hpp>
  15. //<-
  16. #include "../test/get_process_id_name.hpp"
  17. //->
  18. using namespace boost::interprocess;
  19. class my_class
  20. {};
  21. class my_exception
  22. {};
  23. //A functor that destroys the shared memory object
  24. template<class T>
  25. class my_deleter
  26. {
  27. private:
  28. //A typedef to save typing
  29. typedef managed_shared_memory::segment_manager segment_manager;
  30. //This my_deleter is created in the stack, not in shared memory,
  31. //so we can use raw pointers
  32. segment_manager *mp_segment_manager;
  33. public:
  34. //This typedef will specify the pointer type that
  35. //scoped_ptr will store
  36. typedef T *pointer;
  37. //Constructor
  38. my_deleter(segment_manager *s_mngr)
  39. : mp_segment_manager(s_mngr){}
  40. void operator()(pointer object_to_delete)
  41. { mp_segment_manager->destroy_ptr(object_to_delete); }
  42. };
  43. int main ()
  44. {
  45. //Create shared memory
  46. //Remove shared memory on construction and destruction
  47. struct shm_remove
  48. {
  49. //<-
  50. #if 1
  51. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  52. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  53. #else
  54. //->
  55. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  56. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  57. //<-
  58. #endif
  59. //->
  60. } remover;
  61. //<-
  62. (void)remover;
  63. //->
  64. //<-
  65. #if 1
  66. managed_shared_memory shmem(create_only, test::get_process_id_name(), 10000);
  67. #else
  68. //->
  69. managed_shared_memory shmem(create_only, "MySharedMemory", 10000);
  70. //<-
  71. #endif
  72. //->
  73. //In the first try, there will be no exceptions
  74. //in the second try we will throw an exception
  75. for(int i = 0; i < 2; ++i){
  76. //Create an object in shared memory
  77. my_class * my_object = shmem.construct<my_class>("my_object")();
  78. my_class * my_object2 = shmem.construct<my_class>(anonymous_instance)();
  79. shmem.destroy_ptr(my_object2);
  80. //Since the next shared memory allocation can throw
  81. //assign it to a scoped_ptr so that if an exception occurs
  82. //we destroy the object automatically
  83. my_deleter<my_class> d(shmem.get_segment_manager());
  84. try{
  85. scoped_ptr<my_class, my_deleter<my_class> > s_ptr(my_object, d);
  86. //Let's emulate a exception capable operation
  87. //In the second try, throw an exception
  88. if(i == 1){
  89. throw(my_exception());
  90. }
  91. //If we have passed the dangerous zone
  92. //we can release the scoped pointer
  93. //to avoid destruction
  94. s_ptr.release();
  95. }
  96. catch(const my_exception &){}
  97. //Here, scoped_ptr is destroyed
  98. //so it we haven't thrown an exception
  99. //the object should be there, otherwise, destroyed
  100. if(i == 0){
  101. //Make sure the object is alive
  102. if(!shmem.find<my_class>("my_object").first){
  103. return 1;
  104. }
  105. //Now we can use it and delete it manually
  106. shmem.destroy<my_class>("my_object");
  107. }
  108. else{
  109. //Make sure the object has been deleted
  110. if(shmem.find<my_class>("my_object").first){
  111. return 1;
  112. }
  113. }
  114. }
  115. return 0;
  116. }
  117. //]
  118. #include <boost/interprocess/detail/config_end.hpp>