blob: 4d53afa960e1f0ce2652ca8108e5fa1ba2279095 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001#ifndef BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
2#define BOOST_SMART_PTR_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// 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)
13//
14// See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
15//
16
17#include <ndnboost/assert.hpp>
18#include <ndnboost/checked_delete.hpp>
19#include <ndnboost/throw_exception.hpp>
20#include <ndnboost/smart_ptr/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#include <new> // for std::bad_alloc
26
27namespace ndnboost
28{
29
30template<class T> class shared_array
31{
32private:
33
34 typedef detail::atomic_count count_type;
35
36public:
37
38 typedef T element_type;
39
40 explicit shared_array(T * p = 0): px(p)
41 {
42#ifndef BOOST_NO_EXCEPTIONS
43
44 try // prevent leak if new throws
45 {
46 pn = new count_type(1);
47 }
48 catch(...)
49 {
50 ndnboost::checked_array_delete(p);
51 throw;
52 }
53
54#else
55
56 pn = new count_type(1);
57
58 if(pn == 0)
59 {
60 ndnboost::checked_array_delete(p);
61 ndnboost::throw_exception(std::bad_alloc());
62 }
63
64#endif
65 }
66
67 ~shared_array()
68 {
69 if(--*pn == 0)
70 {
71 ndnboost::checked_array_delete(px);
72 delete pn;
73 }
74 }
75
76 shared_array(shared_array const & r) : px(r.px) // never throws
77 {
78 pn = r.pn;
79 ++*pn;
80 }
81
82 shared_array & operator=(shared_array const & r)
83 {
84 shared_array(r).swap(*this);
85 return *this;
86 }
87
88 void reset(T * p = 0)
89 {
90 BOOST_ASSERT(p == 0 || p != px);
91 shared_array(p).swap(*this);
92 }
93
94 T * get() const // never throws
95 {
96 return px;
97 }
98
99 T & operator[](std::ptrdiff_t i) const // never throws
100 {
101 BOOST_ASSERT(px != 0);
102 BOOST_ASSERT(i >= 0);
103 return px[i];
104 }
105
106 long use_count() const // never throws
107 {
108 return *pn;
109 }
110
111 bool unique() const // never throws
112 {
113 return *pn == 1;
114 }
115
116 void swap(shared_array<T> & other) // never throws
117 {
118 std::swap(px, other.px);
119 std::swap(pn, other.pn);
120 }
121
122private:
123
124 T * px; // contained pointer
125 count_type * pn; // ptr to reference counter
126
127}; // shared_array
128
129template<class T, class U> inline bool operator==(shared_array<T> const & a, shared_array<U> const & b)
130{
131 return a.get() == b.get();
132}
133
134template<class T, class U> inline bool operator!=(shared_array<T> const & a, shared_array<U> const & b)
135{
136 return a.get() != b.get();
137}
138
139template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b)
140{
141 return std::less<T*>()(a.get(), b.get());
142}
143
144template<class T> void swap(shared_array<T> & a, shared_array<T> & b)
145{
146 a.swap(b);
147}
148
149} // namespace ndnboost
150
151#endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED