]> 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:28:19 +0000 (12:28 +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 8ae9193d1c039a089b3b00c451871e12c560d4f8..d58df130a88f2f991ea4f5f1b4c969dab509792e 100644 (file)
@@ -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 (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 20ac0544fde3a97ffab930b082e05b75052f0de7..cf9673873f0174415b379eee51a546263ff988af 100644 (file)
@@ -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")]
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 {