Bläddra i källkod

[cpp] Add optimized versions of haxe.ds.Map.get for cpp

Hugh 8 år sedan
förälder
incheckning
2e8088ad4e

+ 30 - 0
src/generators/gencpp.ml

@@ -2236,6 +2236,17 @@ let is_array_splice_call obj member =
    | _,_ -> false
 ;;
 
+let is_map_get_call obj member =
+   member.cf_name="get" &&
+   (match obj.cpptype  with
+   | TCppInst({cl_path=(["haxe";"ds"],"IntMap")}) -> true
+   | TCppInst({cl_path=(["haxe";"ds"],"StringMap")}) -> true
+   | TCppInst({cl_path=(["haxe";"ds"],"ObjectMap")}) -> true
+   | _ -> false
+   )
+;;
+
+
 
 let is_array_concat_call obj member =
    match obj.cpptype, member.cf_name with
@@ -2678,6 +2689,25 @@ let retype_expression ctx request_type function_args function_type expression_tr
                   | _ -> abort "First parameter of template function must be a Class" retypedFunc.cpppos
                   )
 
+               | CppFunction( FuncInstance(obj, InstPtr, member), _ ) when not forCppia && is_map_get_call obj member ->
+                  let retypedArgs = List.map (retype TCppDynamic ) args in
+                  let fname, cppType = match return_type with
+                  | TCppVoid | TCppScalar("bool")  -> "get_bool", return_type
+                  | TCppScalar("int")  -> "get_int", return_type
+                  | TCppScalar("Float")  -> "get_float", return_type
+                  | TCppString  -> "get_string", return_type
+                  | _ -> "get", TCppDynamic
+                  in
+                  let func = FuncInstance(obj, InstPtr, {member with cf_name=fname}) in
+                  (*
+                  if  cpp_can_static_cast cppType return_type then begin
+                     let call = mk_cppexpr (CppCall(func,retypedArgs)) cppType in
+                     CppCastStatic(call, cppType), cppType
+                  end else
+                  *)
+                     CppCall( func, retypedArgs), cppType
+
+
                | CppFunction( FuncInstance(obj,InstPtr,member) as func, returnType ) when cpp_can_static_cast returnType cppType ->
                   let retypedArgs = List.map (retype TCppDynamic ) args in
                   let call = mk_cppexpr (CppCall(func,retypedArgs)) returnType in

+ 5 - 0
std/cpp/_std/haxe/ds/IntMap.hx

@@ -44,6 +44,11 @@ package haxe.ds;
 
   template<typename VALUE>
   inline void set(Dynamic &key, const VALUE &value) { set( (int)key, value ); }
+
+  inline bool get_bool(int key) { return __int_hash_get_bool(h,key); }
+  inline int get_int(int key) { return __int_hash_get_int(h,key); }
+  inline Float get_float(int key) { return __int_hash_get_float(h,key); }
+  inline String get_string(int key) { return __int_hash_get_string(h,key); }
 ")
 @:coreApi class IntMap<T> implements haxe.Constraints.IMap<Int,T> {
 

+ 5 - 0
std/cpp/_std/haxe/ds/ObjectMap.hx

@@ -43,6 +43,11 @@ package haxe.ds;
   template<typename V>
   inline void set(Dynamic key, const ::cpp::Pointer<V> &value) {__object_hash_set(h,key,(Dynamic)value ); }
 
+  inline bool get_bool(Dynamic key) { return __object_hash_get_bool(h,key); }
+  inline int get_int(Dynamic key) { return __object_hash_get_int(h,key); }
+  inline Float get_float(Dynamic key) { return __object_hash_get_float(h,key); }
+  inline String get_string(Dynamic key) { return __object_hash_get_string(h,key); }
+
 ")
 @:coreApi
 class ObjectMap<K:{},V> implements haxe.Constraints.IMap<K,V> {

+ 5 - 0
std/cpp/_std/haxe/ds/StringMap.hx

@@ -44,6 +44,11 @@ package haxe.ds;
 
   template<typename VALUE>
   inline void set(Dynamic &key, const VALUE &value) { set( (String)key, value ); }
+
+  inline bool get_bool(String key) { return __string_hash_get_bool(h,key); }
+  inline int get_int(String key) { return __string_hash_get_int(h,key); }
+  inline Float get_float(String key) { return __string_hash_get_float(h,key); }
+  inline String get_string(String key) { return __string_hash_get_string(h,key); }
 ")
 @:coreApi class StringMap<T> implements haxe.Constraints.IMap<String,T> {
 	@:ifFeature("haxe.ds.StringMap.*")