contrib/objcwrapper: replace `type_convector` by a singleton and `to_nit_type`
authorAlexis Laferrière <alexis.laf@xymus.net>
Wed, 26 Aug 2015 12:45:11 +0000 (08:45 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 27 Aug 2015 18:34:45 +0000 (14:34 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/objcwrapper/src/objc_generator.nit

index 25bd84f..380f61e 100644 (file)
@@ -20,8 +20,29 @@ import opts
 import objc_model
 
 redef class Sys
+
        # Path to the output file
        var opt_output = new OptionString("Output file", "-o")
+
+       private var nit_to_java_types: Map[String, String] is lazy do
+               var types = new HashMap[String, String]
+               types["char"] = "Byte"
+               types["short"] = "Int"
+               types["short int"] = "Int"
+               types["int"] = "Int"
+               types["long"] = "Int"
+               types["long int"] = "Int"
+               types["long long"] = "Int"
+               types["long long int"] = "Int"
+               types["float"] = "Float"
+               types["double"] = "Float"
+               types["long double"] = "Float"
+
+               types["NSUInteger"] = "Int"
+               types["BOOL"] = "Bool"
+               types["id"] = "NSObject"
+               return types
+       end
 end
 
 class CodeGenerator
@@ -65,32 +86,6 @@ class CodeGenerator
                if path != null then file.close
        end
 
-       private fun type_convertor(type_word: String): String
-       do
-               var types = new HashMap[String, String]
-               types["char"] = "Byte"
-               types["short"] = "Int"
-               types["short int"] = "Int"
-               types["int"] = "Int"
-               types["long"] = "Int"
-               types["long int"] = "Int"
-               types["long long"] = "Int"
-               types["long long int"] = "Int"
-               types["float"] = "Float"
-               types["double"] = "Float"
-               types["long double"] = "Float"
-
-               types["NSUInteger"] = "Int"
-               types["BOOL"] = "Bool"
-               types["id"] = "NSObject"
-
-               if types.has_key(type_word) then
-                       return types[type_word]
-               else
-                       return type_word
-               end
-       end
-
        private fun write_class(classe: ObjcClass, file: Writer)
        do
                var commented_methods = new Array[ObjcMethod]
@@ -158,7 +153,7 @@ class CodeGenerator
 
        private fun write_attribute_getter(attribute: ObjcAttribute, file: Writer)
        do
-               file.write """  fun """ + attribute.name.to_snake_case + ": " + type_convertor(attribute.return_type)
+               file.write """  fun """ + attribute.name.to_snake_case + ": " + attribute.return_type.to_nit_type
                file.write """ in "ObjC" `{\n"""
                file.write """          return [self """ + attribute.name + "];\n"
                file.write """  `}\n"""
@@ -166,7 +161,7 @@ class CodeGenerator
 
        private fun write_attribute_setter(attribute: ObjcAttribute, file: Writer)
        do
-               file.write """  fun """ + attribute.name.to_snake_case + "=(value: " + type_convertor(attribute.return_type) + ")"
+               file.write """  fun """ + attribute.name.to_snake_case + "=(value: " + attribute.return_type.to_nit_type + ")"
                file.write " in \"ObjC\" `\{\n"
                file.write """          self.""" + attribute.name + " = value;\n"
                file.write """  `}\n"""
@@ -188,17 +183,17 @@ class CodeGenerator
                file.write name
                for param in method.params do
                        if param == method.params.first and not param.is_single then
-                               file.write "(" + param.variable_name + ": " + type_convertor(param.return_type)
+                               file.write "(" + param.variable_name + ": " + param.return_type.to_nit_type
                        end
                        if param != method.params.first and not param.is_single then
-                               file.write ", " + param.variable_name + ": " + type_convertor(param.return_type)
+                               file.write ", " + param.variable_name + ": " + param.return_type.to_nit_type
                        end
                        if param == method.params.last and not param.is_single then
                                file.write ")"
                        end
                end
                if method.return_type != "void" and not method.params.first.name.has("init") then
-                       file.write ": " + type_convertor(method.return_type)
+                       file.write ": " + method.return_type.to_nit_type
                end
        end
 
@@ -231,3 +226,17 @@ class CodeGenerator
                file.write "];\n"
        end
 end
+
+redef class Text
+       # Nit equivalent to this type
+       private fun to_nit_type: String
+       do
+               var types = sys.nit_to_java_types
+
+               if types.has_key(self) then
+                       return types[self]
+               else
+                       return to_s
+               end
+       end
+end