enable_shared_from_this.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This file is the adaptation for Interprocess of boost/enable_shared_from_this.hpp
  4. //
  5. // (C) Copyright Peter Dimov 2002
  6. // (C) Copyright Ion Gaztanaga 2006-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. #ifndef BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
  14. #define BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
  15. #ifndef BOOST_CONFIG_HPP
  16. # include <boost/config.hpp>
  17. #endif
  18. #
  19. #if defined(BOOST_HAS_PRAGMA_ONCE)
  20. # pragma once
  21. #endif
  22. #include <boost/interprocess/detail/config_begin.hpp>
  23. #include <boost/interprocess/detail/workaround.hpp>
  24. #include <boost/assert.hpp>
  25. #include <boost/interprocess/smart_ptr/weak_ptr.hpp>
  26. #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
  27. //!\file
  28. //!Describes an utility to form a shared pointer from this
  29. namespace boost{
  30. namespace interprocess{
  31. //!This class is used as a base class that allows a shared_ptr to the current
  32. //!object to be obtained from within a member function.
  33. //!enable_shared_from_this defines two member functions called shared_from_this
  34. //!that return a shared_ptr<T> and shared_ptr<T const>, depending on constness, to this.
  35. template<class T, class A, class D>
  36. class enable_shared_from_this
  37. {
  38. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  39. protected:
  40. enable_shared_from_this()
  41. {}
  42. enable_shared_from_this(enable_shared_from_this const &)
  43. {}
  44. enable_shared_from_this & operator=(enable_shared_from_this const &)
  45. { return *this; }
  46. ~enable_shared_from_this()
  47. {}
  48. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  49. public:
  50. shared_ptr<T, A, D> shared_from_this()
  51. {
  52. shared_ptr<T, A, D> p(_internal_weak_this);
  53. BOOST_ASSERT(ipcdetail::to_raw_pointer(p.get()) == this);
  54. return p;
  55. }
  56. shared_ptr<T const, A, D> shared_from_this() const
  57. {
  58. shared_ptr<T const, A, D> p(_internal_weak_this);
  59. BOOST_ASSERT(ipcdetail::to_raw_pointer(p.get()) == this);
  60. return p;
  61. }
  62. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  63. typedef T element_type;
  64. mutable weak_ptr<element_type, A, D> _internal_weak_this;
  65. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  66. };
  67. } // namespace interprocess
  68. } // namespace boost
  69. #include <boost/interprocess/detail/config_end.hpp>
  70. #endif // #ifndef BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED