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