| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED
 | 
					
						
							|  |  |  | #define BOOST_SHARED_ARRAY_HPP_INCLUDED
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  shared_array.hpp
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  (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-02-04 11:15:40 +00:00
										 |  |  | //  See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/config.hpp>   // for broken compiler workarounds
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-24 15:36:25 +00:00
										 |  |  | #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | #include <boost/detail/shared_array_nmt.hpp>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-12 12:31:28 +00:00
										 |  |  | #include <memory>             // TR1 cyclic inclusion fix
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | #include <boost/assert.hpp>
 | 
					
						
							|  |  |  | #include <boost/checked_delete.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/detail/shared_count.hpp>
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | #include <algorithm>          // for std::swap
 | 
					
						
							|  |  |  | #include <functional>         // for std::less
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace boost | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  shared_array
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  shared_array extends shared_ptr to arrays.
 | 
					
						
							|  |  |  | //  The array pointed to is deleted when the last shared_array pointing to it
 | 
					
						
							|  |  |  | //  is destroyed or reset.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> class shared_array | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Borland 5.5.1 specific workarounds
 | 
					
						
							|  |  |  |     typedef checked_array_deleter<T> deleter; | 
					
						
							|  |  |  |     typedef shared_array<T> this_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     typedef T element_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     explicit shared_array(T * p = 0): px(p), pn(p, deleter()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // Requirements: D's copy constructor must not throw
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  |     // shared_array will release p by calling d(p)
 | 
					
						
							|  |  |  |     //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  |     template<class D> shared_array(T * p, D d): px(p), pn(p, d) | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  generated copy constructor, assignment, destructor are fine
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void reset(T * p = 0) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-01-24 19:16:12 +00:00
										 |  |  |         BOOST_ASSERT(p == 0 || p != px); | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |         this_type(p).swap(*this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  |     template <class D> void reset(T * p, D d) | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         this_type(p, d).swap(*this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     T & operator[] (std::ptrdiff_t i) const // never throws
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         BOOST_ASSERT(px != 0); | 
					
						
							|  |  |  |         BOOST_ASSERT(i >= 0); | 
					
						
							|  |  |  |         return px[i]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     T * get() const // never throws
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return px; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 px != 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 px == 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 px == 0? 0: &this_type::px; | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return px == 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     bool unique() const // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |         return pn.unique(); | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |     long use_count() const // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-02-02 18:36:12 +00:00
										 |  |  |         return pn.use_count(); | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void swap(shared_array<T> & other) // never throws
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         std::swap(px, other.px); | 
					
						
							|  |  |  |         pn.swap(other.pn); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     T * px;                     // contained pointer
 | 
					
						
							|  |  |  |     detail::shared_count pn;    // reference counter
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | };  // shared_array
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     return a.get() == b.get(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     return a.get() != b.get(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     return std::less<T*>()(a.get(), b.get()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-23 13:55:18 +00:00
										 |  |  | template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     a.swap(b); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace boost
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-24 15:36:25 +00:00
										 |  |  | #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
 | 
					
						
							| 
									
										
										
										
											2002-01-22 13:38:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif  // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED
 |