[glib] Add GSimpleIOStream class



commit d4e3b82a93c59edd6f8c396868413e0fbf621972
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Wed Dec 17 09:11:29 2014 +0100

    Add GSimpleIOStream class
    
    GSimpleIOStream represents an object that wraps an input and an output
    stream making easy to use them by calling the #GIOStream methods.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=741630

 docs/reference/gio/gio-docs.xml     |    1 +
 docs/reference/gio/gio-sections.txt |   14 +++
 gio/Makefile.am                     |    2 +
 gio/gio.h                           |    1 +
 gio/giotypes.h                      |    1 +
 gio/gsimpleiostream.c               |  222 +++++++++++++++++++++++++++++++++++
 gio/gsimpleiostream.h               |   45 +++++++
 gio/tests/defaultvalue.c            |    1 +
 gio/tests/io-stream.c               |   83 +-------------
 9 files changed, 291 insertions(+), 79 deletions(-)
---
diff --git a/docs/reference/gio/gio-docs.xml b/docs/reference/gio/gio-docs.xml
index 808825f..1c774e2 100644
--- a/docs/reference/gio/gio-docs.xml
+++ b/docs/reference/gio/gio-docs.xml
@@ -57,6 +57,7 @@
         <xi:include href="xml/ginputstream.xml"/>
         <xi:include href="xml/goutputstream.xml"/>
         <xi:include href="xml/giostream.xml"/>
+        <xi:include href="xml/gsimpleiostream.xml"/>
         <xi:include href="xml/gfileinputstream.xml"/>
         <xi:include href="xml/gfileoutputstream.xml"/>
         <xi:include href="xml/gfileiostream.xml"/>
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index 5c186c0..6674c4c 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -1040,6 +1040,20 @@ g_io_stream_get_type
 </SECTION>
 
 <SECTION>
+<FILE>gsimpleiostream</FILE>
+<TITLE>GSimpleIOStream</TITLE>
+GSimpleIOStream
+g_simple_io_stream_new
+<SUBSECTION Standard>
+GIOStreamClass
+G_TYPE_SIMPLE_IO_STREAM
+G_IS_SIMPLE_IO_STREAM
+G_SIMPLE_IO_STREAM
+<SUBSECTION Private>
+g_simple_io_stream_get_type
+</SECTION>
+
+<SECTION>
 <FILE>gfileiostream</FILE>
 <TITLE>GFileIOStream</TITLE>
 GFileIOStream
diff --git a/gio/Makefile.am b/gio/Makefile.am
index 2505149..27369c8 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -427,6 +427,7 @@ libgio_2_0_la_SOURCES =             \
        gresourcefile.h         \
        gseekable.c             \
        gsimpleasyncresult.c    \
+       gsimpleiostream.c       \
        gsimplepermission.c     \
        gsocket.c               \
        gsocketaddress.c        \
@@ -596,6 +597,7 @@ gio_headers =                       \
        gresource.h             \
        gseekable.h             \
        gsimpleasyncresult.h    \
+       gsimpleiostream.h       \
        gsimplepermission.h     \
        gsocket.h               \
        gsocketaddress.h        \
diff --git a/gio/gio.h b/gio/gio.h
index 1a419b4..9c4ee12 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -111,6 +111,7 @@
 #include <gio/gsimpleaction.h>
 #include <gio/gsimpleactiongroup.h>
 #include <gio/gsimpleasyncresult.h>
+#include <gio/gsimpleiostream.h>
 #include <gio/gsimplepermission.h>
 #include <gio/gsocketaddressenumerator.h>
 #include <gio/gsocketaddress.h>
diff --git a/gio/giotypes.h b/gio/giotypes.h
index 11957b6..4342d47 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -135,6 +135,7 @@ typedef struct _GNetworkMonitor               GNetworkMonitor;
 typedef struct _GNetworkService               GNetworkService;
 typedef struct _GOutputStream                 GOutputStream;
 typedef struct _GIOStream                     GIOStream;
+typedef struct _GSimpleIOStream               GSimpleIOStream;
 typedef struct _GPollableInputStream          GPollableInputStream; /* Dummy typedef */
 typedef struct _GPollableOutputStream         GPollableOutputStream; /* Dummy typedef */
 typedef struct _GResolver                     GResolver;
diff --git a/gio/gsimpleiostream.c b/gio/gsimpleiostream.c
new file mode 100644
index 0000000..aa988c1
--- /dev/null
+++ b/gio/gsimpleiostream.c
@@ -0,0 +1,222 @@
+/*
+ * Copyright © 2014 NICE s.r.l.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Ignacio Casal Quinteiro <ignacio casal nice-software com>
+ */
+
+
+#include "config.h"
+#include <glib.h>
+#include "glibintl.h"
+
+#include "gsimpleiostream.h"
+#include "gtask.h"
+
+/**
+ * SECTION:gsimpleiostream
+ * @short_description: A wrapper around an input and an output stream.
+ * @include: gio/gio.h
+ * @see_also: #GIOStream
+ *
+ * GSimpleIOStream creates a #GIOStream from an arbitrary #GInputStream and
+ * #GOutputStream. This allows any pair of input and output streams to be used
+ * with #GIOStream methods.
+ *
+ * This is useful when you obtained a #GInputStream and a #GOutputStream
+ * by other means, for instance creating them with platform specific methods as
+ * g_unix_input_stream_new() or g_win32_input_stream_new(), and you want
+ * to take advantage of the methods provided by #GIOStream.
+ *
+ * Since: 2.44
+ */
+
+/**
+ * GSimpleIOStream:
+ *
+ * A wrapper around a #GInputStream and a #GOutputStream.
+ *
+ * Since: 2.44
+ */
+struct _GSimpleIOStream
+{
+  GIOStream parent;
+
+  GInputStream *input_stream;
+  GOutputStream *output_stream;
+};
+
+struct _GSimpleIOStreamClass
+{
+  GIOStreamClass parent;
+};
+
+typedef struct _GSimpleIOStreamClass GSimpleIOStreamClass;
+
+enum
+{
+  PROP_0,
+  PROP_INPUT_STREAM,
+  PROP_OUTPUT_STREAM
+};
+
+G_DEFINE_TYPE (GSimpleIOStream, g_simple_io_stream, G_TYPE_IO_STREAM)
+
+static void
+g_simple_io_stream_finalize (GObject *object)
+{
+  GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
+
+  if (stream->input_stream)
+    g_object_unref (stream->input_stream);
+
+  if (stream->output_stream)
+    g_object_unref (stream->output_stream);
+
+  G_OBJECT_CLASS (g_simple_io_stream_parent_class)->finalize (object);
+}
+
+static void
+g_simple_io_stream_set_property (GObject      *object,
+                                 guint         prop_id,
+                                 const GValue *value,
+                                 GParamSpec   *pspec)
+{
+  GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
+
+  switch (prop_id)
+    {
+    case PROP_INPUT_STREAM:
+      stream->input_stream = g_value_dup_object (value);
+      break;
+
+    case PROP_OUTPUT_STREAM:
+      stream->output_stream = g_value_dup_object (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+g_simple_io_stream_get_property (GObject    *object,
+                                 guint       prop_id,
+                                 GValue     *value,
+                                 GParamSpec *pspec)
+{
+  GSimpleIOStream *stream = G_SIMPLE_IO_STREAM (object);
+
+  switch (prop_id)
+    {
+    case PROP_INPUT_STREAM:
+      g_value_set_object (value, stream->input_stream);
+      break;
+
+    case PROP_OUTPUT_STREAM:
+      g_value_set_object (value, stream->output_stream);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static GInputStream *
+g_simple_io_stream_get_input_stream (GIOStream *stream)
+{
+  GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
+
+  return simple_stream->input_stream;
+}
+
+static GOutputStream *
+g_simple_io_stream_get_output_stream (GIOStream *stream)
+{
+  GSimpleIOStream *simple_stream = G_SIMPLE_IO_STREAM (stream);
+
+  return simple_stream->output_stream;
+}
+
+static void
+g_simple_io_stream_class_init (GSimpleIOStreamClass *class)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+  GIOStreamClass *io_class = G_IO_STREAM_CLASS (class);
+
+  gobject_class->finalize = g_simple_io_stream_finalize;
+  gobject_class->get_property = g_simple_io_stream_get_property;
+  gobject_class->set_property = g_simple_io_stream_set_property;
+
+  io_class->get_input_stream = g_simple_io_stream_get_input_stream;
+  io_class->get_output_stream = g_simple_io_stream_get_output_stream;
+
+  /**
+   * GSimpleIOStream:input-stream:
+   *
+   * Since: 2.44
+   */
+  g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
+                                   g_param_spec_object ("input-stream",
+                                                        P_("Input stream"),
+                                                        P_("The GInputStream to read from"),
+                                                        G_TYPE_INPUT_STREAM,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS |
+                                                        G_PARAM_CONSTRUCT_ONLY));
+
+  /**
+   * GSimpleIOStream:output-stream:
+   *
+   * Since: 2.44
+   */
+  g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
+                                   g_param_spec_object ("output-stream",
+                                                        P_("Output stream"),
+                                                        P_("The GOutputStream to write to"),
+                                                        G_TYPE_OUTPUT_STREAM,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS |
+                                                        G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+g_simple_io_stream_init (GSimpleIOStream *stream)
+{
+}
+
+/**
+ * g_simple_io_stream_new:
+ * @input_stream: a #GInputStream.
+ * @output_stream: a #GOutputStream.
+ *
+ * Creates a new #GSimpleIOStream wrapping @input_stream and @output_stream.
+ * See also #GIOStream.
+ *
+ * Returns: a new #GSimpleIOStream instance.
+ *
+ * Since: 2.44
+ */
+GIOStream *
+g_simple_io_stream_new (GInputStream  *input_stream,
+                        GOutputStream *output_stream)
+{
+  return g_object_new (G_TYPE_SIMPLE_IO_STREAM,
+                       "input-stream", input_stream,
+                       "output-stream", output_stream,
+                       NULL);
+}
diff --git a/gio/gsimpleiostream.h b/gio/gsimpleiostream.h
new file mode 100644
index 0000000..47c034e
--- /dev/null
+++ b/gio/gsimpleiostream.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright © 2014 NICE s.r.l.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Ignacio Casal Quinteiro <ignacio casal nice-software com>
+ */
+
+#ifndef __G_SIMPLE_IO_STREAM_H__
+#define __G_SIMPLE_IO_STREAM_H__
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#include <gio/giotypes.h>
+#include <gio/giostream.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_SIMPLE_IO_STREAM                  (g_simple_io_stream_get_type ())
+#define G_SIMPLE_IO_STREAM(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
G_TYPE_SIMPLE_IO_STREAM, GSimpleIOStream))
+#define G_IS_SIMPLE_IO_STREAM(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
G_TYPE_SIMPLE_IO_STREAM))
+
+GLIB_AVAILABLE_IN_2_44
+GType                g_simple_io_stream_get_type         (void) G_GNUC_CONST;
+
+GLIB_AVAILABLE_IN_2_44
+GIOStream           *g_simple_io_stream_new              (GInputStream  *input_stream,
+                                                          GOutputStream *output_stream);
+
+G_END_DECLS
+
+#endif /* __G_SIMPLE_IO_STREAM_H__ */
diff --git a/gio/tests/defaultvalue.c b/gio/tests/defaultvalue.c
index 023bc3d..f83327b 100644
--- a/gio/tests/defaultvalue.c
+++ b/gio/tests/defaultvalue.c
@@ -82,6 +82,7 @@ test_type (gconstpointer data)
       g_type_is_a (type, G_TYPE_PROPERTY_ACTION) ||
       g_type_is_a (type, G_TYPE_SETTINGS) ||
       g_type_is_a (type, G_TYPE_SOCKET_CONNECTION) ||
+      g_type_is_a (type, G_TYPE_SIMPLE_IO_STREAM) ||
       g_type_is_a (type, G_TYPE_THEMED_ICON) ||
       FALSE)
     {
diff --git a/gio/tests/io-stream.c b/gio/tests/io-stream.c
index 59b1c26..ee14c53 100644
--- a/gio/tests/io-stream.c
+++ b/gio/tests/io-stream.c
@@ -27,81 +27,6 @@
 
 typedef struct
 {
-  GIOStream parent;
-  GInputStream *input_stream;
-  GOutputStream *output_stream;
-} GTestIOStream;
-
-typedef struct
-{
-  GIOStreamClass parent_class;
-} GTestIOStreamClass;
-
-static GType g_test_io_stream_get_type (void);
-G_DEFINE_TYPE (GTestIOStream, g_test_io_stream, G_TYPE_IO_STREAM);
-
-
-static GInputStream *
-get_input_stream (GIOStream *io_stream)
-{
-  GTestIOStream *self =  (GTestIOStream *) io_stream;
-
-  return self->input_stream;
-}
-
-static GOutputStream *
-get_output_stream (GIOStream *io_stream)
-{
-  GTestIOStream *self =  (GTestIOStream *) io_stream;
-
-  return self->output_stream;
-}
-
-static void
-finalize (GObject *object)
-{
-  GTestIOStream *self = (GTestIOStream *) object;
-
-  if (self->input_stream != NULL)
-    g_object_unref (self->input_stream);
-
-  if (self->output_stream != NULL)
-    g_object_unref (self->output_stream);
-
-  G_OBJECT_CLASS (g_test_io_stream_parent_class)->finalize (object);
-}
-
-static void
-g_test_io_stream_class_init (GTestIOStreamClass *klass)
-{
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
-  GIOStreamClass *io_class = G_IO_STREAM_CLASS (klass);
-
-  object_class->finalize = finalize;
-
-  io_class->get_input_stream = get_input_stream;
-  io_class->get_output_stream = get_output_stream;
-}
-
-static void
-g_test_io_stream_init (GTestIOStream *self)
-{
-}
-
-static GIOStream *
-g_test_io_stream_new (GInputStream *input, GOutputStream *output)
-{
-  GTestIOStream *self;
-
-  self = g_object_new (g_test_io_stream_get_type (), NULL);
-  self->input_stream = g_object_ref (input);
-  self->output_stream = g_object_ref (output);
-
-  return G_IO_STREAM (self);
-}
-
-typedef struct
-{
   GMainLoop *main_loop;
   const gchar *data1;
   const gchar *data2;
@@ -122,11 +47,11 @@ test_copy_chunks_splice_cb (GObject *source_object,
   g_io_stream_splice_finish (res, &error);
   g_assert_no_error (error);
 
-  ostream = G_MEMORY_OUTPUT_STREAM (((GTestIOStream *) data->iostream1)->output_stream);
+  ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream1));
   received_data = g_memory_output_stream_get_data (ostream);
   g_assert_cmpstr (received_data, ==, data->data2);
 
-  ostream = G_MEMORY_OUTPUT_STREAM (((GTestIOStream *) data->iostream2)->output_stream);
+  ostream = G_MEMORY_OUTPUT_STREAM (g_io_stream_get_output_stream (data->iostream2));
   received_data = g_memory_output_stream_get_data (ostream);
   g_assert_cmpstr (received_data, ==, data->data1);
 
@@ -149,13 +74,13 @@ test_copy_chunks (void)
 
   istream = g_memory_input_stream_new_from_data (data.data1, -1, NULL);
   ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
-  data.iostream1 = g_test_io_stream_new (istream, ostream);
+  data.iostream1 = g_simple_io_stream_new (istream, ostream);
   g_object_unref (istream);
   g_object_unref (ostream);
 
   istream = g_memory_input_stream_new_from_data (data.data2, -1, NULL);
   ostream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
-  data.iostream2 = g_test_io_stream_new (istream, ostream);
+  data.iostream2 = g_simple_io_stream_new (istream, ostream);
   g_object_unref (istream);
   g_object_unref (ostream);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]