michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* A class for non-null strong pointers to reference-counted objects. */ michael@0: michael@0: #ifndef mozilla_dom_OwningNonNull_h michael@0: #define mozilla_dom_OwningNonNull_h michael@0: michael@0: #include "nsAutoPtr.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: template michael@0: class OwningNonNull michael@0: { michael@0: public: michael@0: OwningNonNull() michael@0: #ifdef DEBUG michael@0: : mInited(false) michael@0: #endif michael@0: {} michael@0: michael@0: operator T&() michael@0: { michael@0: MOZ_ASSERT(mInited); michael@0: MOZ_ASSERT(mPtr, "OwningNonNull was set to null"); michael@0: return *mPtr; michael@0: } michael@0: michael@0: operator T*() michael@0: { michael@0: MOZ_ASSERT(mInited); michael@0: MOZ_ASSERT(mPtr, "OwningNonNull was set to null"); michael@0: return mPtr; michael@0: } michael@0: michael@0: void operator=(T* aValue) michael@0: { michael@0: init(aValue); michael@0: } michael@0: michael@0: void operator=(const already_AddRefed& aValue) michael@0: { michael@0: init(aValue); michael@0: } michael@0: michael@0: already_AddRefed forget() michael@0: { michael@0: #ifdef DEBUG michael@0: mInited = false; michael@0: #endif michael@0: return mPtr.forget(); michael@0: } michael@0: michael@0: // Make us work with smart pointer helpers that expect a get(). michael@0: T* get() const michael@0: { michael@0: MOZ_ASSERT(mInited); michael@0: MOZ_ASSERT(mPtr); michael@0: return mPtr; michael@0: } michael@0: michael@0: protected: michael@0: template michael@0: void init(U aValue) michael@0: { michael@0: mPtr = aValue; michael@0: MOZ_ASSERT(mPtr); michael@0: #ifdef DEBUG michael@0: mInited = true; michael@0: #endif michael@0: } michael@0: michael@0: nsRefPtr mPtr; michael@0: #ifdef DEBUG michael@0: bool mInited; michael@0: #endif michael@0: }; michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_dom_OwningNonNull_h