LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2010, 02:22 AM   #1
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
[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.

All data querying goes in a loop.

My idea is to have an array of structs like this:
Code:
struct type_func_map
{
  int type;
  void (*func_wrapper_int)(void); //void (*func_wrapper_text)(void); void (*func_wrapper_real)(void);
};
and have:
Code:
void func_wrapper_int()
{
  int data = get_int();
  printf(); //do smth
}

void func_wrapper_text()
{
  char data = get_text();
  printf(); //do smth
}
Any opinions? Is there any cleaner solution which does not pollute your code?

Last edited by Alien_Hominid; 02-01-2010 at 02:24 AM.
 
Old 02-01-2010, 07:39 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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
 
Old 02-01-2010, 11:21 AM   #3
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
OK, I could make this:
Code:
union my_union
{
  int a;
  char *b;
  double c;
};
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.

Basically, what I want is:
Code:
my_union.a = *(funcarray[INTEGER]);
my_union.b = *(funcarray[TEXT]);

Last edited by Alien_Hominid; 02-01-2010 at 11:23 AM.
 
Old 02-01-2010, 12:53 PM   #4
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
For example:

Code:
#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.
 
Old 02-02-2010, 04:04 AM   #5
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
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.
 
Old 02-02-2010, 04:30 AM   #6
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
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().
 
Old 02-02-2010, 09:38 AM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
No, there is function to display data:
Code:
void display_data(column_count, column_type, data, ...)
And your functions basically wrap my data retrieval functions, which I am hoping to avoid.

Last edited by Alien_Hominid; 02-02-2010 at 09:45 AM.
 
Old 02-02-2010, 09:41 AM   #8
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Ok. Problem solved then?
 
Old 02-02-2010, 09:46 AM   #9
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
If there aren't any other way of doing the same thing without creating own wrappers, then yes.

Last edited by Alien_Hominid; 02-02-2010 at 09:49 AM.
 
Old 02-02-2010, 10:00 AM   #10
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
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.
 
Old 02-02-2010, 10:33 AM   #11
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Original Poster
Rep: Reputation: 53
Ok, there is sqlite database: http://www.sqlite.org/c3ref/column_blob.html
There is a GTK function to create GtkListStore: http://library.gnome.org/devel/gtk/2...list-store-set

Now I would like to have a loop:
Code:
 for(int j = 0; j < column_count; j++)
 {
   gtk_list_store_set (store, iter, j, structure_array[j].(*func(statement, j)), -1);
 }
Func is one of these:
  • int sqlite3_column_int(sqlite3_stmt*, int iCol);
  • const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  • double sqlite3_column_double(sqlite3_stmt*, int iCol);

and structure consists of:
Code:
{
  type;
  function_to_call;
}

Last edited by Alien_Hominid; 02-02-2010 at 10:37 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Two return types Ephracis Programming 12 01-16-2007 06:23 AM
c preprocessor commands & return types(?) kpachopoulos Programming 4 12-06-2005 09:18 AM
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 10:22 PM
return statement in functions pantera Programming 2 12-06-2004 06:21 PM
How to use return values from system functions? kpachopoulos Programming 2 07-30-2004 03:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration