2 * Copyright © 2014 NICE s.r.l.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Authors: Ignacio Casal Quinteiro <ignacio.casal@nice-software.com>
25 #include "gsimpleiostream.h"
29 * SECTION:gsimpleiostream
30 * @short_description: A wrapper around an input and an output stream.
32 * @see_also: #GIOStream
34 * GSimpleIOStream creates a #GIOStream from an arbitrary #GInputStream and
35 * #GOutputStream. This allows any pair of input and output streams to be used
36 * with #GIOStream methods.
38 * This is useful when you obtained a #GInputStream and a #GOutputStream
39 * by other means, for instance creating them with platform specific methods as
40 * g_unix_input_stream_new() or g_win32_input_stream_new(), and you want
41 * to take advantage of the methods provided by #GIOStream.
49 * A wrapper around a #GInputStream and a #GOutputStream.
53 struct _GSimpleIOStream
57 GInputStream
*input_stream
;
58 GOutputStream
*output_stream
;
61 struct _GSimpleIOStreamClass
63 GIOStreamClass parent
;
66 typedef struct _GSimpleIOStreamClass GSimpleIOStreamClass
;
75 G_DEFINE_TYPE (GSimpleIOStream
, g_simple_io_stream
, G_TYPE_IO_STREAM
)
78 g_simple_io_stream_finalize (GObject
*object
)
80 GSimpleIOStream
*stream
= G_SIMPLE_IO_STREAM (object
);
82 if (stream
->input_stream
)
83 g_object_unref (stream
->input_stream
);
85 if (stream
->output_stream
)
86 g_object_unref (stream
->output_stream
);
88 G_OBJECT_CLASS (g_simple_io_stream_parent_class
)->finalize (object
);
92 g_simple_io_stream_set_property (GObject
*object
,
97 GSimpleIOStream
*stream
= G_SIMPLE_IO_STREAM (object
);
101 case PROP_INPUT_STREAM
:
102 stream
->input_stream
= g_value_dup_object (value
);
105 case PROP_OUTPUT_STREAM
:
106 stream
->output_stream
= g_value_dup_object (value
);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
116 g_simple_io_stream_get_property (GObject
*object
,
121 GSimpleIOStream
*stream
= G_SIMPLE_IO_STREAM (object
);
125 case PROP_INPUT_STREAM
:
126 g_value_set_object (value
, stream
->input_stream
);
129 case PROP_OUTPUT_STREAM
:
130 g_value_set_object (value
, stream
->output_stream
);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
139 static GInputStream
*
140 g_simple_io_stream_get_input_stream (GIOStream
*stream
)
142 GSimpleIOStream
*simple_stream
= G_SIMPLE_IO_STREAM (stream
);
144 return simple_stream
->input_stream
;
147 static GOutputStream
*
148 g_simple_io_stream_get_output_stream (GIOStream
*stream
)
150 GSimpleIOStream
*simple_stream
= G_SIMPLE_IO_STREAM (stream
);
152 return simple_stream
->output_stream
;
156 g_simple_io_stream_class_init (GSimpleIOStreamClass
*class)
158 GObjectClass
*gobject_class
= G_OBJECT_CLASS (class);
159 GIOStreamClass
*io_class
= G_IO_STREAM_CLASS (class);
161 gobject_class
->finalize
= g_simple_io_stream_finalize
;
162 gobject_class
->get_property
= g_simple_io_stream_get_property
;
163 gobject_class
->set_property
= g_simple_io_stream_set_property
;
165 io_class
->get_input_stream
= g_simple_io_stream_get_input_stream
;
166 io_class
->get_output_stream
= g_simple_io_stream_get_output_stream
;
169 * GSimpleIOStream:input-stream:
173 g_object_class_install_property (gobject_class
, PROP_INPUT_STREAM
,
174 g_param_spec_object ("input-stream",
176 P_("The GInputStream to read from"),
179 G_PARAM_STATIC_STRINGS
|
180 G_PARAM_CONSTRUCT_ONLY
));
183 * GSimpleIOStream:output-stream:
187 g_object_class_install_property (gobject_class
, PROP_OUTPUT_STREAM
,
188 g_param_spec_object ("output-stream",
190 P_("The GOutputStream to write to"),
191 G_TYPE_OUTPUT_STREAM
,
193 G_PARAM_STATIC_STRINGS
|
194 G_PARAM_CONSTRUCT_ONLY
));
198 g_simple_io_stream_init (GSimpleIOStream
*stream
)
203 * g_simple_io_stream_new:
204 * @input_stream: a #GInputStream.
205 * @output_stream: a #GOutputStream.
207 * Creates a new #GSimpleIOStream wrapping @input_stream and @output_stream.
208 * See also #GIOStream.
210 * Returns: a new #GSimpleIOStream instance.
215 g_simple_io_stream_new (GInputStream
*input_stream
,
216 GOutputStream
*output_stream
)
218 return g_object_new (G_TYPE_SIMPLE_IO_STREAM
,
219 "input-stream", input_stream
,
220 "output-stream", output_stream
,