Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > overloading in c

Reply
Thread Tools

overloading in c

 
 
Johnny
Guest
Posts: n/a
 
      07-07-2005
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


 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      07-07-2005
"Johnny" <lglyahoo-> 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
 
Reply With Quote
 
 
 
 
Kasper Dupont
Guest
Posts: n/a
 
      07-07-2005
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.
 
Reply With Quote
 
Tommi Johnsson
Guest
Posts: n/a
 
      07-08-2005
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;
> }
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Overloading __init__ & Function overloading Iyer, Prasad C Python 4 09-30-2005 08:01 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 03:59 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57