Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/boost/shared_array.hpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.9 KB
Line 
1#ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED
2#define BOOST_SHARED_ARRAY_HPP_INCLUDED
3
4//
5//  shared_array.hpp
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 <boost/config.hpp>   // for broken compiler workarounds
18
19#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20#include <boost/detail/shared_array_nmt.hpp>
21#else
22
23#include <boost/assert.hpp>
24#include <boost/checked_delete.hpp>
25
26#include <boost/detail/shared_count.hpp>
27#include <boost/detail/workaround.hpp>
28
29#include <cstddef>            // for std::ptrdiff_t
30#include <algorithm>          // for std::swap
31#include <functional>         // for std::less
32
33namespace boost
34{
35
36//
37//  shared_array
38//
39//  shared_array extends shared_ptr to arrays.
40//  The array pointed to is deleted when the last shared_array pointing to it
41//  is destroyed or reset.
42//
43
44template<class T> class shared_array
45{
46private:
47
48    // Borland 5.5.1 specific workarounds
49    typedef checked_array_deleter<T> deleter;
50    typedef shared_array<T> this_type;
51
52public:
53
54    typedef T element_type;
55
56    explicit shared_array(T * p = 0): px(p), pn(p, deleter())
57    {
58    }
59
60    //
61    // Requirements: D's copy constructor must not throw
62    //
63    // shared_array will release p by calling d(p)
64    //
65
66    template<class D> shared_array(T * p, D d): px(p), pn(p, d)
67    {
68    }
69
70//  generated copy constructor, assignment, destructor are fine
71
72    void reset(T * p = 0)
73    {
74        BOOST_ASSERT(p == 0 || p != px);
75        this_type(p).swap(*this);
76    }
77
78    template <class D> void reset(T * p, D d)
79    {
80        this_type(p, d).swap(*this);
81    }
82
83    T & operator[] (std::ptrdiff_t i) const // never throws
84    {
85        BOOST_ASSERT(px != 0);
86        BOOST_ASSERT(i >= 0);
87        return px[i];
88    }
89   
90    T * get() const // never throws
91    {
92        return px;
93    }
94
95    // implicit conversion to "bool"
96
97#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
98
99    operator bool () const
100    {
101        return px != 0;
102    }
103
104#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
105    typedef T * (this_type::*unspecified_bool_type)() const;
106   
107    operator unspecified_bool_type() const // never throws
108    {
109        return px == 0? 0: &this_type::get;
110    }
111
112#else
113
114    typedef T * this_type::*unspecified_bool_type;
115
116    operator unspecified_bool_type() const // never throws
117    {
118        return px == 0? 0: &this_type::px;
119    }
120
121#endif
122
123    bool operator! () const // never throws
124    {
125        return px == 0;
126    }
127
128    bool unique() const // never throws
129    {
130        return pn.unique();
131    }
132
133    long use_count() const // never throws
134    {
135        return pn.use_count();
136    }
137
138    void swap(shared_array<T> & other) // never throws
139    {
140        std::swap(px, other.px);
141        pn.swap(other.pn);
142    }
143
144private:
145
146    T * px;                     // contained pointer
147    detail::shared_count pn;    // reference counter
148
149};  // shared_array
150
151template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
152{
153    return a.get() == b.get();
154}
155
156template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
157{
158    return a.get() != b.get();
159}
160
161template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
162{
163    return std::less<T*>()(a.get(), b.get());
164}
165
166template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
167{
168    a.swap(b);
169}
170
171} // namespace boost
172
173#endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
174
175#endif  // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED
Note: See TracBrowser for help on using the repository browser.