ipc/chromium/src/base/scoped_ptr.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/scoped_ptr.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,380 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +// Scopers help you manage ownership of a pointer, helping you easily manage the
     1.9 +// a pointer within a scope, and automatically destroying the pointer at the
    1.10 +// end of a scope.  There are two main classes you will use, which coorespond
    1.11 +// to the operators new/delete and new[]/delete[].
    1.12 +//
    1.13 +// Example usage (scoped_ptr):
    1.14 +//   {
    1.15 +//     scoped_ptr<Foo> foo(new Foo("wee"));
    1.16 +//   }  // foo goes out of scope, releasing the pointer with it.
    1.17 +//
    1.18 +//   {
    1.19 +//     scoped_ptr<Foo> foo;          // No pointer managed.
    1.20 +//     foo.reset(new Foo("wee"));    // Now a pointer is managed.
    1.21 +//     foo.reset(new Foo("wee2"));   // Foo("wee") was destroyed.
    1.22 +//     foo.reset(new Foo("wee3"));   // Foo("wee2") was destroyed.
    1.23 +//     foo->Method();                // Foo::Method() called.
    1.24 +//     foo.get()->Method();          // Foo::Method() called.
    1.25 +//     SomeFunc(foo.Release());      // SomeFunc takes owernship, foo no longer
    1.26 +//                                   // manages a pointer.
    1.27 +//     foo.reset(new Foo("wee4"));   // foo manages a pointer again.
    1.28 +//     foo.reset();                  // Foo("wee4") destroyed, foo no longer
    1.29 +//                                   // manages a pointer.
    1.30 +//   }  // foo wasn't managing a pointer, so nothing was destroyed.
    1.31 +//
    1.32 +// Example usage (scoped_array):
    1.33 +//   {
    1.34 +//     scoped_array<Foo> foo(new Foo[100]);
    1.35 +//     foo.get()->Method();  // Foo::Method on the 0th element.
    1.36 +//     foo[10].Method();     // Foo::Method on the 10th element.
    1.37 +//   }
    1.38 +
    1.39 +#ifndef BASE_SCOPED_PTR_H_
    1.40 +#define BASE_SCOPED_PTR_H_
    1.41 +
    1.42 +// This is an implementation designed to match the anticipated future TR2
    1.43 +// implementation of the scoped_ptr class, and its closely-related brethren,
    1.44 +// scoped_array, scoped_ptr_malloc.
    1.45 +
    1.46 +#include <assert.h>
    1.47 +#include <stdlib.h>
    1.48 +#include <cstddef>
    1.49 +
    1.50 +// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
    1.51 +// automatically deletes the pointer it holds (if any).
    1.52 +// That is, scoped_ptr<T> owns the T object that it points to.
    1.53 +// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
    1.54 +// Also like T*, scoped_ptr<T> is thread-compatible, and once you
    1.55 +// dereference it, you get the threadsafety guarantees of T.
    1.56 +//
    1.57 +// The size of a scoped_ptr is small:
    1.58 +// sizeof(scoped_ptr<C>) == sizeof(C*)
    1.59 +template <class C>
    1.60 +class scoped_ptr {
    1.61 + public:
    1.62 +
    1.63 +  // The element type
    1.64 +  typedef C element_type;
    1.65 +
    1.66 +  // Constructor.  Defaults to intializing with NULL.
    1.67 +  // There is no way to create an uninitialized scoped_ptr.
    1.68 +  // The input parameter must be allocated with new.
    1.69 +  explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
    1.70 +
    1.71 +  // Destructor.  If there is a C object, delete it.
    1.72 +  // We don't need to test ptr_ == NULL because C++ does that for us.
    1.73 +  ~scoped_ptr() {
    1.74 +    enum { type_must_be_complete = sizeof(C) };
    1.75 +    delete ptr_;
    1.76 +  }
    1.77 +
    1.78 +  // Reset.  Deletes the current owned object, if any.
    1.79 +  // Then takes ownership of a new object, if given.
    1.80 +  // this->reset(this->get()) works.
    1.81 +  void reset(C* p = NULL) {
    1.82 +    if (p != ptr_) {
    1.83 +      enum { type_must_be_complete = sizeof(C) };
    1.84 +      delete ptr_;
    1.85 +      ptr_ = p;
    1.86 +    }
    1.87 +  }
    1.88 +
    1.89 +  // Accessors to get the owned object.
    1.90 +  // operator* and operator-> will assert() if there is no current object.
    1.91 +  C& operator*() const {
    1.92 +    assert(ptr_ != NULL);
    1.93 +    return *ptr_;
    1.94 +  }
    1.95 +  C* operator->() const  {
    1.96 +    assert(ptr_ != NULL);
    1.97 +    return ptr_;
    1.98 +  }
    1.99 +  C* get() const { return ptr_; }
   1.100 +
   1.101 +  // Comparison operators.
   1.102 +  // These return whether two scoped_ptr refer to the same object, not just to
   1.103 +  // two different but equal objects.
   1.104 +  bool operator==(C* p) const { return ptr_ == p; }
   1.105 +  bool operator!=(C* p) const { return ptr_ != p; }
   1.106 +
   1.107 +  // Swap two scoped pointers.
   1.108 +  void swap(scoped_ptr& p2) {
   1.109 +    C* tmp = ptr_;
   1.110 +    ptr_ = p2.ptr_;
   1.111 +    p2.ptr_ = tmp;
   1.112 +  }
   1.113 +
   1.114 +  // Release a pointer.
   1.115 +  // The return value is the current pointer held by this object.
   1.116 +  // If this object holds a NULL pointer, the return value is NULL.
   1.117 +  // After this operation, this object will hold a NULL pointer,
   1.118 +  // and will not own the object any more.
   1.119 +  C* release() {
   1.120 +    C* retVal = ptr_;
   1.121 +    ptr_ = NULL;
   1.122 +    return retVal;
   1.123 +  }
   1.124 +
   1.125 + private:
   1.126 +  C* ptr_;
   1.127 +
   1.128 +  // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't
   1.129 +  // make sense, and if C2 == C, it still doesn't make sense because you should
   1.130 +  // never have the same object owned by two different scoped_ptrs.
   1.131 +  template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
   1.132 +  template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
   1.133 +
   1.134 +  // Disallow evil constructors
   1.135 +  scoped_ptr(const scoped_ptr&);
   1.136 +  void operator=(const scoped_ptr&);
   1.137 +};
   1.138 +
   1.139 +// Free functions
   1.140 +template <class C>
   1.141 +void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) {
   1.142 +  p1.swap(p2);
   1.143 +}
   1.144 +
   1.145 +template <class C>
   1.146 +bool operator==(C* p1, const scoped_ptr<C>& p2) {
   1.147 +  return p1 == p2.get();
   1.148 +}
   1.149 +
   1.150 +template <class C>
   1.151 +bool operator!=(C* p1, const scoped_ptr<C>& p2) {
   1.152 +  return p1 != p2.get();
   1.153 +}
   1.154 +
   1.155 +// scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
   1.156 +// with new [] and the destructor deletes objects with delete [].
   1.157 +//
   1.158 +// As with scoped_ptr<C>, a scoped_array<C> either points to an object
   1.159 +// or is NULL.  A scoped_array<C> owns the object that it points to.
   1.160 +// scoped_array<T> is thread-compatible, and once you index into it,
   1.161 +// the returned objects have only the threadsafety guarantees of T.
   1.162 +//
   1.163 +// Size: sizeof(scoped_array<C>) == sizeof(C*)
   1.164 +template <class C>
   1.165 +class scoped_array {
   1.166 + public:
   1.167 +
   1.168 +  // The element type
   1.169 +  typedef C element_type;
   1.170 +
   1.171 +  // Constructor.  Defaults to intializing with NULL.
   1.172 +  // There is no way to create an uninitialized scoped_array.
   1.173 +  // The input parameter must be allocated with new [].
   1.174 +  explicit scoped_array(C* p = NULL) : array_(p) { }
   1.175 +
   1.176 +  // Destructor.  If there is a C object, delete it.
   1.177 +  // We don't need to test ptr_ == NULL because C++ does that for us.
   1.178 +  ~scoped_array() {
   1.179 +    enum { type_must_be_complete = sizeof(C) };
   1.180 +    delete[] array_;
   1.181 +  }
   1.182 +
   1.183 +  // Reset.  Deletes the current owned object, if any.
   1.184 +  // Then takes ownership of a new object, if given.
   1.185 +  // this->reset(this->get()) works.
   1.186 +  void reset(C* p = NULL) {
   1.187 +    if (p != array_) {
   1.188 +      enum { type_must_be_complete = sizeof(C) };
   1.189 +      delete[] array_;
   1.190 +      array_ = p;
   1.191 +    }
   1.192 +  }
   1.193 +
   1.194 +  // Get one element of the current object.
   1.195 +  // Will assert() if there is no current object, or index i is negative.
   1.196 +  C& operator[](std::ptrdiff_t i) const {
   1.197 +    assert(i >= 0);
   1.198 +    assert(array_ != NULL);
   1.199 +    return array_[i];
   1.200 +  }
   1.201 +
   1.202 +  // Get a pointer to the zeroth element of the current object.
   1.203 +  // If there is no current object, return NULL.
   1.204 +  C* get() const {
   1.205 +    return array_;
   1.206 +  }
   1.207 +
   1.208 +  // Comparison operators.
   1.209 +  // These return whether two scoped_array refer to the same object, not just to
   1.210 +  // two different but equal objects.
   1.211 +  bool operator==(C* p) const { return array_ == p; }
   1.212 +  bool operator!=(C* p) const { return array_ != p; }
   1.213 +
   1.214 +  // Swap two scoped arrays.
   1.215 +  void swap(scoped_array& p2) {
   1.216 +    C* tmp = array_;
   1.217 +    array_ = p2.array_;
   1.218 +    p2.array_ = tmp;
   1.219 +  }
   1.220 +
   1.221 +  // Release an array.
   1.222 +  // The return value is the current pointer held by this object.
   1.223 +  // If this object holds a NULL pointer, the return value is NULL.
   1.224 +  // After this operation, this object will hold a NULL pointer,
   1.225 +  // and will not own the object any more.
   1.226 +  C* release() {
   1.227 +    C* retVal = array_;
   1.228 +    array_ = NULL;
   1.229 +    return retVal;
   1.230 +  }
   1.231 +
   1.232 + private:
   1.233 +  C* array_;
   1.234 +
   1.235 +  // Forbid comparison of different scoped_array types.
   1.236 +  template <class C2> bool operator==(scoped_array<C2> const& p2) const;
   1.237 +  template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
   1.238 +
   1.239 +  // Disallow evil constructors
   1.240 +  scoped_array(const scoped_array&);
   1.241 +  void operator=(const scoped_array&);
   1.242 +};
   1.243 +
   1.244 +// Free functions
   1.245 +template <class C>
   1.246 +void swap(scoped_array<C>& p1, scoped_array<C>& p2) {
   1.247 +  p1.swap(p2);
   1.248 +}
   1.249 +
   1.250 +template <class C>
   1.251 +bool operator==(C* p1, const scoped_array<C>& p2) {
   1.252 +  return p1 == p2.get();
   1.253 +}
   1.254 +
   1.255 +template <class C>
   1.256 +bool operator!=(C* p1, const scoped_array<C>& p2) {
   1.257 +  return p1 != p2.get();
   1.258 +}
   1.259 +
   1.260 +// This class wraps the c library function free() in a class that can be
   1.261 +// passed as a template argument to scoped_ptr_malloc below.
   1.262 +class ScopedPtrMallocFree {
   1.263 + public:
   1.264 +  inline void operator()(void* x) const {
   1.265 +    free(x);
   1.266 +  }
   1.267 +};
   1.268 +
   1.269 +// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
   1.270 +// second template argument, the functor used to free the object.
   1.271 +
   1.272 +template<class C, class FreeProc = ScopedPtrMallocFree>
   1.273 +class scoped_ptr_malloc {
   1.274 + public:
   1.275 +
   1.276 +  // The element type
   1.277 +  typedef C element_type;
   1.278 +
   1.279 +  // Constructor.  Defaults to intializing with NULL.
   1.280 +  // There is no way to create an uninitialized scoped_ptr.
   1.281 +  // The input parameter must be allocated with an allocator that matches the
   1.282 +  // Free functor.  For the default Free functor, this is malloc, calloc, or
   1.283 +  // realloc.
   1.284 +  explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {}
   1.285 +
   1.286 +  // Destructor.  If there is a C object, call the Free functor.
   1.287 +  ~scoped_ptr_malloc() {
   1.288 +    free_(ptr_);
   1.289 +  }
   1.290 +
   1.291 +  // Reset.  Calls the Free functor on the current owned object, if any.
   1.292 +  // Then takes ownership of a new object, if given.
   1.293 +  // this->reset(this->get()) works.
   1.294 +  void reset(C* p = NULL) {
   1.295 +    if (ptr_ != p) {
   1.296 +      free_(ptr_);
   1.297 +      ptr_ = p;
   1.298 +    }
   1.299 +  }
   1.300 +
   1.301 +  // Get the current object.
   1.302 +  // operator* and operator-> will cause an assert() failure if there is
   1.303 +  // no current object.
   1.304 +  C& operator*() const {
   1.305 +    assert(ptr_ != NULL);
   1.306 +    return *ptr_;
   1.307 +  }
   1.308 +
   1.309 +  C* operator->() const {
   1.310 +    assert(ptr_ != NULL);
   1.311 +    return ptr_;
   1.312 +  }
   1.313 +
   1.314 +  C* get() const {
   1.315 +    return ptr_;
   1.316 +  }
   1.317 +
   1.318 +  // Comparison operators.
   1.319 +  // These return whether a scoped_ptr_malloc and a plain pointer refer
   1.320 +  // to the same object, not just to two different but equal objects.
   1.321 +  // For compatibility wwith the boost-derived implementation, these
   1.322 +  // take non-const arguments.
   1.323 +  bool operator==(C* p) const {
   1.324 +    return ptr_ == p;
   1.325 +  }
   1.326 +
   1.327 +  bool operator!=(C* p) const {
   1.328 +    return ptr_ != p;
   1.329 +  }
   1.330 +
   1.331 +  // Swap two scoped pointers.
   1.332 +  void swap(scoped_ptr_malloc & b) {
   1.333 +    C* tmp = b.ptr_;
   1.334 +    b.ptr_ = ptr_;
   1.335 +    ptr_ = tmp;
   1.336 +  }
   1.337 +
   1.338 +  // Release a pointer.
   1.339 +  // The return value is the current pointer held by this object.
   1.340 +  // If this object holds a NULL pointer, the return value is NULL.
   1.341 +  // After this operation, this object will hold a NULL pointer,
   1.342 +  // and will not own the object any more.
   1.343 +  C* release() {
   1.344 +    C* tmp = ptr_;
   1.345 +    ptr_ = NULL;
   1.346 +    return tmp;
   1.347 +  }
   1.348 +
   1.349 + private:
   1.350 +  C* ptr_;
   1.351 +
   1.352 +  // no reason to use these: each scoped_ptr_malloc should have its own object
   1.353 +  template <class C2, class GP>
   1.354 +  bool operator==(scoped_ptr_malloc<C2, GP> const& p) const;
   1.355 +  template <class C2, class GP>
   1.356 +  bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const;
   1.357 +
   1.358 +  static FreeProc const free_;
   1.359 +
   1.360 +  // Disallow evil constructors
   1.361 +  scoped_ptr_malloc(const scoped_ptr_malloc&);
   1.362 +  void operator=(const scoped_ptr_malloc&);
   1.363 +};
   1.364 +
   1.365 +template<class C, class FP>
   1.366 +FP const scoped_ptr_malloc<C, FP>::free_ = FP();
   1.367 +
   1.368 +template<class C, class FP> inline
   1.369 +void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) {
   1.370 +  a.swap(b);
   1.371 +}
   1.372 +
   1.373 +template<class C, class FP> inline
   1.374 +bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
   1.375 +  return p == b.get();
   1.376 +}
   1.377 +
   1.378 +template<class C, class FP> inline
   1.379 +bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
   1.380 +  return p != b.get();
   1.381 +}
   1.382 +
   1.383 +#endif  // BASE_SCOPED_PTR_H_

mercurial