| 
									
										
										
										
											2002-11-18 14:37:02 +00:00
										 |  |  | #ifndef BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
 | 
					
						
							|  |  |  | #define BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  enable_shared_from_this.hpp
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Copyright (c) 2002 Peter Dimov
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2004-07-26 00:32:12 +00:00
										 |  |  | // Distributed under the Boost Software License, Version 1.0. (See
 | 
					
						
							|  |  |  | // accompanying file LICENSE_1_0.txt or copy at
 | 
					
						
							|  |  |  | // http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							| 
									
										
										
										
											2002-11-18 14:37:02 +00:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/weak_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2002-11-19 16:18:18 +00:00
										 |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							|  |  |  | #include <boost/assert.hpp>
 | 
					
						
							|  |  |  | #include <boost/config.hpp>
 | 
					
						
							| 
									
										
										
										
											2002-11-18 14:37:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace boost | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<class T> class enable_shared_from_this | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  | // dynamic cast to template type doesn't work in constructor, so we have
 | 
					
						
							|  |  |  | // to use lazy initialization
 | 
					
						
							|  |  |  |     void init_internal_shared_once() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if(owned() == false && _internal_shared_this == 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             _internal_shared_this = shared_ptr<T>(dynamic_cast<T *>(const_cast<enable_shared_from_this*>(this)), | 
					
						
							|  |  |  |                 detail::sp_deleter_wrapper(), detail::ignore_enable_shared_from_this_tag()); | 
					
						
							|  |  |  |             BOOST_ASSERT(_internal_shared_this.get() == this); | 
					
						
							|  |  |  |             _internal_weak_this = _internal_shared_this; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool owned() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return _owned; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typedef T _internal_element_type; // for bcc 5.5.1
 | 
					
						
							|  |  |  |     mutable shared_ptr<_internal_element_type> _internal_shared_this; | 
					
						
							|  |  |  |     mutable weak_ptr<_internal_element_type> _internal_weak_this; | 
					
						
							|  |  |  |     mutable bool _owned; | 
					
						
							| 
									
										
										
										
											2008-03-20 22:10:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-28 12:27:55 +00:00
										 |  |  | protected: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  |     enable_shared_from_this(): | 
					
						
							|  |  |  |       _owned(false) | 
					
						
							| 
									
										
										
										
											2003-03-28 12:27:55 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 22:10:52 +00:00
										 |  |  |     enable_shared_from_this(enable_shared_from_this const &): | 
					
						
							|  |  |  |       _owned(false) | 
					
						
							| 
									
										
										
										
											2003-03-28 12:27:55 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enable_shared_from_this & operator=(enable_shared_from_this const &) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  | // virtual destructor because we need a vtable for dynamic_cast from base to derived to work
 | 
					
						
							|  |  |  |     virtual ~enable_shared_from_this() | 
					
						
							| 
									
										
										
										
											2003-03-28 12:27:55 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  | // make sure no dangling shared_ptr objects were created by the
 | 
					
						
							|  |  |  | // user calling shared_from_this() but never passing ownership of the object
 | 
					
						
							|  |  |  | // to a shared_ptr.
 | 
					
						
							|  |  |  |         BOOST_ASSERT(owned() || _internal_shared_this.use_count() <= 1); | 
					
						
							| 
									
										
										
										
											2003-03-28 12:27:55 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-18 14:37:02 +00:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     shared_ptr<T> shared_from_this() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  |         init_internal_shared_once(); | 
					
						
							| 
									
										
										
										
											2002-11-20 12:38:51 +00:00
										 |  |  |         shared_ptr<T> p(_internal_weak_this); | 
					
						
							| 
									
										
										
										
											2002-11-19 16:18:18 +00:00
										 |  |  |         BOOST_ASSERT(p.get() == this); | 
					
						
							|  |  |  |         return p; | 
					
						
							| 
									
										
										
										
											2002-11-18 14:37:02 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     shared_ptr<T const> shared_from_this() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  |         init_internal_shared_once(); | 
					
						
							| 
									
										
										
										
											2002-11-20 12:38:51 +00:00
										 |  |  |         shared_ptr<T const> p(_internal_weak_this); | 
					
						
							| 
									
										
										
										
											2002-11-19 16:18:18 +00:00
										 |  |  |         BOOST_ASSERT(p.get() == this); | 
					
						
							|  |  |  |         return p; | 
					
						
							| 
									
										
										
										
											2002-11-18 14:37:02 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  |     template<typename U> | 
					
						
							|  |  |  |     void _internal_accept_owner(shared_ptr<U> &owner) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         init_internal_shared_once(); | 
					
						
							| 
									
										
										
										
											2008-03-25 15:46:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if( !_owned ) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             detail::sp_deleter_wrapper * pd = get_deleter<detail::sp_deleter_wrapper>(_internal_shared_this); | 
					
						
							|  |  |  |             BOOST_ASSERT( pd != 0 ); | 
					
						
							|  |  |  |             pd->set_deleter(owner); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             owner.reset( _internal_shared_this, owner.get() ); | 
					
						
							|  |  |  |             _internal_shared_this.reset(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             _owned = true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-03-20 19:32:43 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2002-11-18 14:37:02 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace boost
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif  // #ifndef BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
 |