Re: gpointer question




 Since you are dealing with a gpointer (void *) ,you really do not need to know
what the pointer is pointing to.

 If I understand what you are trying to do, you will need to create an id to
every possible struct that you have, that way you can identify what the
gpointer points to based on this id.

  There are 2 ways I can come up with:

/* each type. maps to a valid struct type */
typedef enum { TYPE_A, TYPE_B, TYPE_C } SomeType;

typedef struct {
        int  model_id;
        int  active;
        int  units;
        typGeometry  geodata;
        gpointer  modeldata;
        SomeType type;
         
 } typModel;

  Now you would create an interface to set the modeldata.. 

  set_modeldata(typModel * m, SomeType type; gpointer someValidStruct){
    m -> type = type;
    modeldata = someUnkownStruct.
  } 

  Now, you could cast to the approprite type, some palce else:

  switch(m->type){
    case  TYPE_A:
        struct Type_A_Struct* t = m->modelType;
         do_whaterver_with_it(t);
    ....
  }


  I hope you get the gist.

 The second way is a bit more envolving. It  requires a comom base object
(struct) that is the first member of all other structs and do it like GTK does
with Gobject/GtkWidget and so on, -- in onther words -- inheritance.

  Take a look at how GtkButton inherits from GtkWidget and you will see what I
mean.

  Hope this helps.

Harring


 
--- Jim Parker <hopeye cfl rr com> wrote:
G'Day !

I am writing a CAD application.  The 3-D modeler part is very generic, 
so it can be used for any engineering displine, called modes.

The system data is contained in list of models.  Each model structure 
has pointers to parts, layers, geometric data etc.  But I also need to 
add a struct containing mode specific data.  For example a ship would 
have a length, beam, draft, etc., but this information has no meaning 
for lets say a dam in a civil engineering model.

 From what I have read, it looks like I can use a gpointer to add the 
data to my struct.  Unfortunately, I really do not understand how (self 
taught, and not covered in my C books).

Here is an simplified example of what I would like to try:

typedef struct {
      int  model_id;
      int  active;
      int  units;
      typGeometry  geodata;
      gpointer  modeldata;
} typModel;

typdef struct {
      float length;
      float lwl;
      float beam;
      float draft;
      float depth;
} typShip;

So I want to get the gpointer in the typModel struct to point to the 
typShip struct.  The trick is that I do not
know the size of the struct pointed to until the assignemt at runtime 
(a designer may use the typMachine struct).

Any help would be appreciated.

Thanks.

cheers,
Jim Parker
       

  
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]