473,727 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 MyDatabaseWrapp er(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 MyGenericDataba seWrapper(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 CopyFunctionPtr StructA(struct &A, &data)
{
A.income = data.col(0);
}

void CopyFunctionPtr StructB(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 3556
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 MyDatabaseWrapp er(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 MyGenericDataba seWrapper(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 CopyFunctionPtr StructA(struct &A, &data)
{
A.income = data.col(0);
}

void CopyFunctionPtr StructB(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(CopySt ructA, a);

ReadData(CopySt ructB, b);

return 0;
}

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

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

void CopyStructB(dat a, 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 MyDatabaseWrapp er(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 MyGenericDataba seWrapper(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 CopyFunctionPtr StructA(struct &A, &data)
{
A.income = data.col(0);
}

void CopyFunctionPtr StructB(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(CopySt ructA, a);

ReadData(CopySt ructB, 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(dat a, 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::stringstre am 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
2168
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 togethe template<class T T Addition(T first,T second T tempvar tempvar=first + second return (tempvar)
1
3201
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. generic<typename T> void CopyArrayFromArray(array<T>^ % vVal, int size
1
2527
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 works well until I try to enclose these functions into a class like structure ie: function dTimer(div){ //Insert the Javascript Code Below startTimer(); }
23
1648
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; /* stuff happens to arg1, arg2, and crnt_pos */
3
1716
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 some extra data members (such as a std::vector) to the Call_proxy<class template it started to worry me because if the suffix in the destructor throws, there can be a memory leak. (See relevant code below) The Call_proxy<uses its own...
0
1381
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 Method { inline elemType operator()(std::string & property) { return function(property);
3
3192
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 function i.e how do i send parameters. How do i receive the parameters in add fucntion definition. #include<stdio.h>
32
5664
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 and lccwin32 for example) which I consider to be perfectly reasonable: you can cast every kind of function pointer to a void pointer and void pointers to any kind of function pointer. This follows the general "generics through void scheme" of C....
1
1715
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 screen. I could somehow complete parts 1 and 2. However, I am having lots of problems with the third part. I tried to use a generic function, and I am getting this complaint from my compiler:
0
8890
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9402
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8101
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.