C
cognacc
Hi
I have implemented a program with many different types.
Where i use void as a generic object (pointer).
I have a type i call Record
struct Record {
unsigned int RecordID;
void *fieldtype; // don't know type of field beforehand
struct Record *next_record; // i actually use TAILQ macro
};
So i have a collection of Records, that are read in from a file.
I dont know at any moment what my fieldtype could be.
A field type is another structure of an undeterminate size.
(really 2-20 datatypes (also another struct))
struct S2GD {
unsigned int xcoord;
unsigned int xcoord;
};
struct FRID {
char NAME[8]; // size of char array is also calculated from data
file entry
..
..
..
};
So when i need to manipulate these fields i have a large
swich case with about 20 different fieldtypes. determining type.
To determnine type i have a field_typename in my Record structure.
struct Record{
unsigned int RecordID;
char field_typename[5];
..
..
};
I then compare to field_typename to see what type it is.
To detect which function (function pointer) to use.
When using function pointers, i still need the switch case to choose
which function (ptr) to pass.
code from a function called assign() // not using function pointers
here
----
if ( (c = strcmp(field_name, "DSID")) == 0 )
{
DSID * field_dat_p;
field_dat_p = (DSID *)dr_data_ptr->field_ptr;
assign_dsid(field_dat_p, subfield_nr);
}
else if ( strcmp(field_name, "DSSI") == 0 )
{
DSSI * field_dat_p;
field_dat_p = (DSSI *) dr_data_ptr->field_ptr;
assign_dssi(field_dat_p, subfield_nr);
}
---
Lots of code, seems inefficient.
I need a char string to found out what field type it is (i think)
urgghh!
I thought there was way to get runtime detection of datatype, but i
couldnt get it to work.
like
I could probably solve it with another layer of indirection.
A table of types. (vtable like ?)
Or something?
NB: the field types are generated from reading the data file
so i don't know in advance what they are or what they contain.
That is the datafile has a specific section where it specifies the
datatypes.
A generic fieldtype look like
struct fieldtypename {
subfield1
..
..
subfieldN
};
where a subfield can be any type including structs.
I also looked at some code from embedded.com that implements objects
on
C, but thats overkill, i think.
Isnt there an intermediate way to do this?
When looking at the code i fill quite silly , and think i must have
missed something
totally obvious.
regards Mic
I have implemented a program with many different types.
Where i use void as a generic object (pointer).
I have a type i call Record
struct Record {
unsigned int RecordID;
void *fieldtype; // don't know type of field beforehand
struct Record *next_record; // i actually use TAILQ macro
};
So i have a collection of Records, that are read in from a file.
I dont know at any moment what my fieldtype could be.
A field type is another structure of an undeterminate size.
(really 2-20 datatypes (also another struct))
struct S2GD {
unsigned int xcoord;
unsigned int xcoord;
};
struct FRID {
char NAME[8]; // size of char array is also calculated from data
file entry
..
..
..
};
So when i need to manipulate these fields i have a large
swich case with about 20 different fieldtypes. determining type.
To determnine type i have a field_typename in my Record structure.
struct Record{
unsigned int RecordID;
char field_typename[5];
..
..
};
I then compare to field_typename to see what type it is.
To detect which function (function pointer) to use.
When using function pointers, i still need the switch case to choose
which function (ptr) to pass.
code from a function called assign() // not using function pointers
here
----
if ( (c = strcmp(field_name, "DSID")) == 0 )
{
DSID * field_dat_p;
field_dat_p = (DSID *)dr_data_ptr->field_ptr;
assign_dsid(field_dat_p, subfield_nr);
}
else if ( strcmp(field_name, "DSSI") == 0 )
{
DSSI * field_dat_p;
field_dat_p = (DSSI *) dr_data_ptr->field_ptr;
assign_dssi(field_dat_p, subfield_nr);
}
---
Lots of code, seems inefficient.
I need a char string to found out what field type it is (i think)
urgghh!
I thought there was way to get runtime detection of datatype, but i
couldnt get it to work.
like
I could probably solve it with another layer of indirection.
A table of types. (vtable like ?)
Or something?
NB: the field types are generated from reading the data file
so i don't know in advance what they are or what they contain.
That is the datafile has a specific section where it specifies the
datatypes.
A generic fieldtype look like
struct fieldtypename {
subfield1
..
..
subfieldN
};
where a subfield can be any type including structs.
I also looked at some code from embedded.com that implements objects
on
C, but thats overkill, i think.
Isnt there an intermediate way to do this?
When looking at the code i fill quite silly , and think i must have
missed something
totally obvious.
regards Mic