Forráskód Böngészése

[cpp] Add some more objc helper classes

hughsando 9 éve
szülő
commit
b561068c04

+ 20 - 12
src/generators/gencpp.ml

@@ -4985,21 +4985,29 @@ let generate_protocol_delegate ctx class_def output =
       |  TFun(args,ret) ->
          let retStr = ctx_type_string ctx ret in
          let nativeName = get_meta_string field.cf_meta Meta.ObjcProtocol in
-         let names = if nativeName<>"" then
-            ExtString.String.nsplit nativeName ":"
-         else
-            List.map (fun (n,_,_) -> n ) args
+         let fieldName,argNames = if nativeName<>"" then begin
+            let parts = ExtString.String.nsplit nativeName ":" in
+            List.hd parts, List.tl parts
+         end else
+            field.cf_name, List.map (fun (n,_,_) -> n ) args
          in
-         output ("- (" ^ retStr ^ ") " ^ (if nativeName="" then field.cf_name else (List.hd names) ) );
+         output ("- (" ^ retStr ^ ") " ^ fieldName );
 
          let first = ref true in
-         List.iter2 (fun (name,_,argType) signature_name ->
-             if !first then
-               output (" :(" ^ (ctx_type_string ctx argType) ^ ")" ^ name )
-             else
-               output (" " ^ signature_name ^ ":(" ^ (ctx_type_string ctx argType) ^ ")" ^ name );
-             first := false;
-             ) args names;
+         (try
+            List.iter2 (fun (name,_,argType) signature_name ->
+                if !first then
+                  output (" :(" ^ (ctx_type_string ctx argType) ^ ")" ^ name )
+                else
+                  output (" " ^ signature_name ^ ":(" ^ (ctx_type_string ctx argType) ^ ")" ^ name );
+                first := false;
+                ) args argNames;
+         with Invalid_argument _ -> begin
+           abort (
+              let argString  = String.concat "," (List.map (fun (name,_,_) -> name) args) in
+             "Invalid arg count in delegate in " ^ field.cf_name ^ " '" ^ field.cf_name ^ "," ^
+             (argString) ^ "' != '" ^ (String.concat "," argNames) ^ "'" ) field.cf_pos
+         end);
          output (" {\n");
          output ("\thx::NativeAttach _hx_attach;\n");
          output ( (if retStr="void" then "\t" else "\treturn ") ^ full_class_name ^ "::" ^ (keyword_remap field.cf_name) ^ "(haxeObj");

+ 11 - 6
std/cpp/objc/NSData.hx

@@ -26,21 +26,26 @@ package cpp.objc;
 @:objc
 extern abstract NSData( NSDataData )
 {
-   @:native("_hx_bytes_to_nsdata") @:extern static function BytesToNSData(b:haxe.io.BytesData) : NSData return null;
-   @:native("_hx_bytes_to_nsdata") @:extern static function BytesToNSDataData(b:haxe.io.BytesData) : NSDataData return null;
-   @:native("_hx_nsdata_to_bytes") @:extern static function NSDataDataToBytes(d:NSDataData) : haxe.io.BytesData return null;
+   @:native("_hx_value_to_objc") @:extern static function to_data(b:haxe.io.BytesData) : NSData return null;
+   @:native("_hx_value_to_objc") @:extern static function to_data_data(b:haxe.io.BytesData) : NSDataData return null;
+   @:native("_hx_objc_to_bytes") @:extern static function NSDataDataToBytes(d:NSDataData) : haxe.io.BytesData return null;
 
 
    inline function new(d:NSDataData) this = d;
 
    @:from @:extern
-   static public inline function fromBytesData(d:haxe.io.BytesData):NSData return new NSData( BytesToNSDataData(d) );
+   static public inline function fromBytesData(d:haxe.io.BytesData):NSData return new NSData( to_data_data(d) );
 
    @:from @:extern
-   static public inline function fromBytes(d:haxe.io.Bytes):NSData return new NSData( BytesToNSDataData(d.getData()) );
+   static public inline function fromBytes(d:haxe.io.Bytes):NSData return new NSData( to_data_data(d.getData()) );
 
    @:to @:extern
-   public inline function toBytes():haxe.io.BytesData return NSDataDataToBytes(this);
+   public inline function toBytesData():haxe.io.BytesData return NSDataDataToBytes(this);
+
+   @:to @:extern
+   public inline function toBytes():haxe.io.Bytes return haxe.io.Bytes.ofData(NSDataDataToBytes(this));
+
+   @:to @:extern public inline function toNSObject():NSObject return cast this;
 
 }
 

+ 9 - 7
std/cpp/objc/StringIdMap.hx → std/cpp/objc/NSDictionary.hx

@@ -21,22 +21,24 @@
  */
 package cpp.objc;
 
-@:native("NSDictionary<NSString *, id>") @:objc extern class StringIdMapData { }
+@:native("NSDictionary<NSString *, id>") @:objc extern class DictionaryData { }
 
 @:objc
-extern abstract StringIdMap( StringIdMapData )
+extern abstract NSDictionary( DictionaryData )
 {
-   @:native("_hx_obj_to_nsdictionary") @:extern static function ObjectToStringIdMapData(obj:{}) : StringIdMapData return null;
-   @:native("_hx_nsdictionary_to_obj") @:extern static function StringIdMapDataToObject(d:StringIdMapData) : {} return null;
+   @:native("_hx_obj_to_nsdictionary") @:extern static function _hx_obj_to_nsdictionary(obj:Dynamic) : DictionaryData return null;
+   @:native("_hx_nsdictionary_to_obj") @:extern static function _hx_nsdictionary_to_obj(d:DictionaryData) : Dynamic return null;
 
 
-   inline function new(dict:StringIdMapData) this = dict;
+   inline function new(dict:DictionaryData) this = dict;
 
    @:from @:extern
-   static public inline function fromObject(o:{}):StringIdMap return new StringIdMap( ObjectToStringIdMapData(o) );
+   static public inline function fromDynamic(o:Dynamic):NSDictionary return new NSDictionary( _hx_obj_to_nsdictionary(o) );
 
    @:to @:extern
-   public inline function toObject():{} return StringIdMapDataToObject(this);
+   public inline function toDynamic():Dynamic return _hx_nsdictionary_to_obj(this);
+
+   @:to @:extern public inline function toNSObject():NSObject return cast this;
 
 }
 

+ 33 - 0
std/cpp/objc/NSLog.hx

@@ -0,0 +1,33 @@
+/*
+ * Copyright (C)2005-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package cpp.objc;
+
+extern class NSLog
+{
+   @:native("NSLog")
+   @:overload(function(format:NSString,a0:NSObject):Void { })
+   @:overload(function(format:NSString,a0:NSObject,a1:NSObject):Void { })
+   public static function log(format:NSString):Void;
+
+}
+
+

+ 44 - 0
std/cpp/objc/NSObject.hx

@@ -0,0 +1,44 @@
+/*
+ * Copyright (C)2005-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package cpp.objc;
+
+@:native("id")  @:objc extern class NSObjectData { }
+
+@:objc
+extern abstract NSObject( NSObjectData )
+{
+   @:native("_hx_value_to_objc") @:extern static function _hx_value_to_objc(obj:Dynamic) : NSObject return null;
+   @:native("_hx_objc_to_dynamic") @:extern static function _hx_objc_to_dynamic(d:NSObjectData) : Dynamic return null;
+
+
+   inline function new(d:NSObjectData) this = d;
+
+
+   @:from @:extern
+   static public inline function fromHaxe(d:Dynamic):NSObject return _hx_value_to_objc(d);
+
+
+   @:to @:extern
+   public inline function toHaxe():Dynamic return _hx_objc_to_dynamic(this);
+}
+
+

+ 8 - 8
std/cpp/objc/NSString.hx

@@ -21,25 +21,25 @@
  */
 package cpp.objc;
 
-@:native("NSString") @:objc extern class NSStringData { }
+@:native("NSString")  @:objc extern class NSStringData { }
 
 @:objc
 extern abstract NSString( NSStringData )
 {
-   @:native(" (NSString *)") @:extern static function StringToNSString(s:String) : NSString return null;
-   @:native(" (NSString *)") @:extern static function StringToNSStringData(s:String) : NSStringData return null;
-   @:native("String") @:extern static function NSStringDataToString(s:NSStringData) : String return null;
-
-
    inline function new(s:NSStringData) this = s;
+   @:native("(id)") @:extern static function toObject(d:NSStringData) : NSObject return null;
+
+   @:native("(NSString *)") @:extern static function castFromString(s:String) : NSString return null;
+   @:native("String") @:extern static function castToString(s:NSStringData) : String return null;
 
 
    @:from @:extern
-   static public inline function fromString(s:String):NSString return new NSString( StringToNSStringData(s) );
+   static public inline function fromString(s:String):NSString return castFromString(s);
 
 
    @:to @:extern
-   public inline function toString():String return NSStringDataToString(this);
+   public inline function toString():String return castToString(this);
 
+   @:to @:extern public inline function toNSObject():NSObject return toObject(this);
 }