Add lime.system.CFFIPointer

This commit is contained in:
Joshua Granick
2015-09-23 09:19:45 -07:00
parent fea74fabce
commit 4a8fe40024
11 changed files with 155 additions and 74 deletions

View File

@@ -41,9 +41,9 @@ class AudioManager {
AudioManager.context = FLASH (new FlashAudioContext ());
#elseif lime_console
// TODO
AudioManager.context = CUSTOM (null);
AudioManager.context = CUSTOM (null);
#else

View File

@@ -1,6 +1,8 @@
package lime.audio.openal;
import lime.system.CFFIPointer;
#if !macro
@:build(lime.system.CFFI.build())
#end
@@ -199,18 +201,18 @@ class ALC {
#if ((cpp || neko || nodejs) && lime_openal && !macro)
@:cffi private static function lime_alc_close_device (device:Dynamic):Bool;
@:cffi private static function lime_alc_create_context (device:Dynamic, attrlist:Dynamic):Dynamic;
@:cffi private static function lime_alc_destroy_context (context:Dynamic):Void;
@:cffi private static function lime_alc_get_contexts_device (context:Dynamic):Dynamic;
@:cffi private static function lime_alc_get_current_context ():Dynamic;
@:cffi private static function lime_alc_get_error (device:Dynamic):Int;
@:cffi private static function lime_alc_get_integerv (device:Dynamic, param:Int, size:Int):Dynamic;
@:cffi private static function lime_alc_get_string (device:Dynamic, param:Int):Dynamic;
@:cffi private static function lime_alc_make_context_current (context:Dynamic):Bool;
@:cffi private static function lime_alc_open_device (devicename:String):Dynamic;
@:cffi private static function lime_alc_process_context (context:Dynamic):Void;
@:cffi private static function lime_alc_suspend_context (context:Dynamic):Void;
@:cffi private static function lime_alc_close_device (device:CFFIPointer):Bool;
@:cffi private static function lime_alc_create_context (device:CFFIPointer, attrlist:Dynamic):CFFIPointer;
@:cffi private static function lime_alc_destroy_context (context:CFFIPointer):Void;
@:cffi private static function lime_alc_get_contexts_device (context:CFFIPointer):CFFIPointer;
@:cffi private static function lime_alc_get_current_context ():CFFIPointer;
@:cffi private static function lime_alc_get_error (device:CFFIPointer):Int;
@:cffi private static function lime_alc_get_integerv (device:CFFIPointer, param:Int, size:Int):Dynamic;
@:cffi private static function lime_alc_get_string (device:CFFIPointer, param:Int):Dynamic;
@:cffi private static function lime_alc_make_context_current (context:CFFIPointer):Bool;
@:cffi private static function lime_alc_open_device (devicename:String):CFFIPointer;
@:cffi private static function lime_alc_process_context (context:CFFIPointer):Void;
@:cffi private static function lime_alc_suspend_context (context:CFFIPointer):Void;
#end

View File

@@ -1,14 +1,16 @@
package lime.audio.openal;
import lime.system.CFFIPointer;
@:allow(lime.audio.openal.AL)
@:allow(lime.audio.openal.ALC)
abstract ALContext(Dynamic) {
abstract ALContext(CFFIPointer) from CFFIPointer to CFFIPointer {
private function new (handle:Dynamic) {
private function new (handle:CFFIPointer) {
this = handle;

View File

@@ -1,14 +1,16 @@
package lime.audio.openal;
import lime.system.CFFIPointer;
@:allow(lime.audio.openal.AL)
@:allow(lime.audio.openal.ALC)
abstract ALDevice(Dynamic) {
abstract ALDevice(CFFIPointer) from CFFIPointer to CFFIPointer {
private function new (handle:Dynamic) {
private function new (handle:CFFIPointer) {
this = handle;

View File

@@ -645,7 +645,7 @@ class CFFI {
}
typeSignature += "v";
default:
if (useCPPTypes) {

View File

@@ -0,0 +1,55 @@
package lime.system;
abstract CFFIPointer(Dynamic) {
public function new (handle:Dynamic) {
this = handle;
}
public function get ():Float {
if (this != null) {
#if ((cpp || neko || nodejs) && !macro)
return lime_cffi_get_native_pointer (this);
#end
}
return 0;
}
@:noCompletion @:op(A == B) private static inline function equals (a:CFFIPointer, b:Int):Bool { return a.get () == b; }
@:noCompletion @:op(A == B) private static inline function equalsPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () == b.get (); }
@:noCompletion @:op(A > B) private static inline function greaterThan (a:CFFIPointer, b:Int):Bool { return a.get () > b; }
@:noCompletion @:op(A > B) private static inline function greaterThanPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () > b.get (); }
@:noCompletion @:op(A >= B) private static inline function greaterThanOrEqual (a:CFFIPointer, b:Int):Bool { return a.get () >= b; }
@:noCompletion @:op(A >= B) private static inline function greaterThanOrEqualPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () >= b.get (); }
@:noCompletion @:op(A < B) private static inline function lessThan (a:CFFIPointer, b:Int):Bool { return a.get () < b; }
@:noCompletion @:op(A < B) private static inline function lessThanPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () < b.get (); }
@:noCompletion @:op(A <= B) private static inline function lessThanOrEqual (a:CFFIPointer, b:Int):Bool { return a.get () <= b; }
@:noCompletion @:op(A <= B) private static inline function lessThanOrEqualPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () <= b.get (); }
@:noCompletion @:op(A != B) private static inline function notEquals (a:CFFIPointer, b:Int):Bool { return a.get () != b; }
@:noCompletion @:op(A != B) private static inline function notEqualsPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () != b.get (); }
// Native Methods
#if ((cpp || neko || nodejs) && !macro)
private static var lime_cffi_get_native_pointer = CFFI.load ("lime", "lime_cffi_get_native_pointer", 1);
#end
}

View File

@@ -215,6 +215,7 @@
<file name="src/math/Matrix3.cpp" />
<file name="src/math/Rectangle.cpp" />
<file name="src/math/Vector2.cpp" />
<file name="src/system/CFFIPointer.cpp" />
<file name="src/system/JNI.cpp" if="android" />
<file name="src/system/SensorEvent.cpp" />
<file name="src/ui/GamepadEvent.cpp" />

View File

@@ -0,0 +1,25 @@
#ifndef LIME_SYSTEM_CFFI_POINTER_H
#define LIME_SYSTEM_CFFI_POINTER_H
#include <hx/CFFIPrime.h>
namespace hx {
class Object;
typedef void (*finalizer)(value v);
}
namespace lime {
value CFFIPointer (void* ptr, hx::finalizer finalizer = 0);
}
#endif

View File

@@ -20,6 +20,7 @@
#include <graphics/ImageBuffer.h>
#include <graphics/Renderer.h>
#include <graphics/RenderEvent.h>
#include <system/CFFIPointer.h>
#include <system/Clipboard.h>
#include <system/JNI.h>
#include <system/SensorEvent.h>
@@ -94,9 +95,7 @@ namespace lime {
Application* application = CreateApplication ();
Application::callback = new AutoGCRoot (callback);
value handle = cffi::alloc_pointer (application);
val_gc (handle, gc_application);
return handle;
return CFFIPointer (application, gc_application);
}
@@ -216,6 +215,13 @@ namespace lime {
}
double lime_cffi_get_native_pointer (value handle) {
return (intptr_t)val_data (handle);
}
void lime_cffi_finalizer (value abstract) {
val_call0 ((value)val_data (abstract));
@@ -457,9 +463,7 @@ namespace lime {
if (font->face) {
value handle = cffi::alloc_pointer (font);
val_gc (handle, gc_font);
return handle;
return CFFIPointer (font, gc_font);
} else {
@@ -966,9 +970,7 @@ namespace lime {
value lime_renderer_create (value window) {
Renderer* renderer = CreateRenderer ((Window*)val_data (window));
value handle = cffi::alloc_pointer (renderer);
val_gc (handle, gc_renderer);
return handle;
return CFFIPointer (renderer, gc_renderer);
}
@@ -1068,9 +1070,7 @@ namespace lime {
#if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ)
TextLayout *text = new TextLayout (direction, script.__s, language.__s);
value handle = cffi::alloc_pointer (text);
val_gc (handle, gc_text_layout);
return handle;
return CFFIPointer (text, gc_text_layout);
#else
@@ -1155,9 +1155,7 @@ namespace lime {
value lime_window_create (value application, int width, int height, int flags, HxString title) {
Window* window = CreateWindow ((Application*)val_data (application), width, height, flags, title.__s);
value handle = cffi::alloc_pointer (window);
val_gc (handle, gc_window);
return handle;
return CFFIPointer (window, gc_window);
}
@@ -1295,6 +1293,7 @@ namespace lime {
DEFINE_PRIME2 (lime_bytes_from_data_pointer);
DEFINE_PRIME1 (lime_bytes_get_data_pointer);
DEFINE_PRIME1 (lime_bytes_read_file);
DEFINE_PRIME1 (lime_cffi_get_native_pointer);
DEFINE_PRIME1 (lime_cffi_set_finalizer);
DEFINE_PRIME0 (lime_clipboard_get_text);
DEFINE_PRIME1v (lime_clipboard_set_text);

View File

@@ -7,6 +7,7 @@
#endif
#include <hx/CFFIPrime.h>
#include <system/CFFIPointer.h>
#include <utils/Bytes.h>
@@ -890,16 +891,7 @@ namespace lime {
int* list = val_array_int (attrlist);
ALCcontext* alcContext = alcCreateContext (alcDevice, list);
if (alcContext) {
return cffi::alloc_pointer (alcContext);
} else {
return alloc_null ();
}
return CFFIPointer (alcContext);
}
@@ -916,16 +908,7 @@ namespace lime {
ALCcontext* alcContext = (ALCcontext*)val_data (context);
ALCdevice* alcDevice = alcGetContextsDevice (alcContext);
if (alcDevice) {
return cffi::alloc_pointer (alcDevice);
} else {
return alloc_null ();
}
return CFFIPointer (alcDevice);
}
@@ -933,16 +916,7 @@ namespace lime {
value lime_alc_get_current_context () {
ALCcontext* alcContext = alcGetCurrentContext ();
if (alcContext) {
return cffi::alloc_pointer (alcContext);
} else {
return alloc_null ();
}
return CFFIPointer (alcContext);
}
@@ -997,16 +971,7 @@ namespace lime {
ALCdevice* alcDevice = alcOpenDevice (devicename.__s);
atexit (lime_al_cleanup);
if (alcDevice) {
return cffi::alloc_pointer (alcDevice);
} else {
return alloc_null ();
}
return CFFIPointer (alcDevice);
}

View File

@@ -0,0 +1,30 @@
#include <system/CFFIPointer.h>
namespace lime {
value CFFIPointer (void* ptr, hx::finalizer finalizer) {
if (ptr) {
value handle = cffi::alloc_pointer (ptr);
if (finalizer) {
val_gc (handle, finalizer);
}
return handle;
} else {
return alloc_null ();
}
}
}