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

Side by Side Diff: content/public/test/layout_tests/WebScopedPtr.h

Issue 110533009: Import TestRunner library into chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates 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
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /*
6 * Copyright (C) 2013 Google Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of Google Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #ifndef WebScopedPtr_h
36 #define WebScopedPtr_h
37
38 #include <stddef.h>
39
40 namespace WebTestRunner {
41
42 template<typename Deallocator, typename T>
43 class WebScopedPtrBase {
44 public:
45 // Default constructor. Constructs an empty scoped pointer.
46 inline WebScopedPtrBase() : m_ptr(0) { }
47
48 // Constructs a scoped pointer from a plain one.
49 explicit inline WebScopedPtrBase(T* ptr) : m_ptr(ptr) { }
50
51 // Copy constructor removes the pointer from the original to avoid double
52 // freeing.
53 inline WebScopedPtrBase(const WebScopedPtrBase<Deallocator, T>& rhs)
54 : m_ptr(rhs.m_ptr)
55 {
56 const_cast<WebScopedPtrBase<Deallocator, T>&>(rhs).m_ptr = 0;
57 }
58
59 // When the destructor of the scoped pointer is executed the plain pointer
60 // is deleted using DeleteArray. This implies that you must allocate with
61 // NewArray.
62 inline ~WebScopedPtrBase()
63 {
64 if (m_ptr)
65 Deallocator::Delete(m_ptr);
66 }
67
68 inline T* operator->() const { return m_ptr; }
69
70 // You can get the underlying pointer out with the * operator.
71 inline T* operator*() { return m_ptr; }
72
73 // You can use [n] to index as if it was a plain pointer.
74 inline T& operator[](size_t i)
75 {
76 return m_ptr[i];
77 }
78
79 // You can use [n] to index as if it was a plain pointer.
80 const inline T& operator[](size_t i) const
81 {
82 return m_ptr[i];
83 }
84
85 // We don't have implicit conversion to a T* since that hinders migration:
86 // You would not be able to change a method from returning a T* to
87 // returning an WebScopedArrayPtr<T> and then get errors wherever it is used .
88 inline T* get() const { return m_ptr; }
89
90 inline void reset(T* newValue = 0)
91 {
92 if (m_ptr)
93 Deallocator::Delete(m_ptr);
94 m_ptr = newValue;
95 }
96
97 // Assignment requires an empty (0) WebScopedArrayPtr as the receiver. Like
98 // the copy constructor it removes the pointer in the original to avoid
99 // double freeing.
100 inline WebScopedPtrBase<Deallocator, T>& operator=(const WebScopedPtrBase<De allocator, T>& rhs)
101 {
102 reset(rhs.m_ptr);
103 const_cast<WebScopedPtrBase<Deallocator, T>&>(rhs).m_ptr = 0;
104 return *this;
105 }
106
107 inline bool isEmpty() { return !m_ptr; }
108
109 private:
110 T* m_ptr;
111 };
112
113 // A 'scoped array pointer' that calls DeleteArray on its pointer when the
114 // destructor is called.
115 template<typename T>
116 struct ArrayDeallocator {
117 static void Delete(T* array)
118 {
119 DeleteArray(array);
120 }
121 };
122
123 template<typename T>
124 class WebScopedArrayPtr: public WebScopedPtrBase<ArrayDeallocator<T>, T> {
125 public:
126 inline WebScopedArrayPtr() { }
127 explicit inline WebScopedArrayPtr(T* ptr)
128 : WebScopedPtrBase<ArrayDeallocator<T>, T>(ptr) { }
129 inline WebScopedArrayPtr(const WebScopedArrayPtr<T>& rhs)
130 : WebScopedPtrBase<ArrayDeallocator<T>, T>(rhs) { }
131 };
132
133 template<typename T>
134 struct ObjectDeallocator {
135 static void Delete(T* object)
136 {
137 delete object;
138 }
139 };
140
141 template<typename T>
142 class WebScopedPtr: public WebScopedPtrBase<ObjectDeallocator<T>, T> {
143 public:
144 inline WebScopedPtr() { }
145 explicit inline WebScopedPtr(T* ptr)
146 : WebScopedPtrBase<ObjectDeallocator<T>, T>(ptr) { }
147 inline WebScopedPtr(const WebScopedPtr<T>& rhs)
148 : WebScopedPtrBase<ObjectDeallocator<T>, T>(rhs) { }
149 };
150
151 } // namespace WebTestRunner
152
153 #endif // WebScopedPtr_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698