|
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 /* A class for non-null strong pointers to reference-counted objects. */ |
|
8 |
|
9 #ifndef mozilla_dom_OwningNonNull_h |
|
10 #define mozilla_dom_OwningNonNull_h |
|
11 |
|
12 #include "nsAutoPtr.h" |
|
13 |
|
14 namespace mozilla { |
|
15 namespace dom { |
|
16 |
|
17 template<class T> |
|
18 class OwningNonNull |
|
19 { |
|
20 public: |
|
21 OwningNonNull() |
|
22 #ifdef DEBUG |
|
23 : mInited(false) |
|
24 #endif |
|
25 {} |
|
26 |
|
27 operator T&() |
|
28 { |
|
29 MOZ_ASSERT(mInited); |
|
30 MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null"); |
|
31 return *mPtr; |
|
32 } |
|
33 |
|
34 operator T*() |
|
35 { |
|
36 MOZ_ASSERT(mInited); |
|
37 MOZ_ASSERT(mPtr, "OwningNonNull<T> was set to null"); |
|
38 return mPtr; |
|
39 } |
|
40 |
|
41 void operator=(T* aValue) |
|
42 { |
|
43 init(aValue); |
|
44 } |
|
45 |
|
46 void operator=(const already_AddRefed<T>& aValue) |
|
47 { |
|
48 init(aValue); |
|
49 } |
|
50 |
|
51 already_AddRefed<T> forget() |
|
52 { |
|
53 #ifdef DEBUG |
|
54 mInited = false; |
|
55 #endif |
|
56 return mPtr.forget(); |
|
57 } |
|
58 |
|
59 // Make us work with smart pointer helpers that expect a get(). |
|
60 T* get() const |
|
61 { |
|
62 MOZ_ASSERT(mInited); |
|
63 MOZ_ASSERT(mPtr); |
|
64 return mPtr; |
|
65 } |
|
66 |
|
67 protected: |
|
68 template<typename U> |
|
69 void init(U aValue) |
|
70 { |
|
71 mPtr = aValue; |
|
72 MOZ_ASSERT(mPtr); |
|
73 #ifdef DEBUG |
|
74 mInited = true; |
|
75 #endif |
|
76 } |
|
77 |
|
78 nsRefPtr<T> mPtr; |
|
79 #ifdef DEBUG |
|
80 bool mInited; |
|
81 #endif |
|
82 }; |
|
83 |
|
84 } // namespace dom |
|
85 } // namespace mozilla |
|
86 |
|
87 #endif // mozilla_dom_OwningNonNull_h |