132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Mar 2004: Initial release
|
|
8 Dec 2006: Outback release
|
|
9
|
|
10 author: Kris
|
|
11 Ivan Senji (the "alias put" idea)
|
|
12
|
|
13 *******************************************************************************/
|
|
14
|
|
15 module tango.io.protocol.model.IWriter;
|
|
16
|
|
17 public import tango.io.model.IBuffer;
|
|
18
|
|
19 /*******************************************************************************
|
|
20
|
|
21 IWriter interface. Writers provide the means to append formatted
|
|
22 data to an IBuffer, and expose a convenient method of handling a
|
|
23 variety of data types. In addition to writing native types such
|
|
24 as integer and char[], writers also process any class which has
|
|
25 implemented the IWritable interface (one method).
|
|
26
|
|
27 All writers support the full set of native data types, plus their
|
|
28 fundamental array variants. Operations may be chained back-to-back.
|
|
29
|
|
30 Writers support a Java-esque put() notation. However, the Tango style
|
|
31 is to place IO elements within their own parenthesis, like so:
|
|
32
|
|
33 ---
|
|
34 write (count) (" green bottles");
|
|
35 ---
|
|
36
|
|
37 Note that each written element is distict; this style is affectionately
|
|
38 known as "whisper". The code below illustrates basic operation upon a
|
|
39 memory buffer:
|
|
40
|
|
41 ---
|
|
42 auto buf = new Buffer (256);
|
|
43
|
|
44 // map same buffer into both reader and writer
|
|
45 auto read = new Reader (buf);
|
|
46 auto write = new Writer (buf);
|
|
47
|
|
48 int i = 10;
|
|
49 long j = 20;
|
|
50 double d = 3.14159;
|
|
51 char[] c = "fred";
|
|
52
|
|
53 // write data types out
|
|
54 write (c) (i) (j) (d);
|
|
55
|
|
56 // read them back again
|
|
57 read (c) (i) (j) (d);
|
|
58
|
|
59
|
|
60 // same thing again, but using put() syntax instead
|
|
61 write.put(c).put(i).put(j).put(d);
|
|
62 read.get(c).get(i).get(j).get(d);
|
|
63
|
|
64 ---
|
|
65
|
|
66 Writers may also be used with any class implementing the IWritable
|
|
67 interface, along with any struct implementing an equivalent function.
|
|
68
|
|
69 *******************************************************************************/
|
|
70
|
|
71 abstract class IWriter // could be an interface, but that causes poor codegen
|
|
72 {
|
|
73 alias put opCall;
|
|
74
|
|
75 /***********************************************************************
|
|
76
|
|
77 These are the basic writer methods
|
|
78
|
|
79 ***********************************************************************/
|
|
80
|
|
81 abstract IWriter put (bool x);
|
|
82 abstract IWriter put (ubyte x); ///ditto
|
|
83 abstract IWriter put (byte x); ///ditto
|
|
84 abstract IWriter put (ushort x); ///ditto
|
|
85 abstract IWriter put (short x); ///ditto
|
|
86 abstract IWriter put (uint x); ///ditto
|
|
87 abstract IWriter put (int x); ///ditto
|
|
88 abstract IWriter put (ulong x); ///ditto
|
|
89 abstract IWriter put (long x); ///ditto
|
|
90 abstract IWriter put (float x); ///ditto
|
|
91 abstract IWriter put (double x); ///ditto
|
|
92 abstract IWriter put (real x); ///ditto
|
|
93 abstract IWriter put (char x); ///ditto
|
|
94 abstract IWriter put (wchar x); ///ditto
|
|
95 abstract IWriter put (dchar x); ///ditto
|
|
96
|
|
97 abstract IWriter put (bool[] x);
|
|
98 abstract IWriter put (byte[] x); ///ditto
|
|
99 abstract IWriter put (short[] x); ///ditto
|
|
100 abstract IWriter put (int[] x); ///ditto
|
|
101 abstract IWriter put (long[] x); ///ditto
|
|
102 abstract IWriter put (ubyte[] x); ///ditto
|
|
103 abstract IWriter put (ushort[] x); ///ditto
|
|
104 abstract IWriter put (uint[] x); ///ditto
|
|
105 abstract IWriter put (ulong[] x); ///ditto
|
|
106 abstract IWriter put (float[] x); ///ditto
|
|
107 abstract IWriter put (double[] x); ///ditto
|
|
108 abstract IWriter put (real[] x); ///ditto
|
|
109 abstract IWriter put (char[] x); ///ditto
|
|
110 abstract IWriter put (wchar[] x); ///ditto
|
|
111 abstract IWriter put (dchar[] x); ///ditto
|
|
112
|
|
113 /***********************************************************************
|
|
114
|
|
115 This is the mechanism used for binding arbitrary classes
|
|
116 to the IO system. If a class implements IWritable, it can
|
|
117 be used as a target for IWriter put() operations. That is,
|
|
118 implementing IWritable is intended to transform any class
|
|
119 into an IWriter adaptor for the content held therein
|
|
120
|
|
121 ***********************************************************************/
|
|
122
|
|
123 abstract IWriter put (IWritable);
|
|
124
|
|
125 alias void delegate (IWriter) Closure;
|
|
126
|
|
127 abstract IWriter put (Closure);
|
|
128
|
|
129 /***********************************************************************
|
|
130
|
|
131 Emit a newline
|
|
132
|
|
133 ***********************************************************************/
|
|
134
|
|
135 abstract IWriter newline ();
|
|
136
|
|
137 /***********************************************************************
|
|
138
|
|
139 Flush the output of this writer. Throws an IOException
|
|
140 if the operation fails. These are aliases for each other
|
|
141
|
|
142 ***********************************************************************/
|
|
143
|
|
144 abstract IWriter flush ();
|
|
145 abstract IWriter put (); ///ditto
|
|
146
|
|
147 /***********************************************************************
|
|
148
|
|
149 Return the associated buffer
|
|
150
|
|
151 ***********************************************************************/
|
|
152
|
|
153 abstract IBuffer buffer ();
|
|
154 }
|
|
155
|
|
156
|
|
157 /*******************************************************************************
|
|
158
|
|
159 Interface to make any class compatible with any IWriter
|
|
160
|
|
161 *******************************************************************************/
|
|
162
|
|
163 interface IWritable
|
|
164 {
|
|
165 abstract void write (IWriter input);
|
|
166 }
|
|
167
|