Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: src/smart-pointers.h

Issue 101763003: Replace 'operator*' with explicit 'get' method on SmartPointer (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/scopes.cc ('k') | src/stub-cache.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 18 matching lines...) Expand all
29 #define V8_SMART_POINTERS_H_ 29 #define V8_SMART_POINTERS_H_
30 30
31 namespace v8 { 31 namespace v8 {
32 namespace internal { 32 namespace internal {
33 33
34 34
35 template<typename Deallocator, typename T> 35 template<typename Deallocator, typename T>
36 class SmartPointerBase { 36 class SmartPointerBase {
37 public: 37 public:
38 // Default constructor. Constructs an empty scoped pointer. 38 // Default constructor. Constructs an empty scoped pointer.
39 inline SmartPointerBase() : p_(NULL) {} 39 SmartPointerBase() : p_(NULL) {}
40 40
41 // Constructs a scoped pointer from a plain one. 41 // Constructs a scoped pointer from a plain one.
42 explicit inline SmartPointerBase(T* ptr) : p_(ptr) {} 42 explicit SmartPointerBase(T* ptr) : p_(ptr) {}
43 43
44 // Copy constructor removes the pointer from the original to avoid double 44 // Copy constructor removes the pointer from the original to avoid double
45 // freeing. 45 // freeing.
46 inline SmartPointerBase(const SmartPointerBase<Deallocator, T>& rhs) 46 SmartPointerBase(const SmartPointerBase<Deallocator, T>& rhs)
47 : p_(rhs.p_) { 47 : p_(rhs.p_) {
48 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; 48 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
49 } 49 }
50 50
51 // When the destructor of the scoped pointer is executed the plain pointer 51 // When the destructor of the scoped pointer is executed the plain pointer
52 // is deleted using DeleteArray. This implies that you must allocate with 52 // is deleted using DeleteArray. This implies that you must allocate with
53 // NewArray. 53 // NewArray.
54 inline ~SmartPointerBase() { if (p_) Deallocator::Delete(p_); } 54 ~SmartPointerBase() { if (p_) Deallocator::Delete(p_); }
svenpanne 2013/12/03 16:17:55 Hmmm, this has been like that before, but why is i
yurys 2013/12/04 07:34:01 This is a good question. I believe the problem is
Sven Panne 2013/12/04 07:40:57 I think this class is actually trying to be a (poo
alph 2013/12/04 07:44:48 How about making the SmartPointerBase destructor p
jochen (gone - plz use gerrit) 2013/12/04 08:07:47 Yes, making it protected makes sense. Since this i
55 55
56 inline T* operator->() const { return p_; } 56 T* operator->() const { return p_; }
57 57
58 // You can get the underlying pointer out with the * operator. 58 T& operator*() const { return *p_; }
59 inline T* operator*() { return p_; } 59
60 T* get() const { return p_; }
60 61
61 // You can use [n] to index as if it was a plain pointer. 62 // You can use [n] to index as if it was a plain pointer.
62 inline T& operator[](size_t i) { 63 T& operator[](size_t i) {
63 return p_[i]; 64 return p_[i];
64 } 65 }
65 66
66 // You can use [n] to index as if it was a plain pointer. 67 // You can use [n] to index as if it was a plain pointer.
67 const inline T& operator[](size_t i) const { 68 const T& operator[](size_t i) const {
68 return p_[i]; 69 return p_[i];
69 } 70 }
70 71
71 // We don't have implicit conversion to a T* since that hinders migration: 72 // We don't have implicit conversion to a T* since that hinders migration:
72 // You would not be able to change a method from returning a T* to 73 // You would not be able to change a method from returning a T* to
73 // returning an SmartArrayPointer<T> and then get errors wherever it is used. 74 // returning an SmartArrayPointer<T> and then get errors wherever it is used.
74 75
75 76
76 // If you want to take out the plain pointer and don't want it automatically 77 // If you want to take out the plain pointer and don't want it automatically
77 // deleted then call Detach(). Afterwards, the smart pointer is empty 78 // deleted then call Detach(). Afterwards, the smart pointer is empty
78 // (NULL). 79 // (NULL).
79 inline T* Detach() { 80 T* Detach() {
80 T* temp = p_; 81 T* temp = p_;
81 p_ = NULL; 82 p_ = NULL;
82 return temp; 83 return temp;
83 } 84 }
84 85
85 inline void Reset(T* new_value) { 86 void Reset(T* new_value) {
87 ASSERT(p_ == NULL || p_ != new_value);
alph 2013/12/03 14:15:03 maybe: if (p_ == new_value) return;
yurys 2013/12/03 14:28:20 This should never happen in practice. In fact supp
86 if (p_) Deallocator::Delete(p_); 88 if (p_) Deallocator::Delete(p_);
87 p_ = new_value; 89 p_ = new_value;
88 } 90 }
89 91
90 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like 92 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like
91 // the copy constructor it removes the pointer in the original to avoid 93 // the copy constructor it removes the pointer in the original to avoid
92 // double freeing. 94 // double freeing.
93 inline SmartPointerBase<Deallocator, T>& operator=( 95 SmartPointerBase<Deallocator, T>& operator=(
94 const SmartPointerBase<Deallocator, T>& rhs) { 96 const SmartPointerBase<Deallocator, T>& rhs) {
95 ASSERT(is_empty()); 97 ASSERT(is_empty());
96 T* tmp = rhs.p_; // swap to handle self-assignment 98 T* tmp = rhs.p_; // swap to handle self-assignment
97 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; 99 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
98 p_ = tmp; 100 p_ = tmp;
99 return *this; 101 return *this;
100 } 102 }
101 103
102 inline bool is_empty() { return p_ == NULL; } 104 bool is_empty() const { return p_ == NULL; }
103 105
104 private: 106 private:
105 T* p_; 107 T* p_;
106 }; 108 };
107 109
108 // A 'scoped array pointer' that calls DeleteArray on its pointer when the 110 // A 'scoped array pointer' that calls DeleteArray on its pointer when the
109 // destructor is called. 111 // destructor is called.
110 112
111 template<typename T> 113 template<typename T>
112 struct ArrayDeallocator { 114 struct ArrayDeallocator {
113 static void Delete(T* array) { 115 static void Delete(T* array) {
114 DeleteArray(array); 116 DeleteArray(array);
115 } 117 }
116 }; 118 };
117 119
118 120
119 template<typename T> 121 template<typename T>
120 class SmartArrayPointer: public SmartPointerBase<ArrayDeallocator<T>, T> { 122 class SmartArrayPointer: public SmartPointerBase<ArrayDeallocator<T>, T> {
121 public: 123 public:
122 inline SmartArrayPointer() { } 124 SmartArrayPointer() { }
123 explicit inline SmartArrayPointer(T* ptr) 125 explicit SmartArrayPointer(T* ptr)
124 : SmartPointerBase<ArrayDeallocator<T>, T>(ptr) { } 126 : SmartPointerBase<ArrayDeallocator<T>, T>(ptr) { }
125 inline SmartArrayPointer(const SmartArrayPointer<T>& rhs) 127 SmartArrayPointer(const SmartArrayPointer<T>& rhs)
126 : SmartPointerBase<ArrayDeallocator<T>, T>(rhs) { } 128 : SmartPointerBase<ArrayDeallocator<T>, T>(rhs) { }
127 }; 129 };
128 130
129 131
130 template<typename T> 132 template<typename T>
131 struct ObjectDeallocator { 133 struct ObjectDeallocator {
132 static void Delete(T* object) { 134 static void Delete(T* object) {
133 delete object; 135 delete object;
134 } 136 }
135 }; 137 };
136 138
137 139
138 template<typename T> 140 template<typename T>
139 class SmartPointer: public SmartPointerBase<ObjectDeallocator<T>, T> { 141 class SmartPointer: public SmartPointerBase<ObjectDeallocator<T>, T> {
140 public: 142 public:
141 inline SmartPointer() { } 143 SmartPointer() { }
142 explicit inline SmartPointer(T* ptr) 144 explicit SmartPointer(T* ptr)
143 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { } 145 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { }
144 inline SmartPointer(const SmartPointer<T>& rhs) 146 SmartPointer(const SmartPointer<T>& rhs)
145 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { } 147 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { }
146 }; 148 };
147 149
148 } } // namespace v8::internal 150 } } // namespace v8::internal
149 151
150 #endif // V8_SMART_POINTERS_H_ 152 #endif // V8_SMART_POINTERS_H_
OLDNEW
« no previous file with comments | « src/scopes.cc ('k') | src/stub-cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698