11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2003 Google Inc.
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved.
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met:
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions of source code must retain the above copyright
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer.
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions in binary form must reproduce the above
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution.
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Neither the name of Google Inc. nor the names of its
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission.
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Authors: Dan Egnor (egnor@google.com)
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A "smart" pointer type with reference tracking.  Every pointer to a
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// particular object is kept on a circular linked list.  When the last pointer
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to an object is destroyed or reassigned, the object is deleted.
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Used properly, this deletes the object when the last reference goes away.
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// There are several caveats:
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// - Like all reference counting schemes, cycles lead to leaks.
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// - Each smart pointer is actually two pointers (8 bytes instead of 4).
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// - Every time a pointer is assigned, the entire list of pointers to that
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   object is traversed.  This class is therefore NOT SUITABLE when there
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   will often be more than two or three pointers to a particular object.
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// - References are only tracked as long as linked_ptr<> objects are copied.
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   will happen (double deletion).
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A good use of this class is storing object references in STL containers.
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// You can safely put linked_ptr<> in a vector<>.
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Other uses may not be as good.
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Note: If you use an incomplete type with linked_ptr<>, the class
521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *containing* linked_ptr<> must have a constructor and destructor (even
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// if they do nothing!).
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Bill Gibbons suggested we use something like this.
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Thread Safety:
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   Unlike other linked_ptr implementations, in this implementation
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   a linked_ptr object is thread-safe in the sense that:
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     - it's safe to copy linked_ptr objects concurrently,
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     - it's safe to copy *from* a linked_ptr and read its underlying
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//       raw pointer (e.g. via get()) concurrently, and
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     - it's safe to write to two linked_ptrs that point to the same
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//       shared object concurrently.
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan@google.com): rename this to safe_linked_ptr to avoid
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// confusion with normal linked_ptr.
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdlib.h>
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <assert.h>
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-port.h"
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Protects copying of all linked_ptr objects.
8041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex);
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is used internally by all instances of linked_ptr<>.  It needs to be
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// a non-template class because different types of linked_ptr<> can refer to
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// So, it needs to be possible for different types of linked_ptr to participate
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the same circular linked list, so we need a single class type here.
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DO NOT USE THIS CLASS DIRECTLY YOURSELF.  Use linked_ptr<T>.
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass linked_ptr_internal {
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Create a new circle that includes only this instance.
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void join_new() {
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    next_ = this;
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Many linked_ptr operations may change p.link_ for some linked_ptr
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // variable p in the same circle as this object.  Therefore we need
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to prevent two such operations from occurring concurrently.
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Note that different types of linked_ptr objects can coexist in a
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // linked_ptr<Derived2>).  Therefore we must use a single mutex to
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // protect all linked_ptr objects.  This can create serious
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // contention in production code, but is acceptable in a testing
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // framework.
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Join an existing circle.
108fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  void join(linked_ptr_internal const* ptr)
109fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    MutexLock lock(&g_linked_ptr_mutex);
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    linked_ptr_internal const* p = ptr;
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    while (p->next_ != ptr) p = p->next_;
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    p->next_ = this;
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    next_ = ptr;
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Leave whatever circle we're part of.  Returns true if we were the
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // last member of the circle.  Once this is done, you can join() another.
120fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  bool depart()
121fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    MutexLock lock(&g_linked_ptr_mutex);
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (next_ == this) return true;
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    linked_ptr_internal const* p = next_;
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    while (p->next_ != this) p = p->next_;
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    p->next_ = next_;
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return false;
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  mutable linked_ptr_internal const* next_;
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass linked_ptr {
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef T element_type;
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Take over ownership of a raw pointer.  This should happen as soon as
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // possible after the object is created.
1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ~linked_ptr() { depart(); }
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Copy an existing linked_ptr<>, adding ourselves to the list of references.
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  linked_ptr(linked_ptr const& ptr) {  // NOLINT
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    assert(&ptr != this);
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    copy(&ptr);
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Assignment releases the old value and acquires the new.
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    depart();
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    copy(&ptr);
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return *this;
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  linked_ptr& operator=(linked_ptr const& ptr) {
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (&ptr != this) {
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      depart();
1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      copy(&ptr);
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return *this;
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Smart pointer members.
1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void reset(T* ptr = NULL) {
1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    depart();
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    capture(ptr);
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  T* get() const { return value_; }
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  T* operator->() const { return value_; }
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  T& operator*() const { return *value_; }
1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator==(T* p) const { return value_ == p; }
1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator!=(T* p) const { return value_ != p; }
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <typename U>
1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator==(linked_ptr<U> const& ptr) const {
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return value_ == ptr.get();
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <typename U>
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator!=(linked_ptr<U> const& ptr) const {
1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return value_ != ptr.get();
1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <typename U>
1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  friend class linked_ptr;
1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  T* value_;
1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  linked_ptr_internal link_;
1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void depart() {
1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (link_.depart()) delete value_;
1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void capture(T* ptr) {
1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    value_ = ptr;
2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    link_.join_new();
2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <typename U> void copy(linked_ptr<U> const* ptr) {
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    value_ = ptr->get();
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (value_)
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      link_.join(&ptr->link_);
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else
2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      link_.join_new();
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate<typename T> inline
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool operator==(T* ptr, const linked_ptr<T>& x) {
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return ptr == x.get();
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate<typename T> inline
2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool operator!=(T* ptr, const linked_ptr<T>& x) {
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return ptr != x.get();
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A function to convert T* into linked_ptr<T>
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanialinked_ptr<T> make_linked_ptr(T* ptr) {
2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return linked_ptr<T>(ptr);
2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace testing
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
234