user15150266
user15150266

Reputation: 65

How can I g_signal_connect() by ID rather than string name?

The typical way to handle a button press in GTK is:

g_signal_connect(GTK_BUTTON(myButton), "pressed", G_CALLBACK(myButtonHandler), NULL);

However, I find it bad, slow, and unnecessary to use strings (such as "pressed") for internal identification. If I can find the numerical signal ID that corrosponds to this, I can skip the parsing step. But how do I connect an event by ID rather than string name? I did a lot of digging and found this, and I also learned that g_signal_connect is a macro that expands to g_signal_connect_data, but none of these quite solve my problem.

Is this possible, if so, how do I do this?

Upvotes: 1

Views: 384

Answers (1)

ptomato
ptomato

Reputation: 57920

You can use g_signal_connect_closure_by_id() but then you have to create a GClosure structure to hold your callback and callback data.

I would really recommend against this, as it will add boilerplate to your code for little benefit. You generally only connect signals once. If you are connecting a signal in a tight loop, then you are probably doing something wrong, or you have a very unusual use case. Anyway, signal names are actually interned, which means that you are not even incurring the cost of string comparisons; only the cost of splitting the string at : if the signal has a detail annotation. Don't bother with optimizing this unless it is actually showing up as a bottleneck on your profiler graphs.

Upvotes: 3

Related Questions