Re: g_value_peek_pointer weirdness



On Mon, 15 Dec 2003, Sheldon Simms wrote:

> I'm reading code from glib 2.3.1
> In gvalue.c there are the following two functions:
>
> 158 gboolean
> 159 g_value_fits_pointer (const GValue *value)
> 160 {
> 161   GTypeValueTable *value_table;
> 162
> 163   g_return_val_if_fail (G_IS_VALUE (value),
> FALSE);
> 164
> 165   value_table = g_type_value_table_peek
> (G_VALUE_TYPE (value));
> 166
> 167   return value_table->value_peek_pointer != NULL;
> 168 }
> 169
> 170 gpointer
> 171 g_value_peek_pointer (const GValue *value)
> 172 {
> 173   GTypeValueTable *value_table;
> 174
> 175   g_return_val_if_fail (G_IS_VALUE (value), NULL);
> 176
> 177   value_table = g_type_value_table_peek
> (G_VALUE_TYPE (value));
> 178   if (!value_table->value_peek_pointer)
> 179     g_return_val_if_fail (g_value_fits_pointer
> (value) == TRUE, NULL);
> 180
> 181   return value_table->value_peek_pointer (value);
> 182 }

g_value_peek_pointer() is supposed to only be called
for values for which g_value_fits_pointer() returns TRUE.

> Since I do know know the implementation of
> g_value_fits_pointer(), however,
> I know that everything will be ok; because when
> value_peek_pointer is a null
> pointer, g_value_fits_pointer() always return FALSE,
> and cause
> g_value_peek_pointer() to return NULL. But in that
> case, why not just do
> this:
>
> 178   if (!value_table->value_peek_pointer)
> 179     return NULL;
> 180
> 181   return value_table->value_peek_pointer (value);

because that wouldn't point out that g_value_peek_pointer()
was erroneously used.

since you're not supposed to call g_value_peek_pointer()
with a value for which g_value_fits_pointer() doesn't return
TRUE, you don't actually need to care what g_value_peek_pointer()
does in case of !value_table->value_peek_pointer, except if you
mean to discuss warning style ;)

>
> -Sheldon
>

---
ciaoTJ




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]