[C] Wrapping around functions with different return types
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[C] Wrapping around functions with different return types
Hello,
I have functions which return different data like:
int, char*, double...
I also have a list of datatypes (INTEGER, TEXT, REAL...) which can be returned.
I need to map a datatype with function, which purpose is to return it. Therefore, when I determine a datatype, I would like to call the required function without doing switch all the time.
Why not just make a union of your return types and have your return a variable of that union type, then you just access the member of that union that the function you just called returns
and make functions store their return values into the appropriate members of this union, but I need to know which function to call when I have a datatype TEXT or datatype INTEGER given in advance. Let's say you need to call char *func1() if datatype is TEXT or int func2() if datatype is INTEGER. Is there a way to have some sort of array or structure capable holding functions with different return values.
#include <stdio.h>
union retval {
int INTEGER;
char *TEXT;
double REAL;
};
union retval f_integer(void)
{
return ((union retval) { .INTEGER = 42 });
}
union retval f_text(void)
{
return ((union retval) { .TEXT = "The Answer" });
}
union retval f_real(void)
{
return ((union retval) { .REAL = 1.21 }); // gigawatts
}
int main(void)
{
union retval (*FT[])(void) = { f_integer, f_text, f_real };
enum { INTEGER, TEXT, REAL };
printf("INTEGER: %d\n", FT[INTEGER]().INTEGER);
printf("TEXT: %s\n", FT[TEXT]().TEXT);
printf("REAL: %lf\n", FT[REAL]().REAL);
}
I'm unsure about something. So using an array such as FT in the code above, you can "easily" decide which function to call. The problem is - what do you do with the returned value?
Last edited by carbonfiber; 02-01-2010 at 12:57 PM.
So you actually made union retval returning wrappers around data processing functions.
My problem
I have data stored like this:
Code:
INTEGER | TEXT | REAL
---------------------
1 |hello | 3.14
3 |world | 1.27
666 |doom | 2.18
I can get the type of each column and there are special functions querying data from INTEGER, TEXT, REAL columns (e.get_int(), get_string(), get_real()). What I need to do is to get column type and call appropriate function to get the data. The data processing should work independently from the order of columns.
Last edited by Alien_Hominid; 02-02-2010 at 04:06 AM.
Yes, and you can do that using a table such as FT from above, my question is: what do you have to do with the data once it has been retreived? Example:
Code:
union Data { int i; char *t, double r; };
enum Type { INTEGER, TEXT, REAL };
// ...
enum Type data_type = get_data_type();
union Data data = get_data(data_type);
// now what? what do you have to do with data?
I don't know if it clear where I am getting at. What I'm trying to say is that.. once you have received the data.. you still need to decide how you are going to process it. So you'll need yet more tables, or switch().
To be quite honest, I'm still trying to see the 'big picture'. Do you have different display functions for each column/data type? Do you have only one function? What prototype does it have? It is difficult to suggest design decisions without at least having a decent view of the 'global picture'.
Last edited by carbonfiber; 02-02-2010 at 10:04 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.