M
markww
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
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