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 // Permission to copy, use, modify, sell and distribute this software
11 // is granted provided this copyright notice appears in all copies.
12 // This software is provided "as is" without express or implied
13 // warranty, and with no claim as to its suitability for any purpose.
15 // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
18 #include <boost/assert.hpp>
19 #include <boost/checked_delete.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
29 template<class T> class shared_array
33 typedef detail::atomic_count count_type;
37 typedef T element_type;
39 explicit shared_array(T * p = 0): px(p)
41 try // prevent leak if new throws
43 pn = new count_type(1);
47 checked_array_delete(p);
56 checked_array_delete(px);
61 shared_array(shared_array const & r) : px(r.px) // never throws
67 shared_array & operator=(shared_array const & r)
69 shared_array(r).swap(*this);
75 BOOST_ASSERT(p == 0 || p != px);
76 shared_array(p).swap(*this);
79 T * get() const // never throws
84 T & operator[](std::ptrdiff_t i) const // never throws
86 BOOST_ASSERT(px != 0);
91 long use_count() const // never throws
96 bool unique() const // never throws
101 void swap(shared_array<T> & other) // never throws
103 std::swap(px, other.px);
104 std::swap(pn, other.pn);
109 T * px; // contained pointer
110 count_type * pn; // ptr to reference counter
114 template<class T, class U> inline bool operator==(shared_array<T> const & a, shared_array<U> const & b)
116 return a.get() == b.get();
119 template<class T, class U> inline bool operator!=(shared_array<T> const & a, shared_array<U> const & b)
121 return a.get() != b.get();
124 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b)
126 return std::less<T*>()(a.get(), b.get());
129 template<class T> void swap(shared_array<T> & a, shared_array<T> & b)
136 #endif // #ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED