giunioninfo: Split success and return value for get_discriminator_offset()

Otherwise there’s no obvious suitable return value to return when the
union is *not* discriminated.

This is an API break, but libgirepository has not been in a stable
release yet, so that’s fine.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3155
This commit is contained in:
Philip Withnall 2024-02-06 13:34:17 +00:00
parent 702719dee9
commit 11e8075e51
4 changed files with 20 additions and 7 deletions

View File

@ -72,5 +72,6 @@ your code if integer type warnings are enabled.
| `g_type_info_get_array_length` | [method@GIRepository.TypeInfo.get_array_length_index] | | `g_type_info_get_array_length` | [method@GIRepository.TypeInfo.get_array_length_index] |
| `g_typelib_new_from_*` | All replaced with `gi_typelib_new_from_bytes()` | | `g_typelib_new_from_*` | All replaced with `gi_typelib_new_from_bytes()` |
| `GI_FUNCTION_THROWS` and `GI_VFUNC_THROWS` | [method@GIRepository.CallableInfo.can_throw_gerror] | | `GI_FUNCTION_THROWS` and `GI_VFUNC_THROWS` | [method@GIRepository.CallableInfo.can_throw_gerror] |
| `g_union_info_get_discriminator_offset` | Split success and failure return values out into a new out-argument and return value |
| `g_union_info_get_copy_function` | [method@GIRepository.UnionInfo.get_copy_function_name] | | `g_union_info_get_copy_function` | [method@GIRepository.UnionInfo.get_copy_function_name] |
| `g_union_info_get_free_function` | [method@GIRepository.UnionInfo.get_free_function_name] | | `g_union_info_get_free_function` | [method@GIRepository.UnionInfo.get_free_function_name] |

View File

@ -1283,7 +1283,7 @@ write_union_info (const char *ns,
size_t offset; size_t offset;
GITypeInfo *type; GITypeInfo *type;
offset = gi_union_info_get_discriminator_offset (info); gi_union_info_get_discriminator_offset (info, &offset);
type = gi_union_info_get_discriminator_type (info); type = gi_union_info_get_discriminator_type (info);
xml_start_element (file, "discriminator"); xml_start_element (file, "discriminator");

View File

@ -151,19 +151,30 @@ gi_union_info_is_discriminated (GIUnionInfo *info)
/** /**
* gi_union_info_get_discriminator_offset: * gi_union_info_get_discriminator_offset:
* @info: a #GIUnionInfo * @info: a #GIUnionInfo
* @out_offset: (out) (optional): return location for the offset, in bytes, of
* the discriminator
* *
* Returns the offset of the discriminator field in the structure. * Obtain the offset of the discriminator field within the structure.
* *
* Returns: offset, in bytes, of the discriminator * The union must be discriminated, or `FALSE` will be returned.
*
* Returns: `TRUE` if the union is discriminated
* Since: 2.80 * Since: 2.80
*/ */
size_t gboolean
gi_union_info_get_discriminator_offset (GIUnionInfo *info) gi_union_info_get_discriminator_offset (GIUnionInfo *info,
size_t *out_offset)
{ {
GIRealInfo *rinfo = (GIRealInfo *)info; GIRealInfo *rinfo = (GIRealInfo *)info;
UnionBlob *blob = (UnionBlob *)&rinfo->typelib->data[rinfo->offset]; UnionBlob *blob = (UnionBlob *)&rinfo->typelib->data[rinfo->offset];
size_t discriminator_offset;
return blob->discriminator_offset; discriminator_offset = (blob->discriminated) ? blob->discriminator_offset : 0;
if (out_offset != NULL)
*out_offset = discriminator_offset;
return blob->discriminated;
} }
/** /**

View File

@ -76,7 +76,8 @@ GI_AVAILABLE_IN_ALL
gboolean gi_union_info_is_discriminated (GIUnionInfo *info); gboolean gi_union_info_is_discriminated (GIUnionInfo *info);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
size_t gi_union_info_get_discriminator_offset (GIUnionInfo *info); gboolean gi_union_info_get_discriminator_offset (GIUnionInfo *info,
size_t *out_offset);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GITypeInfo * gi_union_info_get_discriminator_type (GIUnionInfo *info); GITypeInfo * gi_union_info_get_discriminator_type (GIUnionInfo *info);