epoc32/include/stdapis/boost/scoped_ptr.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
#ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
williamr@2
     2
#define BOOST_SCOPED_PTR_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_ptr.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/detail/workaround.hpp>
williamr@2
    17
williamr@2
    18
#ifndef BOOST_NO_AUTO_PTR
williamr@2
    19
# include <memory>          // for std::auto_ptr
williamr@2
    20
#endif
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_scalar_constructor_hook(void * p);
williamr@2
    30
void sp_scalar_destructor_hook(void * p);
williamr@2
    31
williamr@2
    32
#endif
williamr@2
    33
williamr@2
    34
//  scoped_ptr mimics a built-in pointer except that it guarantees deletion
williamr@2
    35
//  of the object pointed to, either on destruction of the scoped_ptr or via
williamr@2
    36
//  an explicit reset(). scoped_ptr is a simple solution for simple needs;
williamr@2
    37
//  use shared_ptr or std::auto_ptr if your needs are more complex.
williamr@2
    38
williamr@2
    39
template<class T> class scoped_ptr // noncopyable
williamr@2
    40
{
williamr@2
    41
private:
williamr@2
    42
williamr@2
    43
    T * ptr;
williamr@2
    44
williamr@2
    45
    scoped_ptr(scoped_ptr const &);
williamr@2
    46
    scoped_ptr & operator=(scoped_ptr const &);
williamr@2
    47
williamr@2
    48
    typedef scoped_ptr<T> this_type;
williamr@2
    49
williamr@2
    50
public:
williamr@2
    51
williamr@2
    52
    typedef T element_type;
williamr@2
    53
williamr@2
    54
    explicit scoped_ptr(T * p = 0): ptr(p) // never throws
williamr@2
    55
    {
williamr@2
    56
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
williamr@2
    57
        boost::sp_scalar_constructor_hook(ptr);
williamr@2
    58
#endif
williamr@2
    59
    }
williamr@2
    60
williamr@2
    61
#ifndef BOOST_NO_AUTO_PTR
williamr@2
    62
williamr@2
    63
    explicit scoped_ptr(std::auto_ptr<T> p): ptr(p.release()) // never throws
williamr@2
    64
    {
williamr@2
    65
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
williamr@2
    66
        boost::sp_scalar_constructor_hook(ptr);
williamr@2
    67
#endif
williamr@2
    68
    }
williamr@2
    69
williamr@2
    70
#endif
williamr@2
    71
williamr@2
    72
    ~scoped_ptr() // never throws
williamr@2
    73
    {
williamr@2
    74
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
williamr@2
    75
        boost::sp_scalar_destructor_hook(ptr);
williamr@2
    76
#endif
williamr@2
    77
        boost::checked_delete(ptr);
williamr@2
    78
    }
williamr@2
    79
williamr@2
    80
    void reset(T * p = 0) // never throws
williamr@2
    81
    {
williamr@2
    82
        BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors
williamr@2
    83
        this_type(p).swap(*this);
williamr@2
    84
    }
williamr@2
    85
williamr@2
    86
    T & operator*() const // never throws
williamr@2
    87
    {
williamr@2
    88
        BOOST_ASSERT(ptr != 0);
williamr@2
    89
        return *ptr;
williamr@2
    90
    }
williamr@2
    91
williamr@2
    92
    T * operator->() const // never throws
williamr@2
    93
    {
williamr@2
    94
        BOOST_ASSERT(ptr != 0);
williamr@2
    95
        return ptr;
williamr@2
    96
    }
williamr@2
    97
williamr@2
    98
    T * get() const // never throws
williamr@2
    99
    {
williamr@2
   100
        return ptr;
williamr@2
   101
    }
williamr@2
   102
williamr@2
   103
    // implicit conversion to "bool"
williamr@2
   104
williamr@2
   105
#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
williamr@2
   106
williamr@2
   107
    operator bool () const
williamr@2
   108
    {
williamr@2
   109
        return ptr != 0;
williamr@2
   110
    }
williamr@2
   111
williamr@2
   112
#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
williamr@2
   113
    typedef T * (this_type::*unspecified_bool_type)() const;
williamr@2
   114
    
williamr@2
   115
    operator unspecified_bool_type() const // never throws
williamr@2
   116
    {
williamr@2
   117
        return ptr == 0? 0: &this_type::get;
williamr@2
   118
    }
williamr@2
   119
williamr@2
   120
#else 
williamr@2
   121
    typedef T * this_type::*unspecified_bool_type;
williamr@2
   122
williamr@2
   123
    operator unspecified_bool_type() const // never throws
williamr@2
   124
    {
williamr@2
   125
        return ptr == 0? 0: &this_type::ptr;
williamr@2
   126
    }
williamr@2
   127
williamr@2
   128
#endif
williamr@2
   129
williamr@2
   130
    bool operator! () const // never throws
williamr@2
   131
    {
williamr@2
   132
        return ptr == 0;
williamr@2
   133
    }
williamr@2
   134
williamr@2
   135
    void swap(scoped_ptr & b) // never throws
williamr@2
   136
    {
williamr@2
   137
        T * tmp = b.ptr;
williamr@2
   138
        b.ptr = ptr;
williamr@2
   139
        ptr = tmp;
williamr@2
   140
    }
williamr@2
   141
};
williamr@2
   142
williamr@2
   143
template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
williamr@2
   144
{
williamr@2
   145
    a.swap(b);
williamr@2
   146
}
williamr@2
   147
williamr@2
   148
// get_pointer(p) is a generic way to say p.get()
williamr@2
   149
williamr@2
   150
template<class T> inline T * get_pointer(scoped_ptr<T> const & p)
williamr@2
   151
{
williamr@2
   152
    return p.get();
williamr@2
   153
}
williamr@2
   154
williamr@2
   155
} // namespace boost
williamr@2
   156
williamr@2
   157
#endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED