Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Which way to handle events?

Reply
Thread Tools

Which way to handle events?

 
 
Glen Able
Guest
Posts: n/a
 
      09-06-2004
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.



 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-06-2004
"Glen Able" <(E-Mail Removed)> wrote in message
news:chhhgm$4hu$1$(E-Mail Removed)...
> 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




 
Reply With Quote
 
 
 
 
Jesper Madsen
Guest
Posts: n/a
 
      09-07-2004
When looking at boost as Ivan suggests, try looking at boost/signal.
That is a great way to implement events...

"Glen Able" <(E-Mail Removed)> wrote in message
news:chhhgm$4hu$1$(E-Mail Removed)...
> 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.
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Package to handle table text render (handle space or tab betweenthe columns) ? =?ISO-8859-1?Q?KLEIN_St=E9phane?= Python 3 10-06-2006 08:46 AM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Possible to handle web requests without an ASPX page? i.e. have DLL handle request. jdlwright@shaw.ca ASP .Net 2 05-31-2005 05:42 PM
how to handle command line output(not terminal handle) Leon Python 2 11-04-2004 05:16 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM



Advertisments