6

各エントリがキーとして文字列を持ち、値として関数を持つLibgee HashMapを埋めようとしています。これは可能ですか?私はこの種のものが欲しい:

var keybindings = new Gee.HashMap<string, function> ();
keybindings.set ("<control>h", this.show_help ());
keybindings.set ("<control>q", this.explode ());

最終的に次のようなことができるようにします。

foreach (var entry in keybindings.entries) {
    uint key_code;
    Gdk.ModifierType accelerator_mods;
    Gtk.accelerator_parse((string) entry.key, out key_code, out accelerator_mods);      
   accel_group.connect(key_code, accelerator_mods, Gtk.AccelFlags.VISIBLE, entry.value);
}

しかし、おそらくこれは最善の方法ではありませんか?

4

2 に答える 2

5

デリゲートはあなたが探しているものです。しかし、前回チェックしたとき、ジェネリックはデリゲートをサポートしていなかったので、それほどエレガントではない方法はそれをラップすることです:

delegate void DelegateType();

private class DelegateWrapper {
    public DelegateType d;
    public DelegateWrapper(DelegateType d) {
        this.d = d;
    }
}

Gee.HashMap keybindings = new Gee.HashMap<string, DelegateWrapper> ();
keybindings.set ("<control>h", new DelegateWrapper(this.show_help));
keybindings.set ("<control>q", new DelegateWrapper(this.explode));

//then connect like you normally would do:
accel_group.connect(entry.value.d);
于 2011-05-26T23:24:22.583 に答える
2

[CCode (has_target = false)] を持つデリゲートでのみ可能です。それ以外の場合は、takoi が提案したようにラッパーを作成する必要があります。

于 2011-05-28T19:24:03.857 に答える