Line data Source code
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 : {
23 : public:
24 3572 : explicit LeakRefPtr(already_AddRefed<T>&& aPtr)
25 3572 : : mRawPtr(aPtr.take()) { }
26 :
27 1580 : explicit operator bool() const { return !!mRawPtr; }
28 :
29 : LeakRefPtr<T>& operator=(already_AddRefed<T>&& aPtr)
30 : {
31 : mRawPtr = aPtr.take();
32 : return *this;
33 : }
34 :
35 : T* get() const { return mRawPtr; }
36 :
37 3572 : already_AddRefed<T> take()
38 : {
39 3572 : T* rawPtr = mRawPtr;
40 3572 : mRawPtr = nullptr;
41 3572 : return already_AddRefed<T>(rawPtr);
42 : }
43 :
44 : void release() { NS_RELEASE(mRawPtr); }
45 :
46 : private:
47 : T* MOZ_OWNING_REF mRawPtr;
48 : };
49 :
50 : } // namespace mozilla
51 :
52 : #endif // LeakRefPtr_h
|