123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
- #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
- #include <stdlib.h>
- #include <assert.h>
- #include "gtest/internal/gtest-port.h"
- namespace testing {
- namespace internal {
- GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex);
- class linked_ptr_internal {
- public:
-
- void join_new() {
- next_ = this;
- }
-
-
-
-
-
-
-
-
-
-
-
- void join(linked_ptr_internal const* ptr)
- GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
- MutexLock lock(&g_linked_ptr_mutex);
- linked_ptr_internal const* p = ptr;
- while (p->next_ != ptr) {
- assert(p->next_ != this &&
- "Trying to join() a linked ring we are already in. "
- "Is GMock thread safety enabled?");
- p = p->next_;
- }
- p->next_ = this;
- next_ = ptr;
- }
-
-
- bool depart()
- GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) {
- MutexLock lock(&g_linked_ptr_mutex);
- if (next_ == this) return true;
- linked_ptr_internal const* p = next_;
- while (p->next_ != this) {
- assert(p->next_ != next_ &&
- "Trying to depart() a linked ring we are not in. "
- "Is GMock thread safety enabled?");
- p = p->next_;
- }
- p->next_ = next_;
- return false;
- }
- private:
- mutable linked_ptr_internal const* next_;
- };
- template <typename T>
- class linked_ptr {
- public:
- typedef T element_type;
-
-
- explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
- ~linked_ptr() { depart(); }
-
- template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
- linked_ptr(linked_ptr const& ptr) {
- assert(&ptr != this);
- copy(&ptr);
- }
-
- template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
- depart();
- copy(&ptr);
- return *this;
- }
- linked_ptr& operator=(linked_ptr const& ptr) {
- if (&ptr != this) {
- depart();
- copy(&ptr);
- }
- return *this;
- }
-
- void reset(T* ptr = NULL) {
- depart();
- capture(ptr);
- }
- T* get() const { return value_; }
- T* operator->() const { return value_; }
- T& operator*() const { return *value_; }
- bool operator==(T* p) const { return value_ == p; }
- bool operator!=(T* p) const { return value_ != p; }
- template <typename U>
- bool operator==(linked_ptr<U> const& ptr) const {
- return value_ == ptr.get();
- }
- template <typename U>
- bool operator!=(linked_ptr<U> const& ptr) const {
- return value_ != ptr.get();
- }
- private:
- template <typename U>
- friend class linked_ptr;
- T* value_;
- linked_ptr_internal link_;
- void depart() {
- if (link_.depart()) delete value_;
- }
- void capture(T* ptr) {
- value_ = ptr;
- link_.join_new();
- }
- template <typename U> void copy(linked_ptr<U> const* ptr) {
- value_ = ptr->get();
- if (value_)
- link_.join(&ptr->link_);
- else
- link_.join_new();
- }
- };
- template<typename T> inline
- bool operator==(T* ptr, const linked_ptr<T>& x) {
- return ptr == x.get();
- }
- template<typename T> inline
- bool operator!=(T* ptr, const linked_ptr<T>& x) {
- return ptr != x.get();
- }
- template <typename T>
- linked_ptr<T> make_linked_ptr(T* ptr) {
- return linked_ptr<T>(ptr);
- }
- }
- }
- #endif
|