Blame googletest/include/gtest/internal/gtest-linked_ptr.h

Packit bd1cd8
// Copyright 2003 Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Authors: Dan Egnor (egnor@google.com)
Packit bd1cd8
//
Packit bd1cd8
// A "smart" pointer type with reference tracking.  Every pointer to a
Packit bd1cd8
// particular object is kept on a circular linked list.  When the last pointer
Packit bd1cd8
// to an object is destroyed or reassigned, the object is deleted.
Packit bd1cd8
//
Packit bd1cd8
// Used properly, this deletes the object when the last reference goes away.
Packit bd1cd8
// There are several caveats:
Packit bd1cd8
// - Like all reference counting schemes, cycles lead to leaks.
Packit bd1cd8
// - Each smart pointer is actually two pointers (8 bytes instead of 4).
Packit bd1cd8
// - Every time a pointer is assigned, the entire list of pointers to that
Packit bd1cd8
//   object is traversed.  This class is therefore NOT SUITABLE when there
Packit bd1cd8
//   will often be more than two or three pointers to a particular object.
Packit bd1cd8
// - References are only tracked as long as linked_ptr<> objects are copied.
Packit bd1cd8
//   If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
Packit bd1cd8
//   will happen (double deletion).
Packit bd1cd8
//
Packit bd1cd8
// A good use of this class is storing object references in STL containers.
Packit bd1cd8
// You can safely put linked_ptr<> in a vector<>.
Packit bd1cd8
// Other uses may not be as good.
Packit bd1cd8
//
Packit bd1cd8
// Note: If you use an incomplete type with linked_ptr<>, the class
Packit bd1cd8
// *containing* linked_ptr<> must have a constructor and destructor (even
Packit bd1cd8
// if they do nothing!).
Packit bd1cd8
//
Packit bd1cd8
// Bill Gibbons suggested we use something like this.
Packit bd1cd8
//
Packit bd1cd8
// Thread Safety:
Packit bd1cd8
//   Unlike other linked_ptr implementations, in this implementation
Packit bd1cd8
//   a linked_ptr object is thread-safe in the sense that:
Packit bd1cd8
//     - it's safe to copy linked_ptr objects concurrently,
Packit bd1cd8
//     - it's safe to copy *from* a linked_ptr and read its underlying
Packit bd1cd8
//       raw pointer (e.g. via get()) concurrently, and
Packit bd1cd8
//     - it's safe to write to two linked_ptrs that point to the same
Packit bd1cd8
//       shared object concurrently.
Packit bd1cd8
// TODO(wan@google.com): rename this to safe_linked_ptr to avoid
Packit bd1cd8
// confusion with normal linked_ptr.
Packit bd1cd8
Packit bd1cd8
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
Packit bd1cd8
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
Packit bd1cd8
Packit bd1cd8
#include <stdlib.h>
Packit bd1cd8
#include <assert.h>
Packit bd1cd8
Packit bd1cd8
#include "gtest/internal/gtest-port.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Protects copying of all linked_ptr objects.
Packit bd1cd8
GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex);
Packit bd1cd8
Packit bd1cd8
// This is used internally by all instances of linked_ptr<>.  It needs to be
Packit bd1cd8
// a non-template class because different types of linked_ptr<> can refer to
Packit bd1cd8
// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
Packit bd1cd8
// So, it needs to be possible for different types of linked_ptr to participate
Packit bd1cd8
// in the same circular linked list, so we need a single class type here.
Packit bd1cd8
//
Packit bd1cd8
// DO NOT USE THIS CLASS DIRECTLY YOURSELF.  Use linked_ptr<T>.
Packit bd1cd8
class linked_ptr_internal {
Packit bd1cd8
 public:
Packit bd1cd8
  // Create a new circle that includes only this instance.
Packit bd1cd8
  void join_new() {
Packit bd1cd8
    next_ = this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Many linked_ptr operations may change p.link_ for some linked_ptr
Packit bd1cd8
  // variable p in the same circle as this object.  Therefore we need
Packit bd1cd8
  // to prevent two such operations from occurring concurrently.
Packit bd1cd8
  //
Packit bd1cd8
  // Note that different types of linked_ptr objects can coexist in a
Packit bd1cd8
  // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and
Packit bd1cd8
  // linked_ptr<Derived2>).  Therefore we must use a single mutex to
Packit bd1cd8
  // protect all linked_ptr objects.  This can create serious
Packit bd1cd8
  // contention in production code, but is acceptable in a testing
Packit bd1cd8
  // framework.
Packit bd1cd8
Packit bd1cd8
  // Join an existing circle.
Packit bd1cd8
  void join(linked_ptr_internal const* ptr)
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
Packit bd1cd8
    MutexLock lock(&g_linked_ptr_mutex);
Packit bd1cd8
Packit bd1cd8
    linked_ptr_internal const* p = ptr;
Packit bd1cd8
    while (p->next_ != ptr) {
Packit bd1cd8
      assert(p->next_ != this &&
Packit bd1cd8
             "Trying to join() a linked ring we are already in. "
Packit bd1cd8
             "Is GMock thread safety enabled?");
Packit bd1cd8
      p = p->next_;
Packit bd1cd8
    }
Packit bd1cd8
    p->next_ = this;
Packit bd1cd8
    next_ = ptr;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Leave whatever circle we're part of.  Returns true if we were the
Packit bd1cd8
  // last member of the circle.  Once this is done, you can join() another.
Packit bd1cd8
  bool depart()
Packit bd1cd8
      GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
Packit bd1cd8
    MutexLock lock(&g_linked_ptr_mutex);
Packit bd1cd8
Packit bd1cd8
    if (next_ == this) return true;
Packit bd1cd8
    linked_ptr_internal const* p = next_;
Packit bd1cd8
    while (p->next_ != this) {
Packit bd1cd8
      assert(p->next_ != next_ &&
Packit bd1cd8
             "Trying to depart() a linked ring we are not in. "
Packit bd1cd8
             "Is GMock thread safety enabled?");
Packit bd1cd8
      p = p->next_;
Packit bd1cd8
    }
Packit bd1cd8
    p->next_ = next_;
Packit bd1cd8
    return false;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  mutable linked_ptr_internal const* next_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
template <typename T>
Packit bd1cd8
class linked_ptr {
Packit bd1cd8
 public:
Packit bd1cd8
  typedef T element_type;
Packit bd1cd8
Packit bd1cd8
  // Take over ownership of a raw pointer.  This should happen as soon as
Packit bd1cd8
  // possible after the object is created.
Packit bd1cd8
  explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
Packit bd1cd8
  ~linked_ptr() { depart(); }
Packit bd1cd8
Packit bd1cd8
  // Copy an existing linked_ptr<>, adding ourselves to the list of references.
Packit bd1cd8
  template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
Packit bd1cd8
  linked_ptr(linked_ptr const& ptr) {  // NOLINT
Packit bd1cd8
    assert(&ptr != this);
Packit bd1cd8
    copy(&ptr);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Assignment releases the old value and acquires the new.
Packit bd1cd8
  template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
Packit bd1cd8
    depart();
Packit bd1cd8
    copy(&ptr);
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  linked_ptr& operator=(linked_ptr const& ptr) {
Packit bd1cd8
    if (&ptr != this) {
Packit bd1cd8
      depart();
Packit bd1cd8
      copy(&ptr);
Packit bd1cd8
    }
Packit bd1cd8
    return *this;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Smart pointer members.
Packit bd1cd8
  void reset(T* ptr = NULL) {
Packit bd1cd8
    depart();
Packit bd1cd8
    capture(ptr);
Packit bd1cd8
  }
Packit bd1cd8
  T* get() const { return value_; }
Packit bd1cd8
  T* operator->() const { return value_; }
Packit bd1cd8
  T& operator*() const { return *value_; }
Packit bd1cd8
Packit bd1cd8
  bool operator==(T* p) const { return value_ == p; }
Packit bd1cd8
  bool operator!=(T* p) const { return value_ != p; }
Packit bd1cd8
  template <typename U>
Packit bd1cd8
  bool operator==(linked_ptr<U> const& ptr) const {
Packit bd1cd8
    return value_ == ptr.get();
Packit bd1cd8
  }
Packit bd1cd8
  template <typename U>
Packit bd1cd8
  bool operator!=(linked_ptr<U> const& ptr) const {
Packit bd1cd8
    return value_ != ptr.get();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  template <typename U>
Packit bd1cd8
  friend class linked_ptr;
Packit bd1cd8
Packit bd1cd8
  T* value_;
Packit bd1cd8
  linked_ptr_internal link_;
Packit bd1cd8
Packit bd1cd8
  void depart() {
Packit bd1cd8
    if (link_.depart()) delete value_;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  void capture(T* ptr) {
Packit bd1cd8
    value_ = ptr;
Packit bd1cd8
    link_.join_new();
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  template <typename U> void copy(linked_ptr<U> const* ptr) {
Packit bd1cd8
    value_ = ptr->get();
Packit bd1cd8
    if (value_)
Packit bd1cd8
      link_.join(&ptr->link_);
Packit bd1cd8
    else
Packit bd1cd8
      link_.join_new();
Packit bd1cd8
  }
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
template<typename T> inline
Packit bd1cd8
bool operator==(T* ptr, const linked_ptr<T>& x) {
Packit bd1cd8
  return ptr == x.get();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
template<typename T> inline
Packit bd1cd8
bool operator!=(T* ptr, const linked_ptr<T>& x) {
Packit bd1cd8
  return ptr != x.get();
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// A function to convert T* into linked_ptr<T>
Packit bd1cd8
// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
Packit bd1cd8
// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
Packit bd1cd8
template <typename T>
Packit bd1cd8
linked_ptr<T> make_linked_ptr(T* ptr) {
Packit bd1cd8
  return linked_ptr<T>(ptr);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_