Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   overloading in c (http://www.velocityreviews.com/forums/t438645-overloading-in-c.html)

Johnny 07-07-2005 06:13 AM

overloading in c
 
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



Richard Bos 07-07-2005 07:01 AM

Re: overloading in c
 
"Johnny" <lglyahoo-misc@yahoo.com.cn> wrote:

> I need a function like: config_write(key, value)


> 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)


> The following is my idea about this:
>
> bool config_write(unsigned long key, ...)


> 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.


You cannot, in C. If you really need this, go to C++; in most
circumstances, the solution you already give would be sufficient.

Richard

Kasper Dupont 07-07-2005 08:03 AM

Re: overloading in c
 
I don't have a solution for this problem, but I know
that gcc have some features that might come in handy.

It can do type checking of printf statements as long
as the format string is a constant. But that doesn't
seem general enough to apply in your case.

I tried to come up with something using typeof, a
bit of macro magic (including the # operator), nested
functions, and finally __PRETTY_FUNCTION__.
Unfortunately it seems __PRETTY_FUNCTION__ contains
only the necesarry information if you are using C++,
and there you don't have nested functions.

The closest I could come up with was a switch on
sizeof. But if two of the types you might be using
are of the same size, then that isn't of much help.


Here is the code. Can somebody suggest a fix? Or is
this really impossible?

#include <stdio.h>
#define config_write(key, value) do { \
char *__foobar (typeof(value) x) { \
return __PRETTY_FUNCTION__; \
} \
do_config_write(key,__foobar(value),value);\
} while(0)
void do_config_write(long key, char *type, ...)
{
printf("%ld: %s\n",key,type);
}
int main()
{
int foobar=42;
config_write(27,foobar);
return 0;
}

--
Kasper Dupont -- der bruger for meget tid på usenet.
Note to self: Don't try to allocate 256000 pages
with GFP_KERNEL on x86.

Tommi Johnsson 07-08-2005 06:51 PM

Re: overloading in c
 
Kasper Dupont wrote:
> I don't have a solution for this problem, but I know
> that gcc have some features that might come in handy.
>


it can be found from gcc manual...

if (__builtin_types_compatible_p (typeof (x), int ))
my_int_func (x);
else if (__builtin_types_compatible_p (typeof (x), float ))
my_float_func (x);


> It can do type checking of printf statements as long
> as the format string is a constant. But that doesn't
> seem general enough to apply in your case.
>
> I tried to come up with something using typeof, a
> bit of macro magic (including the # operator), nested
> functions, and finally __PRETTY_FUNCTION__.
> Unfortunately it seems __PRETTY_FUNCTION__ contains
> only the necesarry information if you are using C++,
> and there you don't have nested functions.
>
> The closest I could come up with was a switch on
> sizeof. But if two of the types you might be using
> are of the same size, then that isn't of much help.
>
>
> Here is the code. Can somebody suggest a fix? Or is
> this really impossible?
>
> #include <stdio.h>
> #define config_write(key, value) do { \
> char *__foobar (typeof(value) x) { \
> return __PRETTY_FUNCTION__; \
> } \
> do_config_write(key,__foobar(value),value);\
> } while(0)
> void do_config_write(long key, char *type, ...)
> {
> printf("%ld: %s\n",key,type);
> }
> int main()
> {
> int foobar=42;
> config_write(27,foobar);
> return 0;
> }
>



All times are GMT. The time now is 02:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.