gibaseinfo: Remove need for casting for gi_base_info_ref() and unref()

Just like is done with `g_object_{ref,unref}()`, make these functions
take a `void*` rather than a `GIBaseInfo*`, since they’ll most likely be
called with a type which is derived from `GIBaseInfo*` rather than a
`GIBaseInfo*` itself.

Add some runtime type checks to make up for lowering the compile time
type safety.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
Helps: #3216
This commit is contained in:
Philip Withnall 2024-01-17 12:30:35 +00:00
parent 5423bf4df7
commit 9debaffe0e
3 changed files with 14 additions and 10 deletions

View File

@ -517,7 +517,7 @@ gi_type_info_init (GIBaseInfo *info,
/** /**
* gi_base_info_ref: * gi_base_info_ref:
* @info: a #GIBaseInfo * @info: (type GIRepository.BaseInfo): a #GIBaseInfo
* *
* Increases the reference count of @info. * Increases the reference count of @info.
* *
@ -525,10 +525,12 @@ gi_type_info_init (GIBaseInfo *info,
* Since: 2.80 * Since: 2.80
*/ */
GIBaseInfo * GIBaseInfo *
gi_base_info_ref (GIBaseInfo *info) gi_base_info_ref (void *info)
{ {
GIRealInfo *rinfo = (GIRealInfo*)info; GIRealInfo *rinfo = (GIRealInfo*)info;
g_return_val_if_fail (GI_IS_BASE_INFO (info), NULL);
g_assert (rinfo->ref_count != INVALID_REFCOUNT); g_assert (rinfo->ref_count != INVALID_REFCOUNT);
g_atomic_ref_count_inc (&rinfo->ref_count); g_atomic_ref_count_inc (&rinfo->ref_count);
@ -537,7 +539,7 @@ gi_base_info_ref (GIBaseInfo *info)
/** /**
* gi_base_info_unref: * gi_base_info_unref:
* @info: (transfer full): a #GIBaseInfo * @info: (type GIRepository.BaseInfo) (transfer full): a #GIBaseInfo
* *
* Decreases the reference count of @info. When its reference count * Decreases the reference count of @info. When its reference count
* drops to 0, the info is freed. * drops to 0, the info is freed.
@ -545,10 +547,12 @@ gi_base_info_ref (GIBaseInfo *info)
* Since: 2.80 * Since: 2.80
*/ */
void void
gi_base_info_unref (GIBaseInfo *info) gi_base_info_unref (void *info)
{ {
GIRealInfo *rinfo = (GIRealInfo*)info; GIRealInfo *rinfo = (GIRealInfo*)info;
g_return_if_fail (GI_IS_BASE_INFO (info));
g_assert (rinfo->ref_count > 0 && rinfo->ref_count != INVALID_REFCOUNT); g_assert (rinfo->ref_count > 0 && rinfo->ref_count != INVALID_REFCOUNT);
if (g_atomic_ref_count_dec (&rinfo->ref_count)) if (g_atomic_ref_count_dec (&rinfo->ref_count))

View File

@ -65,10 +65,10 @@ GI_AVAILABLE_IN_ALL
GType gi_base_info_get_type (void) G_GNUC_CONST; GType gi_base_info_get_type (void) G_GNUC_CONST;
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GIBaseInfo * gi_base_info_ref (GIBaseInfo *info); GIBaseInfo * gi_base_info_ref (void *info);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
void gi_base_info_unref (GIBaseInfo *info); void gi_base_info_unref (void *info);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GIInfoType gi_base_info_get_info_type (GIBaseInfo *info); GIInfoType gi_base_info_get_info_type (GIBaseInfo *info);

View File

@ -116,16 +116,16 @@ test_repository_info (void)
g_assert_nonnull (method_info); g_assert_nonnull (method_info);
g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info)); g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info));
g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), ==, 2); g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), ==, 2);
g_clear_pointer ((GIBaseInfo **) &method_info, gi_base_info_unref); g_clear_pointer (&method_info, gi_base_info_unref);
method_info = gi_object_info_get_method (object_info, method_info = gi_object_info_get_method (object_info,
gi_object_info_get_n_methods (object_info) - 1); gi_object_info_get_n_methods (object_info) - 1);
g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info)); g_assert_true (gi_callable_info_is_method ((GICallableInfo *) method_info));
g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), >, 0); g_assert_cmpuint (gi_callable_info_get_n_args ((GICallableInfo *) method_info), >, 0);
g_clear_pointer ((GIBaseInfo **) &method_info, gi_base_info_unref); g_clear_pointer (&method_info, gi_base_info_unref);
gi_base_info_unref ((GIBaseInfo *) signal_info); gi_base_info_unref (signal_info);
gi_base_info_unref ((GIBaseInfo *) object_info); gi_base_info_unref (object_info);
g_clear_object (&repository); g_clear_object (&repository);
} }