1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_AppData_h
8 #define mozilla_AppData_h
9 
10 #include "nsXREAppData.h"
11 #include "nscore.h"
12 #include "nsStringGlue.h"
13 #include "nsISupportsUtils.h"
14 
15 namespace mozilla {
16 
17 // Like nsXREAppData, but releases all strong refs/allocated memory
18 // in the destructor.
19 class ScopedAppData : public nsXREAppData
20 {
21 public:
ScopedAppData()22   ScopedAppData()
23   {
24     Zero();
25     this->size = sizeof(*this);
26   }
27 
28   explicit ScopedAppData(const nsXREAppData* aAppData);
29 
Zero()30   void Zero() { memset(this, 0, sizeof(*this)); }
31 
32   ~ScopedAppData();
33 };
34 
35 /**
36  * Given |aStr| is holding a string allocated with NS_Alloc, or null:
37  * replace the value in |aStr| with a new value.
38  *
39  * @param aNewValue Null is permitted. The string is cloned with NS_strdup.
40  */
41 void SetAllocatedString(const char*& aStr, const char* aNewValue);
42 
43 /**
44  * Given "str" is holding a string allocated with NS_Alloc, or null:
45  * replace the value in "str" with a new value.
46  *
47  * @param aNewValue If |aNewValue| is the empty string, |aStr| will be set
48  *                  to null.
49  */
50 void SetAllocatedString(const char*& aStr, const nsACString& aNewValue);
51 
52 template<class T>
53 void
SetStrongPtr(T * & aPtr,T * aNewValue)54 SetStrongPtr(T*& aPtr, T* aNewValue)
55 {
56   NS_IF_RELEASE(aPtr);
57   aPtr = aNewValue;
58   NS_IF_ADDREF(aPtr);
59 }
60 
61 } // namespace mozilla
62 
63 #endif
64