1 #ifndef BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
\r
2 #define BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
\r
5 // detail/shared_array_nmt.hpp - shared_array.hpp without member templates
\r
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
\r
8 // Copyright (c) 2001, 2002 Peter Dimov
\r
10 // Distributed under the Boost Software License, Version 1.0. (See
\r
11 // accompanying file LICENSE_1_0.txt or copy at
\r
12 // http://www.boost.org/LICENSE_1_0.txt)
\r
14 // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
\r
17 #include <boost/assert.hpp>
\r
18 #include <boost/checked_delete.hpp>
\r
19 #include <boost/throw_exception.hpp>
\r
20 #include <boost/smart_ptr/detail/atomic_count.hpp>
\r
22 #include <cstddef> // for std::ptrdiff_t
\r
23 #include <algorithm> // for std::swap
\r
24 #include <functional> // for std::less
\r
25 #include <new> // for std::bad_alloc
\r
30 template<class T> class shared_array
\r
34 typedef detail::atomic_count count_type;
\r
38 typedef T element_type;
\r
40 explicit shared_array(T * p = 0): px(p)
\r
42 #ifndef BOOST_NO_EXCEPTIONS
\r
44 try // prevent leak if new throws
\r
46 pn = new count_type(1);
\r
50 boost::checked_array_delete(p);
\r
56 pn = new count_type(1);
\r
60 boost::checked_array_delete(p);
\r
61 boost::throw_exception(std::bad_alloc());
\r
71 boost::checked_array_delete(px);
\r
76 shared_array(shared_array const & r) : px(r.px) // never throws
\r
82 shared_array & operator=(shared_array const & r)
\r
84 shared_array(r).swap(*this);
\r
88 void reset(T * p = 0)
\r
90 BOOST_ASSERT(p == 0 || p != px);
\r
91 shared_array(p).swap(*this);
\r
94 T * get() const // never throws
\r
99 T & operator[](std::ptrdiff_t i) const // never throws
\r
101 BOOST_ASSERT(px != 0);
\r
102 BOOST_ASSERT(i >= 0);
\r
106 long use_count() const // never throws
\r
111 bool unique() const // never throws
\r
116 void swap(shared_array<T> & other) // never throws
\r
118 std::swap(px, other.px);
\r
119 std::swap(pn, other.pn);
\r
124 T * px; // contained pointer
\r
125 count_type * pn; // ptr to reference counter
\r
129 template<class T, class U> inline bool operator==(shared_array<T> const & a, shared_array<U> const & b)
\r
131 return a.get() == b.get();
\r
134 template<class T, class U> inline bool operator!=(shared_array<T> const & a, shared_array<U> const & b)
\r
136 return a.get() != b.get();
\r
139 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b)
\r
141 return std::less<T*>()(a.get(), b.get());
\r
144 template<class T> void swap(shared_array<T> & a, shared_array<T> & b)
\r
149 } // namespace boost
\r
151 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
\r