overloading in c

Discussion in 'C Programming' started by Johnny, Jul 7, 2005.

  1. Johnny

    Johnny Guest

    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
    Johnny, Jul 7, 2005
    #1
    1. Advertising

  2. Johnny

    Richard Bos Guest

    "Johnny" <> 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
    Richard Bos, Jul 7, 2005
    #2
    1. Advertising

  3. 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.
    Kasper Dupont, Jul 7, 2005
    #3
  4. 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;
    > }
    >
    Tommi Johnsson, Jul 8, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,397
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    442
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    423
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    571
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    392
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page