[gnio] Add vtable functions for get_{in,out}put_stream



commit d52fadcfc99d46a866a7987cacaecb46e3181189
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Apr 26 02:06:39 2009 -0400

    Add vtable functions for get_{in,out}put_stream
    
    Make GIOStream get_input_stream() and get_output_stream() go through a
    vtable instead of turning into a property lookup for performance
    reasons.  Implement these functions for GSocketConnection and
    GTLSConnection.
    
    Also, use g_object_class_override_property() where appropriate.
---
 gio/giostream.c         |   31 +++++++++++++++++----
 gio/giostream.h         |    5 +++-
 gio/gsocketconnection.c |   66 ++++++++++++++++++++++++++++++-----------------
 gio/gtls.c              |   65 +++++++++++++++++++++++++++++-----------------
 4 files changed, 112 insertions(+), 55 deletions(-)

diff --git a/gio/giostream.c b/gio/giostream.c
index fb6d5d2..950c4a8 100644
--- a/gio/giostream.c
+++ b/gio/giostream.c
@@ -65,19 +65,38 @@ g_io_stream_get_type (void)
 GInputStream *
 g_io_stream_get_input_stream (GIOStream *io_stream)
 {
-  GInputStream *input = NULL;
+  GIOStreamIface *iface;
 
-  g_object_get (io_stream, "input-stream", &input, NULL);
+  iface = G_IO_STREAM_GET_INTERFACE (io_stream);
 
-  return input;
+  if (iface->get_input_stream == NULL)
+    {
+      GInputStream *input = NULL;
+
+      g_object_get (io_stream, "input-stream", &input, NULL);
+
+      return input;
+    }
+  else
+    return iface->get_input_stream (io_stream);
 }
 
 GOutputStream *
 g_io_stream_get_output_stream (GIOStream *io_stream)
 {
-  GOutputStream *output = NULL;
+  GIOStreamIface *iface;
 
-  g_object_get (io_stream, "output-stream", &output, NULL);
+  iface = G_IO_STREAM_GET_INTERFACE (io_stream);
 
-  return output;
+  if (iface->get_output_stream == NULL)
+    {
+      GOutputStream *output = NULL;
+
+      g_object_get (io_stream, "output-stream", &output, NULL);
+
+      return output;
+    }
+  else
+    return iface->get_output_stream (io_stream);
 }
+
diff --git a/gio/giostream.h b/gio/giostream.h
index 55eeda0..0800aa8 100644
--- a/gio/giostream.h
+++ b/gio/giostream.h
@@ -28,7 +28,7 @@ G_BEGIN_DECLS
                                                              G_TYPE_IO_STREAM, GIOStream))
 #define G_IS_IO_STREAM(inst)                                (G_TYPE_CHECK_INSTANCE_TYPE ((inst),                     \
                                                              G_TYPE_IO_STREAM))
-#define G_IO_STREAM_GET_IFACE(inst)                         (G_TYPE_INSTANCE_GET_INTERFACE ((inst),                  \
+#define G_IO_STREAM_GET_INTERFACE(inst)                     (G_TYPE_INSTANCE_GET_INTERFACE ((inst),                  \
                                                              G_TYPE_IO_STREAM, GIOStreamIface))
 
 typedef struct _GIOStreamIface                              GIOStreamIface;
@@ -37,6 +37,9 @@ typedef struct _GIOStream                                   GIOStream;
 struct _GIOStreamIface
 {
   GTypeInterface g_iface;
+
+  GInputStream *  (*get_input_stream)  (GIOStream *io_stream);
+  GOutputStream * (*get_output_stream) (GIOStream *io_stream);
 };
 
 GType                   g_io_stream_get_type                            (void);
diff --git a/gio/gsocketconnection.c b/gio/gsocketconnection.c
index 4f17873..729c246 100644
--- a/gio/gsocketconnection.c
+++ b/gio/gsocketconnection.c
@@ -29,9 +29,11 @@
 #include "gsocketinputstream.h"
 #include "giostream.h"
 
+static void g_socket_connection_iface_init (GIOStreamIface *iface);
 G_DEFINE_TYPE_WITH_CODE (GSocketConnection,
                          g_socket_connection, G_TYPE_OBJECT,
-  G_IMPLEMENT_INTERFACE (G_TYPE_IO_STREAM, NULL));
+  G_IMPLEMENT_INTERFACE (G_TYPE_IO_STREAM,
+                         g_socket_connection_iface_init));
 
 enum
 {
@@ -48,6 +50,30 @@ struct _GSocketConnectionPrivate
   GSocketOutputStream *output_stream;
 };
 
+static GInputStream *
+g_socket_connection_get_input_stream (GIOStream *io_stream)
+{
+  GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
+
+  if (connection->priv->input_stream == NULL)
+    connection->priv->input_stream =
+      _g_socket_input_stream_new (connection->priv->socket);
+
+  return g_object_ref (connection->priv->input_stream);
+}
+
+static GOutputStream *
+g_socket_connection_get_output_stream (GIOStream *io_stream)
+{
+  GSocketConnection *connection = G_SOCKET_CONNECTION (io_stream);
+
+  if (connection->priv->output_stream == NULL)
+    connection->priv->output_stream =
+      _g_socket_output_stream_new (connection->priv->socket);
+
+  return g_object_ref (connection->priv->output_stream);
+}
+
 static void
 g_socket_connection_get_property (GObject *object, guint prop_id,
                                   GValue *value, GParamSpec *pspec)
@@ -61,19 +87,13 @@ g_socket_connection_get_property (GObject *object, guint prop_id,
       break;
 
      case PROP_INPUT_STREAM:
-      if (connection->priv->input_stream == NULL)
-        connection->priv->input_stream =
-          _g_socket_input_stream_new (connection->priv->socket);
-
-      g_value_set_object (value, connection->priv->input_stream);
+      g_value_take_object (value,
+        g_socket_connection_get_input_stream (G_IO_STREAM (connection)));
       break;
 
      case PROP_OUTPUT_STREAM:
-      if (connection->priv->output_stream == NULL)
-        connection->priv->output_stream =
-          _g_socket_output_stream_new (connection->priv->socket);
-
-      g_value_set_object (value, connection->priv->output_stream);
+      g_value_take_object (value,
+        g_socket_connection_get_output_stream (G_IO_STREAM (connection)));
       break;
 
      default:
@@ -143,19 +163,10 @@ g_socket_connection_class_init (GSocketConnectionClass *klass)
                          G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
                          G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
 
-  g_object_class_install_property (gobject_class, PROP_INPUT_STREAM,
-    g_param_spec_object ("input-stream", "input stream",
-                         "the GSocketInputStream for reading from this socket",
-                         G_TYPE_INPUT_STREAM, G_PARAM_READABLE |
-                         G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB |
-                         G_PARAM_STATIC_NICK));
-
-  g_object_class_install_property (gobject_class, PROP_OUTPUT_STREAM,
-    g_param_spec_object ("output-stream", "output stream",
-                         "the GSocketOutputStream for writing to this socket",
-                         G_TYPE_OUTPUT_STREAM, G_PARAM_READABLE |
-                         G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB |
-                         G_PARAM_STATIC_NICK));
+  g_object_class_override_property (gobject_class, PROP_INPUT_STREAM,
+                                    "input-stream");
+  g_object_class_override_property (gobject_class, PROP_OUTPUT_STREAM,
+                                    "output-stream");
 }
 
 static void
@@ -173,3 +184,10 @@ g_socket_connection_close (GSocketConnection *connection)
 
   g_socket_close (connection->priv->socket);
 }
+
+static void
+g_socket_connection_iface_init (GIOStreamIface *iface)
+{
+  iface->get_input_stream = g_socket_connection_get_input_stream;
+  iface->get_output_stream = g_socket_connection_get_output_stream;
+}
diff --git a/gio/gtls.c b/gio/gtls.c
index 6ddfe9a..3eb910f 100644
--- a/gio/gtls.c
+++ b/gio/gtls.c
@@ -140,8 +140,9 @@ struct OPAQUE_TYPE__GTLSConnection
 
 static GType g_tls_input_stream_get_type (void);
 static GType g_tls_output_stream_get_type (void);
+static void g_tls_connection_iface_init (GIOStreamIface *iface);
 G_DEFINE_TYPE_WITH_CODE (GTLSConnection, g_tls_connection, G_TYPE_OBJECT,
-  G_IMPLEMENT_INTERFACE (G_TYPE_IO_STREAM, NULL));
+  G_IMPLEMENT_INTERFACE (G_TYPE_IO_STREAM, g_tls_connection_iface_init));
 G_DEFINE_TYPE (GTLSSession, g_tls_session, G_TYPE_OBJECT);
 G_DEFINE_TYPE (GTLSInputStream, g_tls_input_stream, G_TYPE_INPUT_STREAM);
 G_DEFINE_TYPE (GTLSOutputStream, g_tls_output_stream, G_TYPE_OUTPUT_STREAM);
@@ -980,6 +981,32 @@ g_tls_connection_set_property (GObject *object, guint prop_id,
     }
 }
 
+static GInputStream *
+g_tls_connection_get_input_stream (GIOStream *io_stream)
+{
+  GTLSConnection *connection = G_TLS_CONNECTION (io_stream);
+
+  if (connection->input == NULL)
+    connection->input = g_object_new (G_TYPE_TLS_INPUT_STREAM,
+                                      "session", connection->session,
+                                      NULL);
+
+  return g_object_ref (connection->input);
+}
+
+static GOutputStream *
+g_tls_connection_get_output_stream (GIOStream *io_stream)
+{
+  GTLSConnection *connection = G_TLS_CONNECTION (io_stream);
+
+  if (connection->output == NULL)
+    connection->output = g_object_new (G_TYPE_TLS_OUTPUT_STREAM,
+                                       "session", connection->session,
+                                       NULL);
+
+  return g_object_ref (connection->output);
+}
+
 static void
 g_tls_connection_get_property (GObject *object, guint prop_id,
                                GValue *value, GParamSpec *pspec)
@@ -989,19 +1016,13 @@ g_tls_connection_get_property (GObject *object, guint prop_id,
   switch (prop_id)
     {
      case PROP_C_INPUT:
-      if (connection->input == NULL)
-        connection->input = g_object_new (G_TYPE_TLS_INPUT_STREAM,
-                                          "session", connection->session,
-                                          NULL);
-      g_value_set_object (value, connection->input);
+      g_value_take_object (value,
+        g_tls_connection_get_input_stream (G_IO_STREAM (connection)));
       break;
 
      case PROP_C_OUTPUT:
-      if (connection->output == NULL)
-        connection->output = g_object_new (G_TYPE_TLS_OUTPUT_STREAM,
-                                           "session", connection->session,
-                                           NULL);
-      g_value_set_object (value, connection->output);
+      g_value_take_object (value,
+        g_tls_connection_get_output_stream (G_IO_STREAM (connection)));
       break;
 
      default:
@@ -1049,19 +1070,8 @@ g_tls_connection_class_init (GObjectClass *class)
                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME |
                          G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
 
-  g_object_class_install_property (class, PROP_C_INPUT,
-    g_param_spec_object ("input-stream", "Input stream",
-                         "the GInputStream to read from",
-                         G_TYPE_INPUT_STREAM,
-                         G_PARAM_READABLE | G_PARAM_STATIC_NAME |
-                         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
-
-  g_object_class_install_property (class, PROP_C_OUTPUT,
-    g_param_spec_object ("output-stream", "Output stream",
-                         "the GOutputStream to write to",
-                         G_TYPE_OUTPUT_STREAM,
-                         G_PARAM_READABLE | G_PARAM_STATIC_NAME |
-                         G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
+  g_object_class_override_property (class, PROP_C_INPUT, "input-stream");
+  g_object_class_override_property (class, PROP_C_OUTPUT, "output-stream");
 }
 
 GTLSSession *
@@ -1069,3 +1079,10 @@ g_tls_session_new (GIOStream *stream)
 {
   return g_object_new (G_TYPE_TLS_SESSION, "base-stream", stream, NULL);
 }
+
+static void
+g_tls_connection_iface_init (GIOStreamIface *iface)
+{
+  iface->get_input_stream = g_tls_connection_get_input_stream;
+  iface->get_output_stream = g_tls_connection_get_output_stream;
+}



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