Add syntax highlighting to C- and Java code

Esse commit está contido em:
Benjamin P. Jung
2014-04-20 20:08:29 +02:00
commit 9f1361b533
+49 -41
Ver Arquivo
@@ -5,61 +5,69 @@ Callback declarations consist of a simple interface that extends the Callback in
Original C declarations Original C declarations
typedef void (*sig_t) (int); ```c
sig_t signal(int sig, sig_t func); typedef void (*sig_t) (int);
int SIGUSR1 = 30; sig_t signal(int sig, sig_t func);
int SIGUSR1 = 30;
```
Equivalent JNA mappings Equivalent JNA mappings
public interface CLibrary extends Library { ```java
int SIGUSR1 = 30; public interface CLibrary extends Library {
interface sig_t extends Callback { int SIGUSR1 = 30;
void invoke(int signal); interface sig_t extends Callback {
} void invoke(int signal);
sig_t signal(int sig, sig_t fn);
int raise(int sig);
} }
... sig_t signal(int sig, sig_t fn);
CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class); int raise(int sig);
// WARNING: you must keep a reference to the callback object }
// until you deregister the callback; if the callback object /* ... */
// is garbage-collected, the native callback invocation will CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class);
// probably crash. // WARNING: you must keep a reference to the callback object
CLibrary.sig_t fn = new CLibrary.sig_t() { // until you deregister the callback; if the callback object
public void invoke(int sig) { // is garbage-collected, the native callback invocation will
System.out.println("signal " + sig + " was raised"); // probably crash.
} CLibrary.sig_t fn = new CLibrary.sig_t() {
}; public void invoke(int sig) {
CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn); System.out.println("signal " + sig + " was raised");
lib.raise(CLibrary.SIGUSR1); }
... };
CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn);
lib.raise(CLibrary.SIGUSR1);
/* ... */
```
Here is a more involved example, using the Win32 APIs to enumerate all native windows: Here is a more involved example, using the Win32 APIs to enumerate all native windows:
Original C declarations Original C declarations
typedef int (__stdcall *WNDENUMPROC)(void*,void*); ```c
int __stdcall EnumWindows(WNDENUMPROC,void*); typedef int (__stdcall *WNDENUMPROC)(void*,void*);
int __stdcall EnumWindows(WNDENUMPROC,void*);
```
Equivalent JNA mappings Equivalent JNA mappings
public interface User32 extends StdCallLibrary { ```java
interface WNDENUMPROC extends StdCallCallback { public interface User32 extends StdCallLibrary {
/** Return whether to continue enumeration. */ interface WNDENUMPROC extends StdCallCallback {
boolean callback(Pointer hWnd, Pointer arg); /** Return whether to continue enumeration. */
} boolean callback(Pointer hWnd, Pointer arg);
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
} }
... boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
User32 user32 = User32.INSTANCE; }
/* ... */
User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() { user32.EnumWindows(new WNDENUMPROC() {
int count; int count;
public boolean callback(Pointer hWnd, Pointer userData) { public boolean callback(Pointer hWnd, Pointer userData) {
System.out.println("Found window " + hWnd + ", total " + ++count); System.out.println("Found window " + hWnd + ", total " + ++count);
return true; return true;
} }
}, null); }, null);
```
If your callback needs to live beyond the method invocation where it is used, make sure you keep a reference to it or the native code will call back to an empty stub after the callback object is garbage collected. If your callback needs to live beyond the method invocation where it is used, make sure you keep a reference to it or the native code will call back to an empty stub after the callback object is garbage collected.