Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 2490

Show
Ignore:
Timestamp:
08/17/07 12:19:02 (1 year ago)
Author:
kris
Message:

- exposed conduit read & write again, since there's really no harm in doing so
- cleaned up close() mechanics across some conduit instances
- added commit() support to OutputStream? (for ZipFilter?, ChunkingFilter?, etc)
- added a dispose() method to conduit, intended to be used instead of close() in general usage (it flushes and commits the filter chain, closes the conduit, and deletes the instance).
- added a tentative dispose() to buffer, for similar usage
- added a divert() method to Cout, Cerr, and Cin. These should be used instead of hacking the filter-chain for those entities
- removed the interim notify() support from conduit, and from console. It was too brittle for long-term usage
- renamed 'next' to 'host' within the stream filters
- added override to a number of conduit-oriented method decls
- removed some remaining support for closing a resource via dtors

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/example/conduits/FileBucket.d

    r2465 r2490  
    265265 
    266266                        bucket.file.seek (offset); 
    267                         if (bucket.file.input.read (data) != length) 
     267                        if (bucket.file.read (data) != length) 
    268268                            eof (bucket); 
    269269 
     
    290290         
    291291                        // write content 
    292                         if (bucket.file.output.write (data) != length) 
     292                        if (bucket.file.write (data) != length) 
    293293                            eof (bucket); 
    294294                } 
  • trunk/example/conduits/filecat.d

    r2465 r2490  
    1717           // copy each file onto dst 
    1818           foreach (char[] arg; args[2..args.length]) 
    19                     dst.output.copy (new FileConduit(arg)); 
     19                    dst.copy (new FileConduit(arg)); 
    2020 
    2121           // flush output and close 
    22            dst.close; 
     22           dst.dispose; 
    2323           } 
    2424        else 
  • trunk/example/conduits/lineio.d

    r2465 r2490  
    88 
    99        Read a file line-by-line, sending each one to the console. This 
    10         illustrates how to bind a conduit to a text iterator. Iterators 
    11         also support the binding of buffer and string instances. 
    12  
    13         Note that iterators are templated for char, wchar and dchar ~  
    14         this example uses char 
     10        illustrates how to bind a conduit to a stream iterator (iterators 
     11        also support the binding of a buffer). Note that stream iterators 
     12        are templated for char, wchar and dchar types. 
    1513 
    1614*******************************************************************************/ 
     
    2119           { 
    2220           // open a file for reading 
    23            auto file = new FileConduit (args[1]); 
     21           scope file = new FileConduit (args[1]); 
    2422 
    2523           // process file one line at a time 
  • trunk/example/networking/selector.d

    r2465 r2490  
    172172                                log.trace(sprint("[{0}] Receiving message from client", i)); 
    173173 
    174                             count = (cast(SocketConduit) selectionKey.conduit).input.read(buffer); 
     174                            count = (cast(SocketConduit) selectionKey.conduit).read(buffer); 
    175175                            if (count != IConduit.Eof) 
    176176                            { 
     
    199199                            log.trace(sprint("[{0}] Sending PONG to client", i)); 
    200200 
    201                         count = (cast(SocketConduit) selectionKey.conduit).output.write("PONG"); 
     201                        count = (cast(SocketConduit) selectionKey.conduit).write("PONG"); 
    202202                        if (count != IConduit.Eof) 
    203203                        { 
     
    334334                try 
    335335                { 
    336                     count = socket.output.write("PING"); 
     336                    count = socket.write("PING"); 
    337337                    break; 
    338338                } 
     
    355355                    try 
    356356                    { 
    357                         count = socket.input.read(buffer); 
     357                        count = socket.read(buffer); 
    358358                        break; 
    359359                    } 
  • trunk/tango/io/Buffer.d

    r2486 r2490  
    1616private import  tango.core.Exception; 
    1717 
    18 public  import  tango.io.model.IBuffer; 
    19  
    20 public  import  tango.io.model.IConduit; 
     18public  import  tango.io.model.IBuffer, 
     19                tango.io.model.IConduit; 
    2120 
    2221/****************************************************************************** 
     
    199198class Buffer : IBuffer 
    200199{ 
    201         protected IConduit      root;           // optional conduit 
     200        protected IConduit      host;           // optional conduit 
    202201        protected OutputStream  sink;           // optional data sink 
    203202        protected InputStream   source;         // optional data source 
     
    10021001        ***********************************************************************/ 
    10031002 
    1004         void flush () 
     1003        override void flush () 
    10051004        { 
    10061005                if (sink) 
     
    10181017                Clear buffer content 
    10191018 
    1020                 Returns: 
    1021                 the buffer instance 
    1022  
    10231019                Remarks: 
    10241020                Reset 'position' and 'limit' to zero. This effectively  
     
    10271023        ***********************************************************************/ 
    10281024 
    1029         void clear () 
     1025        override void clear () 
    10301026        { 
    10311027                index = extent = 0; 
     
    10341030                if (source) 
    10351031                    source.clear; 
    1036  
    1037                 return this; 
    10381032        }                
    10391033 
    10401034        /*********************************************************************** 
    10411035         
    1042                 Truncate buffer content 
    1043  
    1044                 Remarks: 
    1045                 Truncate the buffer within its extent. Returns true if 
    1046                 the new 'extent' is valid, false otherwise. 
    1047  
    1048         ***********************************************************************/ 
    1049  
    1050         bool truncate (uint extent) 
    1051         { 
    1052                 if (extent <= data.length) 
    1053                    { 
    1054                    extent = extent; 
    1055                    return true; 
    1056                    } 
    1057                 return false; 
    1058         }                
     1036                Commit the output 
     1037 
     1038                Remarks: 
     1039                Propogate request to an attached OutputStream (this is a 
     1040                requirment for the OutputStream interface) 
     1041 
     1042        ***********************************************************************/ 
     1043 
     1044        final override void commit ()  
     1045        { 
     1046                if (sink) 
     1047                    sink.commit; 
     1048        } 
    10591049 
    10601050        /*********************************************************************** 
     
    10731063        ***********************************************************************/ 
    10741064 
    1075         OutputStream copy (InputStream src) 
     1065        override OutputStream copy (InputStream src) 
    10761066        { 
    10771067                assert (sink && src); 
     
    10861076        /*********************************************************************** 
    10871077         
     1078                Truncate buffer content 
     1079 
     1080                Remarks: 
     1081                Truncate the buffer within its extent. Returns true if 
     1082                the new length is valid, false otherwise. 
     1083 
     1084        ***********************************************************************/ 
     1085 
     1086        bool truncate (uint length) 
     1087        { 
     1088                if (length <= data.length) 
     1089                   { 
     1090                   extent = length; 
     1091                   return true; 
     1092                   } 
     1093                return false; 
     1094        }                
     1095 
     1096        /*********************************************************************** 
     1097         
    10881098                Access buffer limit 
    10891099 
     
    11601170        ***********************************************************************/ 
    11611171 
    1162         IConduit conduit () 
    1163         { 
    1164                 return root; 
     1172        override IConduit conduit () 
     1173        { 
     1174                return host; 
    11651175        } 
    11661176 
     
    11831193        IBuffer setConduit (IConduit conduit) 
    11841194        { 
    1185                 root = conduit; 
     1195//                setHost (host = conduit); 
     1196                host = conduit; 
    11861197                sink = conduit.output; 
    11871198                source = conduit.input; 
    11881199                return this; 
     1200        } 
     1201 
     1202        /*********************************************************************** 
     1203 
     1204                Dispose of this buffer 
     1205                 
     1206                Remarks: 
     1207                Dispose flushes & commits itself, closes any associated  
     1208                conduit, and deletes both that and the buffer instance.  
     1209 
     1210        ***********************************************************************/ 
     1211 
     1212        final void dispose () 
     1213        { 
     1214                if (sink) 
     1215                   { 
     1216                   flush; 
     1217                   commit; 
     1218                   sink.conduit.dispose (false);  
     1219                   } 
     1220                delete this; 
    11891221        } 
    11901222 
  • trunk/tango/io/Conduit.d

    r2434 r2490  
    3737*******************************************************************************/ 
    3838 
    39 class Conduit : IConduit, InputStream, OutputStream 
     39class Conduit : IConduit 
    4040{ 
    41         private InputStream             input_; 
    42         private OutputStream            output_; 
    43         private void delegate(bool)     notify_; 
     41        private OutputStream    sink; 
     42        private InputStream     source; 
    4443 
    4544        /*********************************************************************** 
     
    7271        ***********************************************************************/ 
    7372 
    74         abstract protected uint read (void[] dst); 
     73        abstract uint read (void[] dst); 
    7574 
    7675        /*********************************************************************** 
     
    8483        ***********************************************************************/ 
    8584 
    86         abstract protected uint write (void [] src); 
    87  
    88         /*********************************************************************** 
    89  
    90                 default ctor assigns input and output streams to their 
    91                 initial value (this conduit) 
     85        abstract uint write (void [] src); 
     86 
     87        /*********************************************************************** 
     88 
     89                Close this conduit 
     90 
     91        ***********************************************************************/ 
     92 
     93        abstract void close (); 
     94 
     95        /*********************************************************************** 
     96         
     97                Constructor to initialize the default sink & source 
    9298 
    9399        ***********************************************************************/ 
     
    95101        this () 
    96102        { 
    97                 input_ = this; 
    98                 output_ = this; 
    99  
    100                 // assign a default notification handler that does nothing 
    101                 notify_ = (bool){return;}; 
    102         } 
    103  
    104         /*********************************************************************** 
    105          
    106                 Return the host conduit. This is part of the Stream interface 
     103                sink = this; 
     104                source = this; 
     105        } 
     106 
     107        /*********************************************************************** 
     108 
     109                Is the conduit alive? Default behaviour returns true 
     110 
     111        ***********************************************************************/ 
     112 
     113        bool isAlive () 
     114        { 
     115                return true; 
     116        } 
     117 
     118        /*********************************************************************** 
     119         
     120                Return the host. This is part of the Stream interface 
    107121 
    108122        ***********************************************************************/ 
     
    115129        /*********************************************************************** 
    116130 
    117                 Is the conduit alive? Default behaviour returns true 
    118  
    119         ***********************************************************************/ 
    120  
    121         bool isAlive () 
    122         { 
    123                 return true; 
    124         } 
    125  
    126         /*********************************************************************** 
    127  
    128                 dump any output buffering 
    129  
    130         ***********************************************************************/ 
    131  
    132         void flush () 
    133         { 
    134         } 
    135  
    136         /*********************************************************************** 
    137  
    138                 clear any input buffering 
    139  
    140         ***********************************************************************/ 
    141  
    142         void clear () 
    143         { 
    144         } 
    145  
    146         /*********************************************************************** 
    147  
    148                 Close this conduit 
    149  
    150         ***********************************************************************/ 
    151  
    152         void close () 
    153         { 
     131                clear any buffered input 
     132 
     133        ***********************************************************************/ 
     134 
     135        void clear () {} 
     136 
     137        /*********************************************************************** 
     138 
     139                Write buffered output 
     140 
     141        ***********************************************************************/ 
     142 
     143        void flush () {} 
     144 
     145        /*********************************************************************** 
     146 
     147                commit the output 
     148 
     149        ***********************************************************************/ 
     150 
     151        final void commit () {} 
     152 
     153        /*********************************************************************** 
     154 
     155                Dispose of this conduit 
     156                 
     157                Remarks: 
     158                Dispose flushes & commits any filters, closes the conduit,  
     159                and deletes it. This should be used in preference to close() 
     160 
     161        ***********************************************************************/ 
     162 
     163        final void dispose (bool clean=true) 
     164        { 
     165                if (clean) 
     166                   { 
     167                   sink.flush; 
     168                   sink.commit; 
     169                   } 
     170 
     171                this.close; 
     172                delete this; 
    154173        } 
    155174 
     
    158177                Return the current input stream. The initial input stream 
    159178                is hosted by the conduit itself. Subsequent attachment of 
    160                 stream 'filters' will alter this value. 
     179                stream filters will alter this value. 
    161180 
    162181        ***********************************************************************/ 
     
    164183        final InputStream input () 
    165184        { 
    166                 return input_
     185                return source
    167186        } 
    168187 
     
    171190                Return the current output stream. The initial output stream 
    172191                is hosted by the conduit itself. Subsequent attachment of 
    173                 stream 'filters' will alter this value. 
     192                stream filters will alter this value. 
    174193 
    175194        ***********************************************************************/ 
     
    177196        final OutputStream output () 
    178197        { 
    179                 return output_; 
    180         } 
    181  
    182         /*********************************************************************** 
    183  
    184                 Replace the input stream and return the prior one. The 
    185                 return value should be used as the 'ancestor' attribute 
    186                 of attached filter, to be invoked appropriately during 
    187                 subsequent filter activity. However, it is entirely up 
    188                 to the intercepting stream to decide whether to follow 
    189                 that recommendation or not. 
    190  
    191         ***********************************************************************/ 
    192  
    193         final InputStream attach (InputStream filter) 
    194         { 
    195                 auto tmp = input_; 
    196                 input_ = filter; 
    197                 notify_ (true); 
    198                 return tmp; 
    199         } 
    200  
    201         /*********************************************************************** 
    202  
    203                 Replace the output stream and return the prior one. The 
    204                 return value should be used as the 'ancestor' attribute 
    205                 of attached filter, to be invoked appropriately during 
    206                 subsequent filter activity. However, it is entirely up 
    207                 to the intercepting stream to decide whether to follow 
    208                 that recommendation or not. 
    209  
    210         ***********************************************************************/ 
    211  
    212         final OutputStream attach (OutputStream filter) 
    213         { 
    214                 auto tmp = output_; 
    215                 output_ = filter; 
    216                 notify_ (false); 
    217                 return tmp; 
    218         } 
    219  
    220         /*********************************************************************** 
    221          
    222                 Attach a notification handler, to be invoked when a filter 
    223                 is added to the conduit. This is used in some very specific  
    224                 cases, and should never need to support multiple clients. 
    225                  
    226         ***********************************************************************/ 
    227  
    228         final void notify (void delegate(bool) dg) 
    229         { 
    230                 notify_ = dg; 
    231         } 
    232                              
     198                return sink; 
     199        } 
     200 
     201        /*********************************************************************** 
     202 
     203                Attach an input filter 
     204 
     205        ***********************************************************************/ 
     206         
     207        final IConduit attach (InputStream source) 
     208        { 
     209                this.source = source; 
     210                return this; 
     211        } 
     212 
     213        /*********************************************************************** 
     214 
     215                Attach an output filter 
     216 
     217        ***********************************************************************/ 
     218         
     219        final IConduit attach (OutputStream sink) 
     220        { 
     221                this.sink = sink; 
     222                return this; 
     223        } 
     224 
    233225        /*********************************************************************** 
    234226 
     
    287279class InputFilter : InputStream 
    288280{ 
    289         protected InputStream next; 
     281        protected InputStream host; 
    290282 
    291283        /*********************************************************************** 
     
    299291        ***********************************************************************/ 
    300292 
    301         this (InputStream stream) 
    302         { 
    303                 next = stream.conduit.attach (this); 
     293        this (InputStream host) 
     294        { 
     295                this.host = host; 
     296                host.conduit.attach (this); 
    304297        } 
    305298 
     
    310303        IConduit conduit () 
    311304        { 
    312                 return next.conduit; 
     305                return host.conduit; 
    313306        } 
    314307 
     
    319312        void clear () 
    320313        { 
    321                 next.clear; 
     314                host.clear; 
    322315        } 
    323316} 
     
    330323class OutputFilter : OutputStream 
    331324{ 
    332         protected OutputStream next; 
     325        protected OutputStream host; 
    333326 
    334327        /*********************************************************************** 
     
    342335        ***********************************************************************/ 
    343336 
    344         this (OutputStream stream) 
    345         { 
    346                 next = stream.conduit.attach (this); 
     337        this (OutputStream host) 
     338        { 
     339                this.host = host; 
     340                host.conduit.attach (this); 
    347341        } 
    348342 
     
    353347        IConduit conduit () 
    354348        { 
    355                 return next.conduit; 
     349                return host.conduit; 
    356350        } 
    357351 
     
    362356        void flush () 
    363357        { 
    364                 next.flush; 
     358                host.flush; 
     359        } 
     360 
     361        /*********************************************************************** 
     362 
     363        ***********************************************************************/ 
     364 
     365        void commit () 
     366        { 
     367                host.commit; 
    365368        } 
    366369 
  • trunk/tango/io/Console.d

    r2474 r2490  
    6868                **************************************************************/ 
    6969 
    70                 private this (Conduit conduit, bool redirect
    71                 { 
    72                         this.redirect = redirected; 
    73                         this.buffer  = new Buffer (conduit); 
    74                 } 
    75  
    76                 /************************************************************** 
    77  
    78                         Return the associated conduit 
     70                private this (Conduit conduit, bool redirected
     71                { 
     72                        redirect = redirected; 
     73                        buffer = new Buffer (conduit); 
     74                } 
     75 
     76                /************************************************************** 
     77 
     78                        Return the associated stream 
    7979 
    8080                **************************************************************/ 
     
    157157                        return redirect; 
    158158                }            
     159 
     160                /************************************************************** 
     161 
     162                        Divert input to an alternate device 
     163 
     164                        Remarks: 
     165                        Diverts input to the specified conduit, and sets 
     166                        the redirection status.  
     167                         
     168                **************************************************************/ 
     169 
     170                final void divert (IConduit conduit, bool redirected) 
     171                { 
     172                        buffer.setConduit (conduit); 
     173                        redirect = redirected; 
     174                }            
    159175        } 
    160176 
     
    162178        /********************************************************************** 
    163179 
    164                 Model console output as a buffer. Note that we accept  
    165                 utf8 only: the superclass append() methods are hidden 
    166                 from view. Buffer.consume is left open, for those who 
    167                 require lower-level access ~ along with Buffer.write 
     180                Console output accepts utf8 only, by default 
    168181 
    169182        **********************************************************************/ 
     
    183196                **************************************************************/ 
    184197 
    185                 private this (Conduit conduit, bool redirect) 
    186                 { 
    187                         // get conduit to notify us of attachments so 
    188                         // that we can adjust our buffer accordingly 
    189                         conduit.notify (&notify); 
    190  
    191                         this.redirect = redirected; 
    192                         this.buffer   = new Buffer (conduit); 
    193                 } 
    194  
    195                 /************************************************************** 
    196  
    197                         Return the associated conduit 
     198                private this (Conduit conduit, bool redirected) 
     199                { 
     200                        redirect = redirected; 
     201                        buffer = new Buffer (conduit); 
     202                } 
     203 
     204                /************************************************************** 
     205 
     206                        Return the associated stream 
    198207 
    199208                **************************************************************/ 
     
    235244                **************************************************************/ 
    236245 
    237                 final Output append (Object o)         
     246                final Output append (Object other)         
    238247                {            
    239                         return append (o.toUtf8); 
     248                        return append (other.toUtf8); 
    240249                } 
    241250 
     
    294303 
    295304                        Remarks: 
    296                         Reflects the console redirection status from when  
    297                         this module was instantiated 
     305                        Reflects the console redirection status 
    298306 
    299307                **************************************************************/ 
     
    306314                /************************************************************** 
    307315 
    308                         Invoked when an attachment is made to the console 
    309                         Conduit. We use this to point our buffer at the  
    310                         filter being attached. Without this, said filter 
    311                         would be ignored since Buffer operates in a mode 
    312                         termed 'snapshot' i.e. it doesn't look for changes 
    313                         in the conduit after being connected to it (which 
    314                         is both correct and required behaviour). 
    315  
    316                         An alternative would be to simply insert a buffered 
    317                         filter. However, Cin requires readln() support and 
    318                         therefore needs to directly address a buffer rather 
    319                         than an InputStream. 
    320  
    321                 **************************************************************/ 
    322  
    323                 private void notify (bool) 
    324                 { 
    325                         buffer.setConduit (buffer.conduit); 
    326                 } 
     316                        Divert output to an alternate device 
     317 
     318                        Remarks: 
     319                        Diverts output to the specified conduit, and sets 
     320                        the redirection status. The latter dictates whether 
     321                        newline() performs automatic flushing or not 
     322                         
     323                **************************************************************/ 
     324 
     325                final void divert (IConduit conduit, bool redirected) 
     326                { 
     327                        buffer.setConduit (conduit); 
     328                        redirect = redirected; 
     329                }            
    327330        } 
    328331 
  • trunk/tango/io/DeviceConduit.d

    r2322 r2490  
    3131        /*********************************************************************** 
    3232 
    33                 Callback to close the file. This is invoked from the Resource 
    34                 base-class when the resource is being closed. 
    35  
    36         ***********************************************************************/ 
    37  
    38         override void close () 
    39         { 
    40                 super.close (); 
    41                 _close (); 
    42         } 
    43  
    44         /*********************************************************************** 
    45  
    46                 Return a preferred size for buffering conduit I/O 
    47  
    48         ***********************************************************************/ 
    49  
    50         uint bufferSize () 
    51         { 
    52                 return 1024 * 16; 
    53         } 
    54  
    55         /*********************************************************************** 
    56  
    57                 Return the name of this device 
    58  
    59         ***********************************************************************/ 
    60  
    61         protected char[] toUtf8() 
    62         { 
    63                 return "<device>"; 
    64         } 
    65  
    66         /*********************************************************************** 
    67  
    6833                Throw an IOException noting the last error 
    6934         
     
    7742        /*********************************************************************** 
    7843 
     44                Return the name of this device 
     45 
     46        ***********************************************************************/ 
     47 
     48        override char[] toUtf8() 
     49        { 
     50                return "<device>"; 
     51        } 
     52 
     53        /*********************************************************************** 
     54 
     55                Return a preferred size for buffering conduit I/O 
     56 
     57        ***********************************************************************/ 
     58 
     59        override uint bufferSize () 
     60        { 
     61                return 1024 * 16; 
     62        } 
     63 
     64        /*********************************************************************** 
     65 
    7966                Windows-specific code 
    8067 
     
    8774                /*************************************************************** 
    8875 
     76                        Gain access to the standard IO handles (console etc). 
     77 
     78                ***************************************************************/ 
     79 
     80                protected void reopen (Handle handle) 
     81                { 
     82                        this.handle = cast(HANDLE) handle; 
     83                } 
     84 
     85                /*************************************************************** 
     86 
    8987                        Return the underlying OS handle of this Conduit 
    9088 
    9189                ***************************************************************/ 
    9290 
    93                 final Handle fileHandle () 
     91                final override Handle fileHandle () 
    9492                { 
    9593                        return cast(Handle) handle; 
     
    9896                /*************************************************************** 
    9997 
    100                         Gain access to the standard IO handles (console etc). 
    101  
    102                 ***************************************************************/ 
    103  
    104                 protected void reopen (Handle handle) 
    105                 { 
    106                         this.handle = cast(HANDLE) handle; 
    107                 } 
    108  
    109                 /*************************************************************** 
    110  
    11198                        Close the underlying file 
    11299 
    113100                ***************************************************************/ 
    114101 
    115                 protected void _close () 
     102                override void close () 
    116103                { 
    117104                        if (handle) 
     
    125112                ***************************************************************/ 
    126113 
    127                 protected override void flush () 
     114                override void flush () 
    128115                { 
    129116                        if (! FlushFileBuffers (handle)) 
    130117                              error (); 
     118                        return this; 
    131119                } 
    132120 
     
    141129                ***************************************************************/ 
    142130 
    143                 protected override uint read (void[] dst) 
     131                override uint read (void[] dst) 
    144132                { 
    145133                        DWORD read; 
     
    165153                ***************************************************************/ 
    166154 
    167                 protected override uint write (void[] src) 
     155                override uint write (void[] src) 
    168156                { 
    169157                        DWORD written; 
     
    189177                /*************************************************************** 
    190178 
     179                        Gain access to the standard IO handles (console etc). 
     180 
     181                ***************************************************************/