author | William Roberts <williamr@symbian.org> |
Wed, 31 Mar 2010 12:27:01 +0100 | |
branch | Symbian2 |
changeset 3 | e1b950c65cb4 |
permissions | -rw-r--r-- |
williamr@2 | 1 |
#ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED |
williamr@2 | 2 |
#define BOOST_SCOPED_ARRAY_HPP_INCLUDED |
williamr@2 | 3 |
|
williamr@2 | 4 |
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. |
williamr@2 | 5 |
// Copyright (c) 2001, 2002 Peter Dimov |
williamr@2 | 6 |
// |
williamr@2 | 7 |
// Distributed under the Boost Software License, Version 1.0. (See |
williamr@2 | 8 |
// accompanying file LICENSE_1_0.txt or copy at |
williamr@2 | 9 |
// http://www.boost.org/LICENSE_1_0.txt) |
williamr@2 | 10 |
// |
williamr@2 | 11 |
// http://www.boost.org/libs/smart_ptr/scoped_array.htm |
williamr@2 | 12 |
// |
williamr@2 | 13 |
|
williamr@2 | 14 |
#include <boost/assert.hpp> |
williamr@2 | 15 |
#include <boost/checked_delete.hpp> |
williamr@2 | 16 |
#include <boost/config.hpp> // in case ptrdiff_t not in std |
williamr@2 | 17 |
|
williamr@2 | 18 |
#include <boost/detail/workaround.hpp> |
williamr@2 | 19 |
|
williamr@2 | 20 |
#include <cstddef> // for std::ptrdiff_t |
williamr@2 | 21 |
|
williamr@2 | 22 |
namespace boost |
williamr@2 | 23 |
{ |
williamr@2 | 24 |
|
williamr@2 | 25 |
// Debug hooks |
williamr@2 | 26 |
|
williamr@2 | 27 |
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) |
williamr@2 | 28 |
|
williamr@2 | 29 |
void sp_array_constructor_hook(void * p); |
williamr@2 | 30 |
void sp_array_destructor_hook(void * p); |
williamr@2 | 31 |
|
williamr@2 | 32 |
#endif |
williamr@2 | 33 |
|
williamr@2 | 34 |
// scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to |
williamr@2 | 35 |
// is guaranteed, either on destruction of the scoped_array or via an explicit |
williamr@2 | 36 |
// reset(). Use shared_array or std::vector if your needs are more complex. |
williamr@2 | 37 |
|
williamr@2 | 38 |
template<class T> class scoped_array // noncopyable |
williamr@2 | 39 |
{ |
williamr@2 | 40 |
private: |
williamr@2 | 41 |
|
williamr@2 | 42 |
T * ptr; |
williamr@2 | 43 |
|
williamr@2 | 44 |
scoped_array(scoped_array const &); |
williamr@2 | 45 |
scoped_array & operator=(scoped_array const &); |
williamr@2 | 46 |
|
williamr@2 | 47 |
typedef scoped_array<T> this_type; |
williamr@2 | 48 |
|
williamr@2 | 49 |
public: |
williamr@2 | 50 |
|
williamr@2 | 51 |
typedef T element_type; |
williamr@2 | 52 |
|
williamr@2 | 53 |
explicit scoped_array(T * p = 0) : ptr(p) // never throws |
williamr@2 | 54 |
{ |
williamr@2 | 55 |
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) |
williamr@2 | 56 |
boost::sp_array_constructor_hook(ptr); |
williamr@2 | 57 |
#endif |
williamr@2 | 58 |
} |
williamr@2 | 59 |
|
williamr@2 | 60 |
~scoped_array() // never throws |
williamr@2 | 61 |
{ |
williamr@2 | 62 |
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) |
williamr@2 | 63 |
boost::sp_array_destructor_hook(ptr); |
williamr@2 | 64 |
#endif |
williamr@2 | 65 |
boost::checked_array_delete(ptr); |
williamr@2 | 66 |
} |
williamr@2 | 67 |
|
williamr@2 | 68 |
void reset(T * p = 0) // never throws |
williamr@2 | 69 |
{ |
williamr@2 | 70 |
BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors |
williamr@2 | 71 |
this_type(p).swap(*this); |
williamr@2 | 72 |
} |
williamr@2 | 73 |
|
williamr@2 | 74 |
T & operator[](std::ptrdiff_t i) const // never throws |
williamr@2 | 75 |
{ |
williamr@2 | 76 |
BOOST_ASSERT(ptr != 0); |
williamr@2 | 77 |
BOOST_ASSERT(i >= 0); |
williamr@2 | 78 |
return ptr[i]; |
williamr@2 | 79 |
} |
williamr@2 | 80 |
|
williamr@2 | 81 |
T * get() const // never throws |
williamr@2 | 82 |
{ |
williamr@2 | 83 |
return ptr; |
williamr@2 | 84 |
} |
williamr@2 | 85 |
|
williamr@2 | 86 |
// implicit conversion to "bool" |
williamr@2 | 87 |
|
williamr@2 | 88 |
#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530) |
williamr@2 | 89 |
|
williamr@2 | 90 |
operator bool () const |
williamr@2 | 91 |
{ |
williamr@2 | 92 |
return ptr != 0; |
williamr@2 | 93 |
} |
williamr@2 | 94 |
|
williamr@2 | 95 |
#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) |
williamr@2 | 96 |
typedef T * (this_type::*unspecified_bool_type)() const; |
williamr@2 | 97 |
|
williamr@2 | 98 |
operator unspecified_bool_type() const // never throws |
williamr@2 | 99 |
{ |
williamr@2 | 100 |
return ptr == 0? 0: &this_type::get; |
williamr@2 | 101 |
} |
williamr@2 | 102 |
|
williamr@2 | 103 |
#else |
williamr@2 | 104 |
|
williamr@2 | 105 |
typedef T * this_type::*unspecified_bool_type; |
williamr@2 | 106 |
|
williamr@2 | 107 |
operator unspecified_bool_type() const // never throws |
williamr@2 | 108 |
{ |
williamr@2 | 109 |
return ptr == 0? 0: &this_type::ptr; |
williamr@2 | 110 |
} |
williamr@2 | 111 |
|
williamr@2 | 112 |
#endif |
williamr@2 | 113 |
|
williamr@2 | 114 |
bool operator! () const // never throws |
williamr@2 | 115 |
{ |
williamr@2 | 116 |
return ptr == 0; |
williamr@2 | 117 |
} |
williamr@2 | 118 |
|
williamr@2 | 119 |
void swap(scoped_array & b) // never throws |
williamr@2 | 120 |
{ |
williamr@2 | 121 |
T * tmp = b.ptr; |
williamr@2 | 122 |
b.ptr = ptr; |
williamr@2 | 123 |
ptr = tmp; |
williamr@2 | 124 |
} |
williamr@2 | 125 |
|
williamr@2 | 126 |
}; |
williamr@2 | 127 |
|
williamr@2 | 128 |
template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) // never throws |
williamr@2 | 129 |
{ |
williamr@2 | 130 |
a.swap(b); |
williamr@2 | 131 |
} |
williamr@2 | 132 |
|
williamr@2 | 133 |
} // namespace boost |
williamr@2 | 134 |
|
williamr@2 | 135 |
#endif // #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED |