1
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
2
// Copyright (c) 2001, 2002 Peter Dimov
4
// Permission to copy, use, modify, sell and distribute this software
5
// is granted provided this copyright notice appears in all copies.
6
// This software is provided "as is" without express or implied
7
// warranty, and with no claim as to its suitability for any purpose.
9
// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
12
// scoped_ptr mimics a built-in pointer except that it guarantees deletion
13
// of the object pointed to, either on destruction of the scoped_ptr or via
14
// an explicit reset(). scoped_ptr is a simple solution for simple needs;
15
// use shared_ptr or std::auto_ptr if your needs are more complex.
17
// scoped_ptr_malloc added in by Google. When one of
18
// these goes out of scope, instead of doing a delete or delete[], it
19
// calls free(). scoped_ptr_malloc<char> is likely to see much more
20
// use than any other specializations.
22
// release() added in by Google. Use this to conditionally
23
// transfer ownership of a heap-allocated object to the caller, usually on
25
#ifndef TALK_BASE_SCOPED_PTR_H__
26
#define TALK_BASE_SCOPED_PTR_H__
28
#include <cstddef> // for std::ptrdiff_t
29
#include <stdlib.h> // for free() decl
31
#include "talk/base/common.h" // for ASSERT
34
namespace std { using ::ptrdiff_t; };
45
scoped_ptr(scoped_ptr const &);
46
scoped_ptr & operator=(scoped_ptr const &);
50
typedef T element_type;
52
explicit scoped_ptr(T* p = NULL): ptr(p) {}
55
typedef char type_must_be_complete[sizeof(T)];
59
void reset(T* p = NULL) {
60
typedef char type_must_be_complete[sizeof(T)];
65
// Delete last, in case obj destructor indirectly results in ~scoped_ptr
70
T& operator*() const {
75
T* operator->() const {
84
void swap(scoped_ptr & b) {
109
template<typename T> inline
110
void swap(scoped_ptr<T>& a, scoped_ptr<T>& b) {
117
// scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
118
// is guaranteed, either on destruction of the scoped_array or via an explicit
119
// reset(). Use shared_array or std::vector if your needs are more complex.
127
scoped_array(scoped_array const &);
128
scoped_array & operator=(scoped_array const &);
132
typedef T element_type;
134
explicit scoped_array(T* p = NULL) : ptr(p) {}
137
typedef char type_must_be_complete[sizeof(T)];
141
void reset(T* p = NULL) {
142
typedef char type_must_be_complete[sizeof(T)];
147
// Delete last, in case arr destructor indirectly results in ~scoped_array
152
T& operator[](std::ptrdiff_t i) const {
162
void swap(scoped_array & b) {
183
template<class T> inline
184
void swap(scoped_array<T>& a, scoped_array<T>& b) {
188
// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
189
// second template argument, the function used to free the object.
191
template<typename T, void (*FF)(void*) = free> class scoped_ptr_malloc {
196
scoped_ptr_malloc(scoped_ptr_malloc const &);
197
scoped_ptr_malloc & operator=(scoped_ptr_malloc const &);
201
typedef T element_type;
203
explicit scoped_ptr_malloc(T* p = 0): ptr(p) {}
205
~scoped_ptr_malloc() {
206
FF(static_cast<void*>(ptr));
209
void reset(T* p = 0) {
211
FF(static_cast<void*>(ptr));
216
T& operator*() const {
221
T* operator->() const {
230
void swap(scoped_ptr_malloc & b) {
244
FF(static_cast<void*>(ptr));
251
template<typename T, void (*FF)(void*)> inline
252
void swap(scoped_ptr_malloc<T,FF>& a, scoped_ptr_malloc<T,FF>& b) {
256
} // namespace talk_base
258
#endif // #ifndef TALK_BASE_SCOPED_PTR_H__