| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED
 | 
					
						
							|  |  |  | #define BOOST_SCOPED_ARRAY_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_array.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>
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | #include <boost/config.hpp>   // in case ptrdiff_t not in std
 | 
					
						
							| 
									
										
										
										
											2003-05-16 12:11:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <boost/detail/workaround.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | #include <cstddef>            // for std::ptrdiff_t
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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_array_constructor_hook(void * p); | 
					
						
							|  |  |  | void sp_array_destructor_hook(void * p); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | //  scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
 | 
					
						
							|  |  |  | //  is guaranteed, either on destruction of the scoped_array or via an explicit
 | 
					
						
							|  |  |  | //  reset(). Use shared_array or std::vector if your needs are more complex.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> class scoped_array // noncopyable
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-04 22:52:48 +00:00
										 |  |  |     T * ptr; | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     scoped_array(scoped_array const &); | 
					
						
							|  |  |  |     scoped_array & operator=(scoped_array const &); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 15:15:39 +00:00
										 |  |  |     typedef scoped_array<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_array(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_array_constructor_hook(ptr); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     ~scoped_array() // 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_array_destructor_hook(ptr); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         boost::checked_array_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-04 22:52:48 +00:00
										 |  |  |     T & operator[](std::ptrdiff_t i) const // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         BOOST_ASSERT(ptr != 0); | 
					
						
							|  |  |  |         BOOST_ASSERT(i >= 0); | 
					
						
							|  |  |  |         return ptr[i]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-04 22:52:48 +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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else 
 | 
					
						
							| 
									
										
										
										
											2003-05-16 12:11:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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_array & 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-02-02 18:36:12 +00:00
										 |  |  | template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     a.swap(b); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace boost
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif  // #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED
 |