Templates Specializations for Event-Driven Programming

  • Thread starter christopher diggins
  • Start date
M

Mike Wahler

christopher diggins said:
I just wrote an article on using template specializations for event-driven
programming ( http://www.artima.com/weblogs/viewpost.jsp?thread=84958 ).

Are there any other examples of this kind of approach? I had trouble finding
some on Google. Thanks.

See the current issue of CUJ: "Win32 GUI Generics" by John Torjo.
Yes, it's Windows specific, but I think the concepts are applicable
to other platforms. He addresses issues I've wrestled with when
doing Windows GUI in C.

-Mike
 
C

christopher diggins

I have made a new post at
http://www.artima.com/weblogs/viewpost.jsp?thread=85301 which demonstrates
the technique of template specialization for static event handlers. Here is
the source code for posterity:

#include <stdio.h>
#include <time.h>

const int BIG_NUM = 100000000;

const int EVENT_A = 0;
const int EVENT_B = 1;

int gnCnt = 0;

/////////////////////////////////////////////////
// static dispatch code

template<typename int>
inline bool StaticHandler(int nArg) {
return false;
}

template<typename Dummy_T>
void StaticDispatch() {
while (StaticHandler<EVENT_A>(BIG_NUM)) {
StaticHandler<EVENT_B>(1);
}
}

/////////////////////////////////////////////////
// dynamic dispatch code

typedef bool (*HandlerFxn)(int);

bool DefaultDynamicHandler(int nArg) {
return false;
}

HandlerFxn FxnPtrTable[2] = {
&DefaultDynamicHandler,
&DefaultDynamicHandler
};

void RegisterHandler(int EventCode, HandlerFxn pFxn) {
FxnPtrTable[EventCode] = pFxn;
}

void DynamicDispatch() {
while (FxnPtrTable[EVENT_A](BIG_NUM)) {
FxnPtrTable[EVENT_B](1);
}
}

/////////////////////////////////////////////////
// dynamic user-defined event handlers

inline bool DynamicHandlerA(int nArg) {
return gnCnt < nArg;
}

inline bool DynamicHandlerB(int nArg) {
gnCnt += nArg;
return true;
}

/////////////////////////////////////////////////
// static user-defined event handlers

template<>
inline bool StaticHandler<EVENT_A>(int nArg) {
return gnCnt < nArg;
}

template<>
inline bool StaticHandler<EVENT_B>(int nArg) {
gnCnt += nArg;
return true;
}

/////////////////////////////////////////////////
// main entry point

int main()
{
int nStart;
int nEnd;

{
gnCnt = 0;
RegisterHandler(EVENT_A, &DynamicHandlerA);
RegisterHandler(EVENT_B, &DynamicHandlerB);
nStart = clock();
DynamicDispatch();
nEnd = clock();
printf("time elapsed for dynamic dispatch %d msec\n", (nEnd - nStart) *
CLOCKS_PER_SEC / 1000);
}

{
gnCnt = 0;
nStart = clock();
StaticDispatch<void>();
nEnd = clock();
printf("time elapsed for static dispatch %d msec\n", (nEnd - nStart) *
CLOCKS_PER_SEC / 1000);
}
getchar();
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top