~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/wx/c_src/wxe_return.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-05-07 15:07:37 UTC
  • mfrom: (1.2.1 upstream) (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090507150737-i4yb5elwinm7r0hc
Tags: 1:13.b-dfsg1-1
* Removed another bunch of non-free RFCs from original tarball
  (closes: #527053).
* Fixed build-dependencies list by adding missing comma. This requires
  libsctp-dev again. Also, added libsctp1 dependency to erlang-base and
  erlang-base-hipe packages because the shared library is loaded via
  dlopen now and cannot be added using dh_slibdeps (closes: #526682).
* Weakened dependency of erlang-webtool on erlang-observer to recommends
  to avoid circular dependencies (closes: #526627).
* Added solaris-i386 to HiPE enabled architectures.
* Made script sources in /usr/lib/erlang/erts-*/bin directory executable,
  which is more convenient if a user wants to create a target Erlang system.
* Shortened extended description line for erlang-dev package to make it
  fit 80x25 terminals.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * %CopyrightBegin%
 
3
 * 
 
4
 * Copyright Ericsson AB 2008-2009. All Rights Reserved.
 
5
 * 
 
6
 * The contents of this file are subject to the Erlang Public License,
 
7
 * Version 1.1, (the "License"); you may not use this file except in
 
8
 * compliance with the License. You should have received a copy of the
 
9
 * Erlang Public License along with this software. If not, it can be
 
10
 * retrieved online at http://www.erlang.org/.
 
11
 * 
 
12
 * Software distributed under the License is distributed on an "AS IS"
 
13
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
14
 * the License for the specific language governing rights and limitations
 
15
 * under the License.
 
16
 * 
 
17
 * %CopyrightEnd% 
 
18
 */
 
19
 
 
20
#include "wxe_return.h"
 
21
 
 
22
// see http://docs.wxwidgets.org/stable/wx_wxarray.html#arraymacros
 
23
// this is a magic incantation which must be done!
 
24
#include <wx/arrimpl.cpp> 
 
25
WX_DEFINE_OBJARRAY(wxErlDrvTermDataArray);
 
26
 
 
27
#define INLINE
 
28
 
 
29
wxeReturn::wxeReturn (ErlDrvPort      _port, 
 
30
                    ErlDrvTermData  _caller, 
 
31
                    bool            _isResult) {
 
32
    port    = _port;
 
33
    caller  = _caller;
 
34
    
 
35
    isResult = _isResult;
 
36
    
 
37
     if (isResult) {
 
38
         addAtom("_wxe_result_");
 
39
     }
 
40
}
 
41
 
 
42
wxeReturn::~wxeReturn () {
 
43
    //depending on which version of wxArray we use, we may have to clear it ourselves.
 
44
}
 
45
 
 
46
int wxeReturn::send() {      
 
47
    if ((rt.GetCount() == 2 && isResult) || rt.GetCount() == 0)
 
48
      return 1;  // not a call bail out
 
49
    
 
50
    if (isResult) {
 
51
        addTupleCount(2);
 
52
    }    
 
53
 
 
54
    // rt to array
 
55
    unsigned int rtLength = rt.GetCount(); //signed int
 
56
 
 
57
    size_t size = sizeof(ErlDrvTermData)*(rtLength);
 
58
    
 
59
    ErlDrvTermData* rtData = (ErlDrvTermData *) driver_alloc(size);
 
60
    for (unsigned int i=0; i < rtLength; i++) {
 
61
        rtData[i] = rt[i];
 
62
    }
 
63
 
 
64
    int res = driver_send_term(port, caller, rtData, rtLength);
 
65
    driver_free(rtData);
 
66
 
 
67
    if(res == -1) {
 
68
      wxString msg;
 
69
      msg.Printf(wxT("Failed to send return or event msg"));
 
70
      send_msg("internal_error", &msg);
 
71
    }
 
72
    reset();
 
73
    return res;
 
74
}
 
75
 
 
76
//clear everything so we can re-use if we want
 
77
 void wxeReturn::reset() {
 
78
     rt.empty();
 
79
     temp_float.empty();
 
80
}
 
81
 
 
82
INLINE
 
83
unsigned  int wxeReturn::size() {
 
84
     return rt.GetCount();
 
85
 }
 
86
 
 
87
INLINE
 
88
void wxeReturn::add(ErlDrvTermData type, ErlDrvTermData data) {
 
89
    rt.Add(type);
 
90
    rt.Add(data);
 
91
}
 
92
 
 
93
 
 
94
// INLINE 
 
95
// void wxeReturn::addRef(const void *ptr, const char* className) {
 
96
//    unsigned int ref_idx = wxe_app->getRef((void *)ptr, memEnv); 
 
97
//    addRef(ref_idx, className);
 
98
// }
 
99
 
 
100
INLINE 
 
101
void wxeReturn::addRef(const unsigned int ref, const char* className) {
 
102
   addAtom("wx_ref");
 
103
   addUint(ref);
 
104
   addAtom(className);
 
105
   rt.Add(ERL_DRV_NIL);
 
106
   addTupleCount(4);
 
107
}
 
108
 
 
109
 
 
110
INLINE 
 
111
void wxeReturn::addAtom(const char* atomName) {
 
112
    add(ERL_DRV_ATOM, driver_mk_atom((char *)atomName));
 
113
}
 
114
 
 
115
INLINE 
 
116
void wxeReturn::addBinary(const char* buf, const size_t size) {
 
117
    rt.Add(ERL_DRV_BUF2BINARY);
 
118
    rt.Add((ErlDrvTermData)buf);
 
119
    rt.Add((ErlDrvTermData)size);
 
120
}
 
121
 
 
122
INLINE 
 
123
void wxeReturn::addExt2Term(wxeErlTerm *term) {
 
124
  if(term) {
 
125
    rt.Add(ERL_DRV_EXT2TERM);
 
126
    rt.Add((ErlDrvTermData)term->bin);
 
127
    rt.Add((ErlDrvTermData)term->size);
 
128
  } else {
 
129
    rt.Add(ERL_DRV_NIL);
 
130
  }
 
131
}
 
132
 
 
133
INLINE
 
134
void wxeReturn::addExt2Term(wxETreeItemData *val) {
 
135
  if(val) {
 
136
    rt.Add(ERL_DRV_EXT2TERM);
 
137
    rt.Add((ErlDrvTermData)(val->bin));
 
138
    rt.Add((ErlDrvTermData)(val->size));
 
139
  } else
 
140
    rt.Add(ERL_DRV_NIL);
 
141
}
 
142
 
 
143
INLINE
 
144
void  wxeReturn::addUint(unsigned int n) {
 
145
    add(ERL_DRV_UINT, (ErlDrvTermData)n);
 
146
}
 
147
 
 
148
INLINE
 
149
void  wxeReturn::addInt(int n) {
 
150
    add(ERL_DRV_INT, (ErlDrvTermData)n);
 
151
}
 
152
 
 
153
INLINE 
 
154
void wxeReturn::addFloat(double f) {
 
155
  // Erlang expects a pointer to double...
 
156
  // Hmm is temp_float moved if reallocated
 
157
  // the pointer may be wrong.
 
158
  // Harryhuk - use a list instead?
 
159
  temp_float.Add(f);
 
160
  add(ERL_DRV_FLOAT, (ErlDrvTermData)&temp_float.Last());
 
161
}
 
162
 
 
163
INLINE  
 
164
void wxeReturn::addTupleCount(unsigned int n) {
 
165
    add(ERL_DRV_TUPLE, (ErlDrvTermData)n);
 
166
}
 
167
 
 
168
INLINE 
 
169
void wxeReturn::endList(unsigned int n) {
 
170
    rt.Add(ERL_DRV_NIL);
 
171
    add(ERL_DRV_LIST, (ErlDrvTermData)(n+1));
 
172
}
 
173
 
 
174
INLINE 
 
175
void wxeReturn::addBool(int val) {
 
176
    if (val) {
 
177
        addAtom("true");
 
178
    } else {
 
179
        addAtom("false");
 
180
    }
 
181
}
 
182
 
 
183
INLINE 
 
184
void wxeReturn::add(const wxString s) {
 
185
    int strLen = s.Len();
 
186
    wxCharBuffer resultCB = s.mb_str(utfConverter);
 
187
    int * resultPtr = (int *) resultCB.data();
 
188
 
 
189
    for (int i = 0; i < strLen; i++, resultPtr++) {
 
190
        addInt(*resultPtr);
 
191
    }
 
192
    endList(strLen);       
 
193
}
 
194
 
 
195
INLINE 
 
196
void wxeReturn::add(const wxString* s) {
 
197
    add(*s);
 
198
}
 
199
 
 
200
INLINE
 
201
void  wxeReturn::add(wxArrayString val) {
 
202
    unsigned int len = val.GetCount();
 
203
 
 
204
    for (unsigned int i = 0; i< len; i++) {
 
205
        add(val[i]);       
 
206
    }
 
207
    endList(len);
 
208
}
 
209
 
 
210
INLINE 
 
211
void  wxeReturn::add(wxArrayInt val) {
 
212
    unsigned int len = val.GetCount();
 
213
 
 
214
    for (unsigned int i = 0; i< len; i++) {
 
215
        addInt(val[i]);       
 
216
    }
 
217
    endList(len);
 
218
}
 
219
 
 
220
INLINE 
 
221
void wxeReturn::add(wxUIntPtr *val) {
 
222
    addInt((ErlDrvTermData)val); // TODO is this right?
 
223
}
 
224
 
 
225
INLINE 
 
226
void wxeReturn::add(wxPoint pt) {
 
227
    addInt(pt.x);
 
228
    addInt(pt.y);
 
229
    addTupleCount(2);
 
230
}
 
231
 
 
232
INLINE
 
233
void  wxeReturn::add(wxPoint2DDouble pt) {
 
234
    addFloat(pt.m_x);
 
235
    addFloat(pt.m_y);
 
236
    addTupleCount(2);
 
237
}
 
238
 
 
239
INLINE 
 
240
void wxeReturn::add(wxSize size) {
 
241
    addInt(size.GetWidth());
 
242
    addInt(size.GetHeight());
 
243
    addTupleCount(2);
 
244
}
 
245
 
 
246
INLINE
 
247
void  wxeReturn::add(wxRect rect) {
 
248
    addInt(rect.x);
 
249
    addInt(rect.y);
 
250
    addInt(rect.width);
 
251
    addInt(rect.height);
 
252
    addTupleCount(4);
 
253
}
 
254
 
 
255
INLINE 
 
256
void wxeReturn::add(wxColour colour) {
 
257
    addInt(colour.Red());
 
258
    addInt(colour.Green());
 
259
    addInt(colour.Blue());
 
260
    addInt(colour.Alpha());
 
261
    addTupleCount(4);
 
262
}
 
263
 
 
264
INLINE
 
265
void  wxeReturn::add(wxDateTime dateTime) {
 
266
  addDate(dateTime);
 
267
  addTime(dateTime);
 
268
  addTupleCount(2);
 
269
}
 
270
 
 
271
inline 
 
272
void  wxeReturn::addDate(wxDateTime dateTime) {
 
273
    addInt(dateTime.GetYear());
 
274
    addInt(dateTime.GetMonth()+1); // c++ month is zero based
 
275
    addInt(dateTime.GetDay());
 
276
    addTupleCount(3);
 
277
}
 
278
 
 
279
INLINE void  wxeReturn::addTime(wxDateTime dateTime) {
 
280
    addInt(dateTime.GetHour());
 
281
    addInt(dateTime.GetMinute());
 
282
    addInt(dateTime.GetSecond());
 
283
    addTupleCount(3);
 
284
}
 
285
 
 
286
INLINE
 
287
void  wxeReturn::add(wxRect2DDouble rect2D) {
 
288
    addFloat(rect2D.m_x);
 
289
    addFloat(rect2D.m_y);
 
290
    addFloat(rect2D.m_width);
 
291
    addFloat(rect2D.m_height);
 
292
    addTupleCount(4);
 
293
}
 
294
 
 
295
INLINE
 
296
void  wxeReturn::add(wxGridCellCoords val) {
 
297
    addInt(val.GetRow());
 
298
    addInt(val.GetCol());
 
299
    addTupleCount(2);
 
300
}
 
301
 
 
302
INLINE
 
303
void  wxeReturn::add(wxGBPosition val) {
 
304
    addInt(val.GetRow());
 
305
    addInt(val.GetCol());
 
306
    addTupleCount(2);
 
307
}
 
308
 
 
309
INLINE
 
310
void  wxeReturn::add(wxGBSpan val) {
 
311
    addInt(val.GetRowspan());
 
312
    addInt(val.GetColspan());
 
313
    addTupleCount(2);
 
314
}
 
315
 
 
316
INLINE
 
317
void  wxeReturn::add(wxMouseState val) {
 
318
    addAtom("wxMouseState");
 
319
    // TODO not int?
 
320
    addUint(val.GetX());
 
321
    addUint(val.GetY());
 
322
    addBool(val.LeftDown());
 
323
    addBool(val.MiddleDown());
 
324
    addBool(val.RightDown());
 
325
    addBool(val.ControlDown());
 
326
    addBool(val.ShiftDown());
 
327
    addBool(val.AltDown());
 
328
    addBool(val.MetaDown());
 
329
    addBool(val.CmdDown());
 
330
    addTupleCount(11);     
 
331
}
 
332