protobuf-c message wrapper
1 import core.stdc.config : c_ulong; 2 3 // protobuf-c + dpp generates structs like these 4 struct Child 5 { 6 ProtobufCMessage base; 7 char* name; 8 } 9 10 struct Sample 11 { 12 ProtobufCMessage base; 13 c_ulong n_farray; 14 float* farray; 15 char* name; 16 Child* child; 17 c_ulong n_children; 18 Child** children; 19 } 20 21 // static tests 22 static assert(isMessage!Child); 23 static assert(isMessage!Sample); 24 static assert(!isMessage!float); 25 26 // example objects 27 Child child = { name: cast(char*) "child" }; 28 Sample base = { n_farray: 2, 29 farray: [0.1f, 0.2f], 30 name: cast(char*) "foo\0", 31 child: &child, 32 n_children: 2, 33 children: [&child, &child] 34 }; 35 36 // D friendly wrapper 37 auto s = Message!Sample(base); 38 39 import std.stdio; 40 writeln(Message!Sample()); 41 writeln(Message!Sample(s)); 42 43 // basic type access 44 assert(s.n_farray == 2); 45 // string conversion 46 assert(s.name == "foo"); 47 // array conversion 48 assert(s.farray == [0.1f, 0.2f]); 49 // nested message conversion 50 assert(s.child.name == "child"); 51 // pretty print for debugging 52 assert(s.toString == 53 `Sample {n_farray: 2, farray: [0.1, 0.2], name: "foo", ` 54 ~ `child: Child {name: "child"}, n_children: 2, ` 55 ~ `children: [Child {name: "child"}, Child {name: "child"}]}`);
test with tf proto
import tensorflow.op_def_pb; import std.stdio; writeln(Message!Tensorflow__OpDef().toString); static assert(isMessage!Tensorflow__OpDef); static assert(isMessage!Tensorflow__OpDef__ArgDef); static assert(isMessage!Tensorflow__OpDef__AttrDef);