Browse Source

[cpp] Add Array-with-length allocator

hughsando 8 years ago
parent
commit
d9f0b809eb
3 changed files with 22 additions and 3 deletions
  1. 8 0
      src/generators/gencpp.ml
  2. 13 1
      std/cpp/NativeArray.hx
  3. 1 2
      std/haxe/ds/Vector.hx

+ 8 - 0
src/generators/gencpp.ml

@@ -2578,6 +2578,14 @@ let retype_expression ctx request_type function_args expression_tree forInjectio
                |  CppFunction( FuncStatic(obj, false, member), _ ) when member.cf_name = "hx::AddressOf" ->
                |  CppFunction( FuncStatic(obj, false, member), _ ) when member.cf_name = "hx::AddressOf" ->
                     let arg = retype TCppUnchanged (List.hd args) in
                     let arg = retype TCppUnchanged (List.hd args) in
                     CppAddressOf(arg), TCppRawPointer("", arg.cpptype)
                     CppAddressOf(arg), TCppRawPointer("", arg.cpptype)
+               |  CppFunction( FuncStatic(obj, false, member), _ ) when member.cf_name = "_hx_create_array_length" ->
+                    (* gc_stack - not needed yet *)
+                    (match return_type with
+                    | TCppObjectArray _
+                    | TCppScalarArray _ -> CppCall( FuncNew(return_type), retypedArgs), return_type
+                    | _ -> CppCall( FuncNew(TCppDynamicArray), retypedArgs), return_type
+                    )
+
                |  CppFunction( FuncStatic(obj, false, member), returnType ) when cpp_is_templated_call ctx member ->
                |  CppFunction( FuncStatic(obj, false, member), returnType ) when cpp_is_templated_call ctx member ->
                      (match retypedArgs with
                      (match retypedArgs with
                      | {cppexpr = CppClassOf(path,native) }::rest ->
                      | {cppexpr = CppClassOf(path,native) }::rest ->

+ 13 - 1
std/cpp/NativeArray.hx

@@ -23,6 +23,18 @@
 
 
 extern class NativeArray {
 extern class NativeArray {
 
 
+   #if cppia
+   public static inline function create<T>(length:Int):Array<T>
+   {
+      var result = new Array<T>();
+      NativeArray.setSize(result,length);
+      return result;
+   }
+   #else
+   @:native("_hx_create_array_length")
+   public static function create<T>(length:Int):Array<T>;
+   #end
+
 	public static inline function blit<T>( ioDestArray:Array<T>,
 	public static inline function blit<T>( ioDestArray:Array<T>,
 		inDestElement:Int, inSourceArray:Array<T>,
 		inDestElement:Int, inSourceArray:Array<T>,
 		inSourceElement:Int, inElementCount:Int ): Void  {
 		inSourceElement:Int, inElementCount:Int ): Void  {
@@ -48,7 +60,7 @@ extern class NativeArray {
 
 
 	public static inline function setData<T>( inArray:Array<T>,inData:Pointer<T>,inElementCount:Int ) : Void {
 	public static inline function setData<T>( inArray:Array<T>,inData:Pointer<T>,inElementCount:Int ) : Void {
       untyped inArray.setData(inData.raw,inElementCount);
       untyped inArray.setData(inData.raw,inElementCount);
-   }
+      }
 	public static inline function setUnmanagedData<T>( inArray:Array<T>,inData:ConstPointer<T>,inElementCount:Int ) : Void {
 	public static inline function setUnmanagedData<T>( inArray:Array<T>,inData:ConstPointer<T>,inElementCount:Int ) : Void {
       untyped inArray.setUnmanagedData(inData.raw,inElementCount);
       untyped inArray.setUnmanagedData(inData.raw,inElementCount);
    }
    }

+ 1 - 2
std/haxe/ds/Vector.hx

@@ -69,8 +69,7 @@ abstract Vector<T>(VectorData<T>) {
 		#elseif java
 		#elseif java
 			this = new java.NativeArray(length);
 			this = new java.NativeArray(length);
 		#elseif cpp
 		#elseif cpp
-			this = new Array<T>();
-			this.setSize(length);
+			this = NativeArray.create(length);
 		#elseif python
 		#elseif python
 			this = python.Syntax.pythonCode("[{0}]*{1}", null, length);
 			this = python.Syntax.pythonCode("[{0}]*{1}", null, length);
 		#elseif lua
 		#elseif lua