-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnsweak.h
180 lines (136 loc) · 3.65 KB
/
nsweak.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
///////////////////////////////////////////////////////////////////////////////
// Name: nsweak.h
// Purpose: wxwebconnect: embedded web browser control library
// Author: Benjamin I. Williams
// Modified by:
// Created: 2006-10-08
// RCS-ID:
// Copyright: (C) Copyright 2006-2010, Kirix Corporation, All Rights Reserved.
// Licence: wxWindows Library Licence, Version 3.1
///////////////////////////////////////////////////////////////////////////////
#ifndef __WXWEBCONNECT_NSWEAK_H
#define __WXWEBCONNECT_NSWEAK_H
class nsSupportsWeakReferenceBase : public nsISupportsWeakReference
{
public:
virtual void OnWeakReferenceDestroyed() = 0;
};
class nsWeakReference : public nsIWeakReference
{
public:
nsWeakReference(nsSupportsWeakReferenceBase* obj)
{
m_w_obj = obj;
m_w_ref_count = 0;
}
~nsWeakReference()
{
if (m_w_obj)
{
m_w_obj->OnWeakReferenceDestroyed();
}
}
NS_IMETHOD QueryReferent(const nsIID& iid, void** result)
{
if (result)
*result = 0;
if (!m_w_obj)
return NS_ERROR_NULL_POINTER;
return m_w_obj->QueryInterface(iid, result);
}
void Clear()
{
m_w_obj = 0;
}
// nsISupports implementation
NS_IMETHOD QueryInterface(const nsIID& iid, void** result)
{
*result = NULL;
if (iid.Equals(NS_GET_IID(nsIWeakReference)))
{
*result = (void*)(nsIWeakReference*)this;
}
else
{
return NS_ERROR_NO_INTERFACE;
}
this->AddRef();
return NS_OK;
}
NS_IMETHOD_(nsrefcnt) AddRef()
{
return ++m_w_ref_count;
}
NS_IMETHOD_(nsrefcnt) Release()
{
if (--m_w_ref_count == 0)
{
delete this;
return 0;
}
return m_w_ref_count;
}
private:
nsSupportsWeakReferenceBase* m_w_obj;
nsrefcnt m_w_ref_count;
};
class nsSupportsWeakReference : public nsSupportsWeakReferenceBase
{
public:
nsSupportsWeakReference()
{
m_weak = NULL;
}
~nsSupportsWeakReference()
{
if (m_weak)
m_weak->Clear();
}
NS_IMETHOD GetWeakReference(nsIWeakReference** result)
{
if (!result)
return NS_ERROR_NULL_POINTER;
if (!m_weak)
{
m_weak = new nsWeakReference(this);
if (!m_weak)
{
*result = NULL;
return NS_ERROR_OUT_OF_MEMORY;
}
}
m_weak->AddRef();
*result = static_cast<nsIWeakReference*>(m_weak);
return NS_OK;
}
void OnWeakReferenceDestroyed()
{
m_weak = NULL;
}
private:
nsWeakReference* m_weak;
};
inline nsIWeakReference* NS_GetWeakReference(nsISupports* obj, nsresult* error = NULL)
{
nsIWeakReference* result = NULL;
if (!obj)
{
if (error)
*error = NS_ERROR_NULL_POINTER;
return NULL;
}
nsISupportsWeakReference* ptr = NULL;
obj->QueryInterface(nsISupportsWeakReference::GetIID(), (void**)&ptr);
if (!ptr)
{
if (error)
*error = NS_ERROR_NULL_POINTER;
return NULL;
}
nsresult retval = ptr->GetWeakReference(&result);
ptr->Release();
if (error)
*error = retval;
return result;
}
#endif