2012-07-26 19:33:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-05-05 17:30:39 +00:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-07-26 19:33:45 +00: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
|
|
|
|
|
2013-04-26 00:53:26 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/NullPtr.h"
|
|
|
|
|
2012-07-26 19:33:45 +00: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
|
|
|
|
* 0, these trivial constructors are safe, so long as you use
|
|
|
|
* Static{Auto,Ref}Ptr as a global variable. If you use Static{Auto,Ref}Ptr as
|
|
|
|
* a stack variable or as a class instance variable, you will get what you
|
|
|
|
* deserve.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
class StaticAutoPtr
|
|
|
|
{
|
2014-05-05 17:30:39 +00: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-09 21:48:42 +00:00
|
|
|
#ifdef DEBUG
|
2014-05-05 17:30:39 +00:00
|
|
|
StaticAutoPtr()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mRawPtr);
|
|
|
|
}
|
2012-11-09 21:48:42 +00:00
|
|
|
#endif
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
StaticAutoPtr<T>& operator=(T* aRhs)
|
2014-05-05 17:30:39 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
Assign(aRhs);
|
2014-05-05 17:30:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-08-25 19:17:24 +00:00
|
|
|
T* get() const { return mRawPtr; }
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-08-25 19:17:24 +00:00
|
|
|
operator T*() const { return get(); }
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-05-05 17:30:39 +00:00
|
|
|
T* operator->() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRawPtr);
|
|
|
|
return get();
|
|
|
|
}
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-08-25 19:17:24 +00:00
|
|
|
T& operator*() const { return *get(); }
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-05-05 17:30:39 +00: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-09 21:48:42 +00:00
|
|
|
#ifdef DEBUG
|
2014-05-14 20:14:58 +00:00
|
|
|
StaticAutoPtr(StaticAutoPtr<T>& aOther);
|
2012-11-09 21:48:42 +00:00
|
|
|
#endif
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
void Assign(T* aNewPtr)
|
2014-05-05 17:30:39 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
MOZ_ASSERT(!aNewPtr || mRawPtr != aNewPtr);
|
2014-05-05 17:30:39 +00:00
|
|
|
T* oldPtr = mRawPtr;
|
2014-05-13 17:41:38 +00:00
|
|
|
mRawPtr = aNewPtr;
|
2014-05-05 17:30:39 +00:00
|
|
|
delete oldPtr;
|
|
|
|
}
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-05-05 17:30:39 +00:00
|
|
|
T* mRawPtr;
|
2012-07-26 19:33:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class StaticRefPtr
|
|
|
|
{
|
|
|
|
public:
|
2012-11-09 21:48:42 +00: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 19:33:45 +00:00
|
|
|
StaticRefPtr()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mRawPtr);
|
|
|
|
}
|
2012-11-09 21:48:42 +00:00
|
|
|
#endif
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
StaticRefPtr<T>& operator=(T* aRhs)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
AssignWithAddref(aRhs);
|
2012-07-26 19:33:45 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
StaticRefPtr<T>& operator=(const StaticRefPtr<T>& aRhs)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
return (this = aRhs.mRawPtr);
|
2012-07-26 19:33:45 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 19:17:24 +00:00
|
|
|
T* get() const { return mRawPtr; }
|
2012-07-26 19:33:45 +00:00
|
|
|
|
2014-08-25 19:17:24 +00:00
|
|
|
operator T*() const { return get(); }
|
2012-07-26 19:33:45 +00:00
|
|
|
|
|
|
|
T* operator->() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRawPtr);
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2014-08-25 19:17:24 +00:00
|
|
|
T& operator*() const { return *get(); }
|
2012-07-26 19:33:45 +00:00
|
|
|
|
|
|
|
private:
|
2014-05-13 17:41:38 +00:00
|
|
|
void AssignWithAddref(T* aNewPtr)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
if (aNewPtr) {
|
|
|
|
aNewPtr->AddRef();
|
2012-07-26 19:33:45 +00:00
|
|
|
}
|
2014-05-13 17:41:38 +00:00
|
|
|
AssignAssumingAddRef(aNewPtr);
|
2012-07-26 19:33:45 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
void AssignAssumingAddRef(T* aNewPtr)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
|
|
|
T* oldPtr = mRawPtr;
|
2014-05-13 17:41:38 +00:00
|
|
|
mRawPtr = aNewPtr;
|
2012-07-26 19:33:45 +00:00
|
|
|
if (oldPtr) {
|
|
|
|
oldPtr->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* mRawPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
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>
|
|
|
|
inline bool
|
2014-05-13 17:41:38 +00:00
|
|
|
operator==(const StaticAutoPtr<T>& aLhs, const StaticAutoPtr<U>& aRhs)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
return aLhs.get() == aRhs.get();
|
2012-07-26 19:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool
|
2014-05-13 17:41:38 +00:00
|
|
|
operator!=(const StaticAutoPtr<T>& aLhs, const StaticAutoPtr<U>& aRhs)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
return !(aLhs == aRhs);
|
2012-07-26 19:33:45 +00: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 00:53:26 +00:00
|
|
|
lhs.get() == nullptr, class T)
|
2012-07-26 19:33:45 +00:00
|
|
|
|
|
|
|
// StaticRefPtr (in)equality operators
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool
|
2014-05-13 17:41:38 +00:00
|
|
|
operator==(const StaticRefPtr<T>& aLhs, const StaticRefPtr<U>& aRhs)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
return aLhs.get() == aRhs.get();
|
2012-07-26 19:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool
|
2014-05-13 17:41:38 +00:00
|
|
|
operator!=(const StaticRefPtr<T>& aLhs, const StaticRefPtr<U>& aRhs)
|
2012-07-26 19:33:45 +00:00
|
|
|
{
|
2014-05-13 17:41:38 +00:00
|
|
|
return !(aLhs == aRhs);
|
2012-07-26 19:33:45 +00: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 00:53:26 +00:00
|
|
|
lhs.get() == nullptr, class T)
|
2012-07-26 19:33:45 +00:00
|
|
|
|
|
|
|
#undef REFLEXIVE_EQUALITY_OPERATORS
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|