473,748 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

design woes ... confused on an implementation approach

Select parameters in a vendors API file is as follows:

#ifdef __cplusplus
typedef void (*VOIDFUNCPTR)( ...); /* ptr to function returning void
*/
#else
typedef void (*VOIDFUNCPTR)( ); /* ptr to function returning void
*/
#endif /* __cplusplus */

enum CAUSE { CHAN0_COMPLETE, CHAN0_MISS, CHAN0_HIT, CHAN0_ISM };
bool IntConnect( CAUSE c, VOIDFUNCPTR p, int idx, int jdx);
// end vendors file

The member function IntConnect that takes a cause; a VOIDFUNCPTR; and
a two integer variables.

The basic premise: You'll pass your desired - static/global - member
function + cause, etc .. to the vendors IntConnect member function.

To bypass the 'procedural approach' I have a class transfer that takes
a map and a function pointer.

class transfer {
static std::auto_ptr<c b_base> user_cb;
map<CAUSE, VOIDFUNCPTR> myMap;
static void callback_mem_fu nc1(...) {
// call user function passed in via connect_interru pt
cb_base &cb = *user_cb;
}
static void callback_mem_fu nc2(...) {
// call user function passed in via connect_interru pt
cb_base &cb = *user_cb;
}
/// more

public:
transfer() {
myMap[CHAN0_COMPLETE] = &transfer::call back_mem_func1;
myMap[CHAN0_MISS] = &transfer::call back_mem_func2;
// more
}

template <typename T>
bool connect_interru pt(
CAUSE c,
int param1,
int param2,
T& t,
void (T::*f)(...) ) )
{
map<CAUSE, VOIDFUNCPTR>::i terator it = myMap.begin();
for (; it != myMap.end(); ++it)
{
if ( it->first == c )
{
bool success = IntConnect(
(*it).first,
(*it).second,
param1,
param2
);

if ( success ) {
user_cb.reset(n ew cb_derived<T>(t , f));
}
break;
}
} // end for
};
With this approach I'll register with IntConnect a static member
function. The static member function will - in turn - call user
desired member function.

At issue: The current approach to user_cb WONT work. I need to have
vector or something similar .. This way within the member functions
callback_mem_fu nc1, etc. I could do:

void callback_mem_fu nc1(...) {
// call user function passed in via connect_interru pt
cb_base &cb = *user_cb[0]; // <<<<NOTE HERE>>>
}
void callback_mem_fu nc2(...) {
// call user function passed in via connect_interru pt
cb_base &cb = *user_cb[1]; // <<<<NOTE HERE>>>
}
or ... I'm open to a re-design worse case.

// cb_base:

# include <iostream>
# include <memory>
# include <string>

using namespace std;
class cb_base
{
public:
virtual void operator()(...) const {};
virtual ~cb_base() = 0;
};

inline cb_base::~cb_ba se() {}

template <typename T>
class cb_derived : public cb_base
{
public:
typedef void (T::*F)(...);
cb_derived( T& t, F f) : t_(&t), f_(f) {}
void operator()(...) const
{
(t_->*f_)();
}

private:
T* t_;
F f_;
};

template <typename T>
cb_derived<T> make_callback (T& t, void (T::*f)(...) )
{
return cb_derived<T>(t , f);
}

template <class T>
std::auto_ptr<c b_base> new_callback(T& t, void (T::*f)(...))
{
return std::auto_ptr<c b_base>(new cb_derived<T>(t , f));
}
Thanks in advance

Aug 1 '05 #1
5 1612
ma******@pegasu s.cc.ucf.edu wrote:
Select parameters in a vendors API file is as follows:

#ifdef __cplusplus
typedef void (*VOIDFUNCPTR)( ...); /* ptr to function returning void
*/
#else
typedef void (*VOIDFUNCPTR)( ); /* ptr to function returning void
*/
#endif /* __cplusplus */

enum CAUSE { CHAN0_COMPLETE, CHAN0_MISS, CHAN0_HIT, CHAN0_ISM };
bool IntConnect( CAUSE c, VOIDFUNCPTR p, int idx, int jdx);
// end vendors file


Are you sure that VOIDFUNCPTR takes no arguments before the ellipsis?
Because as it's declared now there is no way to get arguments from
inside VOIDFUNCPTR callback.

Aug 1 '05 #2
I'm sure. That is the way VOIDFUNCPTR is declared. It was a surprise
to me when I encountered it. I never understood the need for the
ellipsis.

Aug 1 '05 #3
ma******@pegasu s.cc.ucf.edu wrote:
I'm sure. That is the way VOIDFUNCPTR is declared. It was a surprise
to me when I encountered it. I never understood the need for the
ellipsis.


Then it seems to be no way to pass any data to the callback, the
callback has to operate on global data.

Aug 1 '05 #4
Yeah but the more important questions is what's teh workaround for
'user_cb'?. user_cb needs to reflect a list/vector or .......... How
would you achieve this?

Aug 1 '05 #5

ma******@pegasu s.cc.ucf.edu wrote:
Yeah but the more important questions is what's teh workaround for
'user_cb'?. user_cb needs to reflect a list/vector or .......... How
would you achieve this?


I'd do something along these lines:

typedef void (*VOIDFUNCPTR)( );

enum CAUSE { CHAN0_COMPLETE, CHAN0_MISS, CHAN0_HIT, CHAN0_ISM };
bool IntConnect( CAUSE c, VOIDFUNCPTR p, int idx, int jdx);

template<int N> struct int_ {};

template<int N>
void the_callback(in t_<N>); // implement it
// alternatively
// void the_callback(in t_<CHAN0_COMPLE TE>);
// void the_callback(in t_<CHAN0_MISS>) ;

template<int N>
void callback_thunk( )
{
the_callback(in t_<N>());
}

void f()
{
IntConnect(CHAN 0_COMPLETE, callback_thunk< CHAN0_COMPLETE> , 0, 0);
IntConnect(CHAN 0_MISS, callback_thunk< CHAN0_MISS>, 0, 0);
// ...
}

Aug 2 '05 #6

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

Similar topics

3
3148
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
4
2382
by: clintonG | last post by:
I'd like to know about application(s) or the name by which the following process is referred which would allow a business analyst to enter the following statements which when submitted would result in the generation of class library members... "Are you an individual person not working?" if no, ask "have you ever worked for an employer?" if no, ask "Are you thirteen years of age or younger?" ...
10
1382
by: ma740988 | last post by:
I was relieved somewhat when I saw a post by Andrei Alexandrescu highlighting an similar issue I'm faced with: So here's what Andrei Alexandrescu wrote: I code a generic object factory. Obviously, the factory has a member function like CreateObject that returns a newly allocated object. The problem is, I break an important coding standard: never return pointers to newly allocated memory. Never pass ownership by returning pointers. ...
22
5467
by: inigo.villalba | last post by:
Hi, I hope someone can point out my error because I'm starting to lose my hair over this. It's probably a very straigh forward error but after only 4 hours sleep it's doing my head in. It's to do with global variables in Firefox 1. The following code works no problems and generates a whole bunch of numbers <script type="text/javascript">
2
1396
by: Chris | last post by:
Hi, I have been stuck trying to come up with a design for days. I am working on a small project regarding barcode and I want to implement a factory design. I am now confused. I decided factory pattern since I am dealing with EAN13, UPCA12 UPC8 etc. Can someone provide a small sample on how to start. I have looked at the factory implementation online but still can't figure it out. Can someone provide a sample jsut to start with.
0
2508
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
3
1836
by: Mousam | last post by:
Hi All, First of all forgive me for the length of this post. I am developing one library for some text editor. In this library I want to provide a set of C++ classes which will enable client (of the library) to add tool bars to the text editor. The requirements are: -Client can add as many tool bars as he wants to add. -In each toolbar there will be some tools. Tools can be of button
20
3088
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in like 65% of the time of the second routine. Yet both do the same thing. However, the second one seems better in terms of the way the code is written since it helps encapsulate the transformation in the inner loop better making it easier to read,...
8
1631
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I have a couple of questions about the proper design of classes. I'll use a simple Customer class for my question. 1) Lets say that I have this Customer class like I said, and I want to distinguish between different types of customers, for example private, business, other etc. This will allow me to filter customers based on their type. Should I
0
8983
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...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9236
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...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
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
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
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.