1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* Smart pointer which leaks its owning refcounted object by default. */ 8 9 #ifndef LeakRefPtr_h 10 #define LeakRefPtr_h 11 12 #include "mozilla/AlreadyAddRefed.h" 13 14 namespace mozilla { 15 16 /** 17 * Instance of this class behaves like a raw pointer which leaks the 18 * resource it's owning if not explicitly released. 19 */ 20 template <class T> 21 class LeakRefPtr { 22 public: LeakRefPtr(already_AddRefed<T> && aPtr)23 explicit LeakRefPtr(already_AddRefed<T>&& aPtr) : mRawPtr(aPtr.take()) {} 24 25 explicit operator bool() const { return !!mRawPtr; } 26 27 LeakRefPtr<T>& operator=(already_AddRefed<T>&& aPtr) { 28 mRawPtr = aPtr.take(); 29 return *this; 30 } 31 get()32 T* get() const { return mRawPtr; } 33 take()34 already_AddRefed<T> take() { 35 T* rawPtr = mRawPtr; 36 mRawPtr = nullptr; 37 return already_AddRefed<T>(rawPtr); 38 } 39 release()40 void release() { NS_RELEASE(mRawPtr); } 41 42 private: 43 T* MOZ_OWNING_REF mRawPtr; 44 }; 45 46 } // namespace mozilla 47 48 #endif // LeakRefPtr_h 49