gdk_content_formats_unref (first);
gdk_content_formats_builder_add_formats (builder, second);
- return gdk_content_formats_builder_free (builder);
+ return gdk_content_formats_builder_free_to_formats (builder);
}
static gboolean
struct _GdkContentFormatsBuilder
{
+ int ref_count;
+
+ /* (element-type GType) */
GSList *gtypes;
gsize n_gtypes;
+
+ /* (element-type utf8) (interned) */
GSList *mime_types;
gsize n_mime_types;
};
+G_DEFINE_BOXED_TYPE (GdkContentFormatsBuilder,
+ gdk_content_formats_builder,
+ gdk_content_formats_builder_ref,
+ gdk_content_formats_builder_unref)
+
/**
* gdk_content_formats_builder_new:
*
GdkContentFormatsBuilder *
gdk_content_formats_builder_new (void)
{
- return g_slice_new0 (GdkContentFormatsBuilder);
+ GdkContentFormatsBuilder *builder;
+
+ builder = g_slice_new0 (GdkContentFormatsBuilder);
+ builder->ref_count = 1;
+
+ return builder;
}
/**
- * gdk_content_formats_builder_free:
+ * gdk_content_formats_builder_ref:
* @builder: a #GdkContentFormatsBuilder
*
- * Frees @builder and creates a new #GdkContentFormats from it.
+ * Acquires a reference on the given @builder.
*
- * Returns: a new #GdkContentFormats with all the formats added to @builder
- **/
+ * This function is intended primarily for bindings. #GdkContentFormatsBuilder objects
+ * should not be kept around.
+ *
+ * Returns: (transfer none): the given #GdkContentFormatsBuilder with
+ * its reference count increased
+ */
+GdkContentFormatsBuilder *
+gdk_content_formats_builder_ref (GdkContentFormatsBuilder *builder)
+{
+ g_return_val_if_fail (builder != NULL, NULL);
+ g_return_val_if_fail (builder->ref_count > 0, NULL);
+
+ builder->ref_count += 1;
+
+ return builder;
+}
+
+static void
+gdk_content_formats_builder_clear (GdkContentFormatsBuilder *builder)
+{
+ g_clear_pointer (&builder->gtypes, g_slist_free);
+ g_clear_pointer (&builder->mime_types, g_slist_free);
+}
+
+/**
+ * gdk_content_formats_builder_unref:
+ * @builder: a #GdkContentFormatsBuilder
+ *
+ * Releases a reference on the given @builder.
+ */
+void
+gdk_content_formats_builder_unref (GdkContentFormatsBuilder *builder)
+{
+ g_return_if_fail (builder != NULL);
+ g_return_if_fail (builder->ref_count > 0);
+
+ builder->ref_count -= 1;
+
+ if (builder->ref_count > 0)
+ return;
+
+ gdk_content_formats_builder_clear (builder);
+ g_slice_free (GdkContentFormatsBuilder, builder);
+}
+
+/**
+ * gdk_content_formats_builder_free_to_formats: (skip)
+ * @builder: a #GdkContentFormatsBuilder
+ *
+ * Creates a new #GdkContentFormats from the current state of the
+ * given @builder, and frees the @builder instance.
+ *
+ * Returns: (transfer full): the newly created #GdkContentFormats
+ * with all the formats added to @builder
+ */
+GdkContentFormats *
+gdk_content_formats_builder_free_to_formats (GdkContentFormatsBuilder *builder)
+{
+ GdkContentFormats *res;
+
+ g_return_val_if_fail (builder != NULL, NULL);
+
+ res = gdk_content_formats_builder_to_formats (builder);
+
+ gdk_content_formats_builder_unref (builder);
+
+ return res;
+}
+
+/**
+ * gdk_content_formats_builder_to_formats:
+ * @builder: a #GdkContentFormatsBuilder
+ *
+ * Creates a new #GdkContentFormats from the given @builder.
+ *
+ * The given #GdkContentFormatsBuilder is reset once this function returns;
+ * you cannot call this function multiple times on the same @builder instance.
+ *
+ * This function is intended primarily for bindings. C code should use
+ * gdk_content_formats_builder_free_to_formats().
+ *
+ * Returns: (transfer full): the newly created #GdkContentFormats
+ * with all the formats added to @builder
+ */
GdkContentFormats *
-gdk_content_formats_builder_free (GdkContentFormatsBuilder *builder)
+gdk_content_formats_builder_to_formats (GdkContentFormatsBuilder *builder)
{
GdkContentFormats *result;
GType *gtypes;
result = gdk_content_formats_new_take (gtypes, builder->n_gtypes,
mime_types, builder->n_mime_types);
- g_slist_free (builder->gtypes);
- g_slist_free (builder->mime_types);
- g_slice_free (GdkContentFormatsBuilder, builder);
+ gdk_content_formats_builder_clear (builder);
return result;
}
gboolean gdk_content_formats_contain_mime_type (const GdkContentFormats *formats,
const char *mime_type);
+#define GDK_TYPE_CONTENT_FORMATS_BUILDER (gdk_content_formats_builder_get_type ())
+
typedef struct _GdkContentFormatsBuilder GdkContentFormatsBuilder;
GDK_AVAILABLE_IN_3_94
-GdkContentFormatsBuilder*gdk_content_formats_builder_new (void);
+GType gdk_content_formats_builder_get_type (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_3_94
+GdkContentFormatsBuilder *gdk_content_formats_builder_new (void);
+GDK_AVAILABLE_IN_3_94
+GdkContentFormatsBuilder *gdk_content_formats_builder_ref (GdkContentFormatsBuilder *builder);
+GDK_AVAILABLE_IN_3_94
+void gdk_content_formats_builder_unref (GdkContentFormatsBuilder *builder);
+GDK_AVAILABLE_IN_3_94
+GdkContentFormats * gdk_content_formats_builder_free_to_formats (GdkContentFormatsBuilder *builder) G_GNUC_WARN_UNUSED_RESULT;
GDK_AVAILABLE_IN_3_94
-GdkContentFormats * gdk_content_formats_builder_free (GdkContentFormatsBuilder *builder) G_GNUC_WARN_UNUSED_RESULT;
+GdkContentFormats * gdk_content_formats_builder_to_formats (GdkContentFormatsBuilder *builder) G_GNUC_WARN_UNUSED_RESULT;
GDK_AVAILABLE_IN_3_94
void gdk_content_formats_builder_add_formats (GdkContentFormatsBuilder *builder,
const GdkContentFormats *formats);