deconstruct_ptr.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // DEPRECATED in favor of adl_postconstruct and adl_predestruct with
  2. // deconstruct<T>().
  3. // A factory function for creating a shared_ptr that enhances the plain
  4. // shared_ptr constructors by adding support for postconstructors
  5. // and predestructors through the boost::signals2::postconstructible and
  6. // boost::signals2::predestructible base classes.
  7. //
  8. // Copyright Frank Mori Hess 2007-2008.
  9. //
  10. // Use, modification and
  11. // distribution is subject to the Boost Software License, Version
  12. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
  15. #define BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
  16. #include <boost/assert.hpp>
  17. #include <boost/core/checked_delete.hpp>
  18. #include <boost/core/no_exceptions_support.hpp>
  19. #include <boost/signals2/postconstructible.hpp>
  20. #include <boost/signals2/predestructible.hpp>
  21. #include <boost/shared_ptr.hpp>
  22. namespace boost
  23. {
  24. namespace signals2
  25. {
  26. namespace detail
  27. {
  28. inline void do_postconstruct(const postconstructible *ptr)
  29. {
  30. postconstructible *nonconst_ptr = const_cast<postconstructible*>(ptr);
  31. nonconst_ptr->postconstruct();
  32. }
  33. inline void do_postconstruct(...)
  34. {
  35. }
  36. inline void do_predestruct(...)
  37. {
  38. }
  39. inline void do_predestruct(const predestructible *ptr)
  40. {
  41. BOOST_TRY
  42. {
  43. predestructible *nonconst_ptr = const_cast<predestructible*>(ptr);
  44. nonconst_ptr->predestruct();
  45. }
  46. BOOST_CATCH(...)
  47. {
  48. BOOST_ASSERT(false);
  49. }
  50. BOOST_CATCH_END
  51. }
  52. }
  53. template<typename T> class predestructing_deleter
  54. {
  55. public:
  56. void operator()(const T *ptr) const
  57. {
  58. detail::do_predestruct(ptr);
  59. checked_delete(ptr);
  60. }
  61. };
  62. template<typename T>
  63. shared_ptr<T> deconstruct_ptr(T *ptr)
  64. {
  65. if(ptr == 0) return shared_ptr<T>(ptr);
  66. shared_ptr<T> shared(ptr, boost::signals2::predestructing_deleter<T>());
  67. detail::do_postconstruct(ptr);
  68. return shared;
  69. }
  70. template<typename T, typename D>
  71. shared_ptr<T> deconstruct_ptr(T *ptr, D deleter)
  72. {
  73. shared_ptr<T> shared(ptr, deleter);
  74. if(ptr == 0) return shared;
  75. detail::do_postconstruct(ptr);
  76. return shared;
  77. }
  78. }
  79. }
  80. #endif // BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP