Procházet zdrojové kódy

[cpp] Static-cast when accessing Enum Params

Hugh před 9 roky
rodič
revize
905ac6f8e5
1 změnil soubory, kde provedl 22 přidání a 4 odebrání
  1. 22 4
      src/generators/gencpp.ml

+ 22 - 4
src/generators/gencpp.ml

@@ -1909,6 +1909,15 @@ let cpp_variant_type_of t = match t with
    | TCppVariant -> TCppVariant
 ;;
 
+let cpp_cast_variant_type_of t = match t with
+   | TCppObjectArray _
+   | TCppScalarArray _
+   | TCppDynamicArray
+   | TCppClass
+   | TCppEnum _ 
+   | TCppInst _ -> t
+   | _ -> cpp_variant_type_of t;
+;;
 
 let cpp_base_type_of t =
    match cpp_variant_type_of t with
@@ -2083,8 +2092,7 @@ let retype_expression ctx request_type function_args expression_tree forInjectio
          match expr.eexpr with
          | TEnumParameter( enumObj, enumField, enumIndex  ) ->
             let retypedObj = retype TCppDynamic enumObj in
-            (* hxcpp actually stores enum parametes in Array<Dynamic> *)
-            CppEnumParameter( retypedObj, enumField, enumIndex ), cpp_variant_type_of (cpp_type_of (get_nth_type enumField enumIndex))
+            CppEnumParameter( retypedObj, enumField, enumIndex ), cpp_cast_variant_type_of (cpp_type_of (get_nth_type enumField enumIndex))
 
          | TConst TThis ->
             uses_this := Some !this_real;
@@ -3121,11 +3129,21 @@ let gen_cpp_ast_expression_tree ctx class_name func_name function_args injection
          out ((string_of_path enum.e_path) ^ "::" ^ (cpp_enum_name_of field) ^ "_dyn()" );
 
       | CppEnumParameter(obj,field,index) ->
-         let baseType = cpp_base_type_of (cpp_type_of ctx (get_nth_type field index) ) in
+         let valueType = cpp_type_of ctx (get_nth_type field index) in
+         let baseType = cpp_base_type_of valueType in
          gen obj;
          if cpp_is_dynamic_type obj.cpptype then
             out ".StaticCast< ::hx::EnumBase >()";
-         out ( "->_hx_get" ^ baseType ^ "(" ^ (string_of_int index) ^ ")")
+         out ( "->_hx_get" ^ baseType ^ "(" ^ (string_of_int index) ^ ")");
+         (match valueType with
+         | TCppObjectArray _
+         | TCppScalarArray _
+         | TCppDynamicArray
+         | TCppClass
+         | TCppEnum _
+         | TCppInst _ -> out (".StaticCast< " ^ (tcpp_to_string valueType ) ^ ">()")
+         | _ ->() 
+         )
 
       | CppIntSwitch(condition, cases, defVal) ->
          out "switch((int)("; gen condition; out "))";