N
No One
Here is my problem:
I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be performed on the different types of data. Currently I
have a collection of functions that do exactly the same - the only
difference between them is the type of data they act on. Let me present a
toy example:
I have the following two data types:
typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;
Thus, I know that both data_one and data_two have an integer field, x.
The remaining fields can be arbitrarily different.
I want to have a function that takes as an argument a pointer to one of
those structures, increments the x member and returns a pointer to the
same structure. Currently I have two functions:
data_one * f_one(data_one * data) data_two * f_two(data_two * data)
{ {
data->x += 1 ; data->x += 1 ;
return data ; return data ;
} }
The bodies of the functions are identical - which is a waste.
I'd rather use a single function, with a prototype similar to:
void * f(data_type type, void * data)
where "type" would specify the actual type of the data we are dealing with.
Can something along these lines be done? Please remember that the above
is just a toy example.
I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be performed on the different types of data. Currently I
have a collection of functions that do exactly the same - the only
difference between them is the type of data they act on. Let me present a
toy example:
I have the following two data types:
typedef struct { typedef struct {
int x ; int x ;
int y ; char * p ;
char * s ; } data_two ;
} data_one ;
Thus, I know that both data_one and data_two have an integer field, x.
The remaining fields can be arbitrarily different.
I want to have a function that takes as an argument a pointer to one of
those structures, increments the x member and returns a pointer to the
same structure. Currently I have two functions:
data_one * f_one(data_one * data) data_two * f_two(data_two * data)
{ {
data->x += 1 ; data->x += 1 ;
return data ; return data ;
} }
The bodies of the functions are identical - which is a waste.
I'd rather use a single function, with a prototype similar to:
void * f(data_type type, void * data)
where "type" would specify the actual type of the data we are dealing with.
Can something along these lines be done? Please remember that the above
is just a toy example.