Templates Specializations for Event-Driven Programming

Discussion in 'C++' started by christopher diggins, Dec 18, 2004.

  1. christopher diggins, Dec 18, 2004
    #1
    1. Advertising

  2. christopher diggins

    Mike Wahler Guest

    "christopher diggins" <> wrote in message
    news:_jZwd.31251$...
    > 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
     
    Mike Wahler, Dec 18, 2004
    #2
    1. Advertising

  3. "christopher diggins" <> wrote in message
    news:_jZwd.31251$...
    >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
     
    christopher diggins, Dec 19, 2004
    #3
  4. "christopher diggins" <> wrote in message
    news:Y7ixd.7361$...
    > "christopher diggins" <> wrote in message
    > news:_jZwd.31251$...
    >>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
     
    christopher diggins, Dec 21, 2004
    #4
  5. 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
     
    christopher diggins, Dec 21, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    492
  2. Steve
    Replies:
    2
    Views:
    560
    Carl Cerecke
    Jan 23, 2006
  3. recover
    Replies:
    2
    Views:
    831
    recover
    Jul 25, 2006
  4. sebek

    Event-driven programming

    sebek, May 27, 2007, in forum: Java
    Replies:
    10
    Views:
    722
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Jun 3, 2007
  5. Kottiyath

    Event Driven programming - Doubts

    Kottiyath, Dec 22, 2008, in forum: Python
    Replies:
    3
    Views:
    389
    Bryan Olson
    Dec 23, 2008
Loading...

Share This Page