~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/detail/shared_array_nmt.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

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