Add unref-to-strv to GStrvBuilder

For those projects that cannot use `g_autoptr()`, GStrvBuilder's end
plus unref is not really convenient.

We can crib the "unref to data type" model from GBytes, and have an
additional unref function that also returns the just built GStrv.
This commit is contained in:
Emmanuele Bassi 2024-03-05 13:45:53 +00:00
parent afcb839121
commit 1a5b1393bd
3 changed files with 68 additions and 0 deletions

View File

@ -37,7 +37,10 @@
* g_autoptr(GStrvBuilder) builder = g_strv_builder_new ();
* g_strv_builder_add (builder, "hello");
* g_strv_builder_add (builder, "world");
*
* g_auto(GStrv) array = g_strv_builder_end (builder);
*
* g_assert_true (g_strv_equal (array, (const char *[]) { "hello", "world", NULL }));
* ```
*
* Since: 2.68
@ -81,6 +84,43 @@ g_strv_builder_unref (GStrvBuilder *builder)
g_ptr_array_unref (&builder->array);
}
/**
* g_strv_builder_unref_to_strv:
* @builder: (transfer full): a #GStrvBuilder
*
* Decreases the reference count on the string vector builder, and returns
* its contents as a `NULL`-terminated string array.
*
* This function is especially useful for cases where it's not possible
* to use `g_autoptr()`.
*
* ```c
* GStrvBuilder *builder = g_strv_builder_new ();
* g_strv_builder_add (builder, "hello");
* g_strv_builder_add (builder, "world");
*
* GStrv array = g_strv_builder_unref_to_strv (builder);
*
* g_assert_true (g_strv_equal (array, (const char *[]) { "hello", "world", NULL }));
*
* g_strfreev (array);
* ```
*
* Returns: (transfer full) (array zero-terminated=1): the constructed string
* array
*
* Since: 2.82
*/
GStrv
g_strv_builder_unref_to_strv (GStrvBuilder *builder)
{
GStrv res = g_strv_builder_end (builder);
g_strv_builder_unref (builder);
return res;
}
/**
* g_strv_builder_ref:
* @builder: (transfer none): a #GStrvBuilder

View File

@ -38,6 +38,9 @@ GStrvBuilder *g_strv_builder_new (void);
GLIB_AVAILABLE_IN_2_68
void g_strv_builder_unref (GStrvBuilder *builder);
GLIB_AVAILABLE_IN_2_82
GStrv g_strv_builder_unref_to_strv (GStrvBuilder *builder);
GLIB_AVAILABLE_IN_2_68
GStrvBuilder *g_strv_builder_ref (GStrvBuilder *builder);

View File

@ -122,6 +122,30 @@ test_strvbuilder_ref (void)
g_strv_builder_unref (builder);
}
static void
test_strvbuilder_unref_to_strv (void)
{
GStrvBuilder *builder = g_strv_builder_new ();
GStrv result;
g_strv_builder_add_many (builder, "hello", "world", NULL);
result = g_strv_builder_unref_to_strv (builder);
g_assert_true (g_strv_equal ((const char * const *) result,
(const char *[]) {
"hello",
"world",
NULL,
}));
g_strfreev (result);
builder = g_strv_builder_new ();
result = g_strv_builder_unref_to_strv (builder);
g_assert_null (result[0]);
g_strfreev (result);
}
int
main (int argc,
char *argv[])
@ -134,6 +158,7 @@ main (int argc,
g_test_add_func ("/strvbuilder/add_many", test_strvbuilder_add_many);
g_test_add_func ("/strvbuilder/take", test_strvbuilder_take);
g_test_add_func ("/strvbuilder/ref", test_strvbuilder_ref);
g_test_add_func ("/strvbuilder/unref_to_strv", test_strvbuilder_unref_to_strv);
return g_test_run ();
}