Q: Populating a structure with RTTI

Myron Alexander someone at somewhere.com
Mon Jun 11 08:40:19 PDT 2007


Lutger wrote:
> I missed the other one, this could be done in a similar fashion, for 
> example:
> 
> /+ 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;
> +/
> 
> 
> with a helper function:
> 
> void populateRow(R, T...)(inout R r, T t)
> {
>     static assert(is(typeof(r.t) == T));
>     foreach(index, value; t)
>         r.t[index] = value;
> }
> 
> 
> inside fetchOne:
> 
> populateRow(r, "Myron"[], 10, "addr1"[], "addr2"[], "addr3"[], 100001.10);

Thanks. I can't make use of that as the fetch is a generic fetch from a 
database so it does not know the number of parameters upfront.

This is an example of what I would like to do:

for (int i = 0; i < r.t.length; i++) {
    getByType (i, r.t[i]);
}

Where getByType is a method on the statement object that get's the i'th 
column and sets r.t[i].

getByType (int i, ref int value);
getByType (int i, ref long value);
getByType (int i, ref char[] value);
...

That is just a paradigm example, it could also be written using:

for (int i = 0; i < r.t.length; i++) {
   if (r.t[i].type = typeid (int) {
     r.t[i] = getInt (i);
   } else if ...
}

Any ideas?

Thanks,

Myron.


More information about the Digitalmars-d-learn mailing list