toolkit/crashreporter/google-breakpad/src/common/scoped_ptr.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
michael@0 2 // Copyright (c) 2001, 2002 Peter Dimov
michael@0 3 //
michael@0 4 // Permission to copy, use, modify, sell and distribute this software
michael@0 5 // is granted provided this copyright notice appears in all copies.
michael@0 6 // This software is provided "as is" without express or implied
michael@0 7 // warranty, and with no claim as to its suitability for any purpose.
michael@0 8 //
michael@0 9 // See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
michael@0 10 //
michael@0 11
michael@0 12 // scoped_ptr mimics a built-in pointer except that it guarantees deletion
michael@0 13 // of the object pointed to, either on destruction of the scoped_ptr or via
michael@0 14 // an explicit reset(). scoped_ptr is a simple solution for simple needs;
michael@0 15 // use shared_ptr or std::auto_ptr if your needs are more complex.
michael@0 16
michael@0 17 // *** NOTE ***
michael@0 18 // If your scoped_ptr is a class member of class FOO pointing to a
michael@0 19 // forward declared type BAR (as shown below), then you MUST use a non-inlined
michael@0 20 // version of the destructor. The destructor of a scoped_ptr (called from
michael@0 21 // FOO's destructor) must have a complete definition of BAR in order to
michael@0 22 // destroy it. Example:
michael@0 23 //
michael@0 24 // -- foo.h --
michael@0 25 // class BAR;
michael@0 26 //
michael@0 27 // class FOO {
michael@0 28 // public:
michael@0 29 // FOO();
michael@0 30 // ~FOO(); // Required for sources that instantiate class FOO to compile!
michael@0 31 //
michael@0 32 // private:
michael@0 33 // scoped_ptr<BAR> bar_;
michael@0 34 // };
michael@0 35 //
michael@0 36 // -- foo.cc --
michael@0 37 // #include "foo.h"
michael@0 38 // FOO::~FOO() {} // Empty, but must be non-inlined to FOO's class definition.
michael@0 39
michael@0 40 // scoped_ptr_malloc added by Google
michael@0 41 // When one of these goes out of scope, instead of doing a delete or
michael@0 42 // delete[], it calls free(). scoped_ptr_malloc<char> is likely to see
michael@0 43 // much more use than any other specializations.
michael@0 44
michael@0 45 // release() added by Google
michael@0 46 // Use this to conditionally transfer ownership of a heap-allocated object
michael@0 47 // to the caller, usually on method success.
michael@0 48
michael@0 49 #ifndef COMMON_SCOPED_PTR_H_
michael@0 50 #define COMMON_SCOPED_PTR_H_
michael@0 51
michael@0 52 #include <cstddef> // for std::ptrdiff_t
michael@0 53 #include <assert.h> // for assert
michael@0 54 #include <stdlib.h> // for free() decl
michael@0 55
michael@0 56 namespace google_breakpad {
michael@0 57
michael@0 58 template <typename T>
michael@0 59 class scoped_ptr {
michael@0 60 private:
michael@0 61
michael@0 62 T* ptr;
michael@0 63
michael@0 64 scoped_ptr(scoped_ptr const &);
michael@0 65 scoped_ptr & operator=(scoped_ptr const &);
michael@0 66
michael@0 67 public:
michael@0 68
michael@0 69 typedef T element_type;
michael@0 70
michael@0 71 explicit scoped_ptr(T* p = 0): ptr(p) {}
michael@0 72
michael@0 73 ~scoped_ptr() {
michael@0 74 typedef char type_must_be_complete[sizeof(T)];
michael@0 75 delete ptr;
michael@0 76 }
michael@0 77
michael@0 78 void reset(T* p = 0) {
michael@0 79 typedef char type_must_be_complete[sizeof(T)];
michael@0 80
michael@0 81 if (ptr != p) {
michael@0 82 delete ptr;
michael@0 83 ptr = p;
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 T& operator*() const {
michael@0 88 assert(ptr != 0);
michael@0 89 return *ptr;
michael@0 90 }
michael@0 91
michael@0 92 T* operator->() const {
michael@0 93 assert(ptr != 0);
michael@0 94 return ptr;
michael@0 95 }
michael@0 96
michael@0 97 bool operator==(T* p) const {
michael@0 98 return ptr == p;
michael@0 99 }
michael@0 100
michael@0 101 bool operator!=(T* p) const {
michael@0 102 return ptr != p;
michael@0 103 }
michael@0 104
michael@0 105 T* get() const {
michael@0 106 return ptr;
michael@0 107 }
michael@0 108
michael@0 109 void swap(scoped_ptr & b) {
michael@0 110 T* tmp = b.ptr;
michael@0 111 b.ptr = ptr;
michael@0 112 ptr = tmp;
michael@0 113 }
michael@0 114
michael@0 115 T* release() {
michael@0 116 T* tmp = ptr;
michael@0 117 ptr = 0;
michael@0 118 return tmp;
michael@0 119 }
michael@0 120
michael@0 121 private:
michael@0 122
michael@0 123 // no reason to use these: each scoped_ptr should have its own object
michael@0 124 template <typename U> bool operator==(scoped_ptr<U> const& p) const;
michael@0 125 template <typename U> bool operator!=(scoped_ptr<U> const& p) const;
michael@0 126 };
michael@0 127
michael@0 128 template<typename T> inline
michael@0 129 void swap(scoped_ptr<T>& a, scoped_ptr<T>& b) {
michael@0 130 a.swap(b);
michael@0 131 }
michael@0 132
michael@0 133 template<typename T> inline
michael@0 134 bool operator==(T* p, const scoped_ptr<T>& b) {
michael@0 135 return p == b.get();
michael@0 136 }
michael@0 137
michael@0 138 template<typename T> inline
michael@0 139 bool operator!=(T* p, const scoped_ptr<T>& b) {
michael@0 140 return p != b.get();
michael@0 141 }
michael@0 142
michael@0 143 // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
michael@0 144 // is guaranteed, either on destruction of the scoped_array or via an explicit
michael@0 145 // reset(). Use shared_array or std::vector if your needs are more complex.
michael@0 146
michael@0 147 template<typename T>
michael@0 148 class scoped_array {
michael@0 149 private:
michael@0 150
michael@0 151 T* ptr;
michael@0 152
michael@0 153 scoped_array(scoped_array const &);
michael@0 154 scoped_array & operator=(scoped_array const &);
michael@0 155
michael@0 156 public:
michael@0 157
michael@0 158 typedef T element_type;
michael@0 159
michael@0 160 explicit scoped_array(T* p = 0) : ptr(p) {}
michael@0 161
michael@0 162 ~scoped_array() {
michael@0 163 typedef char type_must_be_complete[sizeof(T)];
michael@0 164 delete[] ptr;
michael@0 165 }
michael@0 166
michael@0 167 void reset(T* p = 0) {
michael@0 168 typedef char type_must_be_complete[sizeof(T)];
michael@0 169
michael@0 170 if (ptr != p) {
michael@0 171 delete [] ptr;
michael@0 172 ptr = p;
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 T& operator[](std::ptrdiff_t i) const {
michael@0 177 assert(ptr != 0);
michael@0 178 assert(i >= 0);
michael@0 179 return ptr[i];
michael@0 180 }
michael@0 181
michael@0 182 bool operator==(T* p) const {
michael@0 183 return ptr == p;
michael@0 184 }
michael@0 185
michael@0 186 bool operator!=(T* p) const {
michael@0 187 return ptr != p;
michael@0 188 }
michael@0 189
michael@0 190 T* get() const {
michael@0 191 return ptr;
michael@0 192 }
michael@0 193
michael@0 194 void swap(scoped_array & b) {
michael@0 195 T* tmp = b.ptr;
michael@0 196 b.ptr = ptr;
michael@0 197 ptr = tmp;
michael@0 198 }
michael@0 199
michael@0 200 T* release() {
michael@0 201 T* tmp = ptr;
michael@0 202 ptr = 0;
michael@0 203 return tmp;
michael@0 204 }
michael@0 205
michael@0 206 private:
michael@0 207
michael@0 208 // no reason to use these: each scoped_array should have its own object
michael@0 209 template <typename U> bool operator==(scoped_array<U> const& p) const;
michael@0 210 template <typename U> bool operator!=(scoped_array<U> const& p) const;
michael@0 211 };
michael@0 212
michael@0 213 template<class T> inline
michael@0 214 void swap(scoped_array<T>& a, scoped_array<T>& b) {
michael@0 215 a.swap(b);
michael@0 216 }
michael@0 217
michael@0 218 template<typename T> inline
michael@0 219 bool operator==(T* p, const scoped_array<T>& b) {
michael@0 220 return p == b.get();
michael@0 221 }
michael@0 222
michael@0 223 template<typename T> inline
michael@0 224 bool operator!=(T* p, const scoped_array<T>& b) {
michael@0 225 return p != b.get();
michael@0 226 }
michael@0 227
michael@0 228
michael@0 229 // This class wraps the c library function free() in a class that can be
michael@0 230 // passed as a template argument to scoped_ptr_malloc below.
michael@0 231 class ScopedPtrMallocFree {
michael@0 232 public:
michael@0 233 inline void operator()(void* x) const {
michael@0 234 free(x);
michael@0 235 }
michael@0 236 };
michael@0 237
michael@0 238 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
michael@0 239 // second template argument, the functor used to free the object.
michael@0 240
michael@0 241 template<typename T, typename FreeProc = ScopedPtrMallocFree>
michael@0 242 class scoped_ptr_malloc {
michael@0 243 private:
michael@0 244
michael@0 245 T* ptr;
michael@0 246
michael@0 247 scoped_ptr_malloc(scoped_ptr_malloc const &);
michael@0 248 scoped_ptr_malloc & operator=(scoped_ptr_malloc const &);
michael@0 249
michael@0 250 public:
michael@0 251
michael@0 252 typedef T element_type;
michael@0 253
michael@0 254 explicit scoped_ptr_malloc(T* p = 0): ptr(p) {}
michael@0 255
michael@0 256 ~scoped_ptr_malloc() {
michael@0 257 typedef char type_must_be_complete[sizeof(T)];
michael@0 258 free_((void*) ptr);
michael@0 259 }
michael@0 260
michael@0 261 void reset(T* p = 0) {
michael@0 262 typedef char type_must_be_complete[sizeof(T)];
michael@0 263
michael@0 264 if (ptr != p) {
michael@0 265 free_((void*) ptr);
michael@0 266 ptr = p;
michael@0 267 }
michael@0 268 }
michael@0 269
michael@0 270 T& operator*() const {
michael@0 271 assert(ptr != 0);
michael@0 272 return *ptr;
michael@0 273 }
michael@0 274
michael@0 275 T* operator->() const {
michael@0 276 assert(ptr != 0);
michael@0 277 return ptr;
michael@0 278 }
michael@0 279
michael@0 280 bool operator==(T* p) const {
michael@0 281 return ptr == p;
michael@0 282 }
michael@0 283
michael@0 284 bool operator!=(T* p) const {
michael@0 285 return ptr != p;
michael@0 286 }
michael@0 287
michael@0 288 T* get() const {
michael@0 289 return ptr;
michael@0 290 }
michael@0 291
michael@0 292 void swap(scoped_ptr_malloc & b) {
michael@0 293 T* tmp = b.ptr;
michael@0 294 b.ptr = ptr;
michael@0 295 ptr = tmp;
michael@0 296 }
michael@0 297
michael@0 298 T* release() {
michael@0 299 T* tmp = ptr;
michael@0 300 ptr = 0;
michael@0 301 return tmp;
michael@0 302 }
michael@0 303
michael@0 304 private:
michael@0 305
michael@0 306 // no reason to use these: each scoped_ptr_malloc should have its own object
michael@0 307 template <typename U, typename GP>
michael@0 308 bool operator==(scoped_ptr_malloc<U, GP> const& p) const;
michael@0 309 template <typename U, typename GP>
michael@0 310 bool operator!=(scoped_ptr_malloc<U, GP> const& p) const;
michael@0 311
michael@0 312 static FreeProc const free_;
michael@0 313 };
michael@0 314
michael@0 315 template<typename T, typename FP>
michael@0 316 FP const scoped_ptr_malloc<T,FP>::free_ = FP();
michael@0 317
michael@0 318 template<typename T, typename FP> inline
michael@0 319 void swap(scoped_ptr_malloc<T,FP>& a, scoped_ptr_malloc<T,FP>& b) {
michael@0 320 a.swap(b);
michael@0 321 }
michael@0 322
michael@0 323 template<typename T, typename FP> inline
michael@0 324 bool operator==(T* p, const scoped_ptr_malloc<T,FP>& b) {
michael@0 325 return p == b.get();
michael@0 326 }
michael@0 327
michael@0 328 template<typename T, typename FP> inline
michael@0 329 bool operator!=(T* p, const scoped_ptr_malloc<T,FP>& b) {
michael@0 330 return p != b.get();
michael@0 331 }
michael@0 332
michael@0 333 } // namespace google_breakpad
michael@0 334
michael@0 335 #endif // COMMON_SCOPED_PTR_H_

mercurial