C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Templates Specializations for Event-Driven Programming

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
christopher diggins
Guest





PostPosted: Sat Dec 18, 2004 4:57 pm    Post subject: Templates Specializations for Event-Driven Programming Reply with quote



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.

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com


Back to top
Mike Wahler
Guest





PostPosted: Sat Dec 18, 2004 9:18 pm    Post subject: Re: Templates Specializations for Event-Driven Programming Reply with quote



"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote

Quote:
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



Back to top
christopher diggins
Guest





PostPosted: Sun Dec 19, 2004 4:38 pm    Post subject: Re: Templates Specializations for Event-Driven Programming Reply with quote



"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote

Quote:
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.


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





PostPosted: Tue Dec 21, 2004 3:13 am    Post subject: Re: Templates Specializations for Event-Driven Programming Reply with quote

"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote

Quote:
"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote in message
news:_jZwd.31251$CK4.1802459 (AT) wagner (DOT) videotron.net...
I just wrote an article on using template specializations for event-driven
programming ( http://www.artima.com/weblogs/viewpost.jsp?thread=84958 ).


I have followed this up with a more realistic example of event-driven
programming with template specializations for win32 gui apps at
http://www.codeproject.com/useritems/winevent.asp

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com



Back to top
christopher diggins
Guest





PostPosted: Tue Dec 21, 2004 6:42 am    Post subject: Re: Templates Specializations for Event-Driven Programming Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.