Q: Populating a structure with RTTI

BLS nanali at nospam-wanadoo.fr
Mon Jun 11 05:28:39 PDT 2007


Hi Myron, I think my previous suggestion will not work; (compiletime issue)

However, I guess your question is regarding the database row[][] 
problem, or how to create an adequate datatype for an database-table at 
compile time.

I would like to suggest to have a look on this solution (runtime)
pseudo code first :

program startup

connecttodb
// create object collection , in C# I would choose Arraylist
def tablerow as Arraylist

// browse system tables
foreach table in db

   addTableInformation // name f.i.

     foreach tablerow in table
  	// create an object which is adequate to the row datatyp
         // to do this use the /factory pattern/, see link below
         // next transfer rowinformation into this object
        add this object to tablerow
    end

end
So now you have a chained list of objects, where each object represents 
one table-row (or table-field, if you like)

In D I would choose an associative array containing the tablename as 
index and the Arraylist as value;
factory pattern link :
http://www.dofactory.com/Patterns/PatternAbstract.aspx

probabaly you prefer the prototyp pattern; You will find information 
regarding this pattern on the same site.

Regarding the Arraylist, Tango has to offer a lot of collection classes, 
I guess ArraySeq is what you need, but I am not sure, so you have to ask 
Sean.

your row class can look like this
class row
   private:
   string rowname
   string rowtype
   boolean primeryKey
....
end

your concrete row class may be

class varcharRow inherits row
   // alias varchar char[]	
   private:
   varchar value // the row value
   ...
end
// So if your database-table-row type is varchar, we will add an 
//instance of varcharRow to Arraylist.
Just a quick hack but hopefully I was able to figure out the idea; Some 
feedback would be nice;
Bjoern

Myron Alexander schrieb:
> Hello.
> 
> Is it possible to populate a struct using RTTI?
> 
> Example:
> 
> Say I have a struct as such:
> 
> struct Example {
>    int    x;
>    int    y;
>    char[] z;
> }
> 
> and I want to create and populate the structure from a function:
> 
> T populate(T) () {
>   T t;
>   t.field[0] = 1;
>   t.field[1] = 2;
>   t.field[2] = "testing";
>   return t;
> }
> 
> void main() {
>   auto x = populate!(Example)();
> }
> 
> Is this possible? If so, what is the syntax?
> 
> Thanks ahead,
> 
> Myron.


More information about the Digitalmars-d-learn mailing list