summaryrefslogtreecommitdiff
path: root/include/llvm/ADT/OwningPtr.h
blob: 05bcd40d0862202e9161f46950ea0bae0498ad39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines and implements the OwningPtr class.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ADT_OWNING_PTR_H
#define LLVM_ADT_OWNING_PTR_H

#include "llvm/Support/Compiler.h"
#include <cassert>
#include <cstddef>

namespace llvm {

/// OwningPtr smart pointer - OwningPtr mimics a built-in pointer except that it
/// guarantees deletion of the object pointed to, either on destruction of the
/// OwningPtr or via an explicit reset().  Once created, ownership of the
/// pointee object can be taken away from OwningPtr by using the take method.
template<class T>
class OwningPtr {
  OwningPtr(OwningPtr const &) LLVM_DELETED_FUNCTION;
  OwningPtr &operator=(OwningPtr const &) LLVM_DELETED_FUNCTION;
  T *Ptr;
public:
  explicit OwningPtr(T *P = 0) : Ptr(P) {}

#if LLVM_USE_RVALUE_REFERENCES
  OwningPtr(OwningPtr &&Other) : Ptr(Other.take()) {}

  OwningPtr &operator=(OwningPtr &&Other) {
    reset(Other.take());
    return *this;
  }
#endif

  ~OwningPtr() {
    delete Ptr;
  }

  /// reset - Change the current pointee to the specified pointer.  Note that
  /// calling this with any pointer (including a null pointer) deletes the
  /// current pointer.
  void reset(T *P = 0) {
    if (P == Ptr) return;
    T *Tmp = Ptr;
    Ptr = P;
    delete Tmp;
  }

  /// take - Reset the owning pointer to null and return its pointer.  This does
  /// not delete the pointer before returning it.
  T *take() {
    T *Tmp = Ptr;
    Ptr = 0;
    return Tmp;
  }

  T &operator*() const {
    assert(Ptr && "Cannot dereference null pointer");
    return *Ptr;
  }

  T *operator->() const { return Ptr; }
  T *get() const { return Ptr; }
  operator bool() const { return Ptr != 0; }
  bool operator!() const { return Ptr == 0; }

  void swap(OwningPtr &RHS) {
    T *Tmp = RHS.Ptr;
    RHS.Ptr = Ptr;
    Ptr = Tmp;
  }
};

template<class T>
inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
  a.swap(b);
}

/// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
///  functionality as OwningPtr, except that it works for array types.
template<class T>
class OwningArrayPtr {
  OwningArrayPtr(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
  OwningArrayPtr &operator=(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
  T *Ptr;
public:
  explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}

#if LLVM_USE_RVALUE_REFERENCES
  OwningArrayPtr(OwningArrayPtr &&Other) : Ptr(Other.take()) {}

  OwningArrayPtr &operator=(OwningArrayPtr &&Other) {
    reset(Other.take());
    return *this;
  }
#endif

  ~OwningArrayPtr() {
    delete [] Ptr;
  }

  /// reset - Change the current pointee to the specified pointer.  Note that
  /// calling this with any pointer (including a null pointer) deletes the
  /// current pointer.
  void reset(T *P = 0) {
    if (P == Ptr) return;
    T *Tmp = Ptr;
    Ptr = P;
    delete [] Tmp;
  }

  /// take - Reset the owning pointer to null and return its pointer.  This does
  /// not delete the pointer before returning it.
  T *take() {
    T *Tmp = Ptr;
    Ptr = 0;
    return Tmp;
  }

  T &operator[](std::ptrdiff_t i) const {
    assert(Ptr && "Cannot dereference null pointer");
    return Ptr[i];
  }

  T *get() const { return Ptr; }
  operator bool() const { return Ptr != 0; }
  bool operator!() const { return Ptr == 0; }

  void swap(OwningArrayPtr &RHS) {
    T *Tmp = RHS.Ptr;
    RHS.Ptr = Ptr;
    Ptr = Tmp;
  }
};

template<class T>
inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
  a.swap(b);
}

} // end namespace llvm

#endif