1ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// Copyright 2003 Google Inc.
2ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// All rights reserved.
3ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
4ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// Redistribution and use in source and binary forms, with or without
5ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// modification, are permitted provided that the following conditions are
6ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// met:
7ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
8ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//     * Redistributions of source code must retain the above copyright
9ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// notice, this list of conditions and the following disclaimer.
10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//     * Redistributions in binary form must reproduce the above
11ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// copyright notice, this list of conditions and the following disclaimer
12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// in the documentation and/or other materials provided with the
1391037db265ecdd914a26e056cf69207b4f50924ehkuang// distribution.
14ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//     * Neither the name of Google Inc. nor the names of its
15ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// contributors may be used to endorse or promote products derived from
16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// this software without specific prior written permission.
17ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
18ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
206ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
236ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
246ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
266ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
276ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
286ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
30ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// Authors: Dan Egnor (egnor@google.com)
31ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
321184aebb761cbeac9124c37189a80a1a58f04b6bhkuang// A "smart" pointer type with reference tracking.  Every pointer to a
331184aebb761cbeac9124c37189a80a1a58f04b6bhkuang// particular object is kept on a circular linked list.  When the last pointer
346ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// to an object is destroyed or reassigned, the object is deleted.
355ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang//
365ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang// Used properly, this deletes the object when the last reference goes away.
375ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang// There are several caveats:
385ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang// - Like all reference counting schemes, cycles lead to leaks.
39ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// - Each smart pointer is actually two pointers (8 bytes instead of 4).
40ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// - Every time a pointer is assigned, the entire list of pointers to that
41ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//   object is traversed.  This class is therefore NOT SUITABLE when there
426ac915abcdb404a00d927fe6308a47fcf09d9519hkuang//   will often be more than two or three pointers to a particular object.
436ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// - References are only tracked as long as linked_ptr<> objects are copied.
446ac915abcdb404a00d927fe6308a47fcf09d9519hkuang//   If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
456ac915abcdb404a00d927fe6308a47fcf09d9519hkuang//   will happen (double deletion).
46ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
476ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// A good use of this class is storing object references in STL containers.
48ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// You can safely put linked_ptr<> in a vector<>.
496ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// Other uses may not be as good.
506ac915abcdb404a00d927fe6308a47fcf09d9519hkuang//
51ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// Note: If you use an incomplete type with linked_ptr<>, the class
526ac915abcdb404a00d927fe6308a47fcf09d9519hkuang// *containing* linked_ptr<> must have a constructor and destructor (even
53ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// if they do nothing!).
54ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang//
55a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// Bill Gibbons suggested we use something like this.
56a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//
57a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// Thread Safety:
58a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//   Unlike other linked_ptr implementations, in this implementation
59a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//   a linked_ptr object is thread-safe in the sense that:
60a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//     - it's safe to copy linked_ptr objects concurrently,
61a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//     - it's safe to copy *from* a linked_ptr and read its underlying
62a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//       raw pointer (e.g. via get()) concurrently, and
63a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//     - it's safe to write to two linked_ptrs that point to the same
64a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//       shared object concurrently.
65a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// TODO(wan@google.com): rename this to safe_linked_ptr to avoid
66a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// confusion with normal linked_ptr.
67a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
68a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
69a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
70a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
71a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian#include <stdlib.h>
72a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian#include <assert.h>
73a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
74a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian#include "gtest/internal/gtest-port.h"
75a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
76a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramaniannamespace testing {
77a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramaniannamespace internal {
78a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
79a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// Protects copying of all linked_ptr objects.
80a72801d7d92ababb50eecf27a36bd222d031d2feVignesh VenkatasubramanianGTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex);
81a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
82a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// This is used internally by all instances of linked_ptr<>.  It needs to be
83a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// a non-template class because different types of linked_ptr<> can refer to
84a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
85a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// So, it needs to be possible for different types of linked_ptr to participate
86a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// in the same circular linked list, so we need a single class type here.
87a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian//
88a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian// DO NOT USE THIS CLASS DIRECTLY YOURSELF.  Use linked_ptr<T>.
89a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanianclass linked_ptr_internal {
90a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian public:
91a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // Create a new circle that includes only this instance.
92a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  void join_new() {
93a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian    next_ = this;
94a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  }
95a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
96a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // Many linked_ptr operations may change p.link_ for some linked_ptr
97a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // variable p in the same circle as this object.  Therefore we need
98a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // to prevent two such operations from occurring concurrently.
99a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  //
100a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // Note that different types of linked_ptr objects can coexist in a
101a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and
102a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // linked_ptr<Derived2>).  Therefore we must use a single mutex to
103a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // protect all linked_ptr objects.  This can create serious
104a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // contention in production code, but is acceptable in a testing
105a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // framework.
106a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian
107a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  // Join an existing circle.
1086ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  // L < g_linked_ptr_mutex
1096ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  void join(linked_ptr_internal const* ptr) {
1106ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    MutexLock lock(&g_linked_ptr_mutex);
1116ac915abcdb404a00d927fe6308a47fcf09d9519hkuang
1126ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    linked_ptr_internal const* p = ptr;
1136ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    while (p->next_ != ptr) p = p->next_;
114a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian    p->next_ = this;
115a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian    next_ = ptr;
1169b35249446b07f40ac5fcc3205f2c048616efacchkuang  }
1179b35249446b07f40ac5fcc3205f2c048616efacchkuang
1189b35249446b07f40ac5fcc3205f2c048616efacchkuang  // Leave whatever circle we're part of.  Returns true if we were the
1199b35249446b07f40ac5fcc3205f2c048616efacchkuang  // last member of the circle.  Once this is done, you can join() another.
1209b35249446b07f40ac5fcc3205f2c048616efacchkuang  // L < g_linked_ptr_mutex
1219b35249446b07f40ac5fcc3205f2c048616efacchkuang  bool depart() {
1229b35249446b07f40ac5fcc3205f2c048616efacchkuang    MutexLock lock(&g_linked_ptr_mutex);
123b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian
1249b35249446b07f40ac5fcc3205f2c048616efacchkuang    if (next_ == this) return true;
1259b35249446b07f40ac5fcc3205f2c048616efacchkuang    linked_ptr_internal const* p = next_;
1269b35249446b07f40ac5fcc3205f2c048616efacchkuang    while (p->next_ != this) p = p->next_;
1279b35249446b07f40ac5fcc3205f2c048616efacchkuang    p->next_ = next_;
128a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian    return false;
1296ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  }
1309b35249446b07f40ac5fcc3205f2c048616efacchkuang
1319b35249446b07f40ac5fcc3205f2c048616efacchkuang private:
1329b35249446b07f40ac5fcc3205f2c048616efacchkuang  mutable linked_ptr_internal const* next_;
1339b35249446b07f40ac5fcc3205f2c048616efacchkuang};
1349b35249446b07f40ac5fcc3205f2c048616efacchkuang
1359b35249446b07f40ac5fcc3205f2c048616efacchkuangtemplate <typename T>
136a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanianclass linked_ptr {
1379b35249446b07f40ac5fcc3205f2c048616efacchkuang public:
1389b35249446b07f40ac5fcc3205f2c048616efacchkuang  typedef T element_type;
1399b35249446b07f40ac5fcc3205f2c048616efacchkuang
1406ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  // Take over ownership of a raw pointer.  This should happen as soon as
1416ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  // possible after the object is created.
1426ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
1436ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  ~linked_ptr() { depart(); }
1446ac915abcdb404a00d927fe6308a47fcf09d9519hkuang
1456ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  // Copy an existing linked_ptr<>, adding ourselves to the list of references.
1466ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
1476ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  linked_ptr(linked_ptr const& ptr) {  // NOLINT
1486ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    assert(&ptr != this);
1496ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    copy(&ptr);
1506ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  }
1516ac915abcdb404a00d927fe6308a47fcf09d9519hkuang
1526ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  // Assignment releases the old value and acquires the new.
1536ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
1546ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    depart();
1559b35249446b07f40ac5fcc3205f2c048616efacchkuang    copy(&ptr);
1569b35249446b07f40ac5fcc3205f2c048616efacchkuang    return *this;
1579b35249446b07f40ac5fcc3205f2c048616efacchkuang  }
1589b35249446b07f40ac5fcc3205f2c048616efacchkuang
1599b35249446b07f40ac5fcc3205f2c048616efacchkuang  linked_ptr& operator=(linked_ptr const& ptr) {
1609b35249446b07f40ac5fcc3205f2c048616efacchkuang    if (&ptr != this) {
1619b35249446b07f40ac5fcc3205f2c048616efacchkuang      depart();
1621184aebb761cbeac9124c37189a80a1a58f04b6bhkuang      copy(&ptr);
1631184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    }
1641184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    return *this;
1651184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  }
1661184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
1676ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  // Smart pointer members.
168ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  void reset(T* ptr = NULL) {
1691184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    depart();
170ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    capture(ptr);
171b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  }
172b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  T* get() const { return value_; }
173b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  T* operator->() const { return value_; }
174b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  T& operator*() const { return *value_; }
175ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
176ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  bool operator==(T* p) const { return value_ == p; }
177ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  bool operator!=(T* p) const { return value_ != p; }
178b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  template <typename U>
179b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  bool operator==(linked_ptr<U> const& ptr) const {
180ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    return value_ == ptr.get();
181b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  }
1821184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  template <typename U>
183b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian  bool operator!=(linked_ptr<U> const& ptr) const {
184ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    return value_ != ptr.get();
185ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
1861184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
187b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian private:
188ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  template <typename U>
189ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  friend class linked_ptr;
1901184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
191ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  T* value_;
1926ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  linked_ptr_internal link_;
1931184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
1941184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  void depart() {
1951184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    if (link_.depart()) delete value_;
196ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
1971184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
198a72801d7d92ababb50eecf27a36bd222d031d2feVignesh Venkatasubramanian  void capture(T* ptr) {
1991184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    value_ = ptr;
200ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    link_.join_new();
201ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
2026ac915abcdb404a00d927fe6308a47fcf09d9519hkuang
2036ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  template <typename U> void copy(linked_ptr<U> const* ptr) {
2046ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    value_ = ptr->get();
2056ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    if (value_)
2066ac915abcdb404a00d927fe6308a47fcf09d9519hkuang      link_.join(&ptr->link_);
2076ac915abcdb404a00d927fe6308a47fcf09d9519hkuang    else
2086ac915abcdb404a00d927fe6308a47fcf09d9519hkuang      link_.join_new();
2096ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  }
2106ac915abcdb404a00d927fe6308a47fcf09d9519hkuang};
2116ac915abcdb404a00d927fe6308a47fcf09d9519hkuang
2126ac915abcdb404a00d927fe6308a47fcf09d9519hkuangtemplate<typename T> inline
2136ac915abcdb404a00d927fe6308a47fcf09d9519hkuangbool operator==(T* ptr, const linked_ptr<T>& x) {
2146ac915abcdb404a00d927fe6308a47fcf09d9519hkuang  return ptr == x.get();
215ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
216ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
217ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangtemplate<typename T> inline
2181184aebb761cbeac9124c37189a80a1a58f04b6bhkuangbool operator!=(T* ptr, const linked_ptr<T>& x) {
219ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  return ptr != x.get();
220ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
221ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
2221184aebb761cbeac9124c37189a80a1a58f04b6bhkuang// A function to convert T* into linked_ptr<T>
2231184aebb761cbeac9124c37189a80a1a58f04b6bhkuang// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
224b08e2e23eec181e9951df33cd704ac294c5407b6Vignesh Venkatasubramanian// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
225ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangtemplate <typename T>
226ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuanglinked_ptr<T> make_linked_ptr(T* ptr) {
227ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  return linked_ptr<T>(ptr);
2281184aebb761cbeac9124c37189a80a1a58f04b6bhkuang}
2291184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
230ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}  // namespace internal
231ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}  // namespace testing
232ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
23391037db265ecdd914a26e056cf69207b4f50924ehkuang#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
2341184aebb761cbeac9124c37189a80a1a58f04b6bhkuang