#include "config.h"
#include "gmemoryoutputstream.h"
#include "goutputstream.h"
+#include "gpollableoutputstream.h"
#include "gseekable.h"
#include "gsimpleasyncresult.h"
#include "gioerror.h"
* #GMemoryOutputStream is a class for using arbitrary
* memory chunks as output for GIO streaming output operations.
*
+ * As of GLib 2.34, #GMemoryOutputStream implements
+ * #GPollableOutputStream.
*/
#define MIN_ARRAY_SIZE 16
GCancellable *cancellable,
GError **error);
-static void g_memory_output_stream_write_async (GOutputStream *stream,
- const void *buffer,
- gsize count,
- int io_priority,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer data);
-static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
- GAsyncResult *result,
- GError **error);
static void g_memory_output_stream_close_async (GOutputStream *stream,
int io_priority,
GCancellable *cancellable,
GCancellable *cancellable,
GError **error);
+static gboolean g_memory_output_stream_is_writable (GPollableOutputStream *stream);
+static GSource *g_memory_output_stream_create_source (GPollableOutputStream *stream,
+ GCancellable *cancellable);
+
+static void g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
+
G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
- g_memory_output_stream_seekable_iface_init))
+ g_memory_output_stream_seekable_iface_init);
+ G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
+ g_memory_output_stream_pollable_iface_init))
static void
ostream_class->write_fn = g_memory_output_stream_write;
ostream_class->close_fn = g_memory_output_stream_close;
- ostream_class->write_async = g_memory_output_stream_write_async;
- ostream_class->write_finish = g_memory_output_stream_write_finish;
ostream_class->close_async = g_memory_output_stream_close_async;
ostream_class->close_finish = g_memory_output_stream_close_finish;
}
static void
+g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
+{
+ iface->is_writable = g_memory_output_stream_is_writable;
+ iface->create_source = g_memory_output_stream_create_source;
+}
+
+static void
g_memory_output_stream_set_property (GObject *object,
guint prop_id,
const GValue *value,
/**
* g_memory_output_stream_new: (skip)
- * @data: pointer to a chunk of memory to use, or %NULL
+ * @data: (allow-none): pointer to a chunk of memory to use, or %NULL
* @size: the size of @data
- * @realloc_function: a function with realloc() semantics (like g_realloc())
+ * @realloc_function: (allow-none): a function with realloc() semantics (like g_realloc())
* to be called when @data needs to be grown, or %NULL
- * @destroy_function: a function to be called on @data when the stream is
+ * @destroy_function: (allow-none): a function to be called on @data when the stream is
* finalized, or %NULL
*
* Creates a new #GMemoryOutputStream.
}
static void
-g_memory_output_stream_write_async (GOutputStream *stream,
- const void *buffer,
- gsize count,
- int io_priority,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer data)
-{
- GSimpleAsyncResult *simple;
- GError *error = NULL;
- gssize nwritten;
-
- nwritten = G_OUTPUT_STREAM_GET_CLASS (stream)->write_fn (stream,
- buffer,
- count,
- cancellable,
- &error);
-
- simple = g_simple_async_result_new (G_OBJECT (stream),
- callback,
- data,
- g_memory_output_stream_write_async);
-
- if (error)
- g_simple_async_result_take_error (simple, error);
- else
- g_simple_async_result_set_op_res_gssize (simple, nwritten);
- g_simple_async_result_complete_in_idle (simple);
- g_object_unref (simple);
-}
-
-static gssize
-g_memory_output_stream_write_finish (GOutputStream *stream,
- GAsyncResult *result,
- GError **error)
-{
- GSimpleAsyncResult *simple;
- gssize nwritten;
-
- simple = G_SIMPLE_ASYNC_RESULT (result);
-
- g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
- g_memory_output_stream_write_async);
-
- nwritten = g_simple_async_result_get_op_res_gssize (simple);
-
- return nwritten;
-}
-
-static void
g_memory_output_stream_close_async (GOutputStream *stream,
int io_priority,
GCancellable *cancellable,
return TRUE;
}
+
+static gboolean
+g_memory_output_stream_is_writable (GPollableOutputStream *stream)
+{
+ return TRUE;
+}
+
+static GSource *
+g_memory_output_stream_create_source (GPollableOutputStream *stream,
+ GCancellable *cancellable)
+{
+ GSource *base_source, *pollable_source;
+
+ base_source = g_timeout_source_new (0);
+ pollable_source = g_pollable_source_new_full (stream, base_source,
+ cancellable);
+ g_source_unref (base_source);
+
+ return pollable_source;
+}