gmock/gtest/include/gtest/internal/gtest-linked_ptr.h
Go to the documentation of this file.
1 // Copyright 2003 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Authors: Dan Egnor (egnor@google.com)
31 //
32 // A "smart" pointer type with reference tracking. Every pointer to a
33 // particular object is kept on a circular linked list. When the last pointer
34 // to an object is destroyed or reassigned, the object is deleted.
35 //
36 // Used properly, this deletes the object when the last reference goes away.
37 // There are several caveats:
38 // - Like all reference counting schemes, cycles lead to leaks.
39 // - Each smart pointer is actually two pointers (8 bytes instead of 4).
40 // - Every time a pointer is assigned, the entire list of pointers to that
41 // object is traversed. This class is therefore NOT SUITABLE when there
42 // will often be more than two or three pointers to a particular object.
43 // - References are only tracked as long as linked_ptr<> objects are copied.
44 // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
45 // will happen (double deletion).
46 //
47 // A good use of this class is storing object references in STL containers.
48 // You can safely put linked_ptr<> in a vector<>.
49 // Other uses may not be as good.
50 //
51 // Note: If you use an incomplete type with linked_ptr<>, the class
52 // *containing* linked_ptr<> must have a constructor and destructor (even
53 // if they do nothing!).
54 //
55 // Bill Gibbons suggested we use something like this.
56 //
57 // Thread Safety:
58 // Unlike other linked_ptr implementations, in this implementation
59 // a linked_ptr object is thread-safe in the sense that:
60 // - it's safe to copy linked_ptr objects concurrently,
61 // - it's safe to copy *from* a linked_ptr and read its underlying
62 // raw pointer (e.g. via get()) concurrently, and
63 // - it's safe to write to two linked_ptrs that point to the same
64 // shared object concurrently.
65 // TODO(wan@google.com): rename this to safe_linked_ptr to avoid
66 // confusion with normal linked_ptr.
67 
68 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
69 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
70 
71 #include <stdlib.h>
72 #include <assert.h>
73 
74 #include "gtest/internal/gtest-port.h"
75 
76 namespace testing
77 {
78 namespace internal
79 {
80 
81 // Protects copying of all linked_ptr objects.
82 GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex);
83 
84 // This is used internally by all instances of linked_ptr<>. It needs to be
85 // a non-template class because different types of linked_ptr<> can refer to
86 // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
87 // So, it needs to be possible for different types of linked_ptr to participate
88 // in the same circular linked list, so we need a single class type here.
89 //
90 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
91 class linked_ptr_internal
92 {
93 public:
94  // Create a new circle that includes only this instance.
95  void join_new()
96  {
97  next_ = this;
98  }
99 
100  // Many linked_ptr operations may change p.link_ for some linked_ptr
101  // variable p in the same circle as this object. Therefore we need
102  // to prevent two such operations from occurring concurrently.
103  //
104  // Note that different types of linked_ptr objects can coexist in a
105  // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and
106  // linked_ptr<Derived2>). Therefore we must use a single mutex to
107  // protect all linked_ptr objects. This can create serious
108  // contention in production code, but is acceptable in a testing
109  // framework.
110 
111  // Join an existing circle.
112  void join(linked_ptr_internal const * ptr)
113  GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex)
114  {
115  MutexLock lock(&g_linked_ptr_mutex);
116 
117  linked_ptr_internal const * p = ptr;
118 
119  while (p->next_ != ptr) { p = p->next_; }
120 
121  p->next_ = this;
122  next_ = ptr;
123  }
124 
125  // Leave whatever circle we're part of. Returns true if we were the
126  // last member of the circle. Once this is done, you can join() another.
127  bool depart()
128  GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex)
129  {
130  MutexLock lock(&g_linked_ptr_mutex);
131 
132  if (next_ == this) { return true; }
133 
134  linked_ptr_internal const * p = next_;
135 
136  while (p->next_ != this) { p = p->next_; }
137 
138  p->next_ = next_;
139  return false;
140  }
141 
142 private:
143  mutable linked_ptr_internal const * next_;
144 };
145 
146 template <typename T>
147 class linked_ptr
148 {
149 public:
150  typedef T element_type;
151 
152  // Take over ownership of a raw pointer. This should happen as soon as
153  // possible after the object is created.
154  explicit linked_ptr(T * ptr = NULL) { capture(ptr); }
156 
157  // Copy an existing linked_ptr<>, adding ourselves to the list of references.
158  template <typename U> linked_ptr(linked_ptr<U> const & ptr) { copy(&ptr); }
159  linked_ptr(linked_ptr const & ptr) // NOLINT
160  {
161  assert(&ptr != this);
162  copy(&ptr);
163  }
164 
165  // Assignment releases the old value and acquires the new.
166  template <typename U> linked_ptr & operator=(linked_ptr<U> const & ptr)
167  {
168  depart();
169  copy(&ptr);
170  return *this;
171  }
172 
174  {
175  if (&ptr != this)
176  {
177  depart();
178  copy(&ptr);
179  }
180 
181  return *this;
182  }
183 
184  // Smart pointer members.
185  void reset(T * ptr = NULL)
186  {
187  depart();
188  capture(ptr);
189  }
190  T * get() const { return value_; }
191  T * operator->() const { return value_; }
192  T & operator*() const { return *value_; }
193 
194  bool operator==(T * p) const { return value_ == p; }
195  bool operator!=(T * p) const { return value_ != p; }
196  template <typename U>
197  bool operator==(linked_ptr<U> const & ptr) const
198  {
199  return value_ == ptr.get();
200  }
201  template <typename U>
202  bool operator!=(linked_ptr<U> const & ptr) const
203  {
204  return value_ != ptr.get();
205  }
206 
207 private:
208  template <typename U>
209  friend class linked_ptr;
210 
211  T * value_;
212  linked_ptr_internal link_;
213 
214  void depart()
215  {
216  if (link_.depart()) { delete value_; }
217  }
218 
219  void capture(T * ptr)
220  {
221  value_ = ptr;
222  link_.join_new();
223  }
224 
225  template <typename U> void copy(linked_ptr<U> const * ptr)
226  {
227  value_ = ptr->get();
228 
229  if (value_)
230  { link_.join(&ptr->link_); }
231 
232  else
233  { link_.join_new(); }
234  }
235 };
236 
237 template<typename T> inline
238 bool operator==(T * ptr, const linked_ptr<T> & x)
239 {
240  return ptr == x.get();
241 }
242 
243 template<typename T> inline
244 bool operator!=(T * ptr, const linked_ptr<T> & x)
245 {
246  return ptr != x.get();
247 }
248 
249 // A function to convert T* into linked_ptr<T>
250 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
251 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
252 template <typename T>
254 {
255  return linked_ptr<T>(ptr);
256 }
257 
258 } // namespace internal
259 } // namespace testing
260 
261 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_
#define GTEST_API_
bool operator==(T *ptr, const linked_ptr< T > &x)
GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex)
linked_ptr< T > make_linked_ptr(T *ptr)
void join(linked_ptr_internal const *ptr) GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex)
bool operator!=(T *ptr, const linked_ptr< T > &x)
#define GTEST_LOCK_EXCLUDED_(locks)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:12