0ca3b0a390fb65e5e2c717be374a79fa6fcea810
[platform/upstream/boost.git] / boost / interprocess / smart_ptr / enable_shared_from_this.hpp
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
14 #ifndef BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
15 #define BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
16
17 #if defined(_MSC_VER)
18 #  pragma once
19 #endif
20
21 #include <boost/interprocess/detail/config_begin.hpp>
22 #include <boost/interprocess/detail/workaround.hpp>
23
24 #include <boost/assert.hpp>
25 #include <boost/interprocess/smart_ptr/weak_ptr.hpp>
26 #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
27
28 //!\file
29 //!Describes an utility to form a shared pointer from this
30
31 namespace boost{
32 namespace interprocess{
33
34 //!This class is used as a base class that allows a shared_ptr to the current
35 //!object to be obtained from within a member function.
36 //!enable_shared_from_this defines two member functions called shared_from_this
37 //!that return a shared_ptr<T> and shared_ptr<T const>, depending on constness, to this.
38 template<class T, class A, class D>
39 class enable_shared_from_this
40 {
41    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
42    protected:
43    enable_shared_from_this()
44    {}
45
46    enable_shared_from_this(enable_shared_from_this const &)
47    {}
48
49    enable_shared_from_this & operator=(enable_shared_from_this const &)
50    {  return *this;  }
51
52    ~enable_shared_from_this()
53    {}
54    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
55
56    public:
57    shared_ptr<T, A, D> shared_from_this()
58    {
59       shared_ptr<T, A, D> p(_internal_weak_this);
60       BOOST_ASSERT(ipcdetail::to_raw_pointer(p.get()) == this);
61       return p;
62    }
63
64    shared_ptr<T const, A, D> shared_from_this() const
65    {
66       shared_ptr<T const, A, D> p(_internal_weak_this);
67       BOOST_ASSERT(ipcdetail::to_raw_pointer(p.get()) == this);
68       return p;
69    }
70
71    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
72    typedef T element_type;
73    mutable weak_ptr<element_type, A, D> _internal_weak_this;
74    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
75 };
76
77 } // namespace interprocess
78 } // namespace boost
79
80 #include <boost/interprocess/detail/config_end.hpp>
81
82 #endif  // #ifndef BOOST_INTERPROCESS_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
83