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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2008-2010. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
#include "wxe_return.h"
// see http://docs.wxwidgets.org/stable/wx_wxarray.html#arraymacros
// this is a magic incantation which must be done!
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(wxErlDrvTermDataArray);
#define INLINE
wxeReturn::wxeReturn (ErlDrvPort _port,
ErlDrvTermData _caller,
bool _isResult) {
port = _port;
caller = _caller;
isResult = _isResult;
if (isResult) {
addAtom("_wxe_result_");
}
}
wxeReturn::~wxeReturn () {
//depending on which version of wxArray we use, we may have to clear it ourselves.
}
int wxeReturn::send() {
if ((rt.GetCount() == 2 && isResult) || rt.GetCount() == 0)
return 1; // not a call bail out
if (isResult) {
addTupleCount(2);
}
// rt to array
unsigned int rtLength = rt.GetCount(); //signed int
size_t size = sizeof(ErlDrvTermData)*(rtLength);
ErlDrvTermData* rtData = (ErlDrvTermData *) driver_alloc(size);
for (unsigned int i=0; i < rtLength; i++) {
rtData[i] = rt[i];
}
int res = driver_send_term(port, caller, rtData, rtLength);
driver_free(rtData);
#ifdef DEBUG
if(res == -1) {
wxString msg;
msg.Printf(wxT("Failed to send return or event msg"));
send_msg("internal_error", &msg);
}
#endif
reset();
return res;
}
//clear everything so we can re-use if we want
void wxeReturn::reset() {
rt.empty();
temp_float.empty();
}
INLINE
unsigned int wxeReturn::size() {
return rt.GetCount();
}
INLINE
void wxeReturn::add(ErlDrvTermData type, ErlDrvTermData data) {
rt.Add(type);
rt.Add(data);
}
// INLINE
// void wxeReturn::addRef(const void *ptr, const char* className) {
// unsigned int ref_idx = wxe_app->getRef((void *)ptr, memEnv);
// addRef(ref_idx, className);
// }
INLINE
void wxeReturn::addRef(const unsigned int ref, const char* className) {
addAtom("wx_ref");
addUint(ref);
addAtom(className);
rt.Add(ERL_DRV_NIL);
addTupleCount(4);
}
INLINE
void wxeReturn::addAtom(const char* atomName) {
add(ERL_DRV_ATOM, driver_mk_atom((char *)atomName));
}
INLINE
void wxeReturn::addBinary(const char* buf, const size_t size) {
rt.Add(ERL_DRV_BUF2BINARY);
rt.Add((ErlDrvTermData)buf);
rt.Add((ErlDrvTermData)size);
}
INLINE
void wxeReturn::addExt2Term(wxeErlTerm *term) {
if(term) {
rt.Add(ERL_DRV_EXT2TERM);
rt.Add((ErlDrvTermData)term->bin);
rt.Add((ErlDrvTermData)term->size);
} else {
rt.Add(ERL_DRV_NIL);
}
}
INLINE
void wxeReturn::addExt2Term(wxETreeItemData *val) {
if(val) {
rt.Add(ERL_DRV_EXT2TERM);
rt.Add((ErlDrvTermData)(val->bin));
rt.Add((ErlDrvTermData)(val->size));
} else
rt.Add(ERL_DRV_NIL);
}
INLINE
void wxeReturn::addUint(unsigned int n) {
add(ERL_DRV_UINT, (ErlDrvTermData)n);
}
INLINE
void wxeReturn::addInt(int n) {
add(ERL_DRV_INT, (ErlDrvTermData)n);
}
INLINE
void wxeReturn::addFloat(double f) {
// Erlang expects a pointer to double...
// Hmm is temp_float moved if reallocated
// the pointer may be wrong.
// Harryhuk - use a list instead?
temp_float.Add(f);
add(ERL_DRV_FLOAT, (ErlDrvTermData)&temp_float.Last());
}
INLINE
void wxeReturn::addTupleCount(unsigned int n) {
add(ERL_DRV_TUPLE, (ErlDrvTermData)n);
}
INLINE
void wxeReturn::endList(unsigned int n) {
rt.Add(ERL_DRV_NIL);
add(ERL_DRV_LIST, (ErlDrvTermData)(n+1));
}
INLINE
void wxeReturn::addBool(int val) {
if (val) {
addAtom("true");
} else {
addAtom("false");
}
}
INLINE
void wxeReturn::add(const wxString s) {
int strLen = s.Len();
wxCharBuffer resultCB = s.mb_str(utfConverter);
int * resultPtr = (int *) resultCB.data();
for (int i = 0; i < strLen; i++, resultPtr++) {
addInt(*resultPtr);
}
endList(strLen);
}
INLINE
void wxeReturn::add(const wxString* s) {
add(*s);
}
INLINE
void wxeReturn::add(wxArrayString val) {
unsigned int len = val.GetCount();
for (unsigned int i = 0; i< len; i++) {
add(val[i]);
}
endList(len);
}
INLINE
void wxeReturn::add(wxArrayInt val) {
unsigned int len = val.GetCount();
for (unsigned int i = 0; i< len; i++) {
addInt(val[i]);
}
endList(len);
}
INLINE
void wxeReturn::add(wxUIntPtr *val) {
add(ERL_DRV_UINT, (ErlDrvTermData) val);
}
INLINE
void wxeReturn::add(wxPoint pt) {
addInt(pt.x);
addInt(pt.y);
addTupleCount(2);
}
INLINE
void wxeReturn::add(wxPoint2DDouble pt) {
addFloat(pt.m_x);
addFloat(pt.m_y);
addTupleCount(2);
}
INLINE
void wxeReturn::add(wxSize size) {
addInt(size.GetWidth());
addInt(size.GetHeight());
addTupleCount(2);
}
INLINE
void wxeReturn::add(wxRect rect) {
addInt(rect.x);
addInt(rect.y);
addInt(rect.width);
addInt(rect.height);
addTupleCount(4);
}
INLINE
void wxeReturn::add(wxColour colour) {
addInt(colour.Red());
addInt(colour.Green());
addInt(colour.Blue());
addInt(colour.Alpha());
addTupleCount(4);
}
INLINE
void wxeReturn::add(wxDateTime dateTime) {
addDate(dateTime);
addTime(dateTime);
addTupleCount(2);
}
INLINE
void wxeReturn::addDate(wxDateTime dateTime) {
addInt(dateTime.GetYear());
addInt(dateTime.GetMonth()+1); // c++ month is zero based
addInt(dateTime.GetDay());
addTupleCount(3);
}
INLINE void wxeReturn::addTime(wxDateTime dateTime) {
addInt(dateTime.GetHour());
addInt(dateTime.GetMinute());
addInt(dateTime.GetSecond());
addTupleCount(3);
}
INLINE
void wxeReturn::add(wxRect2DDouble rect2D) {
addFloat(rect2D.m_x);
addFloat(rect2D.m_y);
addFloat(rect2D.m_width);
addFloat(rect2D.m_height);
addTupleCount(4);
}
INLINE
void wxeReturn::add(wxGridCellCoords val) {
addInt(val.GetRow());
addInt(val.GetCol());
addTupleCount(2);
}
INLINE
void wxeReturn::add(wxGBPosition val) {
addInt(val.GetRow());
addInt(val.GetCol());
addTupleCount(2);
}
INLINE
void wxeReturn::add(wxGBSpan val) {
addInt(val.GetRowspan());
addInt(val.GetColspan());
addTupleCount(2);
}
INLINE
void wxeReturn::add(wxMouseState val) {
addAtom("wxMouseState");
// TODO not int?
addUint(val.GetX());
addUint(val.GetY());
addBool(val.LeftDown());
addBool(val.MiddleDown());
addBool(val.RightDown());
addBool(val.ControlDown());
addBool(val.ShiftDown());
addBool(val.AltDown());
addBool(val.MetaDown());
addBool(val.CmdDown());
addTupleCount(11);
}
INLINE
void wxeReturn::add(const wxHtmlLinkInfo *val) {
addAtom("wxHtmlLinkInfo");
add(val->GetHref());
add(val->GetTarget());
addTupleCount(3);
}
INLINE
void wxeReturn::add(const wxHtmlLinkInfo &val) {
addAtom("wxHtmlLinkInfo");
add(val.GetHref());
add(val.GetTarget());
addTupleCount(3);
}
|