00001 /******************************************************************************* 00002 00003 @file IWriter.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, March 2004 00034 @author Kris 00035 @author Ivan Senji (the "alias put" idea) 00036 00037 00038 *******************************************************************************/ 00039 00040 module mango.io.model.IWriter; 00041 00042 public import mango.io.model.IBuffer; 00043 00044 /******************************************************************************* 00045 00046 *******************************************************************************/ 00047 00048 typedef void delegate(void* dst, uint count) BufferEncoder; 00049 00050 /******************************************************************************* 00051 00052 Interface to make any class compatible with any IWriter. 00053 00054 *******************************************************************************/ 00055 00056 interface IWritable 00057 { 00058 void write (IWriter input); 00059 } 00060 00061 /******************************************************************************* 00062 00063 Any class implementing IEncoder can be bound to a writer using 00064 the bind() method. 00065 00066 *******************************************************************************/ 00067 00068 interface IEncoder 00069 { 00070 ConverterType type (); 00071 00072 BufferEncoder bind (IWriter output); 00073 } 00074 00075 /******************************************************************************* 00076 00077 Make a signature for IWritable classes to use when they wish to 00078 avoid being processed by decorating writers, such as TextWriter. 00079 00080 *******************************************************************************/ 00081 00082 interface IPhantomWriter : IWritable 00083 { 00084 } 00085 00086 /******************************************************************************* 00087 00088 Make a signature for Newline classes 00089 00090 *******************************************************************************/ 00091 00092 interface INewlineWriter : IPhantomWriter 00093 { 00094 } 00095 00096 /******************************************************************************* 00097 00098 IWriter interface. IWriter provides the means to append formatted 00099 data to an IBuffer, and exposes a convenient method of handling a 00100 variety of data types. In addition to writing native types such 00101 as integer and char[], writers also process any class which has 00102 implemented the IWritable interface (one method). 00103 00104 *******************************************************************************/ 00105 00106 interface IWriter 00107 { 00108 alias put opShl; 00109 alias put opCall; 00110 00111 /*********************************************************************** 00112 00113 These are the basic writer methods 00114 00115 ***********************************************************************/ 00116 00117 IWriter put (bool x); 00118 IWriter put (ubyte x); 00119 IWriter put (byte x); 00120 IWriter put (ushort x); 00121 IWriter put (short x); 00122 IWriter put (uint x); 00123 IWriter put (int x); 00124 IWriter put (ulong x); 00125 IWriter put (long x); 00126 IWriter put (float x); 00127 IWriter put (double x); 00128 IWriter put (real x); 00129 IWriter put (char x); 00130 IWriter put (wchar x); 00131 IWriter put (dchar x); 00132 00133 IWriter put (byte[] x); 00134 IWriter put (short[] x); 00135 IWriter put (int[] x); 00136 IWriter put (long[] x); 00137 IWriter put (ubyte[] x); 00138 IWriter put (ushort[] x); 00139 IWriter put (uint[] x); 00140 IWriter put (ulong[] x); 00141 IWriter put (float[] x); 00142 IWriter put (double[] x); 00143 IWriter put (real[] x); 00144 IWriter put (char[] x); 00145 00146 // these two will have to wait until Walter fixes 00147 // the string-literal conversion fiasco ... 00148 IWriter putw (wchar[] x); 00149 IWriter putd (dchar[] x); 00150 00151 /*********************************************************************** 00152 00153 This is the mechanism used for binding arbitrary classes 00154 to the IO system. If a class implements IWritable, it can 00155 be used as a target for IWriter put() operations. That is, 00156 implementing IWritable is intended to transform any class 00157 into an IWriter adaptor for the content held therein. 00158 00159 ***********************************************************************/ 00160 00161 IWriter put (IWritable x); 00162 00163 /*********************************************************************** 00164 00165 Bind an IEncoder to the writer. Encoders are intended to 00166 be used as a conversion mechanism between various character 00167 representations (encodings), or the translation of any data 00168 type from one representation to another. Each data type may 00169 be configured with a distinct encoder, covering all native 00170 types (15 in total). 00171 00172 An appropriate encoder set should be attached to each 00173 IWriter, and thus be available for subsequent use. A raw 00174 binary implementation is attached by default (no encoding). 00175 00176 See module mango.icu.UMango for an example of encoder 00177 implementation -- those classes bind the ICU converters 00178 to this IO package. 00179 00180 ***********************************************************************/ 00181 00182 void setEncoder (IEncoder e); 00183 00184 /*********************************************************************** 00185 00186 Output a newline. Do this indirectly so that it can be 00187 intercepted by subclasses. 00188 00189 ***********************************************************************/ 00190 00191 IWriter cr (); 00192 00193 /*********************************************************************** 00194 00195 Flush the output of this writer. Throws an IOException 00196 if the operation fails. 00197 00198 ***********************************************************************/ 00199 00200 IWriter put (); 00201 IWriter flush (); 00202 00203 /*********************************************************************** 00204 00205 Return the associated buffer 00206 00207 ***********************************************************************/ 00208 00209 IBuffer getBuffer (); 00210 } 00211