Browse Source

[cpp] Add unsafeGet, unsafeSet and memcmp to cpp.NativeArray. Add optimised cpp versions for haxe.io.Bytes

Hugh 11 years ago
parent
commit
0dcd798c17
2 changed files with 34 additions and 1 deletions
  1. 11 0
      std/cpp/NativeArray.hx
  2. 23 1
      std/haxe/io/Bytes.hx

+ 11 - 0
std/cpp/NativeArray.hx

@@ -11,4 +11,15 @@ class NativeArray {
 	public static inline function zero<T>( ioDestArray:Array<T>, ?inFirst:Int, ?inElements:Int ) {
 		untyped ioDestArray.zero(inFirst, inElements);
 	};
+
+	public static inline function unsafeGet<T>( inDestArray:Array<T>, inIndex:Int) : T {
+		return untyped inDestArray.__unsafe_get(inIndex);
+	}
+	public static inline function unsafeSet<T>( ioDestArray:Array<T>, inIndex:Int, inValue:T) : T {
+		return untyped ioDestArray.__unsafe_set(inIndex,inValue);
+	}
+
+	public static inline function memcmp<T>( inArrayA:Array<T>, inArrayB:Array<T>) : Int {
+		return untyped inArrayA.memcmp(inArrayB);
+	}
 }

+ 23 - 1
std/haxe/io/Bytes.hx

@@ -21,6 +21,10 @@
  */
 package haxe.io;
 
+#if cpp
+using cpp.NativeArray;
+#end
+
 class Bytes {
 
 	public var length(default,null) : Int;
@@ -83,6 +87,8 @@ class Bytes {
 		java.lang.System.arraycopy(src.b, srcpos, b, pos, len);
 		#elseif cs
 		cs.system.Array.Copy(src.b, srcpos, b, pos, len);
+		#elseif cpp
+		b.blit(pos, src.b, srcpos, len);
 		#else
 		var b1 = b;
 		var b2 = src.b;
@@ -110,6 +116,8 @@ class Bytes {
 		pos += len&~3;
 		for( i in 0...len&3 )
 			set(pos++,value);
+		#elseif cpp
+		untyped __global__.__hxcpp_memory_memset(b,pos,len,value);
 		#else
 		for( i in 0...len )
 			set(pos++, value);
@@ -175,6 +183,8 @@ class Bytes {
 		return untyped __php__("$this->b < $other->b ? -1 : ($this->b == $other->b ? 0 : 1)");
 		//#elseif cs
 		//TODO: memcmp if unsafe flag is on
+		#elseif cpp
+		return b.memcmp(other.b);
 		#else
 		var b1 = b;
 		var b2 = other.b;
@@ -196,6 +206,9 @@ class Bytes {
 		#elseif flash9
 		b.position = pos;
 		return b.readDouble();
+		#elseif cpp
+		if( pos < 0 || pos + 8 > length ) throw Error.OutsideBounds;
+		return untyped __global__.__hxcpp_memory_get_double(b,pos);
 		#else
 		var b = new haxe.io.BytesInput(this,pos,8);
 		return b.readDouble();
@@ -208,6 +221,9 @@ class Bytes {
 		#elseif flash9
 		b.position = pos;
 		return b.readFloat();
+		#elseif cpp
+		if( pos < 0 || pos + 4 > length ) throw Error.OutsideBounds;
+		return untyped __global__.__hxcpp_memory_get_float(b,pos);
 		#else
 		var b = new haxe.io.BytesInput(this,pos,4);
 		return b.readFloat();
@@ -220,6 +236,9 @@ class Bytes {
 		#elseif flash9
 		b.position = pos;
 		b.writeDouble(v);
+		#elseif cpp
+		if( pos < 0 || pos + 8 > length ) throw Error.OutsideBounds;
+		untyped __global__.__hxcpp_memory_set_double(b,pos,v);
 		#else
 		throw "Not supported";
 		#end
@@ -231,6 +250,9 @@ class Bytes {
 		#elseif flash9
 		b.position = pos;
 		b.writeFloat(v);
+		#elseif cpp
+		if( pos < 0 || pos + 4 > length ) throw Error.OutsideBounds;
+		untyped __global__.__hxcpp_memory_set_float(b,pos,v);
 		#else
 		throw "Not supported";
 		#end
@@ -436,7 +458,7 @@ class Bytes {
 		#elseif php
 		return untyped __call__("ord", b[pos]);
 		#elseif cpp
-		return untyped b[pos];
+		return untyped b.unsafeGet(pos);
 		#elseif java
 		return untyped b[pos] & 0xFF;
 		#else