1 /**
2  * Copyright: Copyright Jason White, 2014-2016
3  * License:   $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4  * Authors:   Jason White
5  */
6 module io.buffer.traits;
7 
8 import io.stream.traits;
9 
10 /**
11  * Checks if the stream can be buffered. A stream that is exclusively read from
12  * or written to can always be buffered. However, when both reads and writes
13  * must be buffered, the stream must also be seekable. There are no exceptions
14  * to this last rule when buffering.
15  */
16 enum isBufferable(Stream) = (isSource!Stream ^ isSink!Stream) ||
17     isSeekable!Stream;
18 
19 unittest
20 {
21     import io.stream.interfaces;
22 
23     interface A : Source {}
24     static assert(isBufferable!A);
25 
26     interface B : Sink {}
27     static assert(isBufferable!B);
28 
29     // Not possible. Stream must be seekable.
30     interface C : SourceSink {}
31     static assert(!isBufferable!C);
32 
33     interface D : Seekable!SourceSink {}
34     static assert(isBufferable!D);
35 
36     interface E : Seekable!Source {}
37     static assert(isBufferable!E);
38 
39     interface F : Seekable!Sink {}
40     static assert(isBufferable!F);
41 }
42 
43 /**
44  * Checks if the stream can be flushed.
45  */
46 enum isFlushable(Stream) =
47     is(typeof({
48         Stream s = void;
49         s.flush();
50     }));
51 
52 unittest
53 {
54     import io.stream.interfaces;
55     struct A {}
56     static assert(!isFlushable!A);
57 
58     struct B { void flush(); }
59     static assert(isFlushable!B);
60 }