473,671 Members | 2,371 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 2052
"christophe r diggins" <cd******@video tron.ca> wrote in message
news:_j******** *************@w agner.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
"christophe r diggins" <cd******@video tron.ca> wrote in message
news:_j******** *************@w agner.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
"christophe r diggins" <cd******@video tron.ca> wrote in message
news:Y7******** ***********@wag ner.videotron.n et...
"christophe r diggins" <cd******@video tron.ca> wrote in message
news:_j******** *************@w agner.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<typena me int>
inline bool StaticHandler(i nt nArg) {
return false;
}

template<typena me Dummy_T>
void StaticDispatch( ) {
while (StaticHandler< EVENT_A>(BIG_NU M)) {
StaticHandler<E VENT_B>(1);
}
}

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

typedef bool (*HandlerFxn)(i nt);

bool DefaultDynamicH andler(int nArg) {
return false;
}

HandlerFxn FxnPtrTable[2] = {
&DefaultDynamic Handler,
&DefaultDynamic Handler
};

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<E VENT_A>(int nArg) {
return gnCnt < nArg;
}

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

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

int main()
{
int nStart;
int nEnd;

{
gnCnt = 0;
RegisterHandler (EVENT_A, &DynamicHandler A);
RegisterHandler (EVENT_B, &DynamicHandler B);
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
5249
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 promises me I am! Here follows an example of my code, and i've included the error VC is giving me if that helps. Can anyone point me in the right direction? Cheers, Simon ;o) --
5
4482
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 with code duplication? For example: let's say you've got two template specializations, which differ only slightly... So you put the common behaviour in a
9
3176
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 declared the function template like this, inside a class I'm building: template <typename T> T RandBits(); In my .cpp file I've implemented it like this:
14
1472
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 applications... But now, I think as a full-fledged programmer, this is the thing I have not learned.. And want to start with templates, so, can you please guide me from where to start with ?? What is the general meaning of template ??
5
2049
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 some investigating, I narrowed this down to a very odd behavior (bug?) of the VC++.NET 2003 compiler: If a class that is declared as __declspec(dllimport) derives from a template, that template's methods are never inlined, even if declared with...
7
1424
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 with vfptr pointed to address space of the dll. So when DLL is unloaded vtable is destroyed. The virtual clone() function of such instance creates the class with the same vfptr :)
2
1590
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
4
1777
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? The code is as follows... // main class: template < class Individual,
104
4562
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 feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
7
2315
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
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
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
8667
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
6222
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
4221
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
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2806
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
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
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.