Browse Source

Move most cpp specific code from Date, Xml and Int32 into cpp directory

Hugh Sanderson 16 years ago
parent
commit
43c4a5d81a
6 changed files with 259 additions and 222 deletions
  1. 5 107
      std/Date.hx
  2. 7 72
      std/Xml.hx
  3. 138 0
      std/cpp/CppDate__.hx
  4. 48 0
      std/cpp/CppInt32__.hx
  5. 56 22
      std/cpp/CppXml__.hx
  6. 5 21
      std/haxe/Int32.hx

+ 5 - 107
std/Date.hx

@@ -28,7 +28,10 @@
 	available in the [DateTools] class.
 **/
 
-#if !cpp
+#if cpp
+typedef Date = cpp.CppDate__;
+#else
+
 extern class Date
 {
 	/**
@@ -185,110 +188,5 @@ extern class Date
 	}
 #end
 }
-#else // cpp ....
-class Date
-{
-	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
-				var k = s.split(":");
-				var d : Date = new Date(0,0,0,Std.parseInt(k[0]),Std.parseInt(k[1]),Std.parseInt(k[2]));
-				return d;
-			case 10: // YYYY-MM-DD
-				var k = s.split("-");
-				return new Date(Std.parseInt(k[0]),Std.parseInt(k[1])-1,Std.parseInt(k[2]),0,0,0);
-			case 19: // YYYY-MM-DD hh:mm:ss
-				var k = s.split(" ");
-				var y = k[0].split("-");
-				var t = k[1].split(":");
-				return new Date(Std.parseInt(y[0]),Std.parseInt(y[1]) - 1,Std.parseInt(y[2]),
-					Std.parseInt(t[0]),Std.parseInt(t[1]),Std.parseInt(t[2]));
-			default:
-				throw "Invalid date format : " + s;
-		}
-		return null;
-	}
-}
-#end
+#end // !cpp

+ 7 - 72
std/Xml.hx

@@ -29,19 +29,20 @@
 	use [Std.string(t)] to get a string reprensation
 	of the type.
 **/
-#if !cpp
+
+
+#if cpp
+typedef XmlType = String;
+typedef Xml = cpp.CppXml__;
+#else
+
 enum XmlType {
 }
-#else
-typedef XmlType = String;
-#end
 
 /**
 	The standard Xml class and parsing.
 	More API to manipulate XML are available in the [haxe.xml] package.
 **/
-#if !cpp
-
 
 extern class Xml {
 
@@ -257,72 +258,6 @@ extern class Xml {
 #end
 }
 
-#else
-class Xml {
-	public static var Element(default,null) : XmlType;
-	public static var PCData(default,null) : XmlType;
-	public static var CData(default,null) : XmlType;
-	public static var Comment(default,null) : XmlType;
-	public static var DocType(default,null) : XmlType;
-	public static var Prolog(default,null) : XmlType;
-	public static var Document(default,null) : XmlType;
-	public static function parse( s : String ) : Xml {
-		return cpp.CppXml__.parse(s);
-	}
-	public static function createElement( name : String ) : Xml {
-		return cpp.CppXml__.createElement(name);
-	}
-	public static function createPCData( data : String ) : Xml {
-		return cpp.CppXml__.createPCData(data);
-	}
-	public static function createCData( data : String ) : Xml {
-		return cpp.CppXml__.createCData(data);
-	}
-	public static function createComment( data : String ) : Xml {
-		return cpp.CppXml__.createComment(data);
-	}
-	public static function createDocType( data : String ) : Xml {
-		return cpp.CppXml__.createDocType(data);
-	}
-	public static function createProlog( data : String ) : Xml {
-		return cpp.CppXml__.createProlog(data);
-	}
-	public static function createDocument() : Xml { return null; }
-	public var nodeType(default,null) : XmlType;
-	public var nodeName(getNodeName,setNodeName) : String;
-	private function getNodeName() : String { return null; }
-	private function setNodeName( name : String ) : String { return null; }
-	public var nodeValue(getNodeValue,setNodeValue) : String;
-	private function getNodeValue() : String { return null; }
-	private function setNodeValue( name : String ) : String { return null; }
-	public function get( att : String ) : String { return null; }
-	public function set( att : String, value : String ) : Void { }
-	public function remove( att : String ) : Void { }
-	public function exists( att : String ) : Bool { return false; }
-	public function attributes() : Iterator<String> { return null; }
-	var parent(getParent,null) : Xml;
-	private function getParent() : Xml { return null; }
-	public function iterator() : Iterator<Xml> { return null; }
-	public function elements() : Iterator<Xml> { return null; }
-	public function elementsNamed( name : String ) : Iterator<Xml> { return null; }
-	public function firstChild() : Xml { return null; }
-	public function firstElement() : Xml { return null; }
-	public function addChild( x : Xml ) : Void { }
-	public function removeChild( x : Xml ) : Bool { return null; }
-	public function insertChild( x : Xml, pos : Int ) : Void { }
-	public function toString() : String { return null; }
-	
-	static function __init__() : Void untyped {
-		Xml.Element = "element";
-		Xml.PCData = "pcdata";
-		Xml.CData = "cdata";
-		Xml.Comment = "comment";
-		Xml.DocType = "doctype";
-		Xml.Prolog = "prolog";
-		Xml.Document = "document";
-	}
-}
-
 #end
 
 

+ 138 - 0
std/cpp/CppDate__.hx

@@ -0,0 +1,138 @@
+/*
+ * 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.
+ */
+
+/**
+	The Date class is used for date manipulation. There is some extra functions
+	available in the [DateTools] class.
+**/
+
+package cpp;
+
+class CppDate__
+{
+	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
+				var k = s.split(":");
+				var d : Date = new Date(0,0,0,Std.parseInt(k[0]),Std.parseInt(k[1]),Std.parseInt(k[2]));
+				return d;
+			case 10: // YYYY-MM-DD
+				var k = s.split("-");
+				return new Date(Std.parseInt(k[0]),Std.parseInt(k[1])-1,Std.parseInt(k[2]),0,0,0);
+			case 19: // YYYY-MM-DD hh:mm:ss
+				var k = s.split(" ");
+				var y = k[0].split("-");
+				var t = k[1].split(":");
+				return new Date(Std.parseInt(y[0]),Std.parseInt(y[1]) - 1,Std.parseInt(y[2]),
+					Std.parseInt(t[0]),Std.parseInt(t[1]),Std.parseInt(t[2]));
+			default:
+				throw "Invalid date format : " + s;
+		}
+		return null;
+	}
+}
+

+ 48 - 0
std/cpp/CppInt32__.hx

@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+package cpp;
+
+typedef Int32 = CppInt32__;
+
+extern class CppInt32__ {
+	public static  function make( a : Int, b : Int ) : Int32;
+	public static  function ofInt( x : Int ) : Int32;
+	public static  function toInt( x : Int32 ) : Int;
+	public static  function add( a : Int32, b : Int32 ) : Int32;
+	public static  function sub( a : Int32, b : Int32 ) : Int32;
+	public static  function mul( a : Int32, b : Int32 ) : Int32;
+	public static  function div( a : Int32, b : Int32 ) : Int32;
+	public static  function mod( a : Int32, b : Int32 ) : Int32;
+	public static  function shl( a : Int32, b : Int ) : Int32;
+	public static  function shr( a : Int32, b : Int ) : Int32;
+	public static  function ushr( a : Int32, b : Int ) : Int32;
+	public static  function and( a : Int32, b : Int32 ) : Int32;
+	public static  function or( a : Int32, b : Int32 ) : Int32;
+	public static  function xor( a : Int32, b : Int32 ) : Int32;
+	public static  function neg( a : Int32 ) : Int32;
+	public static  function complement( a : Int32 ) : Int32;
+	public static  function compare( a : Int32, b : Int32 ) : Int;
+}
+

+ 56 - 22
std/cpp/CppXml__.hx

@@ -23,11 +23,18 @@
  * DAMAGE.
  */
 package cpp;
+
 import Xml;
 
-class CppXml__ extends Xml {
+class CppXml__ {
+	public static var Element(default,null) : XmlType;
+	public static var PCData(default,null) : XmlType;
+	public static var CData(default,null) : XmlType;
+	public static var Comment(default,null) : XmlType;
+	public static var DocType(default,null) : XmlType;
+	public static var Prolog(default,null) : XmlType;
+	public static var Document(default,null) : XmlType;
 
-	static var __name__ = ["Xml"];
 
 	private var _nodeName : String;
 	private var _nodeValue : String;
@@ -153,72 +160,90 @@ class CppXml__ extends Xml {
 		return r;
 	}
 
-	private override function getNodeName() : String {
+	/**
+		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.
+	**/
+	var nodeType(default,null) : XmlType;
+
+	/**
+		Returns the node name of an Element.
+	**/
+	var nodeName(getNodeName,setNodeName) : String;
+
+	/**
+		Returns the node value. Only works if the Xml node is not an Element or a Document.
+	**/
+	var nodeValue(getNodeValue,setNodeValue) : String;
+
+
+	private function getNodeName() : String {
 		if( nodeType != Xml.Element )
 			throw "bad nodeType";
 		return _nodeName;
 	}
 
-	private override function setNodeName( n : String ) : String {
+	private function setNodeName( n : String ) : String {
 		if( nodeType != Xml.Element )
 			throw "bad nodeType";
 		return _nodeName = n;
 	}
 
-	private override function getNodeValue() : String {
+	private function getNodeValue() : String {
 		if( nodeType == Xml.Element || nodeType == Xml.Document )
 			throw "bad nodeType";
 		return _nodeValue;
 	}
 
-	private override function setNodeValue( v : String ) : String {
+	private function setNodeValue( v : String ) : String {
 		if( nodeType == Xml.Element || nodeType == Xml.Document )
 			throw "bad nodeType";
 		return _nodeValue = v;
 	}
 
-	private override function getParent() : Xml {
+	private function getParent() : Xml {
 		return _parent;
 	}
 
-	public override function get( att : String ) : String {
+	public function get( att : String ) : String {
 		if( nodeType != Xml.Element )
 			throw "bad nodeType";
 		return Reflect.field( _attributes, att );
 	}
 
-	public override function set( att : String, value : String ) : Void {
+	public function set( att : String, value : String ) : Void {
 		if( nodeType != Xml.Element )
 			throw "bad nodeType";
 		Reflect.setField (_attributes, att, value );
 	}
 
-	public override function remove( att : String ) : Void{
+	public function remove( att : String ) : Void{
 		if( nodeType != Xml.Element )
 			throw "bad nodeType";
 		Reflect.deleteField( _attributes, att );
 	}
 
-	public override function exists( att : String ) : Bool {
+	public function exists( att : String ) : Bool {
 		if( nodeType != Xml.Element )
 			throw "bad nodeType";
 		return Reflect.hasField( _attributes, att );
 	}
 
-	public override function attributes() : Iterator<String> {
+	public function attributes() : Iterator<String> {
 		if( nodeType != Xml.Element )
 			throw "bad nodeType";
 		return Reflect.fields( _attributes ).iterator();
 	}
 
-	public override function iterator() : Iterator<Xml> {
+	public function iterator() : Iterator<Xml> {
 		if( _children == null )
 			throw "bad nodetype";
       return untyped _children.iterator();
 	}
 
 
-	public override function elements() {
+	public function elements(): Iterator<Xml> {
 		if( _children == null )
 			throw "bad nodetype";
       var children = _children;
@@ -251,7 +276,7 @@ class CppXml__ extends Xml {
 		}
 	}
 
-	public override function elementsNamed( name : String ) {
+	public function elementsNamed( name : String ) : Iterator<Xml> {
 		if( _children == null )
 			throw "bad nodetype";
       var children = _children;
@@ -285,13 +310,13 @@ class CppXml__ extends Xml {
 		}
 	}
 
-	public override function firstChild() : Xml {
+	public function firstChild() : Xml {
 		if( _children == null )
 			throw "bad nodetype";
 		return _children[0];
 	}
 
-	public override function firstElement() : Xml {
+	public function firstElement() : Xml {
 		if( _children == null )
 			throw "bad nodetype";
 		for( cur in 0..._children.length ) {
@@ -302,7 +327,7 @@ class CppXml__ extends Xml {
 		return null;
 	}
 
-   public override function addChild( x_ : Xml ) : Void {
+   public function addChild( x_ : Xml ) : Void {
       var x:CppXml__ = cast x_;
 		if( _children == null )
 			throw "bad nodetype";
@@ -311,7 +336,7 @@ class CppXml__ extends Xml {
 		_children.push( x );
 	}
 
-   override function removeChild( x_ : Xml ) : Bool {
+   function removeChild( x_ : Xml ) : Bool {
       var x:CppXml__ = cast x_;
 		if( _children == null )
 			throw "bad nodetype";
@@ -320,7 +345,7 @@ class CppXml__ extends Xml {
 		return b;
 	}
 
-	public override function insertChild( x_ : Xml, pos : Int ) : Void {
+	public function insertChild( x_ : Xml, pos : Int ) : Void {
       var x:CppXml__ = cast x_;
 		if( _children == null )
 			throw "bad nodetype";
@@ -329,7 +354,7 @@ class CppXml__ extends Xml {
 		_children.insert( pos, x );
 	}
 
-	public override function toString() {
+	public function toString() {
 		if( nodeType == Xml.PCData )
 			return _nodeValue;
 		if( nodeType == Xml.CData )
@@ -365,5 +390,14 @@ class CppXml__ extends Xml {
 		return s.toString();
 	}
 
-
+	static function __init__() : Void {
+		Element = "element";
+		PCData = "pcdata";
+		CData = "cdata";
+		Comment = "comment";
+		DocType = "doctype";
+		Prolog = "prolog";
+		Document = "document";
+	}
 }
+

+ 5 - 21
std/haxe/Int32.hx

@@ -24,7 +24,10 @@
  */
 package haxe;
 
-#if !cpp
+#if cpp
+typedef Int32 = cpp.CppInt32__;
+#else
+
 class Int32 {
 
 	public static inline function make( a : Int, b : Int ) : Int32 {
@@ -198,24 +201,5 @@ class Int32 {
 	#end
 
 }
-#else
-extern class Int32 {
-	public static  function make( a : Int, b : Int ) : Int32;
-	public static  function ofInt( x : Int ) : Int32;
-	public static  function toInt( x : Int32 ) : Int;
-	public static  function add( a : Int32, b : Int32 ) : Int32;
-	public static  function sub( a : Int32, b : Int32 ) : Int32;
-	public static  function mul( a : Int32, b : Int32 ) : Int32;
-	public static  function div( a : Int32, b : Int32 ) : Int32;
-	public static  function mod( a : Int32, b : Int32 ) : Int32;
-	public static  function shl( a : Int32, b : Int ) : Int32;
-	public static  function shr( a : Int32, b : Int ) : Int32;
-	public static  function ushr( a : Int32, b : Int ) : Int32;
-	public static  function and( a : Int32, b : Int32 ) : Int32;
-	public static  function or( a : Int32, b : Int32 ) : Int32;
-	public static  function xor( a : Int32, b : Int32 ) : Int32;
-	public static  function neg( a : Int32 ) : Int32;
-	public static  function complement( a : Int32 ) : Int32;
-	public static  function compare( a : Int32, b : Int32 ) : Int;
-}
 #end
+