132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2007 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: Nov 2007
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 Streams to expose simple native types as discrete elements. I/O
|
|
12 is buffered and should yield fair performance.
|
|
13
|
|
14 *******************************************************************************/
|
|
15
|
|
16 module tango.io.stream.TypedStream;
|
|
17
|
|
18 private import tango.io.Buffer,
|
|
19 tango.io.Conduit;
|
|
20
|
|
21 /*******************************************************************************
|
|
22
|
|
23 Type T is the target or destination type
|
|
24
|
|
25 *******************************************************************************/
|
|
26
|
|
27 class TypedInput(T) : InputFilter, Buffered
|
|
28 {
|
|
29 private IBuffer input;
|
|
30
|
|
31 /***********************************************************************
|
|
32
|
|
33 ***********************************************************************/
|
|
34
|
|
35 this (InputStream stream)
|
|
36 {
|
|
37 super (input = Buffer.share (stream));
|
|
38 }
|
|
39
|
|
40 /***********************************************************************
|
|
41
|
|
42 Buffered interface
|
|
43
|
|
44 ***********************************************************************/
|
|
45
|
|
46 final IBuffer buffer ()
|
|
47 {
|
|
48 return input;
|
|
49 }
|
|
50
|
|
51 /***********************************************************************
|
|
52
|
|
53 Override this to give back a useful chaining reference
|
|
54
|
|
55 ***********************************************************************/
|
|
56
|
|
57 final override TypedInput clear ()
|
|
58 {
|
|
59 host.clear;
|
|
60 return this;
|
|
61 }
|
|
62
|
|
63 /***********************************************************************
|
|
64
|
|
65 Read a value from the stream. Returns false when all
|
|
66 content has been consumed
|
|
67
|
|
68 ***********************************************************************/
|
|
69
|
|
70 final bool read (inout T x)
|
|
71 {
|
|
72 return input.read((&x)[0..1]) is T.sizeof;
|
|
73 }
|
|
74
|
|
75 /***********************************************************************
|
|
76
|
|
77 Iterate over all content
|
|
78
|
|
79 ***********************************************************************/
|
|
80
|
|
81 final int opApply (int delegate(ref T x) dg)
|
|
82 {
|
|
83 T x;
|
|
84 int ret;
|
|
85
|
|
86 while ((input.read((&x)[0..1]) is T.sizeof))
|
|
87 if ((ret = dg (x)) != 0)
|
|
88 break;
|
|
89 return ret;
|
|
90 }
|
|
91 }
|
|
92
|
|
93
|
|
94
|
|
95 /*******************************************************************************
|
|
96
|
|
97 Type T is the target or destination type.
|
|
98
|
|
99 *******************************************************************************/
|
|
100
|
|
101 class TypedOutput (T) : OutputFilter, Buffered
|
|
102 {
|
|
103 private IBuffer output;
|
|
104
|
|
105 /***********************************************************************
|
|
106
|
|
107 ***********************************************************************/
|
|
108
|
|
109 this (OutputStream stream)
|
|
110 {
|
|
111 super (output = Buffer.share (stream));
|
|
112 }
|
|
113
|
|
114 /***********************************************************************
|
|
115
|
|
116 Buffered interface
|
|
117
|
|
118 ***********************************************************************/
|
|
119
|
|
120 final IBuffer buffer ()
|
|
121 {
|
|
122 return output;
|
|
123 }
|
|
124
|
|
125 /***********************************************************************
|
|
126
|
|
127 Append a value to the output stream
|
|
128
|
|
129 ***********************************************************************/
|
|
130
|
|
131 final void write (T x)
|
|
132 {
|
|
133 output.append (&x, T.sizeof);
|
|
134 }
|
|
135 }
|
|
136
|
|
137
|
|
138 /*******************************************************************************
|
|
139
|
|
140 *******************************************************************************/
|
|
141
|
|
142 debug (UnitTest)
|
|
143 {
|
|
144 import tango.io.Stdout;
|
|
145 import tango.io.stream.UtfStream;
|
|
146
|
|
147 unittest
|
|
148 {
|
|
149 auto inp = new TypedInput!(char)(new Buffer("hello world"));
|
|
150 auto oot = new TypedOutput!(char)(new Buffer(20));
|
|
151
|
|
152 foreach (x; inp)
|
|
153 oot.write (x);
|
|
154 assert (oot.buffer.slice == "hello world");
|
|
155
|
|
156 auto xx = new TypedInput!(char)(new UtfInput!(char, dchar)(new Buffer("hello world"d)));
|
|
157 char[] yy;
|
|
158 foreach (x; xx)
|
|
159 yy ~= x;
|
|
160 assert (yy == "hello world");
|
|
161 }
|
|
162 }
|