comparison dwt/internal/Callback.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 8b48be5454ce
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *
11 * Port to the D Programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
13 *******************************************************************************/
14 module dwt.internal.Callback;
15
16 import dwt.dwthelper.utils;
17 import dwt.internal.C;
18
19 /**
20 * Instances of this class represent entry points into Java
21 * which can be invoked from operating system level callback
22 * routines.
23 * <p>
24 * IMPORTANT: A callback is only valid when invoked on the
25 * thread which created it. The results are undefined (and
26 * typically bad) when a callback is passed out to the
27 * operating system (or other code) in such a way that the
28 * callback is called from a different thread.
29 */
30
31 public class Callback
32 {
33
34 Object object;
35 String method, signature;
36 int argCount;
37 int /*long*/address, errorResult;
38 bool isStatic, isArrayBased;
39
40 static const String PTR_SIGNATURE = C.PTR_SIZEOF is 4 ? "I" : "J"; //$NON-NLS-1$ //$NON-NLS-2$
41 static const String SIGNATURE_0 = getSignature(0);
42 static const String SIGNATURE_1 = getSignature(1);
43 static const String SIGNATURE_2 = getSignature(2);
44 static const String SIGNATURE_3 = getSignature(3);
45 static const String SIGNATURE_4 = getSignature(4);
46 static const String
47 SIGNATURE_N = "([" + PTR_SIGNATURE + ")" + PTR_SIGNATURE; //$NON-NLS-1$ //$NON-NLS-2$
48
49 /**
50 * Constructs a new instance of this class given an object
51 * to send the message to, a string naming the method to
52 * invoke and an argument count. Note that, if the object
53 * is an instance of <code>Class</code> it is assumed that
54 * the method is a static method on that class.
55 *
56 * @param object the object to send the message to
57 * @param method the name of the method to invoke
58 * @param argCount the number of arguments that the method takes
59 */
60 public this (Object object, String method, int argCount)
61 {
62 this(object, method, argCount, false);
63 }
64
65 /**
66 * Constructs a new instance of this class given an object
67 * to send the message to, a string naming the method to
68 * invoke, an argument count and a flag indicating whether
69 * or not the arguments will be passed in an array. Note
70 * that, if the object is an instance of <code>Class</code>
71 * it is assumed that the method is a static method on that
72 * class.
73 *
74 * @param object the object to send the message to
75 * @param method the name of the method to invoke
76 * @param argCount the number of arguments that the method takes
77 * @param isArrayBased <code>true</code> if the arguments should be passed in an array and false otherwise
78 */
79 public this (Object object, String method, int argCount, bool isArrayBased)
80 {
81 this(object, method, argCount, isArrayBased, 0);
82 }
83
84 /**
85 * Constructs a new instance of this class given an object
86 * to send the message to, a string naming the method to
87 * invoke, an argument count, a flag indicating whether
88 * or not the arguments will be passed in an array and a value
89 * to return when an exception happens. Note that, if
90 * the object is an instance of <code>Class</code>
91 * it is assumed that the method is a static method on that
92 * class.
93 *
94 * @param object the object to send the message to
95 * @param method the name of the method to invoke
96 * @param argCount the number of arguments that the method takes
97 * @param isArrayBased <code>true</code> if the arguments should be passed in an array and false otherwise
98 * @param errorResult the return value if the java code throws an exception
99 */
100 public this (Object object, String method, int argCount, bool isArrayBased,
101 int /*long*/errorResult)
102 {
103
104 /* Set the callback fields */
105 this.object = object;
106 this.method = method;
107 this.argCount = argCount;
108 this.isStatic = cast(ClassInfo) object;
109 this.isArrayBased = isArrayBased;
110 this.errorResult = errorResult;
111
112 /* Inline the common cases */
113 if (isArrayBased)
114 {
115 signature = SIGNATURE_N;
116 }
117 else
118 {
119 switch (argCount)
120 {
121 case 0:
122 signature = SIGNATURE_0;
123 break; //$NON-NLS-1$
124 case 1:
125 signature = SIGNATURE_1;
126 break; //$NON-NLS-1$
127 case 2:
128 signature = SIGNATURE_2;
129 break; //$NON-NLS-1$
130 case 3:
131 signature = SIGNATURE_3;
132 break; //$NON-NLS-1$
133 case 4:
134 signature = SIGNATURE_4;
135 break; //$NON-NLS-1$
136 default:
137 signature = getSignature(argCount);
138 }
139 }
140
141 /* Bind the address */
142 address = bind(this, object, method, signature, argCount, isStatic,
143 isArrayBased, errorResult);
144 }
145
146 /**
147 * Allocates the native level resources associated with the
148 * callback. This method is only invoked from within the
149 * constructor for the argument.
150 *
151 * @param callback the callback to bind
152 * @param object the callback's object
153 * @param method the callback's method
154 * @param signature the callback's method signature
155 * @param argCount the callback's method argument count
156 * @param isStatic whether the callback's method is static
157 * @param isArrayBased whether the callback's method is array based
158 * @param errorResult the callback's error result
159 */
160 static synchronized int /*long*/ bind (Callback callback, Object object, String method, String signature, int argCount, bool isStatic, bool isArrayBased, int /*long*/ errorResult)
161 {
162
163 }
164
165 /**
166 * Releases the native level resources associated with the callback,
167 * and removes all references between the callback and
168 * other objects. This helps to prevent (bad) application code
169 * from accidentally holding onto extraneous garbage.
170 */
171 public void dispose ()
172 {
173 if (object is null)
174 return;
175 unbind(this);
176 object = method = signature = null;
177 address = 0;
178 }
179
180 /**
181 * Returns the address of a block of machine code which will
182 * invoke the callback represented by the receiver.
183 *
184 * @return the callback address
185 */
186 public int /*long*/getAddress ()
187 {
188 return address;
189 }
190
191 /**
192 * Returns the DWT platform name.
193 *
194 * @return the platform name of the currently running DWT
195 */
196 //public static native String getPlatform ();
197 /**
198 * Returns the number of times the system has been recursively entered
199 * through a callback.
200 * <p>
201 * Note: This should not be called by application code.
202 * </p>
203 *
204 * @return the entry count
205 *
206 * @since 2.1
207 */
208 //public static native int getEntryCount ();
209 static String getSignature (int argCount)
210 {
211 String signature = "("; //$NON-NLS-1$
212 for (int i = 0; i < argCount; i++)
213 signature += PTR_SIGNATURE;
214 signature += ")" + PTR_SIGNATURE; //$NON-NLS-1$
215 return signature;
216 }
217
218 /**
219 * Indicates whether or not callbacks which are triggered at the
220 * native level should cause the messages described by the matching
221 * <code>Callback</code> objects to be invoked. This method is used
222 * to safely shut down DWT when it is run within environments
223 * which can generate spurious events.
224 * <p>
225 * Note: This should not be called by application code.
226 * </p>
227 *
228 * @param enable true if callbacks should be invoked
229 */
230 //public static final native synchronized void setEnabled (bool enable);
231 /**
232 * Returns whether or not callbacks which are triggered at the
233 * native level should cause the messages described by the matching
234 * <code>Callback</code> objects to be invoked. This method is used
235 * to safely shut down DWT when it is run within environments
236 * which can generate spurious events.
237 * <p>
238 * Note: This should not be called by application code.
239 * </p>
240 *
241 * @return true if callbacks should not be invoked
242 */
243 //public static final native synchronized bool getEnabled ();
244 /**
245 * This might be called directly from native code in environments
246 * which can generate spurious events. Check before removing it.
247 *
248 * @deprecated
249 *
250 * @param ignore true if callbacks should not be invoked
251 */
252 static final void ignoreCallbacks (bool ignore)
253 {
254 setEnabled(!ignore);
255 }
256
257 /**
258 * Immediately wipes out all native level state associated
259 * with <em>all</em> callbacks.
260 * <p>
261 * <b>WARNING:</b> This operation is <em>extremely</em> dangerous,
262 * and should never be performed by application code.
263 * </p>
264 */
265 //public static final native synchronized void reset ();
266 /**
267 * Releases the native level resources associated with the callback.
268 *
269 * @see #dispose
270 */
271 //static final native synchronized void unbind (Callback callback);
272 }