xpcom/glue/AppData.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/AppData.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,58 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_AppData_h
    1.10 +#define mozilla_AppData_h
    1.11 +
    1.12 +#include "nsXREAppData.h"
    1.13 +#include "nscore.h"
    1.14 +#include "nsStringGlue.h"
    1.15 +#include "nsISupportsUtils.h"
    1.16 +
    1.17 +namespace mozilla {
    1.18 +
    1.19 +// Like nsXREAppData, but releases all strong refs/allocated memory
    1.20 +// in the destructor.
    1.21 +class NS_COM_GLUE ScopedAppData : public nsXREAppData
    1.22 +{
    1.23 +public:
    1.24 +  ScopedAppData() { Zero(); this->size = sizeof(*this); }
    1.25 +
    1.26 +  ScopedAppData(const nsXREAppData* aAppData);
    1.27 +
    1.28 +  void Zero() { memset(this, 0, sizeof(*this)); }
    1.29 +
    1.30 +  ~ScopedAppData();
    1.31 +};
    1.32 +
    1.33 +/**
    1.34 + * Given "str" is holding a string allocated with NS_Alloc, or null:
    1.35 + * replace the value in "str" with a new value.
    1.36 + *
    1.37 + * @param newvalue Null is permitted. The string is cloned with
    1.38 + *                 NS_strdup
    1.39 + */
    1.40 +void SetAllocatedString(const char *&str, const char *newvalue);
    1.41 +
    1.42 +/**
    1.43 + * Given "str" is holding a string allocated with NS_Alloc, or null:
    1.44 + * replace the value in "str" with a new value.
    1.45 + *
    1.46 + * @param newvalue If "newvalue" is the empty string, "str" will be set
    1.47 + *                 to null.
    1.48 + */
    1.49 +void SetAllocatedString(const char *&str, const nsACString &newvalue);
    1.50 +
    1.51 +template<class T>
    1.52 +void SetStrongPtr(T *&ptr, T* newvalue)
    1.53 +{
    1.54 +  NS_IF_RELEASE(ptr);
    1.55 +  ptr = newvalue;
    1.56 +  NS_IF_ADDREF(ptr);
    1.57 +}
    1.58 +
    1.59 +} // namespace mozilla
    1.60 +
    1.61 +#endif

mercurial