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