Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gee HashMap containing methods as values

Tags:

vala

genie

libgee

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?

like image 958
Riazm Avatar asked May 26 '11 22:05

Riazm


People also ask

What is the use of one object in a hashmap?

One object is used as a key (index) to another object (value). It can store different types: String keys and Integer values, or the same type, like: String keys and String values: Create a HashMap object called capitalCities that will store String keys and String values: The HashMap class has many useful methods.

What are keys and values in a hashmap?

Keys and values in a HashMap are actually objects. In the examples above, we used objects of type "String". Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer.

What is the difference between values() and HashMap entryset() methods?

Here, the values () method returns a view of all values. The variable value access each value from the view. Note: The Value of HashMap is of Integer type. Hence, we have used the int variable to access the values. HashMap entrySet () - returns set view of all entries (mappings)

How to iterate through each value of HashMap in Java?

The values () method can also be used with the for-each loop to iterate through each value of the hashmap. In the above example, we have created a hashmap named numbers. Notice the line, Here, the values () method returns a view of all values. The variable value access each value from the view. Note: The Value of HashMap is of Integer type.


2 Answers

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);
like image 136
gwohpq9 Avatar answered Nov 15 '22 11:11

gwohpq9


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

like image 37
lethalman Avatar answered Nov 15 '22 10:11

lethalman