epoc32/include/stdapis/boost/detail/shared_array_nmt.hpp
branchSymbian2
changeset 2 2fe1408b6811
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/stdapis/boost/detail/shared_array_nmt.hpp	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,151 @@
     1.4 +#ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
     1.5 +#define BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
     1.6 +
     1.7 +//
     1.8 +//  detail/shared_array_nmt.hpp - shared_array.hpp without member templates
     1.9 +//
    1.10 +//  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
    1.11 +//  Copyright (c) 2001, 2002 Peter Dimov
    1.12 +//
    1.13 +//  Distributed under the Boost Software License, Version 1.0. (See
    1.14 +//  accompanying file LICENSE_1_0.txt or copy at
    1.15 +//  http://www.boost.org/LICENSE_1_0.txt)
    1.16 +//
    1.17 +//  See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
    1.18 +//
    1.19 +
    1.20 +#include <boost/assert.hpp>
    1.21 +#include <boost/checked_delete.hpp>
    1.22 +#include <boost/throw_exception.hpp>
    1.23 +#include <boost/detail/atomic_count.hpp>
    1.24 +
    1.25 +#include <cstddef>          // for std::ptrdiff_t
    1.26 +#include <algorithm>        // for std::swap
    1.27 +#include <functional>       // for std::less
    1.28 +#include <new>              // for std::bad_alloc
    1.29 +
    1.30 +namespace boost
    1.31 +{
    1.32 +
    1.33 +template<class T> class shared_array
    1.34 +{
    1.35 +private:
    1.36 +
    1.37 +    typedef detail::atomic_count count_type;
    1.38 +
    1.39 +public:
    1.40 +
    1.41 +    typedef T element_type;
    1.42 +      
    1.43 +    explicit shared_array(T * p = 0): px(p)
    1.44 +    {
    1.45 +#ifndef BOOST_NO_EXCEPTIONS
    1.46 +
    1.47 +        try  // prevent leak if new throws
    1.48 +        {
    1.49 +            pn = new count_type(1);
    1.50 +        }
    1.51 +        catch(...)
    1.52 +        {
    1.53 +            boost::checked_array_delete(p);
    1.54 +            throw;
    1.55 +        }
    1.56 +
    1.57 +#else
    1.58 +
    1.59 +        pn = new count_type(1);
    1.60 +
    1.61 +        if(pn == 0)
    1.62 +        {
    1.63 +            boost::checked_array_delete(p);
    1.64 +            boost::throw_exception(std::bad_alloc());
    1.65 +        }
    1.66 +
    1.67 +#endif
    1.68 +    }
    1.69 +
    1.70 +    ~shared_array()
    1.71 +    {
    1.72 +        if(--*pn == 0)
    1.73 +        {
    1.74 +            boost::checked_array_delete(px);
    1.75 +            delete pn;
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    shared_array(shared_array const & r) : px(r.px)  // never throws
    1.80 +    {
    1.81 +        pn = r.pn;
    1.82 +        ++*pn;
    1.83 +    }
    1.84 +
    1.85 +    shared_array & operator=(shared_array const & r)
    1.86 +    {
    1.87 +        shared_array(r).swap(*this);
    1.88 +        return *this;
    1.89 +    }
    1.90 +
    1.91 +    void reset(T * p = 0)
    1.92 +    {
    1.93 +        BOOST_ASSERT(p == 0 || p != px);
    1.94 +        shared_array(p).swap(*this);
    1.95 +    }
    1.96 +
    1.97 +    T * get() const  // never throws
    1.98 +    {
    1.99 +        return px;
   1.100 +    }
   1.101 +
   1.102 +    T & operator[](std::ptrdiff_t i) const  // never throws
   1.103 +    {
   1.104 +        BOOST_ASSERT(px != 0);
   1.105 +        BOOST_ASSERT(i >= 0);
   1.106 +        return px[i];
   1.107 +    }
   1.108 +
   1.109 +    long use_count() const  // never throws
   1.110 +    {
   1.111 +        return *pn;
   1.112 +    }
   1.113 +
   1.114 +    bool unique() const  // never throws
   1.115 +    {
   1.116 +        return *pn == 1;
   1.117 +    }
   1.118 +
   1.119 +    void swap(shared_array<T> & other)  // never throws
   1.120 +    {
   1.121 +        std::swap(px, other.px);
   1.122 +        std::swap(pn, other.pn);
   1.123 +    }
   1.124 +
   1.125 +private:
   1.126 +
   1.127 +    T * px;            // contained pointer
   1.128 +    count_type * pn;   // ptr to reference counter
   1.129 +      
   1.130 +};  // shared_array
   1.131 +
   1.132 +template<class T, class U> inline bool operator==(shared_array<T> const & a, shared_array<U> const & b)
   1.133 +{
   1.134 +    return a.get() == b.get();
   1.135 +}
   1.136 +
   1.137 +template<class T, class U> inline bool operator!=(shared_array<T> const & a, shared_array<U> const & b)
   1.138 +{
   1.139 +    return a.get() != b.get();
   1.140 +}
   1.141 +
   1.142 +template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b)
   1.143 +{
   1.144 +    return std::less<T*>()(a.get(), b.get());
   1.145 +}
   1.146 +
   1.147 +template<class T> void swap(shared_array<T> & a, shared_array<T> & b)
   1.148 +{
   1.149 +    a.swap(b);
   1.150 +}
   1.151 +
   1.152 +} // namespace boost
   1.153 +
   1.154 +#endif  // #ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED