1
//===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file defines and implements the OwningPtr class.
12
//===----------------------------------------------------------------------===//
14
#ifndef LLVM_ADT_OWNING_PTR_H
15
#define LLVM_ADT_OWNING_PTR_H
22
/// OwningPtr smart pointer - OwningPtr mimics a built-in pointer except that it
23
/// guarantees deletion of the object pointed to, either on destruction of the
24
/// OwningPtr or via an explicit reset(). Once created, ownership of the
25
/// pointee object can be taken away from OwningPtr by using the take method.
28
OwningPtr(OwningPtr const &); // DO NOT IMPLEMENT
29
OwningPtr &operator=(OwningPtr const &); // DO NOT IMPLEMENT
32
explicit OwningPtr(T *P = 0) : Ptr(P) {}
38
/// reset - Change the current pointee to the specified pointer. Note that
39
/// calling this with any pointer (including a null pointer) deletes the
41
void reset(T *P = 0) {
48
/// take - Reset the owning pointer to null and return its pointer. This does
49
/// not delete the pointer before returning it.
56
T &operator*() const {
57
assert(Ptr && "Cannot dereference null pointer");
61
T *operator->() const { return Ptr; }
62
T *get() const { return Ptr; }
63
operator bool() const { return Ptr != 0; }
64
bool operator!() const { return Ptr == 0; }
66
void swap(OwningPtr &RHS) {
74
inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
78
/// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
79
/// functionality as OwningPtr, except that it works for array types.
81
class OwningArrayPtr {
82
OwningArrayPtr(OwningArrayPtr const &); // DO NOT IMPLEMENT
83
OwningArrayPtr &operator=(OwningArrayPtr const &); // DO NOT IMPLEMENT
86
explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
92
/// reset - Change the current pointee to the specified pointer. Note that
93
/// calling this with any pointer (including a null pointer) deletes the
95
void reset(T *P = 0) {
102
/// take - Reset the owning pointer to null and return its pointer. This does
103
/// not delete the pointer before returning it.
110
T &operator[](std::ptrdiff_t i) const {
111
assert(Ptr && "Cannot dereference null pointer");
115
T *get() const { return Ptr; }
116
operator bool() const { return Ptr != 0; }
117
bool operator!() const { return Ptr == 0; }
119
void swap(OwningArrayPtr &RHS) {
127
inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
132
} // end namespace llvm