473,511 Members | 12,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates Specializations for Event-Driven Programming

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
Jul 22 '05 #1
4 2040
"christopher diggins" <cd******@videotron.ca> wrote in message
news:_j*********************@wagner.videotron.net. ..
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
Jul 22 '05 #2
"christopher diggins" <cd******@videotron.ca> wrote in message
news:_j*********************@wagner.videotron.net. ..
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
Jul 22 '05 #3
"christopher diggins" <cd******@videotron.ca> wrote in message
news:Y7*******************@wagner.videotron.net...
"christopher diggins" <cd******@videotron.ca> wrote in message
news:_j*********************@wagner.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
Jul 22 '05 #4
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;
}

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
Jul 22 '05 #5

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

Similar topics

12
5235
by: Simon | last post by:
Hi, I'm having a problem with templates and specialisation. I'm using it to overload the same function so it can return different things. I can't see what I'm doing wrong, although my compiler...
5
4473
by: SainTiss | last post by:
Hi, Is there an extra overhead in using virtual functions and templates? Or is is just the same overhead as with regular classes? Either way, if you'd want to avoid it, will you always end up...
9
3166
by: Fred H | last post by:
I'm currently trying to write a function template that can fill a variable of arbitrary type with 'random' stuff, but I can't seem to get my function template working. In my .h file I've...
14
1458
by: Jigar Mehta | last post by:
Hye all, I am a VC++ programmer (have exp. of 2+ years).. But till now, I have never worked on templates.. I have developed whole products but never worked on templates or used them in my...
5
2035
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
7
1419
by: Vyacheslav Lanovets | last post by:
Hello, All! One of our target platforms has only 32 MB of virtual memory (Windows CE), so we decided to explicitly load some of our dlls. But the classes created inside such dlls are created...
2
1571
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
4
1766
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
104
4453
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
2305
by: matthias.neubauer | last post by:
I have problems understanding how overloading of function templates works. Consider first the following code without any function templates ... int foo(const char *& c) { return 0; }
0
7356
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,...
0
7427
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...
1
7085
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...
0
7512
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...
0
5671
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,...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
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 ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
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...

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.