ipc/chromium/src/base/linked_ptr.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/linked_ptr.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     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 +// A "smart" pointer type with reference tracking.  Every pointer to a
     1.9 +// particular object is kept on a circular linked list.  When the last pointer
    1.10 +// to an object is destroyed or reassigned, the object is deleted.
    1.11 +//
    1.12 +// Used properly, this deletes the object when the last reference goes away.
    1.13 +// There are several caveats:
    1.14 +// - Like all reference counting schemes, cycles lead to leaks.
    1.15 +// - Each smart pointer is actually two pointers (8 bytes instead of 4).
    1.16 +// - Every time a pointer is released, the entire list of pointers to that
    1.17 +//   object is traversed.  This class is therefore NOT SUITABLE when there
    1.18 +//   will often be more than two or three pointers to a particular object.
    1.19 +// - References are only tracked as long as linked_ptr<> objects are copied.
    1.20 +//   If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
    1.21 +//   will happen (double deletion).
    1.22 +//
    1.23 +// A good use of this class is storing object references in STL containers.
    1.24 +// You can safely put linked_ptr<> in a vector<>.
    1.25 +// Other uses may not be as good.
    1.26 +//
    1.27 +// Note: If you use an incomplete type with linked_ptr<>, the class
    1.28 +// *containing* linked_ptr<> must have a constructor and destructor (even
    1.29 +// if they do nothing!).
    1.30 +//
    1.31 +// Thread Safety:
    1.32 +//   A linked_ptr is NOT thread safe. Copying a linked_ptr object is
    1.33 +//   effectively a read-write operation.
    1.34 +//
    1.35 +// Alternative: to linked_ptr is shared_ptr, which
    1.36 +//  - is also two pointers in size (8 bytes for 32 bit addresses)
    1.37 +//  - is thread safe for copying and deletion
    1.38 +//  - supports weak_ptrs
    1.39 +
    1.40 +#ifndef BASE_LINKED_PTR_H_
    1.41 +#define BASE_LINKED_PTR_H_
    1.42 +
    1.43 +#include "base/logging.h"  // for CHECK macros
    1.44 +
    1.45 +// This is used internally by all instances of linked_ptr<>.  It needs to be
    1.46 +// a non-template class because different types of linked_ptr<> can refer to
    1.47 +// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
    1.48 +// So, it needs to be possible for different types of linked_ptr to participate
    1.49 +// in the same circular linked list, so we need a single class type here.
    1.50 +//
    1.51 +// DO NOT USE THIS CLASS DIRECTLY YOURSELF.  Use linked_ptr<T>.
    1.52 +class linked_ptr_internal {
    1.53 + public:
    1.54 +  // Create a new circle that includes only this instance.
    1.55 +  void join_new() {
    1.56 +    next_ = this;
    1.57 +  }
    1.58 +
    1.59 +  // Join an existing circle.
    1.60 +  void join(linked_ptr_internal const* ptr) {
    1.61 +    next_ = ptr->next_;
    1.62 +    ptr->next_ = this;
    1.63 +  }
    1.64 +
    1.65 +  // Leave whatever circle we're part of.  Returns true iff we were the
    1.66 +  // last member of the circle.  Once this is done, you can join() another.
    1.67 +  bool depart() {
    1.68 +    if (next_ == this) return true;
    1.69 +    linked_ptr_internal const* p = next_;
    1.70 +    while (p->next_ != this) p = p->next_;
    1.71 +    p->next_ = next_;
    1.72 +    return false;
    1.73 +  }
    1.74 +
    1.75 + private:
    1.76 +  mutable linked_ptr_internal const* next_;
    1.77 +};
    1.78 +
    1.79 +template <typename T>
    1.80 +class linked_ptr {
    1.81 + public:
    1.82 +  typedef T element_type;
    1.83 +
    1.84 +  // Take over ownership of a raw pointer.  This should happen as soon as
    1.85 +  // possible after the object is created.
    1.86 +  explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
    1.87 +  ~linked_ptr() { depart(); }
    1.88 +
    1.89 +  // Copy an existing linked_ptr<>, adding ourselves to the list of references.
    1.90 +  template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
    1.91 +  linked_ptr(linked_ptr const& ptr) { DCHECK_NE(&ptr, this); copy(&ptr); }
    1.92 +
    1.93 +  // Assignment releases the old value and acquires the new.
    1.94 +  template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
    1.95 +    depart();
    1.96 +    copy(&ptr);
    1.97 +    return *this;
    1.98 +  }
    1.99 +
   1.100 +  linked_ptr& operator=(linked_ptr const& ptr) {
   1.101 +    if (&ptr != this) {
   1.102 +      depart();
   1.103 +      copy(&ptr);
   1.104 +    }
   1.105 +    return *this;
   1.106 +  }
   1.107 +
   1.108 +  // Smart pointer members.
   1.109 +  void reset(T* ptr = NULL) { depart(); capture(ptr); }
   1.110 +  T* get() const { return value_; }
   1.111 +  T* operator->() const { return value_; }
   1.112 +  T& operator*() const { return *value_; }
   1.113 +  // Release ownership of the pointed object and returns it.
   1.114 +  // Sole ownership by this linked_ptr object is required.
   1.115 +  T* release() {
   1.116 +    bool last = link_.depart();
   1.117 +    CHECK(last);
   1.118 +    T* v = value_;
   1.119 +    value_ = NULL;
   1.120 +    return v;
   1.121 +  }
   1.122 +
   1.123 +  bool operator==(const T* p) const { return value_ == p; }
   1.124 +  bool operator!=(const T* p) const { return value_ != p; }
   1.125 +  template <typename U>
   1.126 +  bool operator==(linked_ptr<U> const& ptr) const {
   1.127 +    return value_ == ptr.get();
   1.128 +  }
   1.129 +  template <typename U>
   1.130 +  bool operator!=(linked_ptr<U> const& ptr) const {
   1.131 +    return value_ != ptr.get();
   1.132 +  }
   1.133 +
   1.134 + private:
   1.135 +  template <typename U>
   1.136 +  friend class linked_ptr;
   1.137 +
   1.138 +  T* value_;
   1.139 +  linked_ptr_internal link_;
   1.140 +
   1.141 +  void depart() {
   1.142 +    if (link_.depart()) delete value_;
   1.143 +  }
   1.144 +
   1.145 +  void capture(T* ptr) {
   1.146 +    value_ = ptr;
   1.147 +    link_.join_new();
   1.148 +  }
   1.149 +
   1.150 +  template <typename U> void copy(linked_ptr<U> const* ptr) {
   1.151 +    value_ = ptr->get();
   1.152 +    if (value_)
   1.153 +      link_.join(&ptr->link_);
   1.154 +    else
   1.155 +      link_.join_new();
   1.156 +  }
   1.157 +};
   1.158 +
   1.159 +template<typename T> inline
   1.160 +bool operator==(T* ptr, const linked_ptr<T>& x) {
   1.161 +  return ptr == x.get();
   1.162 +}
   1.163 +
   1.164 +template<typename T> inline
   1.165 +bool operator!=(T* ptr, const linked_ptr<T>& x) {
   1.166 +  return ptr != x.get();
   1.167 +}
   1.168 +
   1.169 +// A function to convert T* into linked_ptr<T>
   1.170 +// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
   1.171 +// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
   1.172 +template <typename T>
   1.173 +linked_ptr<T> make_linked_ptr(T* ptr) {
   1.174 +  return linked_ptr<T>(ptr);
   1.175 +}
   1.176 +
   1.177 +#endif  // BASE_LINKED_PTR_H_

mercurial