webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
scoped_ptr.h
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef GOOGLE_PROTOBUF_STUBS_SCOPED_PTR_H_
32 #define GOOGLE_PROTOBUF_STUBS_SCOPED_PTR_H_
33 
35 
36 namespace google {
37 namespace protobuf {
38 
39 // ===================================================================
40 // from google3/base/scoped_ptr.h
41 
42 namespace internal {
43 
44 // This is an implementation designed to match the anticipated future TR2
45 // implementation of the scoped_ptr class, and its closely-related brethren,
46 // scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
47 
48 template <class C> class scoped_ptr;
49 template <class C> class scoped_array;
50 
51 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
52 // automatically deletes the pointer it holds (if any).
53 // That is, scoped_ptr<T> owns the T object that it points to.
54 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
55 //
56 // The size of a scoped_ptr is small:
57 // sizeof(scoped_ptr<C>) == sizeof(C*)
58 template <class C>
59 class scoped_ptr {
60  public:
61 
62  // The element type
63  typedef C element_type;
64 
65  // Constructor. Defaults to initializing with NULL.
66  // There is no way to create an uninitialized scoped_ptr.
67  // The input parameter must be allocated with new.
68  explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
69 
70  // Destructor. If there is a C object, delete it.
71  // We don't need to test ptr_ == NULL because C++ does that for us.
73  enum { type_must_be_complete = sizeof(C) };
74  delete ptr_;
75  }
76 
77  // Reset. Deletes the current owned object, if any.
78  // Then takes ownership of a new object, if given.
79  // this->reset(this->get()) works.
80  void reset(C* p = NULL) {
81  if (p != ptr_) {
82  enum { type_must_be_complete = sizeof(C) };
83  delete ptr_;
84  ptr_ = p;
85  }
86  }
87 
88  // Accessors to get the owned object.
89  // operator* and operator-> will assert() if there is no current object.
90  C& operator*() const {
91  assert(ptr_ != NULL);
92  return *ptr_;
93  }
94  C* operator->() const {
95  assert(ptr_ != NULL);
96  return ptr_;
97  }
98  C* get() const { return ptr_; }
99 
100  // Comparison operators.
101  // These return whether two scoped_ptr refer to the same object, not just to
102  // two different but equal objects.
103  bool operator==(C* p) const { return ptr_ == p; }
104  bool operator!=(C* p) const { return ptr_ != p; }
105 
106  // Swap two scoped pointers.
107  void swap(scoped_ptr& p2) {
108  C* tmp = ptr_;
109  ptr_ = p2.ptr_;
110  p2.ptr_ = tmp;
111  }
112 
113  // Release a pointer.
114  // The return value is the current pointer held by this object.
115  // If this object holds a NULL pointer, the return value is NULL.
116  // After this operation, this object will hold a NULL pointer,
117  // and will not own the object any more.
118  C* release() {
119  C* retVal = ptr_;
120  ptr_ = NULL;
121  return retVal;
122  }
123 
124  private:
125  C* ptr_;
126 
127  // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't
128  // make sense, and if C2 == C, it still doesn't make sense because you should
129  // never have the same object owned by two different scoped_ptrs.
130  template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
131  template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
132 
133  // Disallow evil constructors
134  scoped_ptr(const scoped_ptr&);
135  void operator=(const scoped_ptr&);
136 };
137 
138 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
139 // with new [] and the destructor deletes objects with delete [].
140 //
141 // As with scoped_ptr<C>, a scoped_array<C> either points to an object
142 // or is NULL. A scoped_array<C> owns the object that it points to.
143 //
144 // Size: sizeof(scoped_array<C>) == sizeof(C*)
145 template <class C>
146 class scoped_array {
147  public:
148 
149  // The element type
150  typedef C element_type;
151 
152  // Constructor. Defaults to initializing with NULL.
153  // There is no way to create an uninitialized scoped_array.
154  // The input parameter must be allocated with new [].
155  explicit scoped_array(C* p = NULL) : array_(p) { }
156 
157  // Destructor. If there is a C object, delete it.
158  // We don't need to test ptr_ == NULL because C++ does that for us.
160  enum { type_must_be_complete = sizeof(C) };
161  delete[] array_;
162  }
163 
164  // Reset. Deletes the current owned object, if any.
165  // Then takes ownership of a new object, if given.
166  // this->reset(this->get()) works.
167  void reset(C* p = NULL) {
168  if (p != array_) {
169  enum { type_must_be_complete = sizeof(C) };
170  delete[] array_;
171  array_ = p;
172  }
173  }
174 
175  // Get one element of the current object.
176  // Will assert() if there is no current object, or index i is negative.
178  assert(i >= 0);
179  assert(array_ != NULL);
180  return array_[i];
181  }
182 
183  // Get a pointer to the zeroth element of the current object.
184  // If there is no current object, return NULL.
185  C* get() const {
186  return array_;
187  }
188 
189  // Comparison operators.
190  // These return whether two scoped_array refer to the same object, not just to
191  // two different but equal objects.
192  bool operator==(C* p) const { return array_ == p; }
193  bool operator!=(C* p) const { return array_ != p; }
194 
195  // Swap two scoped arrays.
196  void swap(scoped_array& p2) {
197  C* tmp = array_;
198  array_ = p2.array_;
199  p2.array_ = tmp;
200  }
201 
202  // Release an array.
203  // The return value is the current pointer held by this object.
204  // If this object holds a NULL pointer, the return value is NULL.
205  // After this operation, this object will hold a NULL pointer,
206  // and will not own the object any more.
207  C* release() {
208  C* retVal = array_;
209  array_ = NULL;
210  return retVal;
211  }
212 
213  private:
214  C* array_;
215 
216  // Forbid comparison of different scoped_array types.
217  template <class C2> bool operator==(scoped_array<C2> const& p2) const;
218  template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
219 
220  // Disallow evil constructors
221  scoped_array(const scoped_array&);
222  void operator=(const scoped_array&);
223 };
224 
225 } // namespace internal
226 
227 // We made these internal so that they would show up as such in the docs,
228 // but we don't want to stick "internal::" in front of them everywhere.
231 
232 
233 } // namespace protobuf
234 } // namespace google
235 
236 #endif // GOOGLE_PROTOBUF_STUBS_SCOPED_PTR_H_
void swap(scoped_ptr &p2)
Definition: scoped_ptr.h:107
DOMString p
Definition: WebCryptoAPI.idl:116
C element_type
Definition: scoped_ptr.h:150
void reset(C *p=NULL)
Definition: scoped_ptr.h:80
bool operator==(C *p) const
Definition: scoped_ptr.h:192
bool operator!=(C *p) const
Definition: scoped_ptr.h:104
scoped_array(C *p=NULL)
Definition: scoped_ptr.h:155
bool operator!=(C *p) const
Definition: scoped_ptr.h:193
bool operator==(C *p) const
Definition: scoped_ptr.h:103
Definition: scoped_ptr.h:49
C * release()
Definition: scoped_ptr.h:207
void reset(C *p=NULL)
Definition: scoped_ptr.h:167
Definition: __init__.py:1
~scoped_ptr()
Definition: scoped_ptr.h:72
scoped_ptr(C *p=NULL)
Definition: scoped_ptr.h:68
Definition: scoped_ptr.h:48
Definition: document.h:393
C element_type
Definition: scoped_ptr.h:63
C * release()
Definition: scoped_ptr.h:118
~scoped_array()
Definition: scoped_ptr.h:159
std::integral_constant< std::ptrdiff_t, V > ptrdiff_t
Definition: Brigand.h:449
for i
Definition: complexityMeasures.m:24
Definition: type_traits_unittest.cc:77
C & operator*() const
Definition: scoped_ptr.h:90
#define NULL
Definition: common_types.h:41
C * operator->() const
Definition: scoped_ptr.h:94
Definition: gflags_completions.h:115
void swap(scoped_array &p2)
Definition: scoped_ptr.h:196
C & operator[](std::ptrdiff_t i) const
Definition: scoped_ptr.h:177