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/throw_exception.hpp>
21 #include <boost/detail/atomic_count.hpp>
23 #include <cstddef> // for std::ptrdiff_t
24 #include <algorithm> // for std::swap
25 #include <functional> // for std::less
26 #include <new> // for std::bad_alloc
31 template<class T> class shared_array
35 typedef detail::atomic_count count_type;
39 typedef T element_type;
41 explicit shared_array(T * p = 0): px(p)
43 #ifndef BOOST_NO_EXCEPTIONS
45 try // prevent leak if new throws
47 pn = new count_type(1);
51 boost::checked_array_delete(p);
57 pn = new count_type(1);
61 boost::checked_array_delete(p);
62 boost::throw_exception(std::bad_alloc());
72 boost::checked_array_delete(px);
77 shared_array(shared_array const & r) : px(r.px) // never throws
83 shared_array & operator=(shared_array const & r)
85 shared_array(r).swap(*this);
91 BOOST_ASSERT(p == 0 || p != px);
92 shared_array(p).swap(*this);
95 T * get() const // never throws
100 T & operator[](std::ptrdiff_t i) const // never throws
102 BOOST_ASSERT(px != 0);
103 BOOST_ASSERT(i >= 0);
107 long use_count() const // never throws
112 bool unique() const // never throws
117 void swap(shared_array<T> & other) // never throws
119 std::swap(px, other.px);
120 std::swap(pn, other.pn);
125 T * px; // contained pointer
126 count_type * pn; // ptr to reference counter
130 template<class T, class U> inline bool operator==(shared_array<T> const & a, shared_array<U> const & b)
132 return a.get() == b.get();
135 template<class T, class U> inline bool operator!=(shared_array<T> const & a, shared_array<U> const & b)
137 return a.get() != b.get();
140 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b)
142 return std::less<T*>()(a.get(), b.get());
145 template<class T> void swap(shared_array<T> & a, shared_array<T> & b)
152 #endif // #ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED