smart-pointers.h 3.49 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

rmcilroy's avatar
rmcilroy committed
5 6
#ifndef V8_BASE_SMART_POINTERS_H_
#define V8_BASE_SMART_POINTERS_H_
7

8 9
#include "src/base/logging.h"

10
namespace v8 {
rmcilroy's avatar
rmcilroy committed
11
namespace base {
12

rmcilroy's avatar
rmcilroy committed
13
template <typename Deallocator, typename T>
14
class SmartPointerBase {
15
 public:
16
  // Default constructor. Constructs an empty scoped pointer.
17
  SmartPointerBase() : p_(NULL) {}
18

19
  // Constructs a scoped pointer from a plain one.
20
  explicit SmartPointerBase(T* ptr) : p_(ptr) {}
21 22 23

  // Copy constructor removes the pointer from the original to avoid double
  // freeing.
rmcilroy's avatar
rmcilroy committed
24
  SmartPointerBase(const SmartPointerBase<Deallocator, T>& rhs) : p_(rhs.p_) {
25
    const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
26 27
  }

28
  T* operator->() const { return p_; }
29

30
  T& operator*() const { return *p_; }
31

32
  T* get() const { return p_; }
33

34
  // You can use [n] to index as if it was a plain pointer.
rmcilroy's avatar
rmcilroy committed
35
  T& operator[](size_t i) { return p_[i]; }
36

37
  // You can use [n] to index as if it was a plain pointer.
rmcilroy's avatar
rmcilroy committed
38
  const T& operator[](size_t i) const { return p_[i]; }
39

40 41
  // We don't have implicit conversion to a T* since that hinders migration:
  // You would not be able to change a method from returning a T* to
42
  // returning an SmartArrayPointer<T> and then get errors wherever it is used.
43 44 45 46 47


  // If you want to take out the plain pointer and don't want it automatically
  // deleted then call Detach().  Afterwards, the smart pointer is empty
  // (NULL).
48
  T* Detach() {
49 50
    T* temp = p_;
    p_ = NULL;
51 52 53
    return temp;
  }

54
  void Reset(T* new_value) {
55
    DCHECK(p_ == NULL || p_ != new_value);
56 57 58 59
    if (p_) Deallocator::Delete(p_);
    p_ = new_value;
  }

60
  // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like
61 62
  // the copy constructor it removes the pointer in the original to avoid
  // double freeing.
63
  SmartPointerBase<Deallocator, T>& operator=(
64
      const SmartPointerBase<Deallocator, T>& rhs) {
65
    DCHECK(is_empty());
66
    T* tmp = rhs.p_;  // swap to handle self-assignment
67
    const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
68
    p_ = tmp;
69 70 71
    return *this;
  }

72 73 74 75 76 77
  bool is_empty() const { return p_ == NULL; }

 protected:
  // When the destructor of the scoped pointer is executed the plain pointer
  // is deleted using DeleteArray.  This implies that you must allocate with
  // NewArray.
rmcilroy's avatar
rmcilroy committed
78 79 80
  ~SmartPointerBase() {
    if (p_) Deallocator::Delete(p_);
  }
81

82
 private:
83
  T* p_;
84 85
};

86 87 88
// A 'scoped array pointer' that calls DeleteArray on its pointer when the
// destructor is called.

rmcilroy's avatar
rmcilroy committed
89
template <typename T>
90
struct ArrayDeallocator {
rmcilroy's avatar
rmcilroy committed
91
  static void Delete(T* array) { delete[] array; }
92 93 94
};


rmcilroy's avatar
rmcilroy committed
95 96
template <typename T>
class SmartArrayPointer : public SmartPointerBase<ArrayDeallocator<T>, T> {
97
 public:
rmcilroy's avatar
rmcilroy committed
98
  SmartArrayPointer() {}
99
  explicit SmartArrayPointer(T* ptr)
rmcilroy's avatar
rmcilroy committed
100
      : SmartPointerBase<ArrayDeallocator<T>, T>(ptr) {}
101
  SmartArrayPointer(const SmartArrayPointer<T>& rhs)
rmcilroy's avatar
rmcilroy committed
102
      : SmartPointerBase<ArrayDeallocator<T>, T>(rhs) {}
103 104 105
};


rmcilroy's avatar
rmcilroy committed
106
template <typename T>
107
struct ObjectDeallocator {
rmcilroy's avatar
rmcilroy committed
108
  static void Delete(T* object) { delete object; }
109 110
};

rmcilroy's avatar
rmcilroy committed
111 112
template <typename T>
class SmartPointer : public SmartPointerBase<ObjectDeallocator<T>, T> {
113
 public:
rmcilroy's avatar
rmcilroy committed
114
  SmartPointer() {}
115
  explicit SmartPointer(T* ptr)
rmcilroy's avatar
rmcilroy committed
116
      : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) {}
117
  SmartPointer(const SmartPointer<T>& rhs)
rmcilroy's avatar
rmcilroy committed
118
      : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) {}
119 120
};

rmcilroy's avatar
rmcilroy committed
121 122
}  // namespace base
}  // namespace v8
123

124
#endif  // V8_SMART_POINTERS_H_