1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/base/StaticPtr.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,254 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set sw=2 ts=8 et ft=cpp : */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_StaticPtr_h 1.11 +#define mozilla_StaticPtr_h 1.12 + 1.13 +#include "mozilla/Assertions.h" 1.14 +#include "mozilla/NullPtr.h" 1.15 + 1.16 +namespace mozilla { 1.17 + 1.18 +/** 1.19 + * StaticAutoPtr and StaticRefPtr are like nsAutoPtr and nsRefPtr, except they 1.20 + * are suitable for use as global variables. 1.21 + * 1.22 + * In particular, a global instance of Static{Auto,Ref}Ptr doesn't cause the 1.23 + * compiler to emit a static initializer (in release builds, anyway). 1.24 + * 1.25 + * In order to accomplish this, Static{Auto,Ref}Ptr must have a trivial 1.26 + * constructor and destructor. As a consequence, it cannot initialize its raw 1.27 + * pointer to 0 on construction, and it cannot delete/release its raw pointer 1.28 + * upon destruction. 1.29 + * 1.30 + * Since the compiler guarantees that all global variables are initialized to 1.31 + * 0, these trivial constructors are safe, so long as you use 1.32 + * Static{Auto,Ref}Ptr as a global variable. If you use Static{Auto,Ref}Ptr as 1.33 + * a stack variable or as a class instance variable, you will get what you 1.34 + * deserve. 1.35 + * 1.36 + * Static{Auto,Ref}Ptr have a limited interface as compared to ns{Auto,Ref}Ptr; 1.37 + * this is intentional, since their range of acceptable uses is smaller. 1.38 + */ 1.39 + 1.40 +template<class T> 1.41 +class StaticAutoPtr 1.42 +{ 1.43 + public: 1.44 + // In debug builds, check that mRawPtr is initialized for us as we expect 1.45 + // by the compiler. In non-debug builds, don't declare a constructor 1.46 + // so that the compiler can see that the constructor is trivial. 1.47 +#ifdef DEBUG 1.48 + StaticAutoPtr() 1.49 + { 1.50 + MOZ_ASSERT(!mRawPtr); 1.51 + } 1.52 +#endif 1.53 + 1.54 + StaticAutoPtr<T>& operator=(T* rhs) 1.55 + { 1.56 + Assign(rhs); 1.57 + return *this; 1.58 + } 1.59 + 1.60 + T* get() const 1.61 + { 1.62 + return mRawPtr; 1.63 + } 1.64 + 1.65 + operator T*() const 1.66 + { 1.67 + return get(); 1.68 + } 1.69 + 1.70 + T* operator->() const 1.71 + { 1.72 + MOZ_ASSERT(mRawPtr); 1.73 + return get(); 1.74 + } 1.75 + 1.76 + T& operator*() const 1.77 + { 1.78 + return *get(); 1.79 + } 1.80 + 1.81 + private: 1.82 + // Disallow copy constructor, but only in debug mode. We only define 1.83 + // a default constructor in debug mode (see above); if we declared 1.84 + // this constructor always, the compiler wouldn't generate a trivial 1.85 + // default constructor for us in non-debug mode. 1.86 +#ifdef DEBUG 1.87 + StaticAutoPtr(StaticAutoPtr<T> &other); 1.88 +#endif 1.89 + 1.90 + void Assign(T* newPtr) 1.91 + { 1.92 + MOZ_ASSERT(!newPtr || mRawPtr != newPtr); 1.93 + T* oldPtr = mRawPtr; 1.94 + mRawPtr = newPtr; 1.95 + delete oldPtr; 1.96 + } 1.97 + 1.98 + T* mRawPtr; 1.99 +}; 1.100 + 1.101 +template<class T> 1.102 +class StaticRefPtr 1.103 +{ 1.104 +public: 1.105 + // In debug builds, check that mRawPtr is initialized for us as we expect 1.106 + // by the compiler. In non-debug builds, don't declare a constructor 1.107 + // so that the compiler can see that the constructor is trivial. 1.108 +#ifdef DEBUG 1.109 + StaticRefPtr() 1.110 + { 1.111 + MOZ_ASSERT(!mRawPtr); 1.112 + } 1.113 +#endif 1.114 + 1.115 + StaticRefPtr<T>& operator=(T* rhs) 1.116 + { 1.117 + AssignWithAddref(rhs); 1.118 + return *this; 1.119 + } 1.120 + 1.121 + StaticRefPtr<T>& operator=(const StaticRefPtr<T>& rhs) 1.122 + { 1.123 + return (this = rhs.mRawPtr); 1.124 + } 1.125 + 1.126 + T* get() const 1.127 + { 1.128 + return mRawPtr; 1.129 + } 1.130 + 1.131 + operator T*() const 1.132 + { 1.133 + return get(); 1.134 + } 1.135 + 1.136 + T* operator->() const 1.137 + { 1.138 + MOZ_ASSERT(mRawPtr); 1.139 + return get(); 1.140 + } 1.141 + 1.142 + T& operator*() const 1.143 + { 1.144 + return *get(); 1.145 + } 1.146 + 1.147 +private: 1.148 + void AssignWithAddref(T* newPtr) 1.149 + { 1.150 + if (newPtr) { 1.151 + newPtr->AddRef(); 1.152 + } 1.153 + AssignAssumingAddRef(newPtr); 1.154 + } 1.155 + 1.156 + void AssignAssumingAddRef(T* newPtr) 1.157 + { 1.158 + T* oldPtr = mRawPtr; 1.159 + mRawPtr = newPtr; 1.160 + if (oldPtr) { 1.161 + oldPtr->Release(); 1.162 + } 1.163 + } 1.164 + 1.165 + T* mRawPtr; 1.166 +}; 1.167 + 1.168 +namespace StaticPtr_internal { 1.169 +class Zero; 1.170 +} // namespace StaticPtr_internal 1.171 + 1.172 +#define REFLEXIVE_EQUALITY_OPERATORS(type1, type2, eq_fn, ...) \ 1.173 + template<__VA_ARGS__> \ 1.174 + inline bool \ 1.175 + operator==(type1 lhs, type2 rhs) \ 1.176 + { \ 1.177 + return eq_fn; \ 1.178 + } \ 1.179 + \ 1.180 + template<__VA_ARGS__> \ 1.181 + inline bool \ 1.182 + operator==(type2 lhs, type1 rhs) \ 1.183 + { \ 1.184 + return rhs == lhs; \ 1.185 + } \ 1.186 + \ 1.187 + template<__VA_ARGS__> \ 1.188 + inline bool \ 1.189 + operator!=(type1 lhs, type2 rhs) \ 1.190 + { \ 1.191 + return !(lhs == rhs); \ 1.192 + } \ 1.193 + \ 1.194 + template<__VA_ARGS__> \ 1.195 + inline bool \ 1.196 + operator!=(type2 lhs, type1 rhs) \ 1.197 + { \ 1.198 + return !(lhs == rhs); \ 1.199 + } 1.200 + 1.201 +// StaticAutoPtr (in)equality operators 1.202 + 1.203 +template<class T, class U> 1.204 +inline bool 1.205 +operator==(const StaticAutoPtr<T>& lhs, const StaticAutoPtr<U>& rhs) 1.206 +{ 1.207 + return lhs.get() == rhs.get(); 1.208 +} 1.209 + 1.210 +template<class T, class U> 1.211 +inline bool 1.212 +operator!=(const StaticAutoPtr<T>& lhs, const StaticAutoPtr<U>& rhs) 1.213 +{ 1.214 + return !(lhs == rhs); 1.215 +} 1.216 + 1.217 +REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, const U*, 1.218 + lhs.get() == rhs, class T, class U) 1.219 + 1.220 +REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, U*, 1.221 + lhs.get() == rhs, class T, class U) 1.222 + 1.223 +// Let us compare StaticAutoPtr to 0. 1.224 +REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, StaticPtr_internal::Zero*, 1.225 + lhs.get() == nullptr, class T) 1.226 + 1.227 +// StaticRefPtr (in)equality operators 1.228 + 1.229 +template<class T, class U> 1.230 +inline bool 1.231 +operator==(const StaticRefPtr<T>& lhs, const StaticRefPtr<U>& rhs) 1.232 +{ 1.233 + return lhs.get() == rhs.get(); 1.234 +} 1.235 + 1.236 +template<class T, class U> 1.237 +inline bool 1.238 +operator!=(const StaticRefPtr<T>& lhs, const StaticRefPtr<U>& rhs) 1.239 +{ 1.240 + return !(lhs == rhs); 1.241 +} 1.242 + 1.243 +REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, const U*, 1.244 + lhs.get() == rhs, class T, class U) 1.245 + 1.246 +REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, U*, 1.247 + lhs.get() == rhs, class T, class U) 1.248 + 1.249 +// Let us compare StaticRefPtr to 0. 1.250 +REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, StaticPtr_internal::Zero*, 1.251 + lhs.get() == nullptr, class T) 1.252 + 1.253 +#undef REFLEXIVE_EQUALITY_OPERATORS 1.254 + 1.255 +} // namespace mozilla 1.256 + 1.257 +#endif