shared_array.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
00003 
00004 //
00005 //  shared_array.hpp
00006 //
00007 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
00008 //  Copyright (c) 2001, 2002 Peter Dimov
00009 //
00010 //  Distributed under the Boost Software License, Version 1.0. (See
00011 //  accompanying file LICENSE_1_0.txt or copy at
00012 //  http://www.boost.org/LICENSE_1_0.txt)
00013 //
00014 //  See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
00015 //
00016 
00017 #include <boost/config.hpp>   // for broken compiler workarounds
00018 
00019 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
00020 #include <boost/smart_ptr/detail/shared_array_nmt.hpp>
00021 #else
00022 
00023 #include <memory>             // TR1 cyclic inclusion fix
00024 
00025 #include <boost/assert.hpp>
00026 #include <boost/checked_delete.hpp>
00027 
00028 #include <boost/smart_ptr/detail/shared_count.hpp>
00029 #include <boost/detail/workaround.hpp>
00030 
00031 #include <cstddef>            // for std::ptrdiff_t
00032 #include <algorithm>          // for std::swap
00033 #include <functional>         // for std::less
00034 
00035 namespace boost
00036 {
00037 
00038 //
00039 //  shared_array
00040 //
00041 //  shared_array extends shared_ptr to arrays.
00042 //  The array pointed to is deleted when the last shared_array pointing to it
00043 //  is destroyed or reset.
00044 //
00045 
00046 template<class T> class shared_array
00047 {
00048 private:
00049 
00050     // Borland 5.5.1 specific workarounds
00051     typedef checked_array_deleter<T> deleter;
00052     typedef shared_array<T> this_type;
00053 
00054 public:
00055 
00056     typedef T element_type;
00057 
00058     explicit shared_array(T * p = 0): px(p), pn(p, deleter())
00059     {
00060     }
00061 
00062     //
00063     // Requirements: D's copy constructor must not throw
00064     //
00065     // shared_array will release p by calling d(p)
00066     //
00067 
00068     template<class D> shared_array(T * p, D d): px(p), pn(p, d)
00069     {
00070     }
00071 
00072 //  generated copy constructor, assignment, destructor are fine
00073 
00074     void reset(T * p = 0)
00075     {
00076         BOOST_ASSERT(p == 0 || p != px);
00077         this_type(p).swap(*this);
00078     }
00079 
00080     template <class D> void reset(T * p, D d)
00081     {
00082         this_type(p, d).swap(*this);
00083     }
00084 
00085     T & operator[] (std::ptrdiff_t i) const // never throws
00086     {
00087         BOOST_ASSERT(px != 0);
00088         BOOST_ASSERT(i >= 0);
00089         return px[i];
00090     }
00091     
00092     T * get() const // never throws
00093     {
00094         return px;
00095     }
00096 
00097 // implicit conversion to "bool"
00098 #include <boost/smart_ptr/detail/operator_bool.hpp>
00099 
00100     bool unique() const // never throws
00101     {
00102         return pn.unique();
00103     }
00104 
00105     long use_count() const // never throws
00106     {
00107         return pn.use_count();
00108     }
00109 
00110     void swap(shared_array<T> & other) // never throws
00111     {
00112         std::swap(px, other.px);
00113         pn.swap(other.pn);
00114     }
00115 
00116 private:
00117 
00118     T * px;                     // contained pointer
00119     detail::shared_count pn;    // reference counter
00120 
00121 };  // shared_array
00122 
00123 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
00124 {
00125     return a.get() == b.get();
00126 }
00127 
00128 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
00129 {
00130     return a.get() != b.get();
00131 }
00132 
00133 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
00134 {
00135     return std::less<T*>()(a.get(), b.get());
00136 }
00137 
00138 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
00139 {
00140     a.swap(b);
00141 }
00142 
00143 } // namespace boost
00144 
00145 #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
00146 
00147 #endif  // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29