Typed

Extension template for a buffer that provides methods and properties useful for working with a single type of data.

Members

Functions

get
T get()
T[] get(size_t length)

Reads a value of type T or an array of type T[].

put
void put(T value)
void put(in T[] value...)

Writes a value of type T or array of type T[].

Properties

data
D[] data [@property getter]
D[] data [@property getter]

Gets the data as an array of the template's type. The data can also be obtained in the specified format.

Examples

1 t {
2 
3 	import std.range;
4 	
5 	alias ByteBuffer = Typed!ubyte;
6 
7 	static assert(isOutputRange!(ByteBuffer, ubyte));
8 
9 	auto buffer = new ByteBuffer([0, 0, 0, 4]);
10 	assert(buffer.read!(Endian.bigEndian, uint)() == 4);
11 	
12 	buffer.reset();
13 	buffer.put(1);
14 	buffer.put([2, 3]);
15 	buffer.put(4, 5, 6);
16 	assert(buffer.data == [1, 2, 3, 4, 5, 6]);
17 	assert(buffer.get == 1);
18 	assert(buffer.get(3) == [2, 3, 4]);
1 t {
2 	
3 	alias IntBuffer = Typed!int;
4 	
5 	auto buffer = new IntBuffer(2);
6 	assert(buffer.capacity == 8);
7 	buffer.write(1);
8 	buffer.write(2);
9 	version(BigEndian) assert(buffer.data!ubyte == [0, 0, 0, 1, 0, 0, 0, 2]);
10 	version(LittleEndian) assert(buffer.data!ubyte == [1, 0, 0, 0, 2, 0, 0, 0]);
11 	assert(buffer.read!int() == 1);
12 	assert(buffer.read!int() == 2);
1 t {
2 	
3 	static struct Test {
4 		
5 		int a;
6 		short b;
7 		
8 	}
9 	
10 	static assert(Test.sizeof == 8); // because of the alignment
11 	
12 	alias TestBuffer = Typed!Test;
13 	
14 	auto buffer = new TestBuffer(1);
15 	buffer.put(Test(1, 2));
16 	version(BigEndian) assert(buffer.data!ubyte == [0, 0, 0, 1, 0, 2, 0, 0]);
17 	version(LittleEndian) assert(buffer.data!ubyte == [1, 0, 0, 0, 2, 0, 0, 0]);
18 	assert(buffer.data == [Test(1, 2)]);
19 	assert(buffer.get == Test(1, 2));
20 	
21 	buffer.reset();
22 	buffer.write!(cast(Endian)!endian)(Test(1, 2));
23 	version(BigEndian) assert(buffer.data!ubyte == [0, 0, 2, 0, 1, 0, 0, 0]);
24 	version(LittleEndian) assert(buffer.data!ubyte == [0, 0, 0, 2, 0, 0, 0, 1]);
1 t {
2 	
3 	static struct Test {
4 		
5 		int a;
6 		ubyte[] b;
7 		
8 	}
9 	
10 	static assert(!canSwapEndianness!Test);
1 t {
2 
3 	alias StringBuffer = Typed!string;
4 
5 	static assert(is(StringBuffer == Typed!(immutable(char))));
6 
7 	auto buffer = new StringBuffer(64);
8 	buffer.put("hello");
9 	assert(buffer.data!char == ['h', 'e', 'l', 'l', 'o'])

Meta