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



commit a1a38f31420d093de7a85faca8b83e00982d728a
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.vala | 43 ++++++++++++++++++++++++++++++
 vapi/gio-2.0.vapi                          | 11 ++++++++
 vapi/metadata/Gio-2.0-custom.vala          | 11 ++++++++
 4 files changed, 66 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8ae9193d1..d58df130a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1309,6 +1309,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.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 20ac0544f..cf9673873 100644
--- a/vapi/gio-2.0.vapi
+++ b/vapi/gio-2.0.vapi
@@ -1724,6 +1724,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")]
@@ -1737,6 +1739,15 @@ 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) {
+                       size_t size = data.length;
+                       return new GLib.MemoryOutputStream.from_data ((owned) data, size, 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 2264a984a..9fef17e7a 100644
--- a/vapi/metadata/Gio-2.0-custom.vala
+++ b/vapi/metadata/Gio-2.0-custom.vala
@@ -84,6 +84,17 @@ 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) {
+                       size_t size = data.length;
+                       return new MemoryOutputStream.from_data ((owned) data, size, 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]