J
Johnny
Hi,
I need a function like: config_write(key, value)
I am using gcc in linux.
But different keys associate with different types of values, ex. char,
long, char *,
One way is to provide different functions such as config_write_char(long
key, char value), config_write_buffer(long key, char *value)
But programmers will be happy if only one function is needed to write all
kinds of configuration items.
The following is my idea about this:
bool config_write(unsigned long key, ...)
{
va_list ap;
va_start(ap, key);
int a; char b; long c; double d; char *e;
switch(config_get_type(key)) {
case INT:
a = va_arg(ap, int);
...
break;
case BUFFER:
e = va_arg(ap, char *);
//process e;
break;
...
}
va_end(ap);
return TRUE;
}
Note: config_get_type(unsigned long key) is used to get the data type of a
key , enum will be returned. The data type of a configuration item will be
stored in a table.
But it has shortcomings: The compiler won't do type-checking for you. The
programmers need assure that the type of value they passed in is correct for
that config item
So I want to know how to use only one function: config_write, but still
let compiler do type-checking for me.
Thanks for your advice
I need a function like: config_write(key, value)
I am using gcc in linux.
But different keys associate with different types of values, ex. char,
long, char *,
One way is to provide different functions such as config_write_char(long
key, char value), config_write_buffer(long key, char *value)
But programmers will be happy if only one function is needed to write all
kinds of configuration items.
The following is my idea about this:
bool config_write(unsigned long key, ...)
{
va_list ap;
va_start(ap, key);
int a; char b; long c; double d; char *e;
switch(config_get_type(key)) {
case INT:
a = va_arg(ap, int);
...
break;
case BUFFER:
e = va_arg(ap, char *);
//process e;
break;
...
}
va_end(ap);
return TRUE;
}
Note: config_get_type(unsigned long key) is used to get the data type of a
key , enum will be returned. The data type of a configuration item will be
stored in a table.
But it has shortcomings: The compiler won't do type-checking for you. The
programmers need assure that the type of value they passed in is correct for
that config item
So I want to know how to use only one function: config_write, but still
let compiler do type-checking for me.
Thanks for your advice