Which way to handle events?

Discussion in 'C++' started by Glen Able, Sep 6, 2004.

  1. Glen Able

    Glen Able Guest

    Hello

    I'm currently writing some library code and I want to allow certain events
    to be handled by whatever app is using this library. E.g. responding to
    error messages by printing them/logging them to a file/whatever.

    I can see two good options:

    1) Hold a function ptr which can be set by the application to point to one
    of its (presumably static?) methods.

    2) Hold a ptr to an object of some sort of 'handler' class, so the app can
    derive its own type from this and override the virtual methods. This could
    be an abstract base class, or possibly could have default empty
    implementations of the methods.

    Anyone have any thoughts about which of these various possibilities would be
    preferable, or even come up with something better?

    thanks!
    G.A.
     
    Glen Able, Sep 6, 2004
    #1
    1. Advertising

  2. "Glen Able" <> wrote in message
    news:chhhgm$4hu$1$...
    > I'm currently writing some library code and I want to allow certain events
    > to be handled by whatever app is using this library. E.g. responding to
    > error messages by printing them/logging them to a file/whatever.
    >
    > I can see two good options:
    >
    > 1) Hold a function ptr which can be set by the application to point to one
    > of its (presumably static?) methods.
    >
    > 2) Hold a ptr to an object of some sort of 'handler' class, so the app can
    > derive its own type from this and override the virtual methods. This
    > could
    > be an abstract base class, or possibly could have default empty
    > implementations of the methods.
    >
    > Anyone have any thoughts about which of these various possibilities would
    > be
    > preferable, or even come up with something better?


    Your option 2) is the most straightforward, and is the best
    choice in some cases.

    Plain function pointers (as in 1) are error prone, and are best avoided.
    They could be replaced by function objects (i.e. a callback to a method
    of a specific objects), such as those in boost::function. See:
    http://www.boost.org/doc/html/function.html
    Such a callback template is expected to be part of the standard library
    in the next C++ standard.

    Alternatively, depending on circumstances, using another design pattern
    such as Observer-Listener, might be a better choice...


    Cheers,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Sep 6, 2004
    #2
    1. Advertising

  3. When looking at boost as Ivan suggests, try looking at boost/signal.
    That is a great way to implement events...

    "Glen Able" <> wrote in message
    news:chhhgm$4hu$1$...
    > Hello
    >
    > I'm currently writing some library code and I want to allow certain events
    > to be handled by whatever app is using this library. E.g. responding to
    > error messages by printing them/logging them to a file/whatever.
    >
    > I can see two good options:
    >
    > 1) Hold a function ptr which can be set by the application to point to one
    > of its (presumably static?) methods.
    >
    > 2) Hold a ptr to an object of some sort of 'handler' class, so the app can
    > derive its own type from this and override the virtual methods. This

    could
    > be an abstract base class, or possibly could have default empty
    > implementations of the methods.
    >
    > Anyone have any thoughts about which of these various possibilities would

    be
    > preferable, or even come up with something better?
    >
    > thanks!
    > G.A.
    >
    >
    >
     
    Jesper Madsen, Sep 7, 2004
    #3
    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. Dietrich
    Replies:
    1
    Views:
    668
    Joe Smith
    Jul 22, 2004
  2. Leon
    Replies:
    2
    Views:
    564
  3. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    475
    hanumizzle
    Oct 6, 2006
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    423
    Chris
    Aug 30, 2005
  5. Eddy Xu
    Replies:
    5
    Views:
    141
    Eddy Xu
    Apr 11, 2008
Loading...

Share This Page