xpcom/base/StaticPtr.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set sw=2 ts=8 et ft=cpp : */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla_StaticPtr_h
michael@0 8 #define mozilla_StaticPtr_h
michael@0 9
michael@0 10 #include "mozilla/Assertions.h"
michael@0 11 #include "mozilla/NullPtr.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14
michael@0 15 /**
michael@0 16 * StaticAutoPtr and StaticRefPtr are like nsAutoPtr and nsRefPtr, except they
michael@0 17 * are suitable for use as global variables.
michael@0 18 *
michael@0 19 * In particular, a global instance of Static{Auto,Ref}Ptr doesn't cause the
michael@0 20 * compiler to emit a static initializer (in release builds, anyway).
michael@0 21 *
michael@0 22 * In order to accomplish this, Static{Auto,Ref}Ptr must have a trivial
michael@0 23 * constructor and destructor. As a consequence, it cannot initialize its raw
michael@0 24 * pointer to 0 on construction, and it cannot delete/release its raw pointer
michael@0 25 * upon destruction.
michael@0 26 *
michael@0 27 * Since the compiler guarantees that all global variables are initialized to
michael@0 28 * 0, these trivial constructors are safe, so long as you use
michael@0 29 * Static{Auto,Ref}Ptr as a global variable. If you use Static{Auto,Ref}Ptr as
michael@0 30 * a stack variable or as a class instance variable, you will get what you
michael@0 31 * deserve.
michael@0 32 *
michael@0 33 * Static{Auto,Ref}Ptr have a limited interface as compared to ns{Auto,Ref}Ptr;
michael@0 34 * this is intentional, since their range of acceptable uses is smaller.
michael@0 35 */
michael@0 36
michael@0 37 template<class T>
michael@0 38 class StaticAutoPtr
michael@0 39 {
michael@0 40 public:
michael@0 41 // In debug builds, check that mRawPtr is initialized for us as we expect
michael@0 42 // by the compiler. In non-debug builds, don't declare a constructor
michael@0 43 // so that the compiler can see that the constructor is trivial.
michael@0 44 #ifdef DEBUG
michael@0 45 StaticAutoPtr()
michael@0 46 {
michael@0 47 MOZ_ASSERT(!mRawPtr);
michael@0 48 }
michael@0 49 #endif
michael@0 50
michael@0 51 StaticAutoPtr<T>& operator=(T* rhs)
michael@0 52 {
michael@0 53 Assign(rhs);
michael@0 54 return *this;
michael@0 55 }
michael@0 56
michael@0 57 T* get() const
michael@0 58 {
michael@0 59 return mRawPtr;
michael@0 60 }
michael@0 61
michael@0 62 operator T*() const
michael@0 63 {
michael@0 64 return get();
michael@0 65 }
michael@0 66
michael@0 67 T* operator->() const
michael@0 68 {
michael@0 69 MOZ_ASSERT(mRawPtr);
michael@0 70 return get();
michael@0 71 }
michael@0 72
michael@0 73 T& operator*() const
michael@0 74 {
michael@0 75 return *get();
michael@0 76 }
michael@0 77
michael@0 78 private:
michael@0 79 // Disallow copy constructor, but only in debug mode. We only define
michael@0 80 // a default constructor in debug mode (see above); if we declared
michael@0 81 // this constructor always, the compiler wouldn't generate a trivial
michael@0 82 // default constructor for us in non-debug mode.
michael@0 83 #ifdef DEBUG
michael@0 84 StaticAutoPtr(StaticAutoPtr<T> &other);
michael@0 85 #endif
michael@0 86
michael@0 87 void Assign(T* newPtr)
michael@0 88 {
michael@0 89 MOZ_ASSERT(!newPtr || mRawPtr != newPtr);
michael@0 90 T* oldPtr = mRawPtr;
michael@0 91 mRawPtr = newPtr;
michael@0 92 delete oldPtr;
michael@0 93 }
michael@0 94
michael@0 95 T* mRawPtr;
michael@0 96 };
michael@0 97
michael@0 98 template<class T>
michael@0 99 class StaticRefPtr
michael@0 100 {
michael@0 101 public:
michael@0 102 // In debug builds, check that mRawPtr is initialized for us as we expect
michael@0 103 // by the compiler. In non-debug builds, don't declare a constructor
michael@0 104 // so that the compiler can see that the constructor is trivial.
michael@0 105 #ifdef DEBUG
michael@0 106 StaticRefPtr()
michael@0 107 {
michael@0 108 MOZ_ASSERT(!mRawPtr);
michael@0 109 }
michael@0 110 #endif
michael@0 111
michael@0 112 StaticRefPtr<T>& operator=(T* rhs)
michael@0 113 {
michael@0 114 AssignWithAddref(rhs);
michael@0 115 return *this;
michael@0 116 }
michael@0 117
michael@0 118 StaticRefPtr<T>& operator=(const StaticRefPtr<T>& rhs)
michael@0 119 {
michael@0 120 return (this = rhs.mRawPtr);
michael@0 121 }
michael@0 122
michael@0 123 T* get() const
michael@0 124 {
michael@0 125 return mRawPtr;
michael@0 126 }
michael@0 127
michael@0 128 operator T*() const
michael@0 129 {
michael@0 130 return get();
michael@0 131 }
michael@0 132
michael@0 133 T* operator->() const
michael@0 134 {
michael@0 135 MOZ_ASSERT(mRawPtr);
michael@0 136 return get();
michael@0 137 }
michael@0 138
michael@0 139 T& operator*() const
michael@0 140 {
michael@0 141 return *get();
michael@0 142 }
michael@0 143
michael@0 144 private:
michael@0 145 void AssignWithAddref(T* newPtr)
michael@0 146 {
michael@0 147 if (newPtr) {
michael@0 148 newPtr->AddRef();
michael@0 149 }
michael@0 150 AssignAssumingAddRef(newPtr);
michael@0 151 }
michael@0 152
michael@0 153 void AssignAssumingAddRef(T* newPtr)
michael@0 154 {
michael@0 155 T* oldPtr = mRawPtr;
michael@0 156 mRawPtr = newPtr;
michael@0 157 if (oldPtr) {
michael@0 158 oldPtr->Release();
michael@0 159 }
michael@0 160 }
michael@0 161
michael@0 162 T* mRawPtr;
michael@0 163 };
michael@0 164
michael@0 165 namespace StaticPtr_internal {
michael@0 166 class Zero;
michael@0 167 } // namespace StaticPtr_internal
michael@0 168
michael@0 169 #define REFLEXIVE_EQUALITY_OPERATORS(type1, type2, eq_fn, ...) \
michael@0 170 template<__VA_ARGS__> \
michael@0 171 inline bool \
michael@0 172 operator==(type1 lhs, type2 rhs) \
michael@0 173 { \
michael@0 174 return eq_fn; \
michael@0 175 } \
michael@0 176 \
michael@0 177 template<__VA_ARGS__> \
michael@0 178 inline bool \
michael@0 179 operator==(type2 lhs, type1 rhs) \
michael@0 180 { \
michael@0 181 return rhs == lhs; \
michael@0 182 } \
michael@0 183 \
michael@0 184 template<__VA_ARGS__> \
michael@0 185 inline bool \
michael@0 186 operator!=(type1 lhs, type2 rhs) \
michael@0 187 { \
michael@0 188 return !(lhs == rhs); \
michael@0 189 } \
michael@0 190 \
michael@0 191 template<__VA_ARGS__> \
michael@0 192 inline bool \
michael@0 193 operator!=(type2 lhs, type1 rhs) \
michael@0 194 { \
michael@0 195 return !(lhs == rhs); \
michael@0 196 }
michael@0 197
michael@0 198 // StaticAutoPtr (in)equality operators
michael@0 199
michael@0 200 template<class T, class U>
michael@0 201 inline bool
michael@0 202 operator==(const StaticAutoPtr<T>& lhs, const StaticAutoPtr<U>& rhs)
michael@0 203 {
michael@0 204 return lhs.get() == rhs.get();
michael@0 205 }
michael@0 206
michael@0 207 template<class T, class U>
michael@0 208 inline bool
michael@0 209 operator!=(const StaticAutoPtr<T>& lhs, const StaticAutoPtr<U>& rhs)
michael@0 210 {
michael@0 211 return !(lhs == rhs);
michael@0 212 }
michael@0 213
michael@0 214 REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, const U*,
michael@0 215 lhs.get() == rhs, class T, class U)
michael@0 216
michael@0 217 REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, U*,
michael@0 218 lhs.get() == rhs, class T, class U)
michael@0 219
michael@0 220 // Let us compare StaticAutoPtr to 0.
michael@0 221 REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, StaticPtr_internal::Zero*,
michael@0 222 lhs.get() == nullptr, class T)
michael@0 223
michael@0 224 // StaticRefPtr (in)equality operators
michael@0 225
michael@0 226 template<class T, class U>
michael@0 227 inline bool
michael@0 228 operator==(const StaticRefPtr<T>& lhs, const StaticRefPtr<U>& rhs)
michael@0 229 {
michael@0 230 return lhs.get() == rhs.get();
michael@0 231 }
michael@0 232
michael@0 233 template<class T, class U>
michael@0 234 inline bool
michael@0 235 operator!=(const StaticRefPtr<T>& lhs, const StaticRefPtr<U>& rhs)
michael@0 236 {
michael@0 237 return !(lhs == rhs);
michael@0 238 }
michael@0 239
michael@0 240 REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, const U*,
michael@0 241 lhs.get() == rhs, class T, class U)
michael@0 242
michael@0 243 REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, U*,
michael@0 244 lhs.get() == rhs, class T, class U)
michael@0 245
michael@0 246 // Let us compare StaticRefPtr to 0.
michael@0 247 REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, StaticPtr_internal::Zero*,
michael@0 248 lhs.get() == nullptr, class T)
michael@0 249
michael@0 250 #undef REFLEXIVE_EQUALITY_OPERATORS
michael@0 251
michael@0 252 } // namespace mozilla
michael@0 253
michael@0 254 #endif

mercurial