From b8d216a0575fbdc5a8eeeed07a1aeda8bd83ffea Mon Sep 17 00:00:00 2001 From: fiaxh Date: Sat, 14 Nov 2020 17:00:09 +0100 Subject: Add a WeakMap implementation + tests --- libdino/src/util.vala | 69 ------------------------- libdino/src/util/util.vala | 69 +++++++++++++++++++++++++ libdino/src/util/weak_map.vala | 115 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 69 deletions(-) delete mode 100644 libdino/src/util.vala create mode 100644 libdino/src/util/util.vala create mode 100644 libdino/src/util/weak_map.vala (limited to 'libdino/src') diff --git a/libdino/src/util.vala b/libdino/src/util.vala deleted file mode 100644 index c691a279..00000000 --- a/libdino/src/util.vala +++ /dev/null @@ -1,69 +0,0 @@ -namespace Dino { - -private extern const string SYSTEM_LIBDIR_NAME; -private extern const string SYSTEM_PLUGIN_DIR; - -public class SearchPathGenerator { - - public string? exec_path { get; private set; } - - public SearchPathGenerator(string? exec_path) { - this.exec_path = exec_path; - } - - public string get_locale_path(string gettext_package, string locale_install_dir) { - string? locale_dir = null; - if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { - string exec_locale = Path.build_filename(Path.get_dirname(exec_path), "locale"); - if (FileUtils.test(Path.build_filename(exec_locale, "en", "LC_MESSAGES", gettext_package + ".mo"), FileTest.IS_REGULAR)) { - locale_dir = exec_locale; - } - } - return locale_dir ?? locale_install_dir; - } - - public string[] get_plugin_paths() { - string[] search_paths = new string[0]; - if (Environment.get_variable("DINO_PLUGIN_DIR") != null) { - search_paths += Environment.get_variable("DINO_PLUGIN_DIR"); - } - search_paths += Path.build_filename(Environment.get_home_dir(), ".local", "lib", "dino", "plugins"); - string? exec_path = this.exec_path; - if (exec_path != null) { - if (!exec_path.contains(Path.DIR_SEPARATOR_S)) { - exec_path = Environment.find_program_in_path(this.exec_path); - } - if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { - search_paths += Path.build_filename(Path.get_dirname(exec_path), "plugins"); - } - if (Path.get_basename(Path.get_dirname(exec_path)) == "bin") { - search_paths += Path.build_filename(Path.get_dirname(Path.get_dirname(exec_path)), SYSTEM_LIBDIR_NAME, "dino", "plugins"); - } - } - search_paths += SYSTEM_PLUGIN_DIR; - return search_paths; - } -} - -public static string get_storage_dir() { - return Path.build_filename(Environment.get_user_data_dir(), "dino"); -} - -[CCode (cname = "dino_gettext", cheader_filename = "dino_i18n.h")] -public static extern unowned string _(string s); - -[CCode (cname = "dino_ngettext", cheader_filename = "dino_i18n.h")] -public static extern unowned string n(string msgid, string plural, ulong number); - -[CCode (cname = "bindtextdomain", cheader_filename = "libintl.h")] -private static extern unowned string? bindtextdomain(string domainname, string? dirname); - -[CCode (cname = "bind_textdomain_codeset", cheader_filename = "libintl.h")] -private static extern unowned string? bind_textdomain_codeset(string domainname, string? codeset); - -public static void internationalize(string gettext_package, string locales_dir) { - Intl.bind_textdomain_codeset(gettext_package, "UTF-8"); - Intl.bindtextdomain(gettext_package, locales_dir); -} - -} diff --git a/libdino/src/util/util.vala b/libdino/src/util/util.vala new file mode 100644 index 00000000..c691a279 --- /dev/null +++ b/libdino/src/util/util.vala @@ -0,0 +1,69 @@ +namespace Dino { + +private extern const string SYSTEM_LIBDIR_NAME; +private extern const string SYSTEM_PLUGIN_DIR; + +public class SearchPathGenerator { + + public string? exec_path { get; private set; } + + public SearchPathGenerator(string? exec_path) { + this.exec_path = exec_path; + } + + public string get_locale_path(string gettext_package, string locale_install_dir) { + string? locale_dir = null; + if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { + string exec_locale = Path.build_filename(Path.get_dirname(exec_path), "locale"); + if (FileUtils.test(Path.build_filename(exec_locale, "en", "LC_MESSAGES", gettext_package + ".mo"), FileTest.IS_REGULAR)) { + locale_dir = exec_locale; + } + } + return locale_dir ?? locale_install_dir; + } + + public string[] get_plugin_paths() { + string[] search_paths = new string[0]; + if (Environment.get_variable("DINO_PLUGIN_DIR") != null) { + search_paths += Environment.get_variable("DINO_PLUGIN_DIR"); + } + search_paths += Path.build_filename(Environment.get_home_dir(), ".local", "lib", "dino", "plugins"); + string? exec_path = this.exec_path; + if (exec_path != null) { + if (!exec_path.contains(Path.DIR_SEPARATOR_S)) { + exec_path = Environment.find_program_in_path(this.exec_path); + } + if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { + search_paths += Path.build_filename(Path.get_dirname(exec_path), "plugins"); + } + if (Path.get_basename(Path.get_dirname(exec_path)) == "bin") { + search_paths += Path.build_filename(Path.get_dirname(Path.get_dirname(exec_path)), SYSTEM_LIBDIR_NAME, "dino", "plugins"); + } + } + search_paths += SYSTEM_PLUGIN_DIR; + return search_paths; + } +} + +public static string get_storage_dir() { + return Path.build_filename(Environment.get_user_data_dir(), "dino"); +} + +[CCode (cname = "dino_gettext", cheader_filename = "dino_i18n.h")] +public static extern unowned string _(string s); + +[CCode (cname = "dino_ngettext", cheader_filename = "dino_i18n.h")] +public static extern unowned string n(string msgid, string plural, ulong number); + +[CCode (cname = "bindtextdomain", cheader_filename = "libintl.h")] +private static extern unowned string? bindtextdomain(string domainname, string? dirname); + +[CCode (cname = "bind_textdomain_codeset", cheader_filename = "libintl.h")] +private static extern unowned string? bind_textdomain_codeset(string domainname, string? codeset); + +public static void internationalize(string gettext_package, string locales_dir) { + Intl.bind_textdomain_codeset(gettext_package, "UTF-8"); + Intl.bindtextdomain(gettext_package, locales_dir); +} + +} diff --git a/libdino/src/util/weak_map.vala b/libdino/src/util/weak_map.vala new file mode 100644 index 00000000..5a89be11 --- /dev/null +++ b/libdino/src/util/weak_map.vala @@ -0,0 +1,115 @@ +using Gee; + +public class WeakMap : Gee.AbstractMap { + + private HashMap hash_map; + private HashMap notify_map; + + 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); + } + + public override void clear() { + foreach (K key in notify_map.keys) { + Object o = (Object) hash_map[key]; + o.weak_unref(notify_map[key].func); + } + hash_map.clear(); + notify_map.clear(); + } + + public override V @get(K key) { + if (!hash_map.has_key(key)) return null; + + var v = hash_map[key]; + + return (owned) v; + } + + public override bool has(K key, V value) { + assert_not_reached(); + } + + public override bool has_key(K key) { + return hash_map.has_key(key); + } + + public override Gee.MapIterator map_iterator() { + assert_not_reached(); + } + + public override void @set(K key, V value) { + assert(value != null); + + unset(key); + + Object v_obj = (Object) value; + var notify_wrap = new WeakNotifyWrapper((obj) => { + hash_map.unset(key); + notify_map.unset(key); + }); + notify_map[key] = notify_wrap; + v_obj.weak_ref(notify_wrap.func); + + hash_map[key] = value; + } + + public override bool unset(K key, out V value = null) { + if (!hash_map.has_key(key)) 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); + } + 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; } + } + + public override bool read_only { get { assert_not_reached(); } } + + 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(); + } + } + + public override void dispose() { + foreach (K key in notify_map.keys) { + Object o = (Object) hash_map[key]; + o.weak_unref(notify_map[key].func); + } + } +} + +internal class WeakNotifyWrapper { + public WeakNotify func; + + public WeakNotifyWrapper(owned WeakNotify func) { + this.func = (owned) func; + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2