GMemoryInputStream/GMemoryOutputStream: make these properly subclassable
[platform/upstream/glib.git] / gio / gmemoryoutputstream.c
index 0369ca8..78c1139 100644 (file)
@@ -31,7 +31,6 @@
 #include "string.h"
 #include "glibintl.h"
 
-#include "gioalias.h"
 
 /**
  * SECTION:gmemoryoutputstream
@@ -147,6 +146,13 @@ g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
   ostream_class->close_async  = g_memory_output_stream_close_async;
   ostream_class->close_finish = g_memory_output_stream_close_finish;
 
+  /**
+   * GMemoryOutputStream:data:
+   *
+   * Pointer to buffer where data will be written.
+   *
+   * Since: 2.24
+   **/
   g_object_class_install_property (gobject_class,
                                    PROP_DATA,
                                    g_param_spec_pointer ("data",
@@ -155,6 +161,13 @@ g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                                          G_PARAM_STATIC_STRINGS));
 
+  /**
+   * GMemoryOutputStream:size:
+   *
+   * Current size of the data buffer.
+   *
+   * Since: 2.24
+   **/
   g_object_class_install_property (gobject_class,
                                    PROP_SIZE,
                                    g_param_spec_ulong ("size",
@@ -164,6 +177,13 @@ g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));
 
+  /**
+   * GMemoryOutputStream:data-size:
+   *
+   * Size of data written to the buffer.
+   *
+   * Since: 2.24
+   **/
   g_object_class_install_property (gobject_class,
                                    PROP_DATA_SIZE,
                                    g_param_spec_ulong ("data-size",
@@ -173,6 +193,13 @@ g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
                                                        G_PARAM_READABLE |
                                                        G_PARAM_STATIC_STRINGS));
 
+  /**
+   * GMemoryOutputStream:realloc-function: (skip)
+   *
+   * Function with realloc semantics called to enlarge the buffer.
+   *
+   * Since: 2.24
+   **/
   g_object_class_install_property (gobject_class,
                                    PROP_REALLOC_FUNCTION,
                                    g_param_spec_pointer ("realloc-function",
@@ -181,6 +208,13 @@ g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                                                          G_PARAM_STATIC_STRINGS));
 
+  /**
+   * GMemoryOutputStream:destroy-function: (skip)
+   *
+   * Function called with the buffer as argument when the stream is destroyed.
+   *
+   * Since: 2.24
+   **/
   g_object_class_install_property (gobject_class,
                                    PROP_DESTROY_FUNCTION,
                                    g_param_spec_pointer ("destroy-function",
@@ -294,11 +328,11 @@ g_memory_output_stream_init (GMemoryOutputStream *stream)
 }
 
 /**
- * g_memory_output_stream_new:
+ * g_memory_output_stream_new: (skip)
  * @data: pointer to a chunk of memory to use, or %NULL
  * @size: the size of @data
- * @realloc_function: a function with realloc() semantics to be called when
- *     @data needs to be grown, or %NULL
+ * @realloc_function: 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
  *     finalized, or %NULL
  *
@@ -308,13 +342,17 @@ g_memory_output_stream_init (GMemoryOutputStream *stream)
  * If @realloc_fn is non-%NULL, it will be used for resizing the internal
  * storage when necessary. To construct a fixed-size output stream,
  * pass %NULL as @realloc_fn.
+ *
  * |[
  * /* a stream that can grow */
  * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
  *
+ * /* another stream that can grow */
+ * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+ *
  * /* a fixed-size stream */
  * data = malloc (200);
- * stream2 = g_memory_output_stream_new (data, 200, NULL, free);
+ * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
  * ]|
  *
  * Return value: A newly created #GMemoryOutputStream object.
@@ -346,7 +384,7 @@ g_memory_output_stream_new (gpointer       data,
  * Note that the returned pointer may become invalid on the next
  * write or truncate operation on the stream.
  *
- * Returns: pointer to the stream's data
+ * Returns: (transfer none): pointer to the stream's data
  **/
 gpointer
 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
@@ -360,7 +398,7 @@ g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
  * g_memory_output_stream_get_size:
  * @ostream: a #GMemoryOutputStream
  *
- * Gets the size of the currently allocated data area (availible from
+ * Gets the size of the currently allocated data area (available from
  * g_memory_output_stream_get_data()). If the stream isn't
  * growable (no realloc was passed to g_memory_output_stream_new()) then
  * this is the maximum size of the stream and further writes
@@ -402,6 +440,35 @@ g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
   return ostream->priv->valid_len;
 }
 
+/**
+ * g_memory_output_stream_steal_data:
+ * @ostream: a #GMemoryOutputStream
+ *
+ * Gets any loaded data from the @ostream. Ownership of the data
+ * is transferred to the caller; when no longer needed it must be
+ * freed using the free function set in @ostream's
+ * #GMemoryOutputStream:destroy-function property.
+ *
+ * @ostream must be closed before calling this function.
+ *
+ * Returns: (transfer full): the stream's data
+ *
+ * Since: 2.26
+ **/
+gpointer
+g_memory_output_stream_steal_data (GMemoryOutputStream *ostream)
+{
+  gpointer data;
+
+  g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
+  g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
+
+  data = ostream->priv->data;
+  ostream->priv->data = NULL;
+
+  return data;
+}
+
 static gboolean
 array_resize (GMemoryOutputStream  *ostream,
               gsize                 size,
@@ -555,11 +622,11 @@ g_memory_output_stream_write_async (GOutputStream       *stream,
   GSimpleAsyncResult *simple;
   gssize nwritten;
 
-  nwritten = g_memory_output_stream_write (stream,
-                                           buffer,
-                                           count,
-                                           cancellable,
-                                           NULL);
+  nwritten = g_output_stream_write (stream,
+                                   buffer,
+                                   count,
+                                   cancellable,
+                                   NULL);
 
   simple = g_simple_async_result_new (G_OBJECT (stream),
                                       callback,
@@ -729,6 +796,3 @@ g_memory_output_stream_truncate (GSeekable     *seekable,
 
   return TRUE;
 }
-
-#define __G_MEMORY_OUTPUT_STREAM_C__
-#include "gioaliasdef.c"