| View previous topic :: View next topic |
| Author |
Message |
christopher diggins Guest
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sat Dec 18, 2004 9:18 pm Post subject: Re: Templates Specializations for Event-Driven Programming |
|
|
"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote
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
|
|
| Back to top |
|
 |
christopher diggins Guest
|
Posted: Sun Dec 19, 2004 4:38 pm Post subject: Re: Templates Specializations for Event-Driven Programming |
|
|
"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote
There were a lot of typos in the code. I fixed them and updated the article
a bit to try and reduce confusion.
--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
|
|
| Back to top |
|
 |
christopher diggins Guest
|
|
| Back to top |
|
 |
christopher diggins Guest
|
Posted: Tue Dec 21, 2004 6:42 am Post subject: Re: Templates Specializations for Event-Driven Programming |
|
|
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 msecn", (nEnd - nStart) *
CLOCKS_PER_SEC / 1000);
}
{
gnCnt = 0;
nStart = clock();
StaticDispatch<void>();
nEnd = clock();
printf("time elapsed for static dispatch %d msecn", (nEnd - nStart) *
CLOCKS_PER_SEC / 1000);
}
getchar();
return 0;
}
--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
|
|
| Back to top |
|
 |
|