Q: Populating a structure with RTTI
Myron Alexander
someone at somewhere.com
Mon Jun 11 08:26:51 PDT 2007
Lutger wrote:
> Myron Alexander wrote:
>>> /+ Won't compile:
>>> for (int i = 0; i < T.tupleof.length; i++) {
>>> t.tupleof[i] = r.t[i];
>>> }
>>> +/
>
> You can use foreach on tuples:
>
> foreach(index, value; r.t)
> t.tupleof[index] = value;
Thanks Lutger. One down, one to go.
Regards,
Myron.
P.S. Here is an example using 2 structs:
> import std.stdio;
> import std.traits;
>
> /* Example value object.
> */
> struct ValueStruc {
> char[] name;
> int age;
> char[] addr1;
> char[] addr2;
> char[] addr3;
> double balance;
> }
>
> struct OtherValue {
> int type;
> char[] dirname;
> char[] filename;
> }
>
> struct ValueStrucWNull {
> char[] name;
> int age;
> char[] addr1;
> char[] addr2;
> char[] addr3;
> double balance;
>
> bool[6] nullFlag;
> }
>
> struct OtherValueWNull {
> int type;
> char[] dirname;
> char[] filename;
>
> bool[3] nullFlag;
> }
>
> /* Typesafe database row object.
> */
> struct Row(T ...) {
> T t;
> bool[T.length] nullFlag;
> }
>
> /* Simulate a typesafe fetch from the database.
> */
> Row!(T) fetchone(T ...) () {
> Row!(T) r;
>
> /+ --- Won't compile --------------------------------------------------
> int i = 0;
> r.t[i++] = "Myron";
> r.t[i++] = 10;
> r.t[i++] = "addr1";
> r.t[i++] = "addr2";
> r.t[i++] = "addr3";
> r.t[i++] = 100001.10;
> -----------------------------------------------------------------------
> +/
> static if (T.length == 6) {
> r.t[0] = "Myron";
> r.t[1] = 10;
> r.t[2] = "addr1";
> r.t[3] = "addr2";
> r.t[4] = "addr3";
> r.t[5] = 100001.10;
> r.nullFlag[0] = true;
> r.nullFlag[1] = false;
> r.nullFlag[2] = false;
> r.nullFlag[3] = true;
> r.nullFlag[4] = false;
> r.nullFlag[5] = false;
> }
>
> static if (T.length == 3) {
> r.t [0] = 34;
> r.t [1] = r"c:\dir1\dir2\dir2\";
> r.t [2] = "filename";
> r.nullFlag[0] = false;
> r.nullFlag[1] = true;
> r.nullFlag[2] = false;
> }
>
> return r;
> }
>
> /* Fetch a typesafe record from the database and populate the struct.
> */
> T fetchstruc(T) () {
>
> static if (is (T == struct)) {
> auto r = fetchone!(FieldTypeTuple!(T))();
> T t;
> foreach (i, v; r.t) {
> t.tupleof[i] = v;
> }
> return t;
>
> } else {
> static assert (0);
> }
> }
>
> /* Fetch a typesafe record from the database, populate the struct and set
> * null field flags.
> */
> T fetchnullstruc(T) () {
>
> static if (is (T == struct)) {
> auto r = fetchone!(FieldTypeTuple!(T)[0..length-1])();
> T t;
> foreach (i, v; r.t) {
> t.tupleof[i] = v;
> }
> foreach (i, nullFlagValue; r.nullFlag) {
> t.nullFlag[i] = nullFlagValue;
> }
> return t;
>
> } else {
> static assert (0);
> }
> }
>
>
> void main () {
> auto x = fetchstruc!(ValueStruc) ();
> writefln ("%s, %s, %s, %s, %s, %#.2f", x.name, x.age, x.addr1, x.addr2, x.addr3, x.balance);
>
> auto y = fetchstruc!(OtherValue) ();
> writefln ("%s, %s, %s", y.type, y.dirname, y.filename);
>
> auto a = fetchnullstruc!(ValueStrucWNull) ();
> writefln ("%s, %s, %s, %s, %s, %#.2f, %s", a.name, a.age, a.addr1, a.addr2, a.addr3, a.balance, a.nullFlag);
>
> auto b = fetchnullstruc!(OtherValueWNull) ();
> writefln ("%s, %s, %s, %s", b.type, b.dirname, b.filename, b.nullFlag);
> }
More information about the Digitalmars-d-learn
mailing list