]> git.lyx.org Git - lyx.git/blob - boost/boost/detail/shared_array_nmt.hpp
update
[lyx.git] / boost / boost / detail / shared_array_nmt.hpp
1 #ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
2 #define BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
3
4 //
5 //  detail/shared_array_nmt.hpp - shared_array.hpp without member templates
6 //
7 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 //  Copyright (c) 2001, 2002 Peter Dimov
9 //
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.
14 //
15 //  See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
16 //
17
18 #include <boost/assert.hpp>
19 #include <boost/checked_delete.hpp>
20 #include <boost/detail/atomic_count.hpp>
21
22 #include <cstddef>            // for std::ptrdiff_t
23 #include <algorithm>          // for std::swap
24 #include <functional>         // for std::less
25
26 namespace boost
27 {
28
29 template<class T> class shared_array
30 {
31 private:
32
33     typedef detail::atomic_count count_type;
34
35 public:
36
37     typedef T element_type;
38       
39     explicit shared_array(T * p = 0): px(p)
40     {
41         try  // prevent leak if new throws
42         {
43             pn = new count_type(1);
44         }
45         catch(...)
46         {
47             checked_array_delete(p);
48             throw;
49         } 
50     }
51       
52     ~shared_array()
53     {
54         if(--*pn == 0)
55         {
56             checked_array_delete(px);
57             delete pn;
58         }
59     }
60
61     shared_array(shared_array const & r) : px(r.px)  // never throws
62     {
63         pn = r.pn;
64         ++*pn;
65     }
66       
67     shared_array & operator=(shared_array const & r)
68     {
69         shared_array(r).swap(*this);
70         return *this;
71     }
72       
73     void reset(T * p = 0)
74     {
75         BOOST_ASSERT(p == 0 || p != px);
76         shared_array(p).swap(*this);
77     }
78       
79     T * get() const  // never throws
80     {
81         return px;
82     }
83
84     T & operator[](std::ptrdiff_t i) const  // never throws
85     {
86         BOOST_ASSERT(px != 0);
87         BOOST_ASSERT(i >= 0);
88         return px[i];
89     }
90       
91     long use_count() const  // never throws
92     {
93         return *pn;
94     }
95
96     bool unique() const  // never throws
97     {
98         return *pn == 1;
99     }
100     
101     void swap(shared_array<T> & other)  // never throws
102     {
103         std::swap(px, other.px);
104         std::swap(pn, other.pn);
105     }
106       
107 private:
108       
109     T * px;            // contained pointer
110     count_type * pn;   // ptr to reference counter
111       
112 };  // shared_array
113
114 template<class T, class U> inline bool operator==(shared_array<T> const & a, shared_array<U> const & b)
115 {
116     return a.get() == b.get();
117 }
118
119 template<class T, class U> inline bool operator!=(shared_array<T> const & a, shared_array<U> const & b)
120 {
121     return a.get() != b.get();
122 }
123
124 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b)
125 {
126     return std::less<T*>()(a.get(), b.get());
127 }
128
129 template<class T> void swap(shared_array<T> & a, shared_array<T> & b)
130 {
131     a.swap(b);
132 }
133
134 } // namespace boost
135
136 #endif  // #ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED