Keyword: GValue, Boxed Type
Background
I wanted to store arbitrary values and types in a GHashTable. For that I used GValue and later GValue with a Boxed Type. I had a very hard time figuring out how everything works and I’d like to improve it. Therefore I opened this discussion.
Why don’t I file merge request?
There’s a high chance, I misunderstand things or don’t see the overall picture, so MRs might be a waste of time for both sides. Instead I’d like to try and get together the view and questions of a novice (me) and the answers and knowledge of a pro to come up with an improved content.
After the ok on a specific subject below, I could create an MR. (Althouth, someone with better writing skills is very welcome to make changes instead
)
Points of Discussion
Minor: There’s no “GBoxed” (devhelp) site (/entry)
Problem:
Searching in Devhelp for “GValue” brings up information on it. However, there’s no page page when searching for “GBoxed”. Instead one has to search for “Boxed Types”.
Suggestion:
Create an entry for GBoxed, that at least links to the “Boxed Types” page - and possibly explains, why GBoxed does not exist.
GValue
Minor: Two entries “GValue”
Problem:
“GValue” has basically two entries:
- [1], containing functions like
g_value_init,g_value_copy, etc - [2], containing functions like
g_value_set_string,g_value_get_string, …
Suggestion:
- Difficult: If possible, merge the information into one site
Or - Simple: Add
See alsoright belowDescriptionof each site and link the other one.
[1] GObject – 2.0
[2] GObject – 2.0
It’s not clear, that g_value_unset() is needed
Problem:
- [2] says, that
g_value_inithas to be used, but there is no word ofg_value_unset. - Naturally “init” suggests the existence of “uninit”, while “set” suggests “unset”. This makes it even more difficult /to spot/, that you need
unset
Suggestion:
- Add explicit information under
Description, thatunsetneeds to be called. - For future reference, the opposite of
initmight better beuninit. Maybe it’s even worth thinking of creating an aliasuninitforunset
[2] GObject – 2.0
Memory management of GValue:
Problem:
GValue will work completely differently, depending on the type. Here some (assumed) examples.
- G_TYPE_CHAR: owned value; stack, no
unsetnecessary - G_TYPE_INT, …: owned value; stack; no
unsetnecessary - G_TYPE_BOOLEAN: owned value; stack; no
unsetnecessary - G_TYPE_STRING:
- static: not owned value; heap; no
unsetnecessary - not static: owned value; heap;
unsetcall becomes necessary
- static: not owned value; heap; no
- G_TYPE_OBJECT: owned value; heap; no
unsetnecessary (or did I forget tog_object_refin my test?) - G_TYPE_BOXED: ownage depends on implementation of specific type;
unsetnecessary - G_TYPE_POINTER: not owned; heap; not owned
Suggestion:
Each of these types has a g_value_set_* function. Some also have g_value_take_*, g_value_set_static_*, g_value_set_*_take_ownership.
In each function description, make it clear, if an unset() call is needed
Major: Unify language: set/take is arbitrary
( This would be an interface change, so chances are low, that anyone will support it. But I can’t let it be unmentioned. )
Problem:
Functions names and their meaning feels arbitrary. Here are examples:
_set_stringcopies the given string._set_pointertakes the given pointer._set_boxedcopies the given pointer._set_int(& co) takes the given pointer._set_param… copies (?) the given param… I guess… because there is atakefunction, but the function description doesn’t say anything about it
Suggestion
Unify function names and their meaning.
Add Example on how to use boxed types with GValue
Problem
Especially with the very, very scrace information on boxed types, it was very difficult to figure out how to use a boxed type - and why to use a boxed type.
Suggestion:
-
Add information: “
GValuedoes not provide a destroy function for arbitrary types (G_TYPE_POINTER). If you want to use a GValue with an arbitrary type, wrap it as a G_TYPE_BOXED” -
Add an example of how to use GValue with a boxed type close to the text above. Or maybe better on the page for
Boxed Typesand create a link from the text above.
GType BOXED_STRING = g_boxed_type_register_static (
"boxedString",
boxed_string_copy,
boxed_string_free);
GValue * val = g_new0(GValue, 1);
g_value_init (val, BOXED_STRING);
g_value_take_boxed (val, g_strdup ("hello"));
g_value_unset (val);
g_value_free (val);