enable_shared_from_this_test.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2002, 2003 Peter Dimov
  3. //
  4. // This file is the adaptation of shared_from_this_test.cpp from smart_ptr library
  5. //
  6. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/interprocess for documentation.
  11. //
  12. //////////////////////////////////////////////////////////////////////////////
  13. #include <boost/interprocess/detail/config_begin.hpp>
  14. #include <boost/interprocess/detail/workaround.hpp>
  15. #include <boost/interprocess/smart_ptr/enable_shared_from_this.hpp>
  16. #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
  17. #include <boost/core/lightweight_test.hpp>
  18. #include <boost/interprocess/managed_shared_memory.hpp>
  19. #include "get_process_id_name.hpp"
  20. //
  21. using namespace boost::interprocess;
  22. typedef allocator<void, managed_shared_memory::segment_manager>
  23. v_allocator_t;
  24. struct X;
  25. typedef deleter<X, managed_shared_memory::segment_manager> x_deleter_t;
  26. struct X :
  27. public enable_shared_from_this<X, v_allocator_t, x_deleter_t>
  28. {
  29. };
  30. typedef shared_ptr<X, v_allocator_t, x_deleter_t> v_shared_ptr;
  31. template<class ManagedMemory>
  32. void test_enable_shared_this(ManagedMemory &managed_mem)
  33. {
  34. v_shared_ptr p(make_managed_shared_ptr
  35. (managed_mem.template construct<X>(anonymous_instance)(), managed_mem));
  36. v_shared_ptr q = p->shared_from_this();
  37. BOOST_TEST(p == q);
  38. BOOST_TEST(!(p < q) && !(q < p));
  39. X v2(*p);
  40. try
  41. {
  42. //This should throw bad_weak_ptr
  43. v_shared_ptr r = v2.shared_from_this();
  44. BOOST_ERROR("v2.shared_from_this() failed to throw");
  45. }
  46. catch(boost::interprocess::bad_weak_ptr const &)
  47. {
  48. //This is the expected path
  49. }
  50. catch(...){
  51. BOOST_ERROR("v2.shared_from_this() threw an unexpected exception");
  52. }
  53. try
  54. {
  55. //This should not throw bad_weak_ptr
  56. *p = X();
  57. v_shared_ptr r = p->shared_from_this();
  58. BOOST_TEST(p == r);
  59. BOOST_TEST(!(p < r) && !(r < p));
  60. }
  61. catch(boost::interprocess::bad_weak_ptr const &)
  62. {
  63. BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = X()");
  64. }
  65. catch(...)
  66. {
  67. BOOST_ERROR("p->shared_from_this() threw an unexpected exception after *p = X()");
  68. }
  69. }
  70. int main()
  71. {
  72. std::string process_name;
  73. test::get_process_id_name(process_name);
  74. shared_memory_object::remove(process_name.c_str());
  75. managed_shared_memory shmem(create_only, process_name.c_str(), 65536);
  76. test_enable_shared_this(shmem);
  77. shared_memory_object::remove(process_name.c_str());
  78. return boost::report_errors();
  79. }
  80. #include <boost/interprocess/detail/config_end.hpp>