[vala/wip/issue/1271] gio-2.0: Add custom MemoryOutputStream.with_*data() wrappers




commit e3670d4744eec88ce9043dbe7d8648fc44a20517
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Dec 28 13:18:46 2021 +0100

    gio-2.0: Add custom MemoryOutputStream.with_*data() wrappers
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1271

 tests/Makefile.am                                |   1 +
 tests/bindings/gio/memoryoutputstream.c-expected | 225 +++++++++++++++++++++++
 tests/bindings/gio/memoryoutputstream.vala       |  43 +++++
 vapi/gio-2.0.vapi                                |  10 +
 vapi/metadata/Gio-2.0-custom.vala                |  10 +
 5 files changed, 289 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 788d9e474..5c201124d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1348,6 +1348,7 @@ TESTS = \
        genie/typeof.gs \
        genie/while.gs \
        glib/conditional-glib-api.vala \
+       bindings/gio/memoryoutputstream.vala \
        $(LINUX_TESTS) \
        $(NULL)
 
diff --git a/tests/bindings/gio/memoryoutputstream.c-expected 
b/tests/bindings/gio/memoryoutputstream.c-expected
new file mode 100644
index 000000000..f68e24a6a
--- /dev/null
+++ b/tests/bindings/gio/memoryoutputstream.c-expected
@@ -0,0 +1,225 @@
+/* bindings_gio_memoryoutputstream.c generated by valac, the Vala compiler
+ * generated from bindings_gio_memoryoutputstream.vala, do not modify */
+
+#include <glib.h>
+#include <gio/gio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#if !defined(VALA_EXTERN)
+#if defined(_MSC_VER)
+#define VALA_EXTERN __declspec(dllexport) extern
+#elif __GNUC__ >= 4
+#define VALA_EXTERN __attribute__((visibility("default"))) extern
+#else
+#define VALA_EXTERN extern
+#endif
+#endif
+
+#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
+#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, 
__LINE__, G_STRFUNC, msg);
+#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, 
G_STRFUNC, msg); return; }
+#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning 
(G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
+#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, 
__LINE__, G_STRFUNC, msg);
+
+VALA_EXTERN void test_stack_data (void);
+VALA_EXTERN void test_heap_data (void);
+VALA_EXTERN void test_owned_data (void);
+static void _vala_main (void);
+
+static GMemoryOutputStream*
+vala_g_memory_output_stream_with_data (guint8* data,
+                                       gsize data_length1)
+{
+       GMemoryOutputStream* _tmp0_;
+       GMemoryOutputStream* result = NULL;
+       _tmp0_ = (GMemoryOutputStream*) g_memory_output_stream_new (data, (gsize) data_length1, NULL, NULL);
+       result = _tmp0_;
+       return result;
+}
+
+void
+test_stack_data (void)
+{
+       guint8 buffer[7] = {0};
+       GMemoryOutputStream* mos = NULL;
+       GMemoryOutputStream* _tmp0_;
+       GDataOutputStream* dos = NULL;
+       GDataOutputStream* _tmp1_;
+       GError* _inner_error0_ = NULL;
+       _tmp0_ = vala_g_memory_output_stream_with_data (buffer, (gsize) 7);
+       mos = _tmp0_;
+       _tmp1_ = g_data_output_stream_new (G_TYPE_CHECK_INSTANCE_CAST (mos, g_output_stream_get_type (), 
GOutputStream));
+       dos = _tmp1_;
+       {
+               g_data_output_stream_put_string (dos, "foobar\0", NULL, &_inner_error0_);
+               if (G_UNLIKELY (_inner_error0_ != NULL)) {
+                       goto __catch0_g_error;
+               }
+               _vala_assert (7 == 7, "buffer.length == 7");
+               _vala_assert (g_strcmp0 ((const gchar*) buffer, "foobar") == 0, "(string) buffer == 
\"foobar\"");
+       }
+       goto __finally0;
+       __catch0_g_error:
+       {
+               g_clear_error (&_inner_error0_);
+               g_assert_not_reached ();
+       }
+       __finally0:
+       if (G_UNLIKELY (_inner_error0_ != NULL)) {
+               _g_object_unref0 (dos);
+               _g_object_unref0 (mos);
+               g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, 
_inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
+               g_clear_error (&_inner_error0_);
+               return;
+       }
+       _g_object_unref0 (dos);
+       _g_object_unref0 (mos);
+}
+
+void
+test_heap_data (void)
+{
+       guint8* buffer = NULL;
+       guint8* _tmp0_;
+       gint buffer_length1;
+       gint _buffer_size_;
+       GMemoryOutputStream* mos = NULL;
+       GMemoryOutputStream* _tmp1_;
+       GDataOutputStream* dos = NULL;
+       GDataOutputStream* _tmp2_;
+       GError* _inner_error0_ = NULL;
+       _tmp0_ = g_new0 (guint8, 7);
+       buffer = _tmp0_;
+       buffer_length1 = 7;
+       _buffer_size_ = buffer_length1;
+       _tmp1_ = vala_g_memory_output_stream_with_data (buffer, (gsize) buffer_length1);
+       mos = _tmp1_;
+       _tmp2_ = g_data_output_stream_new (G_TYPE_CHECK_INSTANCE_CAST (mos, g_output_stream_get_type (), 
GOutputStream));
+       dos = _tmp2_;
+       {
+               g_data_output_stream_put_string (dos, "foobar\0", NULL, &_inner_error0_);
+               if (G_UNLIKELY (_inner_error0_ != NULL)) {
+                       goto __catch0_g_error;
+               }
+               _vala_assert (g_strcmp0 ((const gchar*) buffer, "foobar") == 0, "(string) buffer == 
\"foobar\"");
+       }
+       goto __finally0;
+       __catch0_g_error:
+       {
+               g_clear_error (&_inner_error0_);
+               g_assert_not_reached ();
+       }
+       __finally0:
+       if (G_UNLIKELY (_inner_error0_ != NULL)) {
+               _g_object_unref0 (dos);
+               _g_object_unref0 (mos);
+               buffer = (g_free (buffer), NULL);
+               g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, 
_inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
+               g_clear_error (&_inner_error0_);
+               return;
+       }
+       _g_object_unref0 (dos);
+       _g_object_unref0 (mos);
+       buffer = (g_free (buffer), NULL);
+}
+
+static GMemoryOutputStream*
+vala_g_memory_output_stream_with_owned_data (guint8* data,
+                                             gsize data_length1)
+{
+       guint8* _tmp0_;
+       gint _tmp0__length1;
+       GReallocFunc _tmp1_;
+       GDestroyNotify _tmp2_;
+       GMemoryOutputStream* _tmp3_;
+       GMemoryOutputStream* result = NULL;
+       _tmp0_ = data;
+       _tmp0__length1 = data_length1;
+       data = NULL;
+       data_length1 = 0;
+       _tmp1_ = g_realloc;
+       _tmp2_ = g_free;
+       _tmp3_ = (GMemoryOutputStream*) g_memory_output_stream_new (_tmp0_, (gsize) data_length1, _tmp1_, 
_tmp2_);
+       result = _tmp3_;
+       data = (g_free (data), NULL);
+       return result;
+}
+
+void
+test_owned_data (void)
+{
+       guint8* buffer = NULL;
+       guint8* _tmp0_;
+       gint buffer_length1;
+       gint _buffer_size_;
+       GMemoryOutputStream* mos = NULL;
+       guint8* _tmp1_;
+       gint _tmp1__length1;
+       GMemoryOutputStream* _tmp2_;
+       GDataOutputStream* dos = NULL;
+       GDataOutputStream* _tmp3_;
+       GError* _inner_error0_ = NULL;
+       _tmp0_ = g_new0 (guint8, 7);
+       buffer = _tmp0_;
+       buffer_length1 = 7;
+       _buffer_size_ = buffer_length1;
+       _tmp1_ = buffer;
+       _tmp1__length1 = buffer_length1;
+       buffer = NULL;
+       buffer_length1 = 0;
+       _tmp2_ = vala_g_memory_output_stream_with_owned_data (_tmp1_, (gsize) _tmp1__length1);
+       mos = _tmp2_;
+       _tmp3_ = g_data_output_stream_new (G_TYPE_CHECK_INSTANCE_CAST (mos, g_output_stream_get_type (), 
GOutputStream));
+       dos = _tmp3_;
+       {
+               guint8* data = NULL;
+               guint8* _tmp4_;
+               gint data_length1;
+               gint _data_size_;
+               g_data_output_stream_put_string (dos, "foobar\0", NULL, &_inner_error0_);
+               if (G_UNLIKELY (_inner_error0_ != NULL)) {
+                       goto __catch0_g_error;
+               }
+               _tmp4_ = g_memory_output_stream_get_data (mos);
+               data = _tmp4_;
+               data_length1 = -1;
+               _data_size_ = data_length1;
+               _vala_assert (g_strcmp0 ((const gchar*) data, "foobar") == 0, "(string) data == \"foobar\"");
+       }
+       goto __finally0;
+       __catch0_g_error:
+       {
+               g_clear_error (&_inner_error0_);
+               g_assert_not_reached ();
+       }
+       __finally0:
+       if (G_UNLIKELY (_inner_error0_ != NULL)) {
+               _g_object_unref0 (dos);
+               _g_object_unref0 (mos);
+               buffer = (g_free (buffer), NULL);
+               g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, 
_inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
+               g_clear_error (&_inner_error0_);
+               return;
+       }
+       _g_object_unref0 (dos);
+       _g_object_unref0 (mos);
+       buffer = (g_free (buffer), NULL);
+}
+
+static void
+_vala_main (void)
+{
+       test_stack_data ();
+       test_heap_data ();
+       test_owned_data ();
+}
+
+int
+main (int argc,
+      char ** argv)
+{
+       _vala_main ();
+       return 0;
+}
+
diff --git a/tests/bindings/gio/memoryoutputstream.vala b/tests/bindings/gio/memoryoutputstream.vala
new file mode 100644
index 000000000..b3c361d46
--- /dev/null
+++ b/tests/bindings/gio/memoryoutputstream.vala
@@ -0,0 +1,43 @@
+void test_stack_data () {
+       uint8 buffer[7];
+       var mos = MemoryOutputStream.with_data (buffer);
+       var dos = new DataOutputStream (mos);
+       try {
+               dos.put_string ("foobar\0");
+               assert (buffer.length == 7);
+               assert ((string) buffer == "foobar");
+       } catch {
+               assert_not_reached ();
+       }
+}
+
+void test_heap_data () {
+       uint8[] buffer = new uint8[7];
+       var mos = MemoryOutputStream.with_data (buffer);
+       var dos = new DataOutputStream (mos);
+       try {
+               dos.put_string ("foobar\0");
+               assert ((string) buffer == "foobar");
+       } catch {
+               assert_not_reached ();
+       }
+}
+
+void test_owned_data () {
+       uint8[] buffer = new uint8[7];
+       var mos = MemoryOutputStream.with_owned_data ((owned) buffer);
+       var dos = new DataOutputStream (mos);
+       try {
+               dos.put_string ("foobar\0");
+               unowned uint8[] data = mos.get_data ();
+               assert ((string) data == "foobar");
+       } catch {
+               assert_not_reached ();
+       }
+}
+
+void main () {
+       test_stack_data ();
+       test_heap_data ();
+       test_owned_data ();
+}
diff --git a/vapi/gio-2.0.vapi b/vapi/gio-2.0.vapi
index b3dc8277d..f34fb6536 100644
--- a/vapi/gio-2.0.vapi
+++ b/vapi/gio-2.0.vapi
@@ -1773,6 +1773,8 @@ namespace GLib {
        public class MemoryOutputStream : GLib.OutputStream, GLib.PollableOutputStream, GLib.Seekable {
                [CCode (has_construct_function = false, type = "GOutputStream*")]
                public MemoryOutputStream ([CCode (array_length_type = "gsize")] owned uint8[]? data, 
GLib.ReallocFunc? realloc_function = GLib.g_realloc, GLib.DestroyNotify? destroy_function = GLib.g_free);
+               [CCode (cname = "g_memory_output_stream_new", has_construct_function = false, type = 
"GOutputStream*")]
+               public MemoryOutputStream.from_data (void* data, size_t size, GLib.ReallocFunc? 
realloc_function = null, GLib.DestroyNotify? destroy_function = null);
                [CCode (array_length = false)]
                public unowned uint8[] get_data ();
                [Version (since = "2.18")]
@@ -1786,6 +1788,14 @@ namespace GLib {
                [CCode (array_length = false)]
                [Version (since = "2.26")]
                public uint8[] steal_data ();
+               [CCode (cname = "vala_g_memory_output_stream_with_data")]
+               public static GLib.MemoryOutputStream with_data ([CCode (array_length_type = "gsize")] 
uint8[] data) {
+                       return new GLib.MemoryOutputStream.from_data (data, data.length, null, null);
+               }
+               [CCode (cname = "vala_g_memory_output_stream_with_owned_data")]
+               public static GLib.MemoryOutputStream with_owned_data ([CCode (array_length_type = "gsize")] 
owned uint8[] data) {
+                       return new GLib.MemoryOutputStream.from_data ((owned) data, data.length, 
GLib.g_realloc, GLib.g_free);
+               }
                [Version (since = "2.24")]
                public void* data { get; construct; }
                [Version (since = "2.24")]
diff --git a/vapi/metadata/Gio-2.0-custom.vala b/vapi/metadata/Gio-2.0-custom.vala
index 9b3b83c5e..1d37c613d 100644
--- a/vapi/metadata/Gio-2.0-custom.vala
+++ b/vapi/metadata/Gio-2.0-custom.vala
@@ -87,6 +87,16 @@ namespace GLib {
        public class MemoryOutputStream : GLib.OutputStream {
                [CCode (has_construct_function = false, type = "GOutputStream*")]
                public MemoryOutputStream ([CCode (array_length_type = "gsize")] owned uint8[]? data, 
GLib.ReallocFunc? realloc_function = GLib.g_realloc, GLib.DestroyNotify? destroy_function = GLib.g_free);
+               [CCode (cname = "g_memory_output_stream_new", has_construct_function = false, type = 
"GOutputStream*")]
+               public MemoryOutputStream.from_data (void* data, size_t size, GLib.ReallocFunc? 
realloc_function = null, GLib.DestroyNotify? destroy_function = null);
+               [CCode (cname = "vala_g_memory_output_stream_with_data")]
+               public static MemoryOutputStream with_data ([CCode (array_length_type = "gsize")] uint8[] 
data) {
+                       return new MemoryOutputStream.from_data (data, data.length);
+               }
+               [CCode (cname = "vala_g_memory_output_stream_with_owned_data")]
+               public static MemoryOutputStream with_owned_data ([CCode (array_length_type = "gsize")] owned 
uint8[] data) {
+                       return new MemoryOutputStream.from_data ((owned) data, data.length, GLib.g_realloc, 
GLib.g_free);
+               }
        }
 
        public abstract class NativeVolumeMonitor : GLib.VolumeMonitor {


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