Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/base/nsIWeakReferenceUtils.h
Line
Count
Source (jump to first uncovered line)
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 nsIWeakReferenceUtils_h__
8
#define nsIWeakReferenceUtils_h__
9
10
#include "nsCOMPtr.h"
11
#include "nsIWeakReference.h"
12
13
typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
14
15
/**
16
 *
17
 */
18
19
// a type-safe shortcut for calling the |QueryReferent()| member function
20
// T must inherit from nsIWeakReference, but the cast may be ambiguous.
21
template<class T, class DestinationType>
22
inline nsresult
23
CallQueryReferent(T* aSource, DestinationType** aDestination)
24
0
{
25
0
  MOZ_ASSERT(aSource, "null parameter");
26
0
  MOZ_ASSERT(aDestination, "null parameter");
27
0
28
0
  return aSource->QueryReferent(NS_GET_TEMPLATE_IID(DestinationType),
29
0
                                reinterpret_cast<void**>(aDestination));
30
0
}
31
32
33
inline const nsQueryReferent
34
do_QueryReferent(nsIWeakReference* aRawPtr, nsresult* aError = 0)
35
{
36
  return nsQueryReferent(aRawPtr, aError);
37
}
38
39
40
/**
41
 * Deprecated, use |do_GetWeakReference| instead.
42
 */
43
extern nsIWeakReference* NS_GetWeakReference(nsISupports*,
44
                                             nsresult* aResult = 0);
45
extern nsIWeakReference* NS_GetWeakReference(nsISupportsWeakReference*,
46
                                             nsresult* aResult = 0);
47
48
/**
49
 * |do_GetWeakReference| is a convenience function that bundles up all the work needed
50
 * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
51
 * call through to |GetWeakReference|, and put it into your |nsCOMPtr|.
52
 * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
53
 * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|.
54
 */
55
inline already_AddRefed<nsIWeakReference>
56
do_GetWeakReference(nsISupports* aRawPtr, nsresult* aError = 0)
57
{
58
  return dont_AddRef(NS_GetWeakReference(aRawPtr, aError));
59
}
60
61
inline already_AddRefed<nsIWeakReference>
62
do_GetWeakReference(nsISupportsWeakReference* aRawPtr, nsresult* aError = 0)
63
{
64
  return dont_AddRef(NS_GetWeakReference(aRawPtr, aError));
65
}
66
67
inline void
68
do_GetWeakReference(nsIWeakReference* aRawPtr, nsresult* aError = 0)
69
{
70
  // This signature exists solely to _stop_ you from doing a bad thing.
71
  //  Saying |do_GetWeakReference()| on a weak reference itself,
72
  //  is very likely to be a programmer error.
73
}
74
75
template<class T>
76
inline void
77
do_GetWeakReference(already_AddRefed<T>&)
78
{
79
  // This signature exists solely to _stop_ you from doing the bad thing.
80
  //  Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
81
  //  someone else is an automatic leak.  See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
82
}
83
84
template<class T>
85
inline void
86
do_GetWeakReference(already_AddRefed<T>&, nsresult*)
87
{
88
  // This signature exists solely to _stop_ you from doing the bad thing.
89
  //  Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
90
  //  someone else is an automatic leak.  See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
91
}
92
93
#endif