1 | /* Simple I/O stream. This is a utility class for tests, not a test. |
2 | * |
3 | * Copyright © 2008-2010 Red Hat, Inc. |
4 | * Copyright © 2011 Nokia Corporation |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General |
17 | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | * |
19 | * Author: David Zeuthen <davidz@redhat.com> |
20 | * Author: Simon McVittie <simon.mcvittie@collabora.co.uk> |
21 | */ |
22 | |
23 | #include <gio/gio.h> |
24 | |
25 | #include "test-io-stream.h" |
26 | |
27 | G_DEFINE_TYPE (TestIOStream, test_io_stream, G_TYPE_IO_STREAM) |
28 | |
29 | static void |
30 | test_io_stream_finalize (GObject *object) |
31 | { |
32 | TestIOStream *stream = TEST_IO_STREAM (object); |
33 | |
34 | /* strictly speaking we should unref these in dispose, but |
35 | * g_io_stream_dispose() wants them to still exist |
36 | */ |
37 | g_clear_object (&stream->input_stream); |
38 | g_clear_object (&stream->output_stream); |
39 | |
40 | G_OBJECT_CLASS (test_io_stream_parent_class)->finalize (object); |
41 | } |
42 | |
43 | static void |
44 | test_io_stream_init (TestIOStream *stream) |
45 | { |
46 | } |
47 | |
48 | static GInputStream * |
49 | test_io_stream_get_input_stream (GIOStream *_stream) |
50 | { |
51 | TestIOStream *stream = TEST_IO_STREAM (_stream); |
52 | |
53 | return stream->input_stream; |
54 | } |
55 | |
56 | static GOutputStream * |
57 | test_io_stream_get_output_stream (GIOStream *_stream) |
58 | { |
59 | TestIOStream *stream = TEST_IO_STREAM (_stream); |
60 | |
61 | return stream->output_stream; |
62 | } |
63 | |
64 | static void |
65 | test_io_stream_class_init (TestIOStreamClass *klass) |
66 | { |
67 | GObjectClass *gobject_class; |
68 | GIOStreamClass *giostream_class; |
69 | |
70 | gobject_class = G_OBJECT_CLASS (klass); |
71 | gobject_class->finalize = test_io_stream_finalize; |
72 | |
73 | giostream_class = G_IO_STREAM_CLASS (klass); |
74 | giostream_class->get_input_stream = test_io_stream_get_input_stream; |
75 | giostream_class->get_output_stream = test_io_stream_get_output_stream; |
76 | } |
77 | |
78 | /** |
79 | * test_io_stream_new: |
80 | * @input_stream: an input stream |
81 | * @output_stream: an output stream |
82 | * |
83 | * Return a simple #GIOStream binding together @input_stream and |
84 | * @output_stream. They have no additional semantics as a result of being |
85 | * part of this I/O stream: in particular, closing one does not close |
86 | * the other (although closing the #GIOStream will close both sub-streams). |
87 | * |
88 | * Returns: (transfer full): a new #GIOStream |
89 | */ |
90 | GIOStream * |
91 | test_io_stream_new (GInputStream *input_stream, |
92 | GOutputStream *output_stream) |
93 | { |
94 | TestIOStream *stream; |
95 | |
96 | g_return_val_if_fail (G_IS_INPUT_STREAM (input_stream), NULL); |
97 | g_return_val_if_fail (G_IS_OUTPUT_STREAM (output_stream), NULL); |
98 | stream = TEST_IO_STREAM (g_object_new (TEST_TYPE_IO_STREAM, NULL)); |
99 | stream->input_stream = g_object_ref (input_stream); |
100 | stream->output_stream = g_object_ref (output_stream); |
101 | return G_IO_STREAM (stream); |
102 | } |
103 | |