AppData.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef mozilla_AppData_h
  6. #define mozilla_AppData_h
  7. #include "nsXREAppData.h"
  8. #include "nscore.h"
  9. #include "nsStringGlue.h"
  10. #include "nsISupportsUtils.h"
  11. namespace mozilla {
  12. // Like nsXREAppData, but releases all strong refs/allocated memory
  13. // in the destructor.
  14. class ScopedAppData : public nsXREAppData
  15. {
  16. public:
  17. ScopedAppData()
  18. {
  19. Zero();
  20. this->size = sizeof(*this);
  21. }
  22. explicit ScopedAppData(const nsXREAppData* aAppData);
  23. void Zero() { memset(this, 0, sizeof(*this)); }
  24. ~ScopedAppData();
  25. };
  26. /**
  27. * Given |aStr| is holding a string allocated with NS_Alloc, or null:
  28. * replace the value in |aStr| with a new value.
  29. *
  30. * @param aNewValue Null is permitted. The string is cloned with NS_strdup.
  31. */
  32. void SetAllocatedString(const char*& aStr, const char* aNewValue);
  33. /**
  34. * Given "str" is holding a string allocated with NS_Alloc, or null:
  35. * replace the value in "str" with a new value.
  36. *
  37. * @param aNewValue If |aNewValue| is the empty string, |aStr| will be set
  38. * to null.
  39. */
  40. void SetAllocatedString(const char*& aStr, const nsACString& aNewValue);
  41. template<class T>
  42. void
  43. SetStrongPtr(T*& aPtr, T* aNewValue)
  44. {
  45. NS_IF_RELEASE(aPtr);
  46. aPtr = aNewValue;
  47. NS_IF_ADDREF(aPtr);
  48. }
  49. } // namespace mozilla
  50. #endif