ipc/chromium/src/base/linked_ptr.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4 //
michael@0 5 // A "smart" pointer type with reference tracking. Every pointer to a
michael@0 6 // particular object is kept on a circular linked list. When the last pointer
michael@0 7 // to an object is destroyed or reassigned, the object is deleted.
michael@0 8 //
michael@0 9 // Used properly, this deletes the object when the last reference goes away.
michael@0 10 // There are several caveats:
michael@0 11 // - Like all reference counting schemes, cycles lead to leaks.
michael@0 12 // - Each smart pointer is actually two pointers (8 bytes instead of 4).
michael@0 13 // - Every time a pointer is released, the entire list of pointers to that
michael@0 14 // object is traversed. This class is therefore NOT SUITABLE when there
michael@0 15 // will often be more than two or three pointers to a particular object.
michael@0 16 // - References are only tracked as long as linked_ptr<> objects are copied.
michael@0 17 // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
michael@0 18 // will happen (double deletion).
michael@0 19 //
michael@0 20 // A good use of this class is storing object references in STL containers.
michael@0 21 // You can safely put linked_ptr<> in a vector<>.
michael@0 22 // Other uses may not be as good.
michael@0 23 //
michael@0 24 // Note: If you use an incomplete type with linked_ptr<>, the class
michael@0 25 // *containing* linked_ptr<> must have a constructor and destructor (even
michael@0 26 // if they do nothing!).
michael@0 27 //
michael@0 28 // Thread Safety:
michael@0 29 // A linked_ptr is NOT thread safe. Copying a linked_ptr object is
michael@0 30 // effectively a read-write operation.
michael@0 31 //
michael@0 32 // Alternative: to linked_ptr is shared_ptr, which
michael@0 33 // - is also two pointers in size (8 bytes for 32 bit addresses)
michael@0 34 // - is thread safe for copying and deletion
michael@0 35 // - supports weak_ptrs
michael@0 36
michael@0 37 #ifndef BASE_LINKED_PTR_H_
michael@0 38 #define BASE_LINKED_PTR_H_
michael@0 39
michael@0 40 #include "base/logging.h" // for CHECK macros
michael@0 41
michael@0 42 // This is used internally by all instances of linked_ptr<>. It needs to be
michael@0 43 // a non-template class because different types of linked_ptr<> can refer to
michael@0 44 // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
michael@0 45 // So, it needs to be possible for different types of linked_ptr to participate
michael@0 46 // in the same circular linked list, so we need a single class type here.
michael@0 47 //
michael@0 48 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
michael@0 49 class linked_ptr_internal {
michael@0 50 public:
michael@0 51 // Create a new circle that includes only this instance.
michael@0 52 void join_new() {
michael@0 53 next_ = this;
michael@0 54 }
michael@0 55
michael@0 56 // Join an existing circle.
michael@0 57 void join(linked_ptr_internal const* ptr) {
michael@0 58 next_ = ptr->next_;
michael@0 59 ptr->next_ = this;
michael@0 60 }
michael@0 61
michael@0 62 // Leave whatever circle we're part of. Returns true iff we were the
michael@0 63 // last member of the circle. Once this is done, you can join() another.
michael@0 64 bool depart() {
michael@0 65 if (next_ == this) return true;
michael@0 66 linked_ptr_internal const* p = next_;
michael@0 67 while (p->next_ != this) p = p->next_;
michael@0 68 p->next_ = next_;
michael@0 69 return false;
michael@0 70 }
michael@0 71
michael@0 72 private:
michael@0 73 mutable linked_ptr_internal const* next_;
michael@0 74 };
michael@0 75
michael@0 76 template <typename T>
michael@0 77 class linked_ptr {
michael@0 78 public:
michael@0 79 typedef T element_type;
michael@0 80
michael@0 81 // Take over ownership of a raw pointer. This should happen as soon as
michael@0 82 // possible after the object is created.
michael@0 83 explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
michael@0 84 ~linked_ptr() { depart(); }
michael@0 85
michael@0 86 // Copy an existing linked_ptr<>, adding ourselves to the list of references.
michael@0 87 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
michael@0 88 linked_ptr(linked_ptr const& ptr) { DCHECK_NE(&ptr, this); copy(&ptr); }
michael@0 89
michael@0 90 // Assignment releases the old value and acquires the new.
michael@0 91 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
michael@0 92 depart();
michael@0 93 copy(&ptr);
michael@0 94 return *this;
michael@0 95 }
michael@0 96
michael@0 97 linked_ptr& operator=(linked_ptr const& ptr) {
michael@0 98 if (&ptr != this) {
michael@0 99 depart();
michael@0 100 copy(&ptr);
michael@0 101 }
michael@0 102 return *this;
michael@0 103 }
michael@0 104
michael@0 105 // Smart pointer members.
michael@0 106 void reset(T* ptr = NULL) { depart(); capture(ptr); }
michael@0 107 T* get() const { return value_; }
michael@0 108 T* operator->() const { return value_; }
michael@0 109 T& operator*() const { return *value_; }
michael@0 110 // Release ownership of the pointed object and returns it.
michael@0 111 // Sole ownership by this linked_ptr object is required.
michael@0 112 T* release() {
michael@0 113 bool last = link_.depart();
michael@0 114 CHECK(last);
michael@0 115 T* v = value_;
michael@0 116 value_ = NULL;
michael@0 117 return v;
michael@0 118 }
michael@0 119
michael@0 120 bool operator==(const T* p) const { return value_ == p; }
michael@0 121 bool operator!=(const T* p) const { return value_ != p; }
michael@0 122 template <typename U>
michael@0 123 bool operator==(linked_ptr<U> const& ptr) const {
michael@0 124 return value_ == ptr.get();
michael@0 125 }
michael@0 126 template <typename U>
michael@0 127 bool operator!=(linked_ptr<U> const& ptr) const {
michael@0 128 return value_ != ptr.get();
michael@0 129 }
michael@0 130
michael@0 131 private:
michael@0 132 template <typename U>
michael@0 133 friend class linked_ptr;
michael@0 134
michael@0 135 T* value_;
michael@0 136 linked_ptr_internal link_;
michael@0 137
michael@0 138 void depart() {
michael@0 139 if (link_.depart()) delete value_;
michael@0 140 }
michael@0 141
michael@0 142 void capture(T* ptr) {
michael@0 143 value_ = ptr;
michael@0 144 link_.join_new();
michael@0 145 }
michael@0 146
michael@0 147 template <typename U> void copy(linked_ptr<U> const* ptr) {
michael@0 148 value_ = ptr->get();
michael@0 149 if (value_)
michael@0 150 link_.join(&ptr->link_);
michael@0 151 else
michael@0 152 link_.join_new();
michael@0 153 }
michael@0 154 };
michael@0 155
michael@0 156 template<typename T> inline
michael@0 157 bool operator==(T* ptr, const linked_ptr<T>& x) {
michael@0 158 return ptr == x.get();
michael@0 159 }
michael@0 160
michael@0 161 template<typename T> inline
michael@0 162 bool operator!=(T* ptr, const linked_ptr<T>& x) {
michael@0 163 return ptr != x.get();
michael@0 164 }
michael@0 165
michael@0 166 // A function to convert T* into linked_ptr<T>
michael@0 167 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
michael@0 168 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
michael@0 169 template <typename T>
michael@0 170 linked_ptr<T> make_linked_ptr(T* ptr) {
michael@0 171 return linked_ptr<T>(ptr);
michael@0 172 }
michael@0 173
michael@0 174 #endif // BASE_LINKED_PTR_H_

mercurial