Răsfoiți Sursa

Remove BasePointer, add RawPointer types. Add macro for conversion of string signature to function pointer

hughsando 11 ani în urmă
părinte
comite
1c1085bc9f

+ 29 - 13
gencpp.ml

@@ -65,7 +65,8 @@ let is_internal_class = function
    |  ([],"Array") | ([], "Class") | ([], "Enum") | ([], "Bool")
    |  ([], "Dynamic") | ([], "ArrayAccess") | (["cpp"], "FastIterator")
    |  (["cpp"],"Pointer") | (["cpp"],"ConstPointer")
-   |  (["cpp"],"BasePointer") | (["cpp"],"Function") -> true
+   |  (["cpp"],"RawPointer") | (["cpp"],"RawConstPointer")
+   |  (["cpp"],"Function") -> true
    |  ([],"Math") | (["haxe";"io"], "Unsigned_char__") -> true
    |  (["cpp"],"Int8") | (["cpp"],"UInt8") | (["cpp"],"Char")
    |  (["cpp"],"Int16") | (["cpp"],"UInt16")
@@ -492,21 +493,23 @@ let is_lvalue var =
 
 
 
-let is_pointer haxe_type =
+let is_pointer haxe_type includeRaw =
    match follow haxe_type with
    | TInst (klass,params) ->
       (match klass.cl_path with
       | ["cpp"] , "Pointer"
       | ["cpp"] , "ConstPointer"
-      | ["cpp"] , "BasePointer"
       | ["cpp"] , "Function" -> true
+      | ["cpp"] , "RawPointer" when includeRaw -> true
+      | ["cpp"] , "RawConstPointer" when includeRaw -> true
       | _ -> false )
    | TType (type_def,params) ->
       (match type_def.t_path with
       | ["cpp"] , "Pointer"
       | ["cpp"] , "ConstPointer"
-      | ["cpp"] , "BasePointer"
       | ["cpp"] , "Function" -> true
+      | ["cpp"] , "RawPointer" when includeRaw -> true
+      | ["cpp"] , "RawConstPointer" when includeRaw -> true
       | _ -> false )
    | _ -> false
    ;;
@@ -530,9 +533,12 @@ let rec class_string klass suffix params =
    |  (["cpp"],"FastIterator") -> "::cpp::FastIterator" ^ suffix ^ "< " ^ (String.concat ","
                (List.map type_string  params) ) ^ " >"
    |  (["cpp"],"Pointer")
-   |  (["cpp"],"ConstPointer")
-   |  (["cpp"],"BasePointer") ->
+   |  (["cpp"],"ConstPointer") ->
         "::cpp::Pointer< " ^ (String.concat "," (List.map type_string params) ) ^ " >"
+   |  (["cpp"],"RawPointer") ->
+        " " ^ (String.concat "," (List.map type_string params) ) ^ " "
+   |  (["cpp"],"RawConstPointer") ->
+        " const " ^ (String.concat "," (List.map type_string params) ) ^ " * "
    |  (["cpp"],"Function") ->
         "::cpp::Function< " ^ (cpp_function_signature_params params) ^ " >"
    | _ when is_dynamic_type_param klass.cl_kind -> "Dynamic"
@@ -594,11 +600,18 @@ and type_string_suff suffix haxe_type =
          | [t] -> "::cpp::FastIterator< " ^ (type_string (follow t) ) ^ " >"
          | _ -> assert false)
       | ["cpp"] , "Pointer"
-      | ["cpp"] , "ConstPointer"
-      | ["cpp"] , "BasePointer" ->
+      | ["cpp"] , "ConstPointer" ->
          (match params with
          | [t] -> "::cpp::Pointer< " ^ (type_string (follow t) ) ^ " >"
          | _ -> assert false)
+      | ["cpp"] , "RawPointer" ->
+         (match params with
+         | [t] -> " " ^ (type_string (follow t) ) ^ " *"
+         | _ -> assert false)
+      | ["cpp"] , "RawConstPointer" ->
+         (match params with
+         | [t] -> "const " ^ (type_string (follow t) ) ^ " *"
+         | _ -> assert false)
       | ["cpp"] , "Function" -> "::cpp::Function< " ^ (cpp_function_signature_params params) ^ " >"
       | _ ->  type_string_suff suffix (apply_params type_def.t_types params type_def.t_type)
       )
@@ -632,6 +645,7 @@ and is_dynamic_array_param haxe_type =
    | TInst (klass,params) ->
          (match klass.cl_path with
          | ([],"Array") | ([],"Class") | (["cpp"],"FastIterator")
+         | (["cpp"],"RawPointer") |(["cpp"],"ConstRawPointer")
          | (["cpp"],"Pointer") |(["cpp"],"ConstPointer")|(["cpp"],"Function") -> false
          | _ -> (match klass.cl_kind with KTypeParameter _ -> true | _ -> false)
          )
@@ -1198,7 +1212,7 @@ and is_dynamic_member_lookup_in_cpp ctx field_object field =
    let member = field_name field in
    ctx.ctx_dbgout ("/*mem."^member^".*/");
    if (is_internal_member member) then false else
-   if (is_pointer field_object.etype) then false else
+   if (is_pointer field_object.etype true) then false else
    if (match field_object.eexpr with | TTypeExpr _ -> ctx.ctx_dbgout "/*!TTypeExpr*/"; true | _ -> false) then false else
    if (is_dynamic_in_cpp ctx field_object) then true else
    if (is_array field_object.etype) then false else (
@@ -1219,7 +1233,7 @@ and is_dynamic_member_lookup_in_cpp ctx field_object field =
 and is_dynamic_member_return_in_cpp ctx field_object field =
    let member = field_name field in
    if (is_array field_object.etype) then false else
-   if (is_pointer field_object.etype) then false else
+   if (is_pointer field_object.etype true) then false else
    if (is_internal_member member) then false else
    match field_object.eexpr with
    | TTypeExpr t ->
@@ -1932,7 +1946,7 @@ and gen_expression ctx retval expression =
          when class_def.cl_extern ->
          (try
             let return_type = expression.etype in
-            is_pointer return_type &&
+            (is_pointer return_type false) &&
                ( output ( (type_string return_type) ^ "(" ); true; )
          with Not_found -> false )
       | _ -> false
@@ -2078,7 +2092,7 @@ and gen_expression ctx retval expression =
          output "->__get(";
          gen_expression ctx true index;
          output ")";
-         if not (is_pointer array_expr.etype ) then
+         if not (is_pointer array_expr.etype true) then
             check_array_element_cast array_expr.etype ".StaticCast" "()";
       end
    (* Get precidence matching haxe ? *)
@@ -2662,7 +2676,9 @@ let find_referenced_types ctx obj super_deps constructor_deps header_only for_de
          for the Array, Class, FastIterator or Pointer classes, for which we do a fully typed object *)
       | TInst (klass,params) ->
          (match klass.cl_path with
-         | ([],"Array") | ([],"Class") | (["cpp"],"FastIterator") | (["cpp"],"Pointer") | (["cpp"],"ConstPointer") | (["cpp"],"Function") -> List.iter visit_type params
+         | ([],"Array") | ([],"Class") | (["cpp"],"FastIterator")
+         | (["cpp"],"Pointer") | (["cpp"],"ConstPointer") | (["cpp"],"Function")
+         | (["cpp"],"RawPointer") | (["cpp"],"RawConstPointer") -> List.iter visit_type params
          | _ when is_extern_class klass -> add_extern_class klass
          | _ -> (match klass.cl_kind with KTypeParameter _ -> () | _ -> add_type klass.cl_path);
          )

+ 0 - 19
std/cpp/BasePointer.hx

@@ -1,19 +0,0 @@
-package cpp;
-
-@:coreType @:include("cpp/Pointer.h")
-extern class BasePointer<T>
-{
-   // ptr actually returns the pointer - not strictly a 'T' - for pointers to smart pointers
-   // Use value or ref to get dereferenced value
-	public var ptr:T;
-
-	public var value(get,never):T;
-
-	public function lt(inOther:BasePointer<T>):Bool;
-	public function leq(inOther:BasePointer<T>):Bool;
-	public function gt(inOther:BasePointer<T>):Bool;
-	public function geq(inOther:BasePointer<T>):Bool;
-
-}
-
-

+ 17 - 1
std/cpp/ConstPointer.hx

@@ -1,8 +1,24 @@
 package cpp;
 
 @:coreType @:include("cpp/Pointer.h") @:native("cpp.Pointer")
-extern class ConstPointer<T> extends BasePointer<T>
+extern class ConstPointer<T>
 {
+   // ptr actually returns the pointer - not strictly a 'T' - for pointers to smart pointers
+   // Use value or ref to get dereferenced value
+	private var ptr:T;
+
+	public var value(get,never):T;
+	public var raw(get,never):RawConstPointer<T>;
+
+   public function get_raw() : RawConstPointer<T>;
+
+	public function lt(inOther:Pointer<T>):Bool;
+	public function leq(inOther:Pointer<T>):Bool;
+	public function gt(inOther:Pointer<T>):Bool;
+	public function geq(inOther:Pointer<T>):Bool;
+
+
+
    public static function fromPointer<T>(inNativePointer:Dynamic) : ConstPointer<T>;
 
 	public function reinterpret<Other>():Pointer<Other>;

+ 2 - 0
std/cpp/Function.hx

@@ -3,6 +3,8 @@ package cpp;
 @:coreType @:structAccess @:include("cpp/Pointer.h")
 extern class Function<T>
 {
+   public function new(d:Dynamic);
+
    // Actually a function pointer, but can be called using haxe notation
 	public var call(default,null):T;
 

+ 58 - 2
std/cpp/Lib.hx

@@ -21,8 +21,19 @@
  */
 package cpp;
 
+#if macro
+import haxe.macro.Context;
+import haxe.macro.Type;
+import haxe.macro.Expr;
+#end
+
+using cpp.NativeString;
+using cpp.RawConstPointer;
+using cpp.Char;
+
 class Lib {
 
+   #if !macro
 	/**
 		Load and return a Cpp primitive from a DLL library.
 	**/
@@ -37,10 +48,23 @@ class Lib {
 	/**
 		Load and return a Cpp primitive from a DLL library.
 	**/
-	@:extern public static inline function getProcAddress( lib : String, prim : String ) : Dynamic {
-		return untyped __global__.__hxcpp_cast_get_proc_address(lib,prim);
+	@:extern public static inline function getProcAddress( lib : String, prim : String, quietFail=false ) : Dynamic {
+		return untyped __global__.__hxcpp_cast_get_proc_address(lib,prim,quietFail);
+	}
+
+	public static function _loadPrime( lib : String, prim : String, signature : String, quietFail = false ) : Dynamic {
+		var factory:Function< RawConstPointer<Char> -> RawPointer<Object> > = getProcAddress(lib, prim + "__prime", quietFail);
+      if (factory!=null)
+      {
+         var func:Dynamic = factory.call(signature.raw());
+         if (func==null && !quietFail)
+            throw '$prim does not have signature $signature';
+         return func;
+      }
+      return null;
 	}
 
+
 	/**
 		Tries to load, and always returns a valid function, but the function may throw
 		if called.
@@ -94,4 +118,36 @@ class Lib {
 		untyped __global__.__hxcpp_println(v);
 	}
 
+   #else
+   static function codeToType(code:String) : String
+   {
+      switch(code)
+      {
+         case "b" : return "Bool";
+         case "i" : return "Int";
+         case "d" : return "Float";
+         case "f" : return "cpp.Float32";
+         case "s" : return "String";
+         case "o" : return "cpp.Object";
+         case "v" : return "cpp.Void";
+         case "c" : return "cpp.RawConstPtr<cpp.Char> ";
+         default:
+            throw "Unknown signature type :" + code;
+      }
+   }
+   #end
+
+   public static macro function loadPrime(inModule:String, inName:String, inSig:String,inAllowFail:Bool = false)
+   {
+      var parts = inSig.split("");
+      if (parts.length<1)
+         throw "Invalid function signature " + inSig;
+      var typeString = parts.length==1 ? "Void" : codeToType(parts.shift());
+      for(p in parts)
+         typeString += "->" + codeToType(p);
+      typeString = "cpp.Function<" + typeString + ">";
+      var expr = 'new $typeString(cpp.Lib._loadPrime("$inModule","$inName","$inSig",$inAllowFail))';
+      return Context.parse( expr, Context.currentPos() );
+   }
+
 }

+ 3 - 0
std/cpp/NativeString.hx

@@ -2,6 +2,9 @@ package cpp;
 
 extern class NativeString {
 
+	public static inline function raw( inString:String ) : RawConstPointer<Char> {
+      return untyped inString.__s;
+   }
 	public static inline function c_str( inString:String ) : ConstPointer<Char> {
 		return cpp.ConstPointer.fromPointer(untyped inString.__s);
    }

+ 15 - 0
std/cpp/Object.hx

@@ -0,0 +1,15 @@
+package cpp;
+
+@:native("::hx::Object *")
+extern class HxObjectPtr
+{
+   @:native("hx::DynamicPtr")
+   static function fromDynamic(x:Dynamic):Object;
+   @:native("Dynamic")
+   static function toDynamic(x:Object):Dynamic;
+}
+
+abstract Object(HxObjectPtr) {
+	@:from public inline static function from(x:Dynamic):Object return HxObjectPtr.fromDynamic(x);
+	@:to public inline function to():Dynamic return HxObjectPtr.toDynamic(this);
+}

+ 2 - 0
std/cpp/Pointer.hx

@@ -13,6 +13,8 @@ extern class Pointer<T> extends ConstPointer<T> implements ArrayAccess<T>
 
 	public static function arrayElem<T>(array:Array<T>, inElem:Int):Pointer<T>;
 
+   override public function get_raw() : RawPointer<T>;
+
 	override public function inc():Pointer<T>;
 	override public function dec():Pointer<T>;
 	override public function incBy(inT:Int):Pointer<T>;

+ 6 - 0
std/cpp/RawConstPointer.hx

@@ -0,0 +1,6 @@
+package cpp;
+
+@:unreflective
+extern class RawConstPointer<T>
+{
+}

+ 6 - 0
std/cpp/RawPointer.hx

@@ -0,0 +1,6 @@
+package cpp;
+
+@:unreflective
+extern class RawPointer<T> extends RawConstPointer<T>
+{
+}

+ 5 - 0
std/cpp/Void.hx

@@ -0,0 +1,5 @@
+package cpp;
+
+@:native("void")
+extern class Void { }
+