diff --git a/libdino/src/util/weak_map.vala b/libdino/src/util/weak_map.vala index 5a89be11..0fd9d55d 100644 --- a/libdino/src/util/weak_map.vala +++ b/libdino/src/util/weak_map.vala @@ -5,13 +5,30 @@ public class WeakMap : Gee.AbstractMap { private HashMap hash_map; private HashMap notify_map; + public HashDataFunc? key_hash_func = null; + public EqualDataFunc? key_equal_func = null; + public EqualDataFunc? value_equal_func = null; + public WeakMap(owned HashDataFunc? key_hash_func = null, owned EqualDataFunc? key_equal_func = null, owned EqualDataFunc? value_equal_func = null) { if (!typeof(V).is_object()) { error("WeakMap only takes values that are Objects"); } - hash_map = new HashMap(key_hash_func, key_equal_func, value_equal_func); - notify_map = new HashMap(key_hash_func, key_equal_func, value_equal_func); + this.key_hash_func = (owned)key_hash_func; + this.key_equal_func = (owned)key_equal_func; + this.value_equal_func = (owned)value_equal_func; + + if (this.key_equal_func == null || this.key_equal_func == null || this.value_equal_func == null) { + hash_map = new HashMap(); + notify_map = new HashMap(); + } else { + hash_map = new HashMap((v) => { return this.key_hash_func(v); }, + (a, b) => { return this.key_equal_func(a, b); }, + (a, b) => { return this.value_equal_func(a, b); }); + notify_map = new HashMap((v) => { return this.key_hash_func(v); }, + (a, b) => { return this.key_equal_func(a, b); }, + (a, b) => { return this.value_equal_func(a, b); }); + } } public override void clear() { @@ -60,25 +77,18 @@ public class WeakMap : Gee.AbstractMap { } public override bool unset(K key, out V value = null) { - if (!hash_map.has_key(key)) return false; + if (!hash_map.has_key(key)) { + value = null; + return false; + } Object v_obj = (Object) hash_map[key]; v_obj.weak_unref(notify_map[key].func); notify_map.unset(key); - return hash_map.unset(key); + return hash_map.unset(key, out value); } public override Gee.Set> entries { owned get; } - [CCode (notify = false)] - public Gee.EqualDataFunc key_equal_func { - get { return hash_map.key_equal_func; } - } - - [CCode (notify = false)] - public Gee.HashDataFunc key_hash_func { - get { return hash_map.key_hash_func; } - } - public override Gee.Set keys { owned get { return hash_map.keys; } } @@ -87,11 +97,6 @@ public class WeakMap : Gee.AbstractMap { public override int size { get { return hash_map.size; } } - [CCode (notify = false)] - public Gee.EqualDataFunc value_equal_func { - get { return hash_map.value_equal_func; } - } - public override Gee.Collection values { owned get { assert_not_reached();