]> git.lyx.org Git - lyx.git/blob - boost/boost/smart_ptr/enable_shared_from_this2.hpp
attempt to make LyX linkable without NLS, assuming dummy available() should return...
[lyx.git] / boost / boost / smart_ptr / enable_shared_from_this2.hpp
1 #ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
2 #define BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
3
4 //
5 //  enable_shared_from_this2.hpp
6 //
7 //  Copyright 2002, 2009 Peter Dimov
8 //  Copyright 2008 Frank Mori Hess
9 //
10 //  Distributed under the Boost Software License, Version 1.0.
11 //  See accompanying file LICENSE_1_0.txt or copy at
12 //  http://www.boost.org/LICENSE_1_0.txt
13 //
14
15 #include <boost/config.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/assert.hpp>
18 #include <boost/detail/workaround.hpp>
19
20 namespace boost
21 {
22
23 namespace detail
24 {
25
26 class esft2_deleter_wrapper
27 {
28 private:
29
30     shared_ptr<void> deleter_;
31
32 public:
33
34     esft2_deleter_wrapper()
35     {
36     }
37
38     template< class T > void set_deleter( shared_ptr<T> const & deleter )
39     {
40         deleter_ = deleter;
41     }
42
43     template< class T> void operator()( T* )
44     {
45         BOOST_ASSERT( deleter_.use_count() <= 1 );
46         deleter_.reset();
47     }
48 };
49
50 } // namespace detail
51
52 template< class T > class enable_shared_from_this2
53 {
54 protected:
55
56     enable_shared_from_this2()
57     {
58     }
59
60     enable_shared_from_this2( enable_shared_from_this2 const & )
61     {
62     }
63
64     enable_shared_from_this2 & operator=( enable_shared_from_this2 const & )
65     {
66         return *this;
67     }
68
69     ~enable_shared_from_this2()
70     {
71         BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
72     }
73
74 private:
75
76     mutable weak_ptr<T> weak_this_;
77     mutable shared_ptr<T> shared_this_;
78
79 public:
80
81     shared_ptr<T> shared_from_this()
82     {
83         init_weak_once();
84         return shared_ptr<T>( weak_this_ );
85     }
86
87     shared_ptr<T const> shared_from_this() const
88     {
89         init_weak_once();
90         return shared_ptr<T>( weak_this_ );
91     }
92
93 private:
94
95     void init_weak_once() const
96     {
97         if( weak_this_._empty() )
98         {
99             shared_this_.reset( static_cast< T* >( 0 ), detail::esft2_deleter_wrapper() );
100             weak_this_ = shared_this_;
101         }
102     }
103
104 public: // actually private, but avoids compiler template friendship issues
105
106     // Note: invoked automatically by shared_ptr; do not call
107     template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
108     {
109         BOOST_ASSERT( ppx != 0 );
110
111         if( weak_this_.use_count() == 0 )
112         {
113             weak_this_ = shared_ptr<T>( *ppx, py );
114         }
115         else if( shared_this_.use_count() != 0 )
116         {
117             BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
118
119             detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
120             BOOST_ASSERT( pd != 0 );
121
122             pd->set_deleter( *ppx );
123
124             ppx->reset( shared_this_, ppx->get() );
125             shared_this_.reset();
126         }
127     }
128 };
129
130 } // namespace boost
131
132 #endif  // #ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED