henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 11 | // Borrowed from Chromium's src/base/memory/scoped_ptr.h. |
| 12 | |
| 13 | // Scopers help you manage ownership of a pointer, helping you easily manage a |
| 14 | // pointer within a scope, and automatically destroying the pointer at the end |
| 15 | // of a scope. There are two main classes you will use, which correspond to the |
| 16 | // operators new/delete and new[]/delete[]. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | // |
| 18 | // Example usage (scoped_ptr<T>): |
| 19 | // { |
| 20 | // scoped_ptr<Foo> foo(new Foo("wee")); |
| 21 | // } // foo goes out of scope, releasing the pointer with it. |
| 22 | // |
| 23 | // { |
| 24 | // scoped_ptr<Foo> foo; // No pointer managed. |
| 25 | // foo.reset(new Foo("wee")); // Now a pointer is managed. |
| 26 | // foo.reset(new Foo("wee2")); // Foo("wee") was destroyed. |
| 27 | // foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed. |
| 28 | // foo->Method(); // Foo::Method() called. |
| 29 | // foo.get()->Method(); // Foo::Method() called. |
| 30 | // SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer |
| 31 | // // manages a pointer. |
| 32 | // foo.reset(new Foo("wee4")); // foo manages a pointer again. |
| 33 | // foo.reset(); // Foo("wee4") destroyed, foo no longer |
| 34 | // // manages a pointer. |
| 35 | // } // foo wasn't managing a pointer, so nothing was destroyed. |
| 36 | // |
| 37 | // Example usage (scoped_ptr<T[]>): |
| 38 | // { |
| 39 | // scoped_ptr<Foo[]> foo(new Foo[100]); |
| 40 | // foo.get()->Method(); // Foo::Method on the 0th element. |
| 41 | // foo[10].Method(); // Foo::Method on the 10th element. |
| 42 | // } |
| 43 | // |
| 44 | // These scopers also implement part of the functionality of C++11 unique_ptr |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 45 | // in that they are "movable but not copyable." You can use the scopers in the |
| 46 | // parameter and return types of functions to signify ownership transfer in to |
| 47 | // and out of a function. When calling a function that has a scoper as the |
| 48 | // argument type, it must be called with the result of calling std::move on an |
| 49 | // analogous scoper, or another function that generates a temporary; passing by |
| 50 | // copy will NOT work. Here is an example using scoped_ptr: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | // |
| 52 | // void TakesOwnership(scoped_ptr<Foo> arg) { |
| 53 | // // Do something with arg |
| 54 | // } |
| 55 | // scoped_ptr<Foo> CreateFoo() { |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 56 | // // No need for calling std::move because we are constructing a temporary |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | // // for the return value. |
| 58 | // return scoped_ptr<Foo>(new Foo("new")); |
| 59 | // } |
| 60 | // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) { |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 61 | // return std::move(arg); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | // } |
| 63 | // |
| 64 | // { |
| 65 | // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay"). |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 66 | // TakesOwnership(std::move(ptr)); // ptr no longer owns Foo("yay"). |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo. |
| 68 | // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2. |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 69 | // PassThru(std::move(ptr2)); // ptr2 is correspondingly nullptr. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | // } |
| 71 | // |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 72 | // Notice that if you do not call std::move when returning from PassThru(), or |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | // when invoking TakesOwnership(), the code will not compile because scopers |
| 74 | // are not copyable; they only implement move semantics which require calling |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 75 | // std::move to signify a destructive transfer of state. CreateFoo() is |
| 76 | // different though because we are constructing a temporary on the return line |
| 77 | // and thus can avoid needing to call std::move. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 78 | |
| 79 | #ifndef WEBRTC_BASE_SCOPED_PTR_H__ |
| 80 | #define WEBRTC_BASE_SCOPED_PTR_H__ |
| 81 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 82 | // This is an implementation designed to match the anticipated future TR2 |
| 83 | // implementation of the scoped_ptr class. |
| 84 | |
| 85 | #include <assert.h> |
| 86 | #include <stddef.h> |
| 87 | #include <stdlib.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | |
| 89 | #include <algorithm> // For std::swap(). |
kwiberg | 9390f84 | 2015-12-17 06:20:27 -0800 | [diff] [blame] | 90 | #include <cstddef> |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame^] | 91 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 93 | #include "webrtc/base/constructormagic.h" |
kwiberg | 36220ae | 2016-01-12 07:24:19 -0800 | [diff] [blame] | 94 | #include "webrtc/base/deprecation.h" |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 95 | #include "webrtc/base/template_util.h" |
| 96 | #include "webrtc/typedefs.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | |
| 98 | namespace rtc { |
| 99 | |
| 100 | // Function object which deletes its parameter, which must be a pointer. |
| 101 | // If C is an array type, invokes 'delete[]' on the parameter; otherwise, |
| 102 | // invokes 'delete'. The default deleter for scoped_ptr<T>. |
| 103 | template <class T> |
| 104 | struct DefaultDeleter { |
| 105 | DefaultDeleter() {} |
| 106 | template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) { |
| 107 | // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor |
| 108 | // if U* is implicitly convertible to T* and U is not an array type. |
| 109 | // |
| 110 | // Correct implementation should use SFINAE to disable this |
| 111 | // constructor. However, since there are no other 1-argument constructors, |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 112 | // using a static_assert based on is_convertible<> and requiring |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | // complete types is simpler and will cause compile failures for equivalent |
| 114 | // misuses. |
| 115 | // |
| 116 | // Note, the is_convertible<U*, T*> check also ensures that U is not an |
| 117 | // array. T is guaranteed to be a non-array, so any U* where U is an array |
| 118 | // cannot convert to T*. |
| 119 | enum { T_must_be_complete = sizeof(T) }; |
| 120 | enum { U_must_be_complete = sizeof(U) }; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 121 | static_assert(rtc::is_convertible<U*, T*>::value, |
| 122 | "U* must implicitly convert to T*"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | } |
| 124 | inline void operator()(T* ptr) const { |
| 125 | enum { type_must_be_complete = sizeof(T) }; |
| 126 | delete ptr; |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | // Specialization of DefaultDeleter for array types. |
| 131 | template <class T> |
| 132 | struct DefaultDeleter<T[]> { |
| 133 | inline void operator()(T* ptr) const { |
| 134 | enum { type_must_be_complete = sizeof(T) }; |
| 135 | delete[] ptr; |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | // Disable this operator for any U != T because it is undefined to execute |
| 140 | // an array delete when the static type of the array mismatches the dynamic |
| 141 | // type. |
| 142 | // |
| 143 | // References: |
| 144 | // C++98 [expr.delete]p3 |
| 145 | // http://cplusplus.github.com/LWG/lwg-defects.html#938 |
| 146 | template <typename U> void operator()(U* array) const; |
| 147 | }; |
| 148 | |
| 149 | template <class T, int n> |
| 150 | struct DefaultDeleter<T[n]> { |
| 151 | // Never allow someone to declare something like scoped_ptr<int[10]>. |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 152 | static_assert(sizeof(T) == -1, "do not use array with size as type"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | // Function object which invokes 'free' on its parameter, which must be |
| 156 | // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: |
| 157 | // |
| 158 | // scoped_ptr<int, rtc::FreeDeleter> foo_ptr( |
| 159 | // static_cast<int*>(malloc(sizeof(int)))); |
| 160 | struct FreeDeleter { |
| 161 | inline void operator()(void* ptr) const { |
| 162 | free(ptr); |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | namespace internal { |
| 167 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 168 | template <typename T> |
| 169 | struct ShouldAbortOnSelfReset { |
| 170 | template <typename U> |
| 171 | static rtc::internal::NoType Test(const typename U::AllowSelfReset*); |
| 172 | |
| 173 | template <typename U> |
| 174 | static rtc::internal::YesType Test(...); |
| 175 | |
| 176 | static const bool value = |
| 177 | sizeof(Test<T>(0)) == sizeof(rtc::internal::YesType); |
| 178 | }; |
| 179 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | // Minimal implementation of the core logic of scoped_ptr, suitable for |
| 181 | // reuse in both scoped_ptr and its specializations. |
| 182 | template <class T, class D> |
| 183 | class scoped_ptr_impl { |
| 184 | public: |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 185 | explicit scoped_ptr_impl(T* p) : data_(p) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 186 | |
| 187 | // Initializer for deleters that have data parameters. |
| 188 | scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} |
| 189 | |
| 190 | // Templated constructor that destructively takes the value from another |
| 191 | // scoped_ptr_impl. |
| 192 | template <typename U, typename V> |
| 193 | scoped_ptr_impl(scoped_ptr_impl<U, V>* other) |
| 194 | : data_(other->release(), other->get_deleter()) { |
| 195 | // We do not support move-only deleters. We could modify our move |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 196 | // emulation to have rtc::subtle::move() and rtc::subtle::forward() |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 197 | // functions that are imperfect emulations of their C++11 equivalents, |
| 198 | // but until there's a requirement, just assume deleters are copyable. |
| 199 | } |
| 200 | |
| 201 | template <typename U, typename V> |
| 202 | void TakeState(scoped_ptr_impl<U, V>* other) { |
| 203 | // See comment in templated constructor above regarding lack of support |
| 204 | // for move-only deleters. |
| 205 | reset(other->release()); |
| 206 | get_deleter() = other->get_deleter(); |
| 207 | } |
| 208 | |
| 209 | ~scoped_ptr_impl() { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 210 | if (data_.ptr != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 211 | // Not using get_deleter() saves one function call in non-optimized |
| 212 | // builds. |
| 213 | static_cast<D&>(data_)(data_.ptr); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void reset(T* p) { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 218 | // This is a self-reset, which is no longer allowed for default deleters: |
| 219 | // https://crbug.com/162971 |
| 220 | assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | |
| 222 | // Note that running data_.ptr = p can lead to undefined behavior if |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 223 | // get_deleter()(get()) deletes this. In order to prevent this, reset() |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | // should update the stored pointer before deleting its old value. |
| 225 | // |
| 226 | // However, changing reset() to use that behavior may cause current code to |
| 227 | // break in unexpected ways. If the destruction of the owned object |
| 228 | // dereferences the scoped_ptr when it is destroyed by a call to reset(), |
| 229 | // then it will incorrectly dispatch calls to |p| rather than the original |
| 230 | // value of |data_.ptr|. |
| 231 | // |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 232 | // During the transition period, set the stored pointer to nullptr while |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 233 | // deleting the object. Eventually, this safety check will be removed to |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 234 | // prevent the scenario initially described from occurring and |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | // http://crbug.com/176091 can be closed. |
| 236 | T* old = data_.ptr; |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 237 | data_.ptr = nullptr; |
| 238 | if (old != nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | static_cast<D&>(data_)(old); |
| 240 | data_.ptr = p; |
| 241 | } |
| 242 | |
| 243 | T* get() const { return data_.ptr; } |
| 244 | |
| 245 | D& get_deleter() { return data_; } |
| 246 | const D& get_deleter() const { return data_; } |
| 247 | |
| 248 | void swap(scoped_ptr_impl& p2) { |
| 249 | // Standard swap idiom: 'using std::swap' ensures that std::swap is |
| 250 | // present in the overload set, but we call swap unqualified so that |
| 251 | // any more-specific overloads can be used, if available. |
| 252 | using std::swap; |
| 253 | swap(static_cast<D&>(data_), static_cast<D&>(p2.data_)); |
| 254 | swap(data_.ptr, p2.data_.ptr); |
| 255 | } |
| 256 | |
| 257 | T* release() { |
| 258 | T* old_ptr = data_.ptr; |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 259 | data_.ptr = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | return old_ptr; |
| 261 | } |
| 262 | |
| 263 | T** accept() { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 264 | reset(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 265 | return &(data_.ptr); |
| 266 | } |
| 267 | |
| 268 | T** use() { |
| 269 | return &(data_.ptr); |
| 270 | } |
| 271 | |
| 272 | private: |
| 273 | // Needed to allow type-converting constructor. |
| 274 | template <typename U, typename V> friend class scoped_ptr_impl; |
| 275 | |
| 276 | // Use the empty base class optimization to allow us to have a D |
| 277 | // member, while avoiding any space overhead for it when D is an |
| 278 | // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good |
| 279 | // discussion of this technique. |
| 280 | struct Data : public D { |
| 281 | explicit Data(T* ptr_in) : ptr(ptr_in) {} |
| 282 | Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {} |
| 283 | T* ptr; |
| 284 | }; |
| 285 | |
| 286 | Data data_; |
| 287 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 288 | RTC_DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | } // namespace internal |
| 292 | |
| 293 | // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> |
| 294 | // automatically deletes the pointer it holds (if any). |
| 295 | // That is, scoped_ptr<T> owns the T object that it points to. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 296 | // Like a T*, a scoped_ptr<T> may hold either nullptr or a pointer to a T |
| 297 | // object. Also like T*, scoped_ptr<T> is thread-compatible, and once you |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 298 | // dereference it, you get the thread safety guarantees of T. |
| 299 | // |
| 300 | // The size of scoped_ptr is small. On most compilers, when using the |
| 301 | // DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will |
| 302 | // increase the size proportional to whatever state they need to have. See |
| 303 | // comments inside scoped_ptr_impl<> for details. |
| 304 | // |
| 305 | // Current implementation targets having a strict subset of C++11's |
| 306 | // unique_ptr<> features. Known deficiencies include not supporting move-only |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 307 | // deleters, function pointers as deleters, and deleters with reference |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 308 | // types. |
| 309 | template <class T, class D = rtc::DefaultDeleter<T> > |
| 310 | class scoped_ptr { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 311 | |
| 312 | // TODO(ajm): If we ever import RefCountedBase, this check needs to be |
| 313 | // enabled. |
| 314 | //static_assert(rtc::internal::IsNotRefCounted<T>::value, |
| 315 | // "T is refcounted type and needs scoped refptr"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 316 | |
| 317 | public: |
| 318 | // The element and deleter types. |
| 319 | typedef T element_type; |
| 320 | typedef D deleter_type; |
| 321 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 322 | // Constructor. Defaults to initializing with nullptr. |
| 323 | scoped_ptr() : impl_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 324 | |
| 325 | // Constructor. Takes ownership of p. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 326 | explicit scoped_ptr(element_type* p) : impl_(p) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 327 | |
| 328 | // Constructor. Allows initialization of a stateful deleter. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 329 | scoped_ptr(element_type* p, const D& d) : impl_(p, d) {} |
| 330 | |
| 331 | // Constructor. Allows construction from a nullptr. |
kwiberg | 9390f84 | 2015-12-17 06:20:27 -0800 | [diff] [blame] | 332 | scoped_ptr(std::nullptr_t) : impl_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 333 | |
| 334 | // Constructor. Allows construction from a scoped_ptr rvalue for a |
| 335 | // convertible type and deleter. |
| 336 | // |
| 337 | // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct |
| 338 | // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor |
| 339 | // has different post-conditions if D is a reference type. Since this |
| 340 | // implementation does not support deleters with reference type, |
| 341 | // we do not need a separate move constructor allowing us to avoid one |
| 342 | // use of SFINAE. You only need to care about this if you modify the |
| 343 | // implementation of scoped_ptr. |
| 344 | template <typename U, typename V> |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 345 | scoped_ptr(scoped_ptr<U, V>&& other) |
| 346 | : impl_(&other.impl_) { |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 347 | static_assert(!rtc::is_array<U>::value, "U cannot be an array"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 348 | } |
| 349 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 350 | // operator=. Allows assignment from a scoped_ptr rvalue for a convertible |
| 351 | // type and deleter. |
| 352 | // |
| 353 | // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from |
| 354 | // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated |
| 355 | // form has different requirements on for move-only Deleters. Since this |
| 356 | // implementation does not support move-only Deleters, we do not need a |
| 357 | // separate move assignment operator allowing us to avoid one use of SFINAE. |
| 358 | // You only need to care about this if you modify the implementation of |
| 359 | // scoped_ptr. |
| 360 | template <typename U, typename V> |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 361 | scoped_ptr& operator=(scoped_ptr<U, V>&& rhs) { |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 362 | static_assert(!rtc::is_array<U>::value, "U cannot be an array"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 363 | impl_.TakeState(&rhs.impl_); |
| 364 | return *this; |
| 365 | } |
| 366 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 367 | // operator=. Allows assignment from a nullptr. Deletes the currently owned |
| 368 | // object, if any. |
kwiberg | 9390f84 | 2015-12-17 06:20:27 -0800 | [diff] [blame] | 369 | scoped_ptr& operator=(std::nullptr_t) { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 370 | reset(); |
| 371 | return *this; |
| 372 | } |
| 373 | |
Karl Wiberg | a8e285d | 2015-04-22 19:44:19 +0200 | [diff] [blame] | 374 | // Deleted copy constructor and copy assignment, to make the type move-only. |
| 375 | scoped_ptr(const scoped_ptr& other) = delete; |
| 376 | scoped_ptr& operator=(const scoped_ptr& other) = delete; |
| 377 | |
| 378 | // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) |
kwiberg | 36220ae | 2016-01-12 07:24:19 -0800 | [diff] [blame] | 379 | // Deprecated; remove in March 2016 (bug 5373). |
| 380 | RTC_DEPRECATED scoped_ptr&& Pass() { |
| 381 | return std::move(*this); |
| 382 | } |
Karl Wiberg | a8e285d | 2015-04-22 19:44:19 +0200 | [diff] [blame] | 383 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 384 | // Reset. Deletes the currently owned object, if any. |
| 385 | // Then takes ownership of a new object, if given. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 386 | void reset(element_type* p = nullptr) { impl_.reset(p); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 387 | |
| 388 | // Accessors to get the owned object. |
| 389 | // operator* and operator-> will assert() if there is no current object. |
| 390 | element_type& operator*() const { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 391 | assert(impl_.get() != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 392 | return *impl_.get(); |
| 393 | } |
| 394 | element_type* operator->() const { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 395 | assert(impl_.get() != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 396 | return impl_.get(); |
| 397 | } |
| 398 | element_type* get() const { return impl_.get(); } |
| 399 | |
| 400 | // Access to the deleter. |
| 401 | deleter_type& get_deleter() { return impl_.get_deleter(); } |
| 402 | const deleter_type& get_deleter() const { return impl_.get_deleter(); } |
| 403 | |
| 404 | // Allow scoped_ptr<element_type> to be used in boolean expressions, but not |
| 405 | // implicitly convertible to a real bool (which is dangerous). |
| 406 | // |
| 407 | // Note that this trick is only safe when the == and != operators |
| 408 | // are declared explicitly, as otherwise "scoped_ptr1 == |
| 409 | // scoped_ptr2" will compile but do the wrong thing (i.e., convert |
| 410 | // to Testable and then do the comparison). |
| 411 | private: |
| 412 | typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type> |
| 413 | scoped_ptr::*Testable; |
| 414 | |
| 415 | public: |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 416 | operator Testable() const { |
| 417 | return impl_.get() ? &scoped_ptr::impl_ : nullptr; |
| 418 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 419 | |
| 420 | // Comparison operators. |
| 421 | // These return whether two scoped_ptr refer to the same object, not just to |
| 422 | // two different but equal objects. |
| 423 | bool operator==(const element_type* p) const { return impl_.get() == p; } |
| 424 | bool operator!=(const element_type* p) const { return impl_.get() != p; } |
| 425 | |
| 426 | // Swap two scoped pointers. |
| 427 | void swap(scoped_ptr& p2) { |
| 428 | impl_.swap(p2.impl_); |
| 429 | } |
| 430 | |
| 431 | // Release a pointer. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 432 | // The return value is the current pointer held by this object. If this object |
| 433 | // holds a nullptr, the return value is nullptr. After this operation, this |
| 434 | // object will hold a nullptr, and will not own the object any more. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 435 | element_type* release() WARN_UNUSED_RESULT { |
| 436 | return impl_.release(); |
| 437 | } |
| 438 | |
| 439 | // Delete the currently held pointer and return a pointer |
| 440 | // to allow overwriting of the current pointer address. |
| 441 | element_type** accept() WARN_UNUSED_RESULT { |
| 442 | return impl_.accept(); |
| 443 | } |
| 444 | |
| 445 | // Return a pointer to the current pointer address. |
| 446 | element_type** use() WARN_UNUSED_RESULT { |
| 447 | return impl_.use(); |
| 448 | } |
| 449 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 450 | private: |
| 451 | // Needed to reach into |impl_| in the constructor. |
| 452 | template <typename U, typename V> friend class scoped_ptr; |
| 453 | rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_; |
| 454 | |
| 455 | // Forbidden for API compatibility with std::unique_ptr. |
| 456 | explicit scoped_ptr(int disallow_construction_from_null); |
| 457 | |
| 458 | // Forbid comparison of scoped_ptr types. If U != T, it totally |
| 459 | // doesn't make sense, and if U == T, it still doesn't make sense |
| 460 | // because you should never have the same object owned by two different |
| 461 | // scoped_ptrs. |
| 462 | template <class U> bool operator==(scoped_ptr<U> const& p2) const; |
| 463 | template <class U> bool operator!=(scoped_ptr<U> const& p2) const; |
| 464 | }; |
| 465 | |
| 466 | template <class T, class D> |
| 467 | class scoped_ptr<T[], D> { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 468 | public: |
| 469 | // The element and deleter types. |
| 470 | typedef T element_type; |
| 471 | typedef D deleter_type; |
| 472 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 473 | // Constructor. Defaults to initializing with nullptr. |
| 474 | scoped_ptr() : impl_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 475 | |
| 476 | // Constructor. Stores the given array. Note that the argument's type |
| 477 | // must exactly match T*. In particular: |
| 478 | // - it cannot be a pointer to a type derived from T, because it is |
| 479 | // inherently unsafe in the general case to access an array through a |
| 480 | // pointer whose dynamic type does not match its static type (eg., if |
| 481 | // T and the derived types had different sizes access would be |
| 482 | // incorrectly calculated). Deletion is also always undefined |
| 483 | // (C++98 [expr.delete]p3). If you're doing this, fix your code. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 484 | // - it cannot be const-qualified differently from T per unique_ptr spec |
| 485 | // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting |
| 486 | // to work around this may use implicit_cast<const T*>(). |
| 487 | // However, because of the first bullet in this comment, users MUST |
| 488 | // NOT use implicit_cast<Base*>() to upcast the static type of the array. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 489 | explicit scoped_ptr(element_type* array) : impl_(array) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 490 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 491 | // Constructor. Allows construction from a nullptr. |
kwiberg | 9390f84 | 2015-12-17 06:20:27 -0800 | [diff] [blame] | 492 | scoped_ptr(std::nullptr_t) : impl_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 493 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 494 | // Constructor. Allows construction from a scoped_ptr rvalue. |
| 495 | scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {} |
| 496 | |
| 497 | // operator=. Allows assignment from a scoped_ptr rvalue. |
| 498 | scoped_ptr& operator=(scoped_ptr&& rhs) { |
| 499 | impl_.TakeState(&rhs.impl_); |
| 500 | return *this; |
| 501 | } |
| 502 | |
| 503 | // operator=. Allows assignment from a nullptr. Deletes the currently owned |
| 504 | // array, if any. |
kwiberg | 9390f84 | 2015-12-17 06:20:27 -0800 | [diff] [blame] | 505 | scoped_ptr& operator=(std::nullptr_t) { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 506 | reset(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 507 | return *this; |
| 508 | } |
| 509 | |
Karl Wiberg | a8e285d | 2015-04-22 19:44:19 +0200 | [diff] [blame] | 510 | // Deleted copy constructor and copy assignment, to make the type move-only. |
| 511 | scoped_ptr(const scoped_ptr& other) = delete; |
| 512 | scoped_ptr& operator=(const scoped_ptr& other) = delete; |
| 513 | |
| 514 | // Get an rvalue reference. (sp.Pass() does the same thing as std::move(sp).) |
kwiberg | 36220ae | 2016-01-12 07:24:19 -0800 | [diff] [blame] | 515 | // Deprecated; remove in March 2016 (bug 5373). |
| 516 | RTC_DEPRECATED scoped_ptr&& Pass() { |
| 517 | return std::move(*this); |
| 518 | } |
Karl Wiberg | a8e285d | 2015-04-22 19:44:19 +0200 | [diff] [blame] | 519 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 520 | // Reset. Deletes the currently owned array, if any. |
| 521 | // Then takes ownership of a new object, if given. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 522 | void reset(element_type* array = nullptr) { impl_.reset(array); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 523 | |
| 524 | // Accessors to get the owned array. |
| 525 | element_type& operator[](size_t i) const { |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 526 | assert(impl_.get() != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 527 | return impl_.get()[i]; |
| 528 | } |
| 529 | element_type* get() const { return impl_.get(); } |
| 530 | |
| 531 | // Access to the deleter. |
| 532 | deleter_type& get_deleter() { return impl_.get_deleter(); } |
| 533 | const deleter_type& get_deleter() const { return impl_.get_deleter(); } |
| 534 | |
| 535 | // Allow scoped_ptr<element_type> to be used in boolean expressions, but not |
| 536 | // implicitly convertible to a real bool (which is dangerous). |
| 537 | private: |
| 538 | typedef rtc::internal::scoped_ptr_impl<element_type, deleter_type> |
| 539 | scoped_ptr::*Testable; |
| 540 | |
| 541 | public: |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 542 | operator Testable() const { |
| 543 | return impl_.get() ? &scoped_ptr::impl_ : nullptr; |
| 544 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 545 | |
| 546 | // Comparison operators. |
| 547 | // These return whether two scoped_ptr refer to the same object, not just to |
| 548 | // two different but equal objects. |
| 549 | bool operator==(element_type* array) const { return impl_.get() == array; } |
| 550 | bool operator!=(element_type* array) const { return impl_.get() != array; } |
| 551 | |
| 552 | // Swap two scoped pointers. |
| 553 | void swap(scoped_ptr& p2) { |
| 554 | impl_.swap(p2.impl_); |
| 555 | } |
| 556 | |
| 557 | // Release a pointer. |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 558 | // The return value is the current pointer held by this object. If this object |
| 559 | // holds a nullptr, the return value is nullptr. After this operation, this |
| 560 | // object will hold a nullptr, and will not own the object any more. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 561 | element_type* release() WARN_UNUSED_RESULT { |
| 562 | return impl_.release(); |
| 563 | } |
| 564 | |
| 565 | // Delete the currently held pointer and return a pointer |
| 566 | // to allow overwriting of the current pointer address. |
| 567 | element_type** accept() WARN_UNUSED_RESULT { |
| 568 | return impl_.accept(); |
| 569 | } |
| 570 | |
| 571 | // Return a pointer to the current pointer address. |
| 572 | element_type** use() WARN_UNUSED_RESULT { |
| 573 | return impl_.use(); |
| 574 | } |
| 575 | |
| 576 | private: |
| 577 | // Force element_type to be a complete type. |
| 578 | enum { type_must_be_complete = sizeof(element_type) }; |
| 579 | |
| 580 | // Actually hold the data. |
| 581 | rtc::internal::scoped_ptr_impl<element_type, deleter_type> impl_; |
| 582 | |
| 583 | // Disable initialization from any type other than element_type*, by |
| 584 | // providing a constructor that matches such an initialization, but is |
| 585 | // private and has no definition. This is disabled because it is not safe to |
| 586 | // call delete[] on an array whose static type does not match its dynamic |
| 587 | // type. |
| 588 | template <typename U> explicit scoped_ptr(U* array); |
| 589 | explicit scoped_ptr(int disallow_construction_from_null); |
| 590 | |
| 591 | // Disable reset() from any type other than element_type*, for the same |
| 592 | // reasons as the constructor above. |
| 593 | template <typename U> void reset(U* array); |
| 594 | void reset(int disallow_reset_from_null); |
| 595 | |
| 596 | // Forbid comparison of scoped_ptr types. If U != T, it totally |
| 597 | // doesn't make sense, and if U == T, it still doesn't make sense |
| 598 | // because you should never have the same object owned by two different |
| 599 | // scoped_ptrs. |
| 600 | template <class U> bool operator==(scoped_ptr<U> const& p2) const; |
| 601 | template <class U> bool operator!=(scoped_ptr<U> const& p2) const; |
| 602 | }; |
| 603 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 604 | template <class T, class D> |
| 605 | void swap(rtc::scoped_ptr<T, D>& p1, rtc::scoped_ptr<T, D>& p2) { |
| 606 | p1.swap(p2); |
| 607 | } |
| 608 | |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame^] | 609 | // Convert between the most common kinds of scoped_ptr and unique_ptr. |
| 610 | template <typename T> |
| 611 | std::unique_ptr<T> ScopedToUnique(scoped_ptr<T> sp) { |
| 612 | return std::unique_ptr<T>(sp.release()); |
| 613 | } |
| 614 | template <typename T> |
| 615 | scoped_ptr<T> UniqueToScoped(std::unique_ptr<T> up) { |
| 616 | return scoped_ptr<T>(up.release()); |
| 617 | } |
| 618 | |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 619 | } // namespace rtc |
| 620 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 621 | template <class T, class D> |
| 622 | bool operator==(T* p1, const rtc::scoped_ptr<T, D>& p2) { |
| 623 | return p1 == p2.get(); |
| 624 | } |
| 625 | |
| 626 | template <class T, class D> |
| 627 | bool operator!=(T* p1, const rtc::scoped_ptr<T, D>& p2) { |
| 628 | return p1 != p2.get(); |
| 629 | } |
| 630 | |
kwiberg@webrtc.org | 73ca194 | 2015-01-29 09:12:47 +0000 | [diff] [blame] | 631 | // A function to convert T* into scoped_ptr<T> |
| 632 | // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 633 | // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
| 634 | template <typename T> |
| 635 | rtc::scoped_ptr<T> rtc_make_scoped_ptr(T* ptr) { |
| 636 | return rtc::scoped_ptr<T>(ptr); |
| 637 | } |
| 638 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 639 | #endif // #ifndef WEBRTC_BASE_SCOPED_PTR_H__ |