Browse Source

[cpp] Add special cases for cpp.Functions as Map values

Hugh 10 years ago
parent
commit
84850fd6ab

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

@@ -37,6 +37,8 @@ package haxe.ds;
 
   template<typename V, typename H>
   inline void set(int key, const ::cpp::Struct<V,H> &value) {__int_hash_set(h,key,value); }
+  template<typename F>
+  inline void set(int key, const ::cpp::Function<F> &value) {__int_hash_set(h,key,value); }
   template<typename V>
   inline void set(int key, const ::cpp::Pointer<V> &value) {__int_hash_set(h,key,(Dynamic)value ); }
 

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

@@ -39,6 +39,8 @@ package haxe.ds;
   template<typename V, typename H>
   inline void set(Dynamic key, const ::cpp::Struct<V,H> &value) {__object_hash_set(h,key,value); }
   template<typename V>
+  inline void set(Dynamic key, const ::cpp::Function<V> &value) {__object_hash_set(h,key,(Dynamic)value ); }
+  template<typename V>
   inline void set(Dynamic key, const ::cpp::Pointer<V> &value) {__object_hash_set(h,key,(Dynamic)value ); }
 
 ")

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

@@ -38,6 +38,8 @@ package haxe.ds;
   template<typename V, typename H>
   inline void set(String key, const ::cpp::Struct<V,H> &value) {__string_hash_set(h,key,value); }
   template<typename V>
+  inline void set(String key, const ::cpp::Function<V> &value) {__string_hash_set(h,key,(Dynamic)value ); }
+  template<typename V>
   inline void set(String key, const ::cpp::Pointer<V> &value) {__string_hash_set(h,key,(Dynamic)value ); }
 
   template<typename VALUE>

+ 2 - 0
std/cpp/_std/haxe/ds/WeakMap.hx

@@ -39,6 +39,8 @@ package haxe.ds;
   inline void set(Dynamic key, const ::cpp::Struct<V,H> &value) {__object_hash_set(h,key,value,true); }
   template<typename V>
   inline void set(Dynamic key, const ::cpp::Pointer<V> &value) {__object_hash_set(h,key,(Dynamic)value,true ); }
+  template<typename V>
+  inline void set(Dynamic key, const ::cpp::Function<V> &value) {__object_hash_set(h,key,(Dynamic)value,true ); }
 ")
 @:coreApi
 class WeakMap<K:{},V> implements haxe.Constraints.IMap<K,V> {

+ 29 - 0
tests/unit/src/unit/issues/Issue4130.hx

@@ -65,8 +65,37 @@ class Issue4130 extends Test
         var weak2Ptr = new haxe.ds.WeakMap<TestKey, cpp.Pointer<Int> >();
         weak2Ptr.set( key, valPtr );
         eq( weak2Ptr.get(key), valPtr );
+
+        // Functions
+        var valFunc = cpp.Callable.fromStaticFunction( someFunc );
+
+        var int2Func = new haxe.ds.IntMap< cpp.Callable<Int->Int> >();
+        int2Func.set( 42, valFunc );
+        var key:Dynamic = 42;
+        int2Func.set( key, valFunc );
+        eq( int2Func.get(42), valFunc);
+        eq( int2Func.get(key), valFunc);
+
+        var string2Func = new haxe.ds.StringMap< cpp.Callable<Int->Int> >();
+        string2Func.set( "42", valFunc );
+        var key:Dynamic = "42";
+        string2Func.set( key, valFunc );
+        eq( string2Func.get("42"), valFunc );
+        eq( string2Func.get(key), valFunc );
+
+        var key = new TestKey();
+        var obj2Func = new haxe.ds.ObjectMap<TestKey, cpp.Callable<Int->Int> >();
+        obj2Func.set( key, valFunc );
+        eq( obj2Func.get(key), valFunc );
+
+        var weak2Func = new haxe.ds.WeakMap<TestKey, cpp.Callable<Int->Int> >();
+        weak2Func.set( key, valFunc );
+        eq( weak2Func.get(key), valFunc );
+
         #end
 	}
+
+   static function someFunc(a:Int) : Int return a;
    #end
 }