]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
gio-2.0: Add custom MemoryOutputStream.with_*data() wrappers
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 28 Dec 2021 12:18:46 +0000 (13:18 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 4 Jan 2022 11:37:57 +0000 (12:37 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1271

tests/Makefile.am
tests/bindings/gio/memoryoutputstream.vala [new file with mode: 0644]
vapi/gio-2.0.vapi
vapi/metadata/Gio-2.0-custom.vala

index 727d3e8c0ad16efd7d604aec7013db284619c481..a3a62c22eddbe97e81ec3d2e846b0b83cfa0f1da 100644 (file)
@@ -1247,6 +1247,7 @@ TESTS = \
        genie/try-except-finally.gs \
        genie/typeof.gs \
        genie/while.gs \
+       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 (file)
index 0000000..b3c361d
--- /dev/null
@@ -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 ();
+}
index 6798eac4f8a401235d4df0e56bdeb099f44479bf..a6d6d67a0934f2f4b9ba0983088326c886e6fa90 100644 (file)
@@ -1715,6 +1715,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")]
@@ -1728,6 +1730,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")]
index 2264a984a3eade75dbb48c00930eb4e672ca2b4d..9fef17e7a904664d3c71dbdb397330cf71383f1a 100644 (file)
@@ -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 {