-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnsptr.h
233 lines (180 loc) · 4.11 KB
/
nsptr.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
///////////////////////////////////////////////////////////////////////////////
// Name: nsptr.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_NSPTR_H
#define __WXWEBCONNECT_NSPTR_H
class ns_smartptr_unknown
{
public:
virtual nsresult qi(const nsIID& iid, void** result) const = 0;
};
template <class T>
class ns_smartptr : public ns_smartptr_unknown
{
public:
T* p;
public:
ns_smartptr() : ns_smartptr_unknown()
{
p = 0;
}
ns_smartptr(T* const& _p) : ns_smartptr_unknown()
{
p = _p;
if (p)
{
p->AddRef();
}
}
ns_smartptr(const ns_smartptr<T>& c) : ns_smartptr_unknown()
{
p = c.p;
if (p)
{
p->AddRef();
}
}
ns_smartptr(const ns_smartptr_unknown& u) : ns_smartptr_unknown()
{
u.qi(T::GetIID(), (void**)&p);
}
~ns_smartptr()
{
if (p)
{
p->Release();
}
}
operator nsISupports**() const
{
return static_cast<nsISupports*>(p);
}
operator T*() const
{
return p;
}
T* operator->() const
{
return p;
}
bool operator!() const
{
return (p == 0);
}
operator bool() const
{
return (p != 0);
}
bool operator()() const
{
return (p != 0);
}
ns_smartptr<T>& operator=(const ns_smartptr<T>& c)
{
if (p)
{
p->Release();
}
p = c.p;
if (p)
{
p->AddRef();
}
return *this;
}
ns_smartptr<T>& operator=(T* _p)
{
if (p)
{
p->Release();
}
p = _p;
if (p)
{
p->AddRef();
}
return *this;
}
ns_smartptr<T>& operator=(const ns_smartptr_unknown& u)
{
if (p)
{
p->Release();
p = 0;
}
u.qi(T::GetIID(), (void**)&p);
return *this;
}
bool empty() const
{
return (p == 0);
}
void clear()
{
if (p)
{
p->Release();
}
p = 0;
}
nsresult qi(const nsIID& iid, void** result) const
{
if (!p)
{
*result = 0;
return NS_ERROR_NO_INTERFACE;
}
return p->QueryInterface(iid, result);
}
};
class nsRequestInterface : public ns_smartptr_unknown
{
public:
nsRequestInterface(nsISupports* p)
{
ptr.p = p;
p->AddRef();
}
nsRequestInterface(ns_smartptr_unknown& u)
{
static nsIID nsISupportsIID = NS_ISUPPORTS_IID;
u.qi(nsISupportsIID, (void**)&ptr.p);
}
nsresult qi(const nsIID& iid, void** result) const
{
if (ptr)
{
ns_smartptr<nsIInterfaceRequestor> factory_ptr = ptr;
if (!factory_ptr)
{
*result = 0;
return NS_ERROR_NO_INTERFACE;
}
return factory_ptr->GetInterface(iid, result);
}
return NS_ERROR_NULL_POINTER;
}
public:
ns_smartptr<nsISupports> ptr;
};
class nsToSmart : public ns_smartptr_unknown
{
public:
nsToSmart(nsISupports* _ptr) : ptr(_ptr) { }
nsresult qi(const nsIID& iid, void** result) const
{
if (!ptr)
return NS_ERROR_NULL_POINTER;
return ptr->QueryInterface(iid, result);
}
public:
nsISupports* ptr;
};
#endif