RCBin

Struct for parsing and serializing into binary.

Constructors

this
this(T range)

Create a parser or serializer with the given range.

Members

Functions

get
auto get(T target)
auto get(T input)

Get a value.

getArray
void getArray(T[] target)
void getArray(T[] input)

Read or write a dynamic array to the stream.

getArray
void getArray(T[size] target)
void getArray(T[size] input)

Read or write a static array to the stream.

getBoolean
void getBoolean(bool target)

Read or write a boolean to the stream.

getNumber
void getNumber(T target)
void getNumber(T input)

Read or write a number to the stream.

getStruct
void getStruct(T target)

Read or write all struct fields.

read
T read()

Read and return a value. Should be avoided to enable making serializers.

Manifest constants

isSerializer
enum isSerializer;
Undocumented in source.
isSerializer
enum isSerializer;
Undocumented in source.

Variables

range
T* range;

Range to read or write to.

Parameters

T

A range. If parsing, this must be an input range, or if serializing, an output range.

isParser

If true this should be a parser

Examples

1 struct Foo {
2 
3     int id;
4     float value;
5     string name;
6     string[] arguments;
7     string unicodeString;
8     int[] numbers;
9     int[3] staticArray;
10 
11 }
12 
13 void getData(T)(ref T bin, ref Foo target)
14 if (isRCBin!T) {
15 
16     bin.get(target.id)
17        .get(target.value)
18        .get(target.name)
19        .get(target.arguments)
20        .get(target.unicodeString)
21        .get(target.numbers)
22        .get(target.staticArray);
23 
24 }
25 
26 Foo foo = {
27     id: 123,
28     value: 42.01,
29     name: "John Doe",
30     arguments: ["a", "ab", "b"],
31     unicodeString: "Ich fühle mich gut.",
32     numbers: [1, 2, 3, 4],
33     staticArray: [1, 2, 3],
34 };
35 
36 // Serialize the data
37 auto data = appender!(ubyte[]);
38 auto serializer = rcbinSerializer(data);
39 getData(serializer, foo);
40 
41 // Read the data
42 ubyte[] buffer = data[];
43 Foo newFoo;
44 auto parser = rcbinParser(buffer);
45 getData(parser, newFoo);
46 
47 assert(newFoo.id == 123);
48 assert(newFoo.value == 42.01f);
49 assert(newFoo.name == "John Doe");
50 assert(newFoo.arguments == ["a", "ab", "b"]);
51 assert(newFoo.unicodeString == "Ich fühle mich gut.");
52 assert(newFoo.numbers == [1, 2, 3, 4]);
53 assert(newFoo.staticArray == [1, 2, 3]);
54 
55 // Or even better — serialize the whole struct
56 auto data2 = appender!(ubyte[]);
57 rcbinSerializer(data2)
58     .getStruct(foo);
59 
60 auto buffer2 = data2[];
61 assert(data[] == buffer2);
62 
63 // And read the data later
64 Foo anotherFoo;
65 rcbinParser(buffer2)
66     .getStruct(anotherFoo);
67 
68 assert(foo == anotherFoo);

Meta