Sfoglia il codice sorgente

Rework cpp specific code into _std

Hugh Sanderson 15 anni fa
parent
commit
102eeda6db
11 ha cambiato i file con 688 aggiunte e 123 eliminazioni
  1. 1 6
      std/Date.hx
  2. 3 3
      std/EReg.hx
  3. 0 5
      std/Xml.hx
  4. 2 54
      std/cpp/_std/Date.hx
  5. 162 0
      std/cpp/_std/EReg.hx
  6. 89 0
      std/cpp/_std/Hash.hx
  7. 79 0
      std/cpp/_std/IntHash.hx
  8. 98 0
      std/cpp/_std/Reflect.hx
  9. 51 0
      std/cpp/_std/Std.hx
  10. 152 0
      std/cpp/_std/Type.hx
  11. 51 55
      std/cpp/_std/Xml.hx

+ 1 - 6
std/Date.hx

@@ -28,10 +28,6 @@
 	available in the [DateTools] class.
 **/
 
-#if (cpp && !xmldoc)
-typedef Date = cpp.CppDate__;
-#else
-
 extern class Date
 {
 	/**
@@ -115,7 +111,7 @@ extern class Date
 	function getLocaleTime():String;
 	#end
 
-#if !(php || neko)
+#if !(php || neko || cpp)
 	private static function __init__() : Void untyped {
 		var d #if !swf_mark : Dynamic #end = Date;
 		d.now = function() {
@@ -185,4 +181,3 @@ extern class Date
 #end
 }
 
-#end // !cpp

+ 3 - 3
std/EReg.hx

@@ -335,9 +335,9 @@ class EReg {
 	}
 
 #if cpp
-	static var regexp_new_options = cpp.Lib.load("regexp","regexp_new_options",2);
-	static var regexp_match = cpp.Lib.load("regexp","regexp_match",4);
-	static var regexp_matched = cpp.Lib.load("regexp","regexp_matched",2);
+	static var regexp_new_options : String -> String -> Dynamic = cpp.Lib.load("regexp","regexp_new_options",2);
+	static var regexp_match : Dynamic -> String -> Int -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_match",4);
+	static var regexp_matched : Dynamic -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_matched",2);
 	static var regexp_matched_pos : Dynamic -> Int -> { pos : Int, len : Int } = cpp.Lib.load("regexp","regexp_matched_pos",2);
 #end
 

+ 0 - 5
std/Xml.hx

@@ -31,10 +31,6 @@
 **/
 
 
-#if (cpp && !xmldoc)
-typedef XmlType = String;
-typedef Xml = cpp.CppXml__;
-#else
 
 enum XmlType {
 }
@@ -250,7 +246,6 @@ extern class Xml {
 #end
 }
 
-#end
 
 
 

+ 2 - 54
std/cpp/CppDate__.hx → std/cpp/_std/Date.hx

@@ -23,97 +23,45 @@
  * DAMAGE.
  */
 
-/**
-	The Date class is used for date manipulation. There is some extra functions
-	available in the [DateTools] class.
-**/
 
-package cpp;
+@:core_api  class Date {
 
-class CppDate__
-{
-	var mSeconds:Float;
+	private var mSeconds:Float;
 
-	/**
-		Creates a new date object.
-	**/
 	public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void	{
 		mSeconds = untyped __global__.__hxcpp_new_date(year,month,day,hour,min,sec);
 	}
 
-	/**
-		Returns the timestamp of the date. It's the number of milliseconds
-		elapsed since 1st January 1970. It might only have a per-second precision
-		depending on the platforms.
-	**/
 	public function getTime() : Float {
 		return mSeconds * 1000.0;
 	}
 
-	/**
-		Returns the hours value of the date (0-23 range).
-	**/
 	public function getHours() : Int { return untyped __global__.__hxcpp_get_hours(mSeconds); }
 
-	/**
-		Returns the minutes value of the date (0-59 range).
-	**/
 	public function getMinutes() : Int { return untyped __global__.__hxcpp_get_minutes(mSeconds); }
 
-	/**
-		Returns the seconds of the date (0-59 range).
-	**/
 	public function getSeconds() : Int { return untyped __global__.__hxcpp_get_seconds(mSeconds); }
 
-	/**
-		Returns the full year of the date.
-	**/
 	public function getFullYear() : Int { return untyped __global__.__hxcpp_get_year(mSeconds); }
 
-	/**
-		Returns the month of the date (0-11 range).
-	**/
 	public function getMonth() : Int { return untyped __global__.__hxcpp_get_month(mSeconds); }
 
-	/**
-		Returns the day of the date (1-31 range).
-	**/
 	public function getDate() : Int { return untyped __global__.__hxcpp_get_date(mSeconds); }
 
-	/**
-		Returns the week day of the date (0-6 range).
-	**/
 	public function getDay() : Int { return untyped __global__.__hxcpp_get_day(mSeconds); }
 
-	/**
-		Returns a string representation for the Date, by using the
-		standard format [YYYY-MM-DD HH:MM:SS]. See [DateTools.format] for
-		other formating rules.
-	**/
 	public function toString():String { return untyped __global__.__hxcpp_to_string(mSeconds); }
 
-	/**
-		Returns a Date representing the current local time.
-	**/
 	public static function now() : Date {
 		return fromTime( untyped __global__.__hxcpp_date_now()*1000.0);
 	}
 
-	/**
-		Returns a Date from a timestamp [t] which is the number of
-		milliseconds elapsed since 1st January 1970.
-	**/
 	public static function fromTime( t : Float ) : Date {
 		var result = new Date(0,0,0,0,0,0);
 		result.mSeconds = t*0.001;
 		return result;
 	}
 
-	/**
-		Returns a Date from a formated string of one of the following formats :
-		[YYYY-MM-DD hh:mm:ss] or [YYYY-MM-DD] or [hh:mm:ss]. The first two formats
-		are expressed in local time, the third in UTC Epoch.
-	**/
 	public static function fromString( s : String ) : Date {
 		switch( s.length ) {
 			case 8: // hh:mm:ss

+ 162 - 0
std/cpp/_std/EReg.hx

@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class EReg {
+
+	var r : Dynamic;
+	var last : String;
+	var global : Bool;
+
+	public function new( r : String, opt : String ) : Void {
+			var a = opt.split("g");
+			global = a.length > 1;
+			if( global )
+				opt = a.join("");
+			this.r = regexp_new_options(r, opt);
+	}
+
+	public function match( s : String ) : Bool {
+			var p = regexp_match(r,s,0,s.length);
+			if( p )
+				last = s;
+			else
+				last = null;
+			return p;
+	}
+
+	public function matched( n : Int ) : String {
+			var m = regexp_matched(r,n);
+			return m;
+	}
+
+	public function matchedLeft() : String {
+			var p = regexp_matched_pos(r,0);
+			return last.substr(0,p.pos);
+	}
+
+	public function matchedRight() : String {
+			var p = regexp_matched_pos(r,0);
+			var sz = p.pos+p.len;
+			return last.substr(sz,last.length-sz);
+	}
+
+	public function matchedPos() : { pos : Int, len : Int } {
+			return regexp_matched_pos(r,0);
+	}
+
+	public function split( s : String ) : Array<String> {
+			var pos = 0;
+			var len = s.length;
+			var a = new Array();
+			var first = true;
+			do {
+				if( !regexp_match(r,s,pos,len) )
+					break;
+				var p = regexp_matched_pos(r,0);
+				if( p.len == 0 && !first ) {
+					if( p.pos == s.length )
+						break;
+					p.pos += 1;
+				}
+				a.push(s.substr(pos,p.pos - pos));
+				var tot = p.pos + p.len - pos;
+				pos += tot;
+				len -= tot;
+				first = false;
+			} while( global );
+			a.push(s.substr(pos,len));
+			return a;
+	}
+
+	public function replace( s : String, by : String ) : String {
+			var b = new StringBuf();
+			var pos = 0;
+			var len = s.length;
+			var a = by.split("$");
+			var first = true;
+			do {
+				if( !regexp_match(r,s,pos,len) )
+					break;
+				var p = regexp_matched_pos(r,0);
+				if( p.len == 0 && !first ) {
+					if( p.pos == s.length )
+						break;
+					p.pos += 1;
+				}
+				b.addSub(s,pos,p.pos-pos);
+				if( a.length > 0 )
+					b.add(a[0]);
+				var i = 1;
+				while( i < a.length ) {
+					var k = a[i];
+					var c = k.charCodeAt(0);
+					// 1...9
+					if( c >= 49 && c <= 57 ) {
+						var p = try regexp_matched_pos(r,Std.int(c)-48) catch( e : String ) null;
+						if( p == null ){
+							b.add("$");
+							b.add(k);
+						}else{
+						b.addSub(s,p.pos,p.len);
+						b.addSub(k,1,k.length - 1);
+						}
+					} else if( c == null ) {
+						b.add("$");
+						i++;
+						var k2 = a[i];
+						if( k2 != null && k2.length > 0 )
+							b.add(k2);
+					} else
+						b.add("$"+k);
+					i++;
+				}
+				var tot = p.pos + p.len - pos;
+				pos += tot;
+				len -= tot;
+				first = false;
+			} while( global );
+			b.addSub(s,pos,len);
+			return b.toString();
+	}
+
+	public function customReplace( s : String, f : EReg -> String ) : String {
+		var buf = new StringBuf();
+		while( true ) {
+			if( !match(s) )
+				break;
+			buf.add(matchedLeft());
+			buf.add(f(this));
+			s = matchedRight();
+		}
+		buf.add(s);
+		return buf.toString();
+	}
+
+	static var regexp_new_options : String -> String -> Dynamic = cpp.Lib.load("regexp","regexp_new_options",2);
+	static var regexp_match : Dynamic -> String -> Int -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_match",4);
+	static var regexp_matched : Dynamic -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_matched",2);
+	static var regexp_matched_pos : Dynamic -> Int -> { pos : Int, len : Int } = cpp.Lib.load("regexp","regexp_matched_pos",2);
+
+}

+ 89 - 0
std/cpp/_std/Hash.hx

@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class Hash<T>  {
+	private var h : Dynamic;
+
+	public function new() : Void {
+		h = {};
+	}
+
+	public function set( key : String, value : T ) : Void {
+		untyped h.__SetField(key,value);
+	}
+
+	public function get( key : String ) : Null<T> {
+		return untyped h.__Field(key);
+	}
+
+	public function exists( key : String ) : Bool {
+		return untyped h.__HasField(key);
+	}
+
+	public function remove( key : String ) : Bool {
+		return untyped __global__.__hxcpp_anon_remove(h,key);
+	}
+
+	/**
+		Returns an iterator of all keys in the hashtable.
+	**/
+	public function keys() : Iterator<String> {
+		var a:Array<String> = [];
+		untyped h.__GetFields(a);
+		return a.iterator();
+	}
+
+	/**
+		Returns an iterator of all values in the hashtable.
+	**/
+	public function iterator() : Iterator<T> {
+		var a:Array<String> = [];
+		untyped h.__GetFields(a);
+		var it = a.iterator();
+		return untyped {
+			hasNext : function() { return it.hasNext(); },
+			next : function() { return  untyped h.__Field(it.next()); }
+		};
+	}
+
+	/**
+		Returns an displayable representation of the hashtable content.
+	**/
+
+	public function toString() : String {
+		var s = new StringBuf();
+		s.add("{");
+		var it = keys();
+		for( i in it ) {
+			s.add(i);
+			s.add(" => ");
+			s.add(Std.string(get(i)));
+			if( it.hasNext() )
+				s.add(", ");
+		}
+		s.add("}");
+		return s.toString();
+	}
+}

+ 79 - 0
std/cpp/_std/IntHash.hx

@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+/**
+	Hashtable over a set of elements, using [Int] as keys.
+	On Flash and Javascript, the underlying structure is an Object.
+**/
+@:core_api class IntHash<T> {
+
+	private var h : Dynamic;
+
+	public function new() : Void {
+		h = untyped __global__.__int_hash_create();
+	}
+
+	public function set( key : Int, value : T ) : Void {
+		untyped __global__.__int_hash_set(h,key,value);
+	}
+
+	public function get( key : Int ) : Null<T> {
+		return untyped __global__.__int_hash_get(h,key);
+	}
+
+	public function exists( key : Int ) : Bool {
+		return untyped __global__.__int_hash_exists(h,key);
+	}
+
+	public function remove( key : Int ) : Bool {
+		return untyped __global__.__int_hash_remove(h,key);
+	}
+
+	public function keys() : Iterator<Int> {
+		var a:Array<Int> = untyped __global__.__int_hash_keys(h);
+		return a.iterator();
+	}
+
+	public function iterator() : Iterator<T> {
+		var a:Array<Dynamic> = untyped __global__.__int_hash_values(h);
+		return a.iterator();
+	}
+
+	public function toString() : String {
+		var s = new StringBuf();
+		s.add("{");
+		var it = keys();
+		for( i in it ) {
+			s.add(i);
+			s.add(" => ");
+			s.add(Std.string(get(i)));
+			if( it.hasNext() )
+				s.add(", ");
+		}
+		s.add("}");
+		return s.toString();
+	}
+
+}

+ 98 - 0
std/cpp/_std/Reflect.hx

@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+
+@:core_api class Reflect {
+
+	public  static function hasField( o : Dynamic, field : String ) : Bool untyped {
+		return o!=null && o.__HasField(field);
+	}
+
+	public static function field( o : Dynamic, field : String ) : Dynamic untyped {
+		return (o==null) ? null : o.__Field(field);
+	}
+
+	public inline static function setField( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
+		if (o!=null)
+			o.__SetField(field,value);
+	}
+
+	public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
+			if (func!=null && func.__GetType()==__global__.vtString)
+				func = o.__Field(func);
+			untyped func.__SetThis(o);
+         return untyped func.__Run(args);
+	}
+
+	public static function fields( o : Dynamic ) : Array<String> untyped {
+		if( o == null ) return new Array();
+		var a : Array<String> = [];
+		o.__GetFields(a);
+		return a;
+	}
+
+	public static function isFunction( f : Dynamic ) : Bool untyped {
+		return f!=null && f.__GetType() ==  __global__.vtFunction;
+	}
+
+	public static function compare<T>( a : T, b : T ) : Int {
+		return ( a == b ) ? 0 : (((cast a) > (cast b)) ? 1 : -1);
+	}
+
+	public static function compareMethods( f1 : Dynamic, f2 : Dynamic ) : Bool {
+		if( f1 == f2 )
+			return true;
+		if( !isFunction(f1) || !isFunction(f2) )
+			return false;
+		return untyped __global__.__hxcpp_same_closure(f1,f2);
+	}
+
+	public static function isObject( v : Dynamic ) : Bool untyped {
+		if (v==null) return false;
+		var t:Int = v.__GetType();
+		return t ==  __global__.vtObject || t==__global__.vtClass || t==__global__.vtString ||
+				t==__global__.vtArray;
+	}
+
+	public static function deleteField( o : Dynamic, f : String ) : Bool untyped {
+		if (o==null) return false;
+		return __hxcpp_anon_remove(o,f);
+	}
+
+	public static function copy<T>( o : T ) : T {
+		if (o==null) return null;
+		if(untyped o.__GetType()==__global__.vtString ) return o;
+		if(untyped o.__GetType()==__global__.vtArray )
+			return untyped o.__Field("copy")();
+		var o2 : Dynamic = {};
+		for( f in Reflect.fields(o) )
+			Reflect.setField(o2,f,Reflect.field(o,f));
+		return o2;
+	}
+
+	public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
+		return untyped __global__.__hxcpp_create_var_args(f);
+	}
+}

+ 51 - 0
std/cpp/_std/Std.hx

@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+@:core_api class Std {
+	public static function is( v : Dynamic, t : Dynamic ) : Bool {
+		return untyped __global__.__instanceof(v,t);
+	}
+
+	public static function string( s : Dynamic ) : String {
+		return untyped s==null ? "null" : s.toString();
+	}
+
+	public static function int( x : Float ) : Int {
+		return untyped __global__.__int__(x);
+	}
+
+	public static function parseInt( x : String ) : Null<Int> {
+		return untyped __global__.__hxcpp_parse_int(x);
+	}
+
+	public static function parseFloat( x : String ) : Float {
+		return untyped __global__.__hxcpp_parse_float(x);
+	}
+
+	public static function random( x : Int ) : Int {
+		return untyped __global__.rand() % x;
+	}
+
+}

+ 152 - 0
std/cpp/_std/Type.hx

@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+enum ValueType {
+	TNull;
+	TInt;
+	TFloat;
+	TBool;
+	TObject;
+	TFunction;
+	TClass( c : Class<Dynamic> );
+	TEnum( e : Enum<Dynamic> );
+	TUnknown;
+}
+
+@:core_api class Type {
+	public static function getClass<T>( o : T ) : Class<T> untyped {
+			if (o==null || !Reflect.isObject(o))  return null;
+			var c = o.__GetClass();
+			switch(c.toString())
+			{
+				case "__Anon" : return null;
+				case "Class" : return null;
+			}
+			return c;
+	}
+
+	public static function getEnum( o : Dynamic ) : Enum<Dynamic> untyped {
+		if (o==null) return null;
+		return o.__GetClass();
+	}
+
+
+	public static function getSuperClass( c : Class<Dynamic> ) : Class<Dynamic> untyped {
+		return c.GetSuper();
+	}
+
+	public static function getClassName( c : Class<Dynamic> ) : String {
+		if( c == null )
+			return null;
+		return untyped c.mName;
+	}
+
+	public static function getEnumName( e : Enum<Dynamic> ) : String {
+		return untyped e.__ToString();
+	}
+
+	public static function resolveClass( name : String ) : Class<Dynamic> untyped {
+		var result:Class<Dynamic> = Class.Resolve(name);
+		if (result!=null && result.__IsEnum() )
+			return null;
+		return result;
+	}
+
+	public static function resolveEnum( name : String ) : Enum<Dynamic> untyped {
+		var result:Class<Dynamic> = Class.Resolve(name);
+		if (result!=null && !result.__IsEnum() )
+			return null;
+		return result;
+	}
+
+	public static function createInstance<T>( cl : Class<T>, args : Array<Dynamic> ) : T untyped {
+		if (cl!=null)
+			return cl.mConstructArgs(args);
+		return null;
+	}
+
+	public static function createEmptyInstance<T>( cl : Class<T> ) : T untyped {
+		return cl.mConstructEmpty();
+	}
+
+	public static function createEnum<T>( e : Enum<T>, constr : String, ?params : Array<Dynamic> ) : T {
+		if (untyped e.mConstructEnum != null)
+			return untyped e.mConstructEnum(constr,params);
+		return null;
+	}
+
+	public static function createEnumIndex<T>( e : Enum<T>, index : Int, ?params : Array<Dynamic> ) : T {
+		var c = Type.getEnumConstructs(e)[index];
+		if( c == null ) throw index+" is not a valid enum constructor index";
+		return createEnum(e,c,params);
+	}
+
+	public static function getInstanceFields( c : Class<Dynamic> ) : Array<String> {
+		return untyped c.GetInstanceFields();
+	}
+
+	public static function getClassFields( c : Class<Dynamic> ) : Array<String> {
+			return untyped c.GetClassFields();
+	}
+
+	public static function getEnumConstructs( e : Enum<Dynamic> ) : Array<String> untyped {
+			return untyped e.GetClassFields();
+	}
+
+	public static function typeof( v : Dynamic ) : ValueType untyped {
+			if (v==null) return TNull;
+			var t:Int = untyped v.__GetType();
+			switch(t)
+			{
+				case untyped __global__.vtBool : return TBool;
+				case untyped __global__.vtInt : return TInt;
+				case untyped __global__.vtFloat : return TFloat;
+				case untyped __global__.vtFunction : return TFunction;
+				case untyped __global__.vtObject : return TObject;
+				case untyped __global__.vtEnum : return TEnum(v.__GetClass());
+				default:
+					return untyped TClass(v.__GetClass());
+			}
+	}
+
+	public static function enumEq<T>( a : T, b : T ) : Bool untyped {
+			return a==b;
+	}
+
+	public static function enumConstructor( e : Dynamic ) : String {
+			return e.__Tag();
+	}
+
+	public static function enumParameters( e : Dynamic ) : Array<Dynamic> {
+			var result : Array<Dynamic> =  untyped e.__EnumParams();
+			return result==null ? [] : result;
+	}
+
+	public inline static function enumIndex( e : Dynamic ) : Int {
+			return e.__Index();
+	}
+
+}
+

+ 51 - 55
std/cpp/CppXml__.hx → std/cpp/_std/Xml.hx

@@ -22,11 +22,19 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  */
-package cpp;
 
-import Xml;
+enum XmlType {
+   element;
+   pcdata;
+   cdata;
+   comment;
+   doctype;
+   prolog;
+   document;
+}
+
 
-class CppXml__ {
+@:core_api class Xml {
 	public static var Element(default,null) : XmlType;
 	public static var PCData(default,null) : XmlType;
 	public static var CData(default,null) : XmlType;
@@ -40,20 +48,20 @@ class CppXml__ {
 	private var _nodeValue : String;
 	private var _attributes : Dynamic<String>;
 	private var _children : Array<Dynamic>;
-	private var _parent : CppXml__;
+	private var _parent : Xml;
 
-	private function new() {
+	function new() : Void {
 	}
 
 	private static var _parse = cpp.Lib.load("std","parse_xml",2);
 
-	public static function parse( xmlData : String ) : CppXml__ {
-		var x = new CppXml__();
+	public static function parse( xmlData : String ) : Xml {
+		var x = new Xml();
 		x._children = new Array();
 		var parser = {
 			cur : x,
 			xml : function(name,att) {
-				var x : Dynamic = new CppXml__();
+				var x : Dynamic = new Xml();
 				x._parent = untyped this.cur;
 				x.nodeType = Xml.Element;
 				x._nodeName = new String(name);
@@ -66,21 +74,21 @@ class CppXml__ {
 				}
 			},
 			cdata : function(text) {
-				var x = new CppXml__();
+				var x = new Xml();
 				x._parent = untyped this.cur;
 				x.nodeType = Xml.CData;
 				x._nodeValue = new String(text);
 				untyped this.cur.addChild(x);
 			},
 			pcdata : function(text) {
-				var x = new CppXml__();
+				var x = new Xml();
 				x._parent = untyped this.cur;
 				x.nodeType = Xml.PCData;
 				x._nodeValue = new String(text);
 				untyped this.cur.addChild(x);
 			},
 			comment : function(text:String) {
-				var x = new CppXml__();
+				var x = new Xml();
 				x._parent = untyped this.cur;
 				if( untyped text.cca(0) == 63 ) {
 					x.nodeType = Xml.Prolog;
@@ -94,7 +102,7 @@ class CppXml__ {
 				untyped this.cur.addChild(x);
 			},
 			doctype : function(text) {
-				var x = new CppXml__();
+				var x = new Xml();
 				x._parent = untyped this.cur;
 				x.nodeType = Xml.DocType;
 				x._nodeValue = (new String(text)).substr(1);
@@ -110,8 +118,8 @@ class CppXml__ {
 	}
 
 
-	public static function createElement( name : String ) : CppXml__ {
-		var r = new CppXml__();
+	public static function createElement( name : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Element;
 		r._nodeName = name;
 		r._attributes = null;
@@ -119,63 +127,52 @@ class CppXml__ {
 		return r;
 	}
 
-	public static function createPCData( data : String ) : CppXml__ {
-		var r = new CppXml__();
+	public static function createPCData( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.PCData;
 		r._nodeValue = data;
 		return r;
 	}
 
-	public static function createCData( data : String ) : CppXml__ {
-		var r = new CppXml__();
+	public static function createCData( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.CData;
 		r._nodeValue = data;
 		return r;
 	}
 
-	public static function createComment( data : String ) : CppXml__ {
-		var r = new CppXml__();
+	public static function createComment( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Comment;
 		r._nodeValue = data;
 		return r;
 	}
 
-	public static function createDocType( data : String ) : CppXml__ {
-		var r = new CppXml__();
+	public static function createDocType( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.DocType;
 		r._nodeValue = data;
 		return r;
 	}
 
-	public static function createProlog( data : String ) : CppXml__ {
-		var r = new CppXml__();
+	public static function createProlog( data : String ) : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Prolog;
 		r._nodeValue = data;
 		return r;
 	}
 
-	public static function createDocument() : CppXml__ {
-		var r = new CppXml__();
+	public static function createDocument() : Xml {
+		var r = new Xml();
 		r.nodeType = Xml.Document;
 		r._children = new Array();
 		return r;
 	}
 
-	/**
-		Returns the type of the Xml Node. This should be used before
-		accessing other functions since some might raise an exception
-		if the node type is not correct.
-	**/
 	public var nodeType(default,null) : XmlType;
 
-	/**
-		Returns the node name of an Element.
-	**/
 	public var nodeName(getNodeName,setNodeName) : String;
 
-	/**
-		Returns the node value. Only works if the Xml node is not an Element or a Document.
-	**/
 	public var nodeValue(getNodeValue,setNodeValue) : String;
 
 
@@ -203,7 +200,7 @@ class CppXml__ {
 		return _nodeValue = v;
 	}
 
-	var parent(getParent,null) : Xml;
+	public var parent(getParent,null) : Xml;
 	private function getParent() : Xml {
 		return _parent;
 	}
@@ -326,15 +323,14 @@ class CppXml__ {
 		if( _children == null )
 			throw "bad nodetype";
 		for( cur in 0..._children.length ) {
-			var n:CppXml__ = _children[cur];
+			var n:Xml = _children[cur];
 			if( n.nodeType == Xml.Element )
 				return n;
 		}
 		return null;
 	}
 
-   public function addChild( x_ : Xml ) : Void {
-      var x:CppXml__ = cast x_;
+   public function addChild( x : Xml ) : Void {
 		if( _children == null )
 			throw "bad nodetype";
 		if( x._parent != null ) x._parent._children.remove(x);
@@ -343,8 +339,7 @@ class CppXml__ {
 		return null;
 	}
 
-   public function removeChild( x_ : Xml ) : Bool {
-      var x:CppXml__ = cast x_;
+   public function removeChild( x : Xml ) : Bool {
 		if( _children == null )
 			throw "bad nodetype";
 		var b = _children.remove( x );
@@ -352,8 +347,7 @@ class CppXml__ {
 		return b;
 	}
 
-	public function insertChild( x_ : Xml, pos : Int ) : Void {
-      var x:CppXml__ = cast x_;
+	public function insertChild( x : Xml, pos : Int ) : Void {
 		if( _children == null )
 			throw "bad nodetype";
 		if( x._parent != null ) x._parent._children.remove(x);
@@ -362,13 +356,13 @@ class CppXml__ {
 		return null;
 	}
 
-	public function toString() {
+	public function toString() : String {
 		var s = new StringBuf();
 		toStringRec(s);
 		return s.toString();
 	}
 
-	public function toStringRec(s: StringBuf) {
+	private function toStringRec(s: StringBuf) : Void {
 		switch( nodeType ) {
 		case Xml.Document:
 			for( x in _children )
@@ -417,14 +411,16 @@ class CppXml__ {
 		}
 	}
 
-	static function __init__() : Void {
-		Element = "element";
-		PCData = "pcdata";
-		CData = "cdata";
-		Comment = "comment";
-		DocType = "doctype";
-		Prolog = "prolog";
-		Document = "document";
+	// Must call this after XML elements have been constructed
+	static function __do_init() : Void {
+		Element = XmlType.element;
+		PCData = XmlType.pcdata;
+		CData = XmlType.cdata;
+		Comment = XmlType.comment;
+		DocType = XmlType.doctype;
+		Prolog = XmlType.prolog;
+		Document = XmlType.document;
 	}
+	static var __init_later = __do_init();
 }