Gee HashMap 包含方法作为值

发布于 2024-11-09 20:00:01 字数 622 浏览 2 评论 0原文

我正在尝试填充 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);
}

但这也许不是最好的方法?

I'm trying to fill a Libgee HashMap where each entry has a string as key, and a function as value. Is this possible? I want this sort of thing:

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

so that I can eventually do something like this:

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);
}

But perhaps this isn't the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

英雄似剑 2024-11-16 20:00:01

代表就是您正在寻找的人。但上次我检查时,泛型不支持委托,所以一个不太优雅的方法是包装它:

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);

Delegates are what you're looking for. But last time I checked, generics didn't support delegates, so a not-so-elegant way is to wrap it:

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);
潦草背影 2024-11-16 20:00:01

仅适用于具有 [CCode (has_target = false)] 的委托,否则您必须按照 takoi 建议创建一个包装器。

It is possible only for delegates with [CCode (has_target = false)], otherwise you have to create a wrapper as takoi suggested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文