473,378 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

making a generic function wrapper

Hi,

I have a wrapper around some 3rd party database library function. The
pseudo code looks like the following - it is meant to open a table in a
database, extract values from a table, then copy it into my own user
defined structures. Since the process of opening and retrieving data
from the database is exactly the same for all struct types, and the
only part that's different is how the data is copied into the diff
structs, I was wondering if there was a way I could have one generic
wrapper, then somehow pass a callback or something to handle the
copying over specific stuff: This is what I have now, an entire wrapper
which reproduces the entire open table and copy to struct code for
every different type of structure I have:
bool MyDatabaseWrapper(structure A,
structure B,
structure N)
{
// Generic open stuff
// Generic open stuff
// Generic open stuff

while (...) {

// Generic execute stuff
// Generic execute stuff

// Now data is copied from the retrieved record into
// my specific passed structure.
if (struct type A) {
a.income = data.col(0);
}
else if (struct type B) {
b.name = data.col(0);
b.age = data.col(1);
}
// and so on for many different struct types.
}

return true;
}
See I would rather have it like:

bool MyGenericDatabaseWrapper(void **structType, CopyFunctionPtr)
{
// Generic open stuff

while (...) {

// Generic execute stuff

// Ok now call the function pointer to handle copying the
specifics
// into the unknown struct type.
CopyFunctionPtr(structType, data);
}
return true;
}

void CopyFunctionPtrStructA(struct &A, &data)
{
A.income = data.col(0);
}

void CopyFunctionPtrStructB(struct &B, &data)
{
B.name = data.col(0);
B.age = data.col(1);
}

Ok I hope that was clear. This whole wrapper thing is to make sure this
database lib I'm using is thread safe, and in the function there are
like 30 lines of code that I don't want to recopy for every type of
struct retrieval I have to do from the database.

Thanks,
Mark

Aug 15 '06 #1
3 3515
markww wrote:
Hi,

I have a wrapper around some 3rd party database library function. The
pseudo code looks like the following - it is meant to open a table in a
database, extract values from a table, then copy it into my own user
defined structures. Since the process of opening and retrieving data
from the database is exactly the same for all struct types, and the
only part that's different is how the data is copied into the diff
structs, I was wondering if there was a way I could have one generic
wrapper, then somehow pass a callback or something to handle the
copying over specific stuff: This is what I have now, an entire wrapper
which reproduces the entire open table and copy to struct code for
every different type of structure I have:
bool MyDatabaseWrapper(structure A,
structure B,
structure N)
{
// Generic open stuff
// Generic open stuff
// Generic open stuff

while (...) {

// Generic execute stuff
// Generic execute stuff

// Now data is copied from the retrieved record into
// my specific passed structure.
if (struct type A) {
a.income = data.col(0);
}
else if (struct type B) {
b.name = data.col(0);
b.age = data.col(1);
}
// and so on for many different struct types.
}

return true;
}
See I would rather have it like:

bool MyGenericDatabaseWrapper(void **structType, CopyFunctionPtr)
{
// Generic open stuff

while (...) {

// Generic execute stuff

// Ok now call the function pointer to handle copying the
specifics
// into the unknown struct type.
CopyFunctionPtr(structType, data);
}
return true;
}

void CopyFunctionPtrStructA(struct &A, &data)
{
A.income = data.col(0);
}

void CopyFunctionPtrStructB(struct &B, &data)
{
B.name = data.col(0);
B.age = data.col(1);
}

Ok I hope that was clear. This whole wrapper thing is to make sure this
database lib I'm using is thread safe, and in the function there are
like 30 lines of code that I don't want to recopy for every type of
struct retrieval I have to do from the database.
You might be able to do something like the second one, but you'll still
have to have logic to determine what type to copy somewhere. In your
second pseudo-code snippet above, that logic disappeared altogether,
which makes me wonder if you had enough information to select the copy
function outside the database wrapper function. If so, then you could
use templates to come up with a solution, and if the different types
have a common base class, you might consider using an object factory
(see chapter 8 of _Modern C++ Design_ by Alexandrescu; download the
accompanying library from http://sourceforge.net/projects/loki-lib).

Cheers! --M

Aug 15 '06 #2
Hi mlimber,

Thanks for replying. I think my example is overly complicated and I may
have explained it incorrectly. Here's a much clearer example of what
I'm trying to do - I'm really new with function pointers and the like,
but if you have any suggestions with the following example that'd be
great. In the meantime I'll do the reading you suggested,

Thanks
struct TYPE_A {
string name, incoming;
};
struct TYPE_B {
int age;
};

int main()
{
TYPE_A a;
TYPE_B b;

ReadData(CopyStructA, a);

ReadData(CopyStructB, b);

return 0;
}

void ReadData(void *pStruct, void *fnPtr)
{
// Read the data into 'data'.
fnPtr(data, pStruct);
}

void CopyStructA(data, TYPE_A *a)
{
a.name = data.col(0);
a.incoming = data.col(1);
}

void CopyStructB(data, TYPE_B *b)
{
b.age = data.col(0);
}



mlimber wrote:
markww wrote:
Hi,

I have a wrapper around some 3rd party database library function. The
pseudo code looks like the following - it is meant to open a table in a
database, extract values from a table, then copy it into my own user
defined structures. Since the process of opening and retrieving data
from the database is exactly the same for all struct types, and the
only part that's different is how the data is copied into the diff
structs, I was wondering if there was a way I could have one generic
wrapper, then somehow pass a callback or something to handle the
copying over specific stuff: This is what I have now, an entire wrapper
which reproduces the entire open table and copy to struct code for
every different type of structure I have:
bool MyDatabaseWrapper(structure A,
structure B,
structure N)
{
// Generic open stuff
// Generic open stuff
// Generic open stuff

while (...) {

// Generic execute stuff
// Generic execute stuff

// Now data is copied from the retrieved record into
// my specific passed structure.
if (struct type A) {
a.income = data.col(0);
}
else if (struct type B) {
b.name = data.col(0);
b.age = data.col(1);
}
// and so on for many different struct types.
}

return true;
}
See I would rather have it like:

bool MyGenericDatabaseWrapper(void **structType, CopyFunctionPtr)
{
// Generic open stuff

while (...) {

// Generic execute stuff

// Ok now call the function pointer to handle copying the
specifics
// into the unknown struct type.
CopyFunctionPtr(structType, data);
}
return true;
}

void CopyFunctionPtrStructA(struct &A, &data)
{
A.income = data.col(0);
}

void CopyFunctionPtrStructB(struct &B, &data)
{
B.name = data.col(0);
B.age = data.col(1);
}

Ok I hope that was clear. This whole wrapper thing is to make sure this
database lib I'm using is thread safe, and in the function there are
like 30 lines of code that I don't want to recopy for every type of
struct retrieval I have to do from the database.

You might be able to do something like the second one, but you'll still
have to have logic to determine what type to copy somewhere. In your
second pseudo-code snippet above, that logic disappeared altogether,
which makes me wonder if you had enough information to select the copy
function outside the database wrapper function. If so, then you could
use templates to come up with a solution, and if the different types
have a common base class, you might consider using an object factory
(see chapter 8 of _Modern C++ Design_ by Alexandrescu; download the
accompanying library from http://sourceforge.net/projects/loki-lib).

Cheers! --M
Aug 15 '06 #3
markww wrote:
Hi mlimber,
Hi. Please don't top-post here. Put your reply inline or below the post
you are responding to.
Thanks for replying. I think my example is overly complicated and I may
have explained it incorrectly. Here's a much clearer example of what
I'm trying to do - I'm really new with function pointers and the like,
but if you have any suggestions with the following example that'd be
great. In the meantime I'll do the reading you suggested,

Thanks
struct TYPE_A {
string name, incoming;
};
N.B., this is not a POD struct (see
http://www.parashift.com/c++-faq-lit...html#faq-26.7).
Referring to it with a void pointer is not recommended.
struct TYPE_B {
int age;
};

int main()
{
TYPE_A a;
TYPE_B b;

ReadData(CopyStructA, a);

ReadData(CopyStructB, b);
I think you meant:

ReadData( &a, CopyStructA );
ReadData( &b, CopyStructB );

You'll also need CopyStructA and CopyStructB defined or have a
prototype above.
>
return 0;
}

void ReadData(void *pStruct, void *fnPtr)
I think you meant something like:

void ReadData(void *pStruct, void (*fnPtr)(const Data&, void*) )
{
// Read the data into 'data'.
fnPtr(data, pStruct);
}
Void pointers are generally discouraged in C++ since it throws away
type information. Use templates instead:

template<class T, class FN>
void ReadData( T& t, FN fn )
{
const Data data = /* get data somehow */;

// Convert data into the given struct
fn( data, t );
}
>
void CopyStructA( data, TYPE_A *a)
{
a.name = data.col(0);
a.incoming = data.col(1);
}

void CopyStructB(data, TYPE_B *b)
{
b.age = data.col(0);
}
What type is data? A POD struct? An object? What is the return type of
data.col()?

You might consider writing the data to a std::stringstream and then
extracting it with your custom std::ostream extraction operators for
each type.

Try writing a minimal but *complete* example that demonstrates what you
are trying to do. Then we can help you further. See
http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8 on how to
post code here.

Cheers! --M

Aug 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul | last post by:
Hi I am trying to learn how to set up and use generic functions (can operate on different data types). I have a simple function to add any type, set up a //template function to add any 2 types...
1
by: marco_segurini | last post by:
Hi, I like to know if is it possible to use sizeof(T) inside a generic function. I don't know why the following function compiles while if I define ON_LINE_STATEMENT it does not. ...
1
by: CES | last post by:
All, I was wondering if someone could point me to a tutorial on creating & accessing functions from within a wrapper function. I've created a group of functions related to a timer event. All...
23
by: I.M. !Knuth | last post by:
A while back, I was mucking around with a recursive function. For brevity's sake, I'll define it like this: int func_recurs(int arg1, int arg2, int prev_pos) { int crnt_pos = 0; int result; ...
3
by: benben | last post by:
I have always found Stroustrup's paper on generalized member function wrapper (http://www.research.att.com/~bs/wrapper.pdf) an interesting one. Recently I started to play with it. As I tried to put...
0
by: inquisitive | last post by:
Is it possible to have a vector of generic function pointers? I am able to devise a generic function pointer, this way: template <typename elemType, elemType (*function)(std::string&)> struct...
3
by: anoopsr | last post by:
hI, IHave a doubt in writing a generic function in C. Say for Example, I need to add 2 numbers. 2 interger Numbers or 2 float Numbers using the same function add(). How do i call the add...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
1
by: jlbfunes | last post by:
Hello, I am new to c++. I tried to create a function for doing the following: 1. Read a matrix from a .txt file 2. Store the retrieved data as a vector of vectors 3. Put the result in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.