xpcom/glue/AppData.h

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

mercurial