michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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: #ifndef mozilla_AppData_h michael@0: #define mozilla_AppData_h michael@0: michael@0: #include "nsXREAppData.h" michael@0: #include "nscore.h" michael@0: #include "nsStringGlue.h" michael@0: #include "nsISupportsUtils.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: // Like nsXREAppData, but releases all strong refs/allocated memory michael@0: // in the destructor. michael@0: class NS_COM_GLUE ScopedAppData : public nsXREAppData michael@0: { michael@0: public: michael@0: ScopedAppData() { Zero(); this->size = sizeof(*this); } michael@0: michael@0: ScopedAppData(const nsXREAppData* aAppData); michael@0: michael@0: void Zero() { memset(this, 0, sizeof(*this)); } michael@0: michael@0: ~ScopedAppData(); michael@0: }; michael@0: michael@0: /** michael@0: * Given "str" is holding a string allocated with NS_Alloc, or null: michael@0: * replace the value in "str" with a new value. michael@0: * michael@0: * @param newvalue Null is permitted. The string is cloned with michael@0: * NS_strdup michael@0: */ michael@0: void SetAllocatedString(const char *&str, const char *newvalue); michael@0: michael@0: /** michael@0: * Given "str" is holding a string allocated with NS_Alloc, or null: michael@0: * replace the value in "str" with a new value. michael@0: * michael@0: * @param newvalue If "newvalue" is the empty string, "str" will be set michael@0: * to null. michael@0: */ michael@0: void SetAllocatedString(const char *&str, const nsACString &newvalue); michael@0: michael@0: template michael@0: void SetStrongPtr(T *&ptr, T* newvalue) michael@0: { michael@0: NS_IF_RELEASE(ptr); michael@0: ptr = newvalue; michael@0: NS_IF_ADDREF(ptr); michael@0: } michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif