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

Ticket #1203: NullConduit.d

File NullConduit.d, 1.4 kB (added by fawzi, 3 months ago)

A null conduit implementation

Line 
1 /*******************************************************************************
2     A Conduit that ignores all that is written to it
3         copyright:      Copyright (c) 2008. Fawzi Mohamed
4         license:        BSD style: $(LICENSE)
5         version:        Initial release: July 2008
6         author:         Fawzi Mohamed
7 *******************************************************************************/
8 module tango.io.device.NullConduit;
9 import tango.io.device.Conduit;
10
11 /// a conduit that discards its input
12 class NullConduit: Conduit{
13     char[] toString (){
14         return "NullConduit";
15     }
16
17     uint bufferSize () { return 256u;}
18
19     uint read (void[] dst) { return Eof; }
20
21     uint write (void[] src) { return src.length; }
22
23     void detach () { }
24    
25 }
26
27 debug(UnitTest){
28     unittest{
29         auto a=new NullConduit();
30         a.write("bla");
31         a.flush();
32         a.detach();
33         a.write("b"); // at the moment it works, disallow?
34         uint[4] b=0;
35         a.read(b);
36         foreach (el;b)
37             assert(el==0);
38     }
39 }
40
41 /// a stream that discards its input
42 /// (make it a singleton? would be slower due to locking somewhere?)
43 OutputStream nullStream(){
44     return (new NullConduit).output;
45 }
46
47
48 debug(UnitTest){
49     unittest{
50         OutputStream a=nullStream();
51         a.write("bla");
52         a.flush();
53         a.close();
54         a.write("b"); // at the moment it works, disallow?
55     }
56 }