| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
 | 
					
						
							|  |  |  | #define BOOST_SCOPED_PTR_HPP_INCLUDED
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
 | 
					
						
							|  |  |  | //  Copyright (c) 2001, 2002 Peter Dimov
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2004-08-19 15:23:47 +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-01-22 13:38:52 +00:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 |  |  | //  http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
 | 
					
						
							| 
									
										
										
										
											2002-02-04 11:15:40 +00:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/assert.hpp>
 | 
					
						
							| 
									
										
										
										
											2002-02-16 13:23:01 +00:00
										 |  |  | #include <boost/checked_delete.hpp>
 | 
					
						
							| 
									
										
										
										
											2003-05-16 12:11:17 +00:00
										 |  |  | #include <boost/detail/workaround.hpp>
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  | #ifndef BOOST_NO_AUTO_PTR
 | 
					
						
							|  |  |  | # include <memory>          // for std::auto_ptr
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | namespace boost | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 |  |  | // Debug hooks
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-10 12:54:43 +00:00
										 |  |  | #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
 | 
					
						
							| 
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void sp_scalar_constructor_hook(void * p); | 
					
						
							|  |  |  | void sp_scalar_destructor_hook(void * p); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | //  scoped_ptr mimics a built-in pointer except that it guarantees deletion
 | 
					
						
							|  |  |  | //  of the object pointed to, either on destruction of the scoped_ptr or via
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  | //  an explicit reset(). scoped_ptr is a simple solution for simple needs;
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | //  use shared_ptr or std::auto_ptr if your needs are more complex.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> class scoped_ptr // noncopyable
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     T * ptr; | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     scoped_ptr(scoped_ptr const &); | 
					
						
							|  |  |  |     scoped_ptr & operator=(scoped_ptr const &); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     typedef scoped_ptr<T> this_type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typedef T element_type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     explicit scoped_ptr(T * p = 0): ptr(p) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2003-02-10 12:54:43 +00:00
										 |  |  | #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
 | 
					
						
							| 
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 |  |  |         boost::sp_scalar_constructor_hook(ptr); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  | #ifndef BOOST_NO_AUTO_PTR
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     explicit scoped_ptr(std::auto_ptr<T> p): ptr(p.release()) // never throws
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2003-02-10 12:54:43 +00:00
										 |  |  | #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
 | 
					
						
							| 
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 |  |  |         boost::sp_scalar_constructor_hook(ptr); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     ~scoped_ptr() // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2003-02-10 12:54:43 +00:00
										 |  |  | #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
 | 
					
						
							| 
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 |  |  |         boost::sp_scalar_destructor_hook(ptr); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         boost::checked_delete(ptr); | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     void reset(T * p = 0) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-11-21 13:10:18 +00:00
										 |  |  |         BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors
 | 
					
						
							|  |  |  |         this_type(p).swap(*this); | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     T & operator*() const // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         BOOST_ASSERT(ptr != 0); | 
					
						
							|  |  |  |         return *ptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     T * operator->() const // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         BOOST_ASSERT(ptr != 0); | 
					
						
							|  |  |  |         return ptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     T * get() const // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         return ptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     // implicit conversion to "bool"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-12 17:09:24 +00:00
										 |  |  | #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
 | 
					
						
							| 
									
										
										
										
											2003-05-16 12:11:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     operator bool () const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return ptr != 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-10-02 17:49:06 +00:00
										 |  |  | #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
 | 
					
						
							|  |  |  |     typedef T * (this_type::*unspecified_bool_type)() const; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     operator unspecified_bool_type() const // never throws
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return ptr == 0? 0: &this_type::get; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2003-05-16 12:11:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-10-02 17:49:06 +00:00
										 |  |  | #else 
 | 
					
						
							| 
									
										
										
										
											2003-10-01 11:12:15 +00:00
										 |  |  |     typedef T * this_type::*unspecified_bool_type; | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     operator unspecified_bool_type() const // never throws
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2003-10-01 11:12:15 +00:00
										 |  |  |         return ptr == 0? 0: &this_type::ptr; | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-16 12:11:17 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     bool operator! () const // never throws
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-26 14:18:21 +00:00
										 |  |  |         return ptr == 0; | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     void swap(scoped_ptr & b) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |         T * tmp = b.ptr; | 
					
						
							|  |  |  |         b.ptr = ptr; | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |         ptr = tmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     a.swap(b); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  | // get_pointer(p) is a generic way to say p.get()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> inline T * get_pointer(scoped_ptr<T> const & p) | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     return p.get(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | } // namespace boost
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  | #endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
 |