2012-07-26 23:33:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-05-05 21:30:39 +04:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-07-26 23:33:45 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_StaticPtr_h
|
|
|
|
#define mozilla_StaticPtr_h
|
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-12-23 02:10:44 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2016-07-21 19:54:17 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
|
2012-07-26 23:33:45 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* StaticAutoPtr and StaticRefPtr are like nsAutoPtr and nsRefPtr, except they
|
|
|
|
* are suitable for use as global variables.
|
|
|
|
*
|
|
|
|
* In particular, a global instance of Static{Auto,Ref}Ptr doesn't cause the
|
|
|
|
* compiler to emit a static initializer (in release builds, anyway).
|
|
|
|
*
|
|
|
|
* In order to accomplish this, Static{Auto,Ref}Ptr must have a trivial
|
|
|
|
* constructor and destructor. As a consequence, it cannot initialize its raw
|
|
|
|
* pointer to 0 on construction, and it cannot delete/release its raw pointer
|
|
|
|
* upon destruction.
|
|
|
|
*
|
|
|
|
* Since the compiler guarantees that all global variables are initialized to
|
2014-12-23 02:10:44 +03:00
|
|
|
* 0, these trivial constructors are safe. Since we rely on this, the clang
|
|
|
|
* plugin, run as part of our "static analysis" builds, makes it a compile-time
|
|
|
|
* error to use Static{Auto,Ref}Ptr as anything except a global variable.
|
2012-07-26 23:33:45 +04:00
|
|
|
*
|
|
|
|
* Static{Auto,Ref}Ptr have a limited interface as compared to ns{Auto,Ref}Ptr;
|
|
|
|
* this is intentional, since their range of acceptable uses is smaller.
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <class T>
|
2014-12-23 02:10:44 +03:00
|
|
|
class MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS StaticAutoPtr {
|
2014-05-05 21:30:39 +04:00
|
|
|
public:
|
|
|
|
// In debug builds, check that mRawPtr is initialized for us as we expect
|
|
|
|
// by the compiler. In non-debug builds, don't declare a constructor
|
|
|
|
// so that the compiler can see that the constructor is trivial.
|
2012-11-10 01:48:42 +04:00
|
|
|
#ifdef DEBUG
|
2014-05-05 21:30:39 +04:00
|
|
|
StaticAutoPtr() {
|
2018-01-17 17:58:00 +03:00
|
|
|
# ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wuninitialized"
|
|
|
|
// False positive with gcc. See bug 1430729
|
|
|
|
# endif
|
2014-05-05 21:30:39 +04:00
|
|
|
MOZ_ASSERT(!mRawPtr);
|
2018-01-17 17:58:00 +03:00
|
|
|
# ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
# endif
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
2012-11-10 01:48:42 +04:00
|
|
|
#endif
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
StaticAutoPtr<T>& operator=(T* aRhs) {
|
|
|
|
Assign(aRhs);
|
2014-05-05 21:30:39 +04:00
|
|
|
return *this;
|
|
|
|
}
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-08-25 23:17:24 +04:00
|
|
|
T* get() const { return mRawPtr; }
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-08-25 23:17:24 +04:00
|
|
|
operator T*() const { return get(); }
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
T* operator->() const {
|
|
|
|
MOZ_ASSERT(mRawPtr);
|
|
|
|
return get();
|
|
|
|
}
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-08-25 23:17:24 +04:00
|
|
|
T& operator*() const { return *get(); }
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2017-07-06 01:19:47 +03:00
|
|
|
T* forget() {
|
|
|
|
T* temp = mRawPtr;
|
|
|
|
mRawPtr = nullptr;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
private:
|
|
|
|
// Disallow copy constructor, but only in debug mode. We only define
|
|
|
|
// a default constructor in debug mode (see above); if we declared
|
|
|
|
// this constructor always, the compiler wouldn't generate a trivial
|
|
|
|
// default constructor for us in non-debug mode.
|
2012-11-10 01:48:42 +04:00
|
|
|
#ifdef DEBUG
|
2014-05-15 00:14:58 +04:00
|
|
|
StaticAutoPtr(StaticAutoPtr<T>& aOther);
|
2012-11-10 01:48:42 +04:00
|
|
|
#endif
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void Assign(T* aNewPtr) {
|
|
|
|
MOZ_ASSERT(!aNewPtr || mRawPtr != aNewPtr);
|
2014-05-05 21:30:39 +04:00
|
|
|
T* oldPtr = mRawPtr;
|
2014-05-13 21:41:38 +04:00
|
|
|
mRawPtr = aNewPtr;
|
2014-05-05 21:30:39 +04:00
|
|
|
delete oldPtr;
|
|
|
|
}
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
T* mRawPtr;
|
2012-07-26 23:33:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2014-12-23 02:10:44 +03:00
|
|
|
class MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS StaticRefPtr {
|
2012-07-26 23:33:45 +04:00
|
|
|
public:
|
2012-11-10 01:48:42 +04:00
|
|
|
// In debug builds, check that mRawPtr is initialized for us as we expect
|
|
|
|
// by the compiler. In non-debug builds, don't declare a constructor
|
|
|
|
// so that the compiler can see that the constructor is trivial.
|
|
|
|
#ifdef DEBUG
|
2012-07-26 23:33:45 +04:00
|
|
|
StaticRefPtr() {
|
2018-01-16 18:19:33 +03:00
|
|
|
# ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wuninitialized"
|
|
|
|
// False positive with gcc. See bug 1430729
|
|
|
|
# endif
|
2012-07-26 23:33:45 +04:00
|
|
|
MOZ_ASSERT(!mRawPtr);
|
2018-01-16 18:19:33 +03:00
|
|
|
# ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
# endif
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
2012-11-10 01:48:42 +04:00
|
|
|
#endif
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
StaticRefPtr<T>& operator=(T* aRhs) {
|
|
|
|
AssignWithAddref(aRhs);
|
2012-07-26 23:33:45 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
StaticRefPtr<T>& operator=(const StaticRefPtr<T>& aRhs) {
|
|
|
|
return (this = aRhs.mRawPtr);
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
StaticRefPtr<T>& operator=(already_AddRefed<T>& aRhs) {
|
|
|
|
AssignAssumingAddRef(aRhs.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticRefPtr<T>& operator=(already_AddRefed<T>&& aRhs) {
|
|
|
|
AssignAssumingAddRef(aRhs.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<T> forget() {
|
|
|
|
T* temp = mRawPtr;
|
|
|
|
mRawPtr = nullptr;
|
|
|
|
return already_AddRefed<T>(temp);
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:24 +04:00
|
|
|
T* get() const { return mRawPtr; }
|
2012-07-26 23:33:45 +04:00
|
|
|
|
2014-08-25 23:17:24 +04:00
|
|
|
operator T*() const { return get(); }
|
2012-07-26 23:33:45 +04:00
|
|
|
|
|
|
|
T* operator->() const {
|
|
|
|
MOZ_ASSERT(mRawPtr);
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2014-08-25 23:17:24 +04:00
|
|
|
T& operator*() const { return *get(); }
|
2012-07-26 23:33:45 +04:00
|
|
|
|
|
|
|
private:
|
2014-05-13 21:41:38 +04:00
|
|
|
void AssignWithAddref(T* aNewPtr) {
|
|
|
|
if (aNewPtr) {
|
|
|
|
aNewPtr->AddRef();
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
AssignAssumingAddRef(aNewPtr);
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void AssignAssumingAddRef(T* aNewPtr) {
|
2012-07-26 23:33:45 +04:00
|
|
|
T* oldPtr = mRawPtr;
|
2014-05-13 21:41:38 +04:00
|
|
|
mRawPtr = aNewPtr;
|
2012-07-26 23:33:45 +04:00
|
|
|
if (oldPtr) {
|
|
|
|
oldPtr->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 05:17:50 +03:00
|
|
|
T* MOZ_OWNING_REF mRawPtr;
|
2012-07-26 23:33:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace StaticPtr_internal {
|
|
|
|
class Zero;
|
|
|
|
} // namespace StaticPtr_internal
|
|
|
|
|
|
|
|
#define REFLEXIVE_EQUALITY_OPERATORS(type1, type2, eq_fn, ...) \
|
|
|
|
template <__VA_ARGS__> \
|
|
|
|
inline bool operator==(type1 lhs, type2 rhs) { \
|
|
|
|
return eq_fn; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template <__VA_ARGS__> \
|
|
|
|
inline bool operator==(type2 lhs, type1 rhs) { \
|
|
|
|
return rhs == lhs; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template <__VA_ARGS__> \
|
|
|
|
inline bool operator!=(type1 lhs, type2 rhs) { \
|
|
|
|
return !(lhs == rhs); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template <__VA_ARGS__> \
|
|
|
|
inline bool operator!=(type2 lhs, type1 rhs) { \
|
|
|
|
return !(lhs == rhs); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// StaticAutoPtr (in)equality operators
|
|
|
|
|
|
|
|
template <class T, class U>
|
2014-05-13 21:41:38 +04:00
|
|
|
inline bool operator==(const StaticAutoPtr<T>& aLhs,
|
|
|
|
const StaticAutoPtr<U>& aRhs) {
|
|
|
|
return aLhs.get() == aRhs.get();
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
2014-05-13 21:41:38 +04:00
|
|
|
inline bool operator!=(const StaticAutoPtr<T>& aLhs,
|
|
|
|
const StaticAutoPtr<U>& aRhs) {
|
|
|
|
return !(aLhs == aRhs);
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, const U*,
|
|
|
|
lhs.get() == rhs, class T, class U)
|
|
|
|
|
|
|
|
REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, U*, lhs.get() == rhs,
|
|
|
|
class T, class U)
|
|
|
|
|
|
|
|
// Let us compare StaticAutoPtr to 0.
|
|
|
|
REFLEXIVE_EQUALITY_OPERATORS(const StaticAutoPtr<T>&, StaticPtr_internal::Zero*,
|
2013-04-26 04:53:26 +04:00
|
|
|
lhs.get() == nullptr, class T)
|
2012-07-26 23:33:45 +04:00
|
|
|
|
|
|
|
// StaticRefPtr (in)equality operators
|
|
|
|
|
|
|
|
template <class T, class U>
|
2014-05-13 21:41:38 +04:00
|
|
|
inline bool operator==(const StaticRefPtr<T>& aLhs,
|
|
|
|
const StaticRefPtr<U>& aRhs) {
|
|
|
|
return aLhs.get() == aRhs.get();
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
2014-05-13 21:41:38 +04:00
|
|
|
inline bool operator!=(const StaticRefPtr<T>& aLhs,
|
|
|
|
const StaticRefPtr<U>& aRhs) {
|
|
|
|
return !(aLhs == aRhs);
|
2012-07-26 23:33:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, const U*, lhs.get() == rhs,
|
|
|
|
class T, class U)
|
|
|
|
|
|
|
|
REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, U*, lhs.get() == rhs,
|
|
|
|
class T, class U)
|
|
|
|
|
|
|
|
// Let us compare StaticRefPtr to 0.
|
|
|
|
REFLEXIVE_EQUALITY_OPERATORS(const StaticRefPtr<T>&, StaticPtr_internal::Zero*,
|
2013-04-26 04:53:26 +04:00
|
|
|
lhs.get() == nullptr, class T)
|
2012-07-26 23:33:45 +04:00
|
|
|
|
|
|
|
#undef REFLEXIVE_EQUALITY_OPERATORS
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-07-21 19:54:17 +03:00
|
|
|
// Declared in mozilla/RefPtr.h
|
|
|
|
template <class T>
|
|
|
|
template <class U>
|
|
|
|
RefPtr<T>::RefPtr(const mozilla::StaticRefPtr<U>& aOther)
|
|
|
|
: RefPtr(aOther.get()) {}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
template <class U>
|
|
|
|
RefPtr<T>& RefPtr<T>::operator=(const mozilla::StaticRefPtr<U>& aOther) {
|
|
|
|
return operator=(aOther.get());
|
|
|
|
}
|
|
|
|
|
2017-09-05 01:05:10 +03:00
|
|
|
template <class T>
|
|
|
|
inline already_AddRefed<T> do_AddRef(const mozilla::StaticRefPtr<T>& aObj) {
|
|
|
|
RefPtr<T> ref(aObj);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2012-07-26 23:33:45 +04:00
|
|
|
#endif
|