Allow passing unset GValues to g_value_unset()

This makes it more useful as an autocleanup func.

Also, add a minimal test of g_value_init/g_value_reset/g_value_unset.

https://bugzilla.gnome.org/show_bug.cgi?id=755766
This commit is contained in:
Dan Winship 2015-10-02 10:06:22 -04:00
parent b4a3c1bb11
commit 4b2d92a864
4 changed files with 42 additions and 8 deletions

View File

@ -254,16 +254,19 @@ g_value_reset (GValue *value)
* g_value_unset: * g_value_unset:
* @value: An initialized #GValue structure. * @value: An initialized #GValue structure.
* *
* Clears the current value in @value and "unsets" the type, * Clears the current value in @value (if any) and "unsets" the type,
* this releases all resources associated with this GValue. * this releases all resources associated with this GValue. An unset
* An unset value is the same as an uninitialized (zero-filled) * value is the same as an uninitialized (zero-filled) #GValue
* #GValue structure. * structure.
*/ */
void void
g_value_unset (GValue *value) g_value_unset (GValue *value)
{ {
GTypeValueTable *value_table; GTypeValueTable *value_table;
if (value->g_type == 0)
return;
g_return_if_fail (G_IS_VALUE (value)); g_return_if_fail (G_IS_VALUE (value));
value_table = g_type_value_table_peek (G_VALUE_TYPE (value)); value_table = g_type_value_table_peek (G_VALUE_TYPE (value));

View File

@ -12,6 +12,6 @@ reference
signals signals
threadtests threadtests
type type
valuearray value
private private
marshalers.[ch] marshalers.[ch]

View File

@ -17,7 +17,7 @@ test_programs = \
binding \ binding \
properties \ properties \
reference \ reference \
valuearray \ value \
type \ type \
private \ private \
closure \ closure \

View File

@ -1,6 +1,36 @@
#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_30 #define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_30
#include <glib-object.h> #include <glib-object.h>
static void
test_value_basic (void)
{
GValue value = G_VALUE_INIT;
g_assert_false (G_IS_VALUE (&value));
g_assert_false (G_VALUE_HOLDS_INT (&value));
g_value_unset (&value);
g_assert_false (G_IS_VALUE (&value));
g_assert_false (G_VALUE_HOLDS_INT (&value));
g_value_init (&value, G_TYPE_INT);
g_assert_true (G_IS_VALUE (&value));
g_assert_true (G_VALUE_HOLDS_INT (&value));
g_assert_false (G_VALUE_HOLDS_UINT (&value));
g_assert_cmpint (g_value_get_int (&value), ==, 0);
g_value_set_int (&value, 10);
g_assert_cmpint (g_value_get_int (&value), ==, 10);
g_value_reset (&value);
g_assert_true (G_IS_VALUE (&value));
g_assert_true (G_VALUE_HOLDS_INT (&value));
g_assert_cmpint (g_value_get_int (&value), ==, 0);
g_value_unset (&value);
g_assert_false (G_IS_VALUE (&value));
g_assert_false (G_VALUE_HOLDS_INT (&value));
}
static gint static gint
cmpint (gconstpointer a, gconstpointer b) cmpint (gconstpointer a, gconstpointer b)
{ {
@ -11,7 +41,7 @@ cmpint (gconstpointer a, gconstpointer b)
} }
static void static void
test_basic (void) test_valuearray_basic (void)
{ {
GValueArray *a; GValueArray *a;
GValueArray *a2; GValueArray *a2;
@ -58,7 +88,8 @@ main (int argc, char *argv[])
{ {
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
g_test_add_func ("/valuearray/basic", test_basic); g_test_add_func ("/value/basic", test_value_basic);
g_test_add_func ("/value/array/basic", test_valuearray_basic);
return g_test_run (); return g_test_run ();
} }