473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Which way to handle events?

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.

Jul 22 '05 #1
2 1271
"Glen Able" <sm*************@hotmTHISail.com> wrote in message
news:ch*******************@news.demon.co.uk...
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


Jul 22 '05 #2
When looking at boost as Ivan suggests, try looking at boost/signal.
That is a great way to implement events...

"Glen Able" <sm*************@hotmTHISail.com> wrote in message
news:ch*******************@news.demon.co.uk...
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.

Jul 22 '05 #3

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

Similar topics

13
by: DraguVaso | last post by:
Hi, I have a Generic List, for instance: Public MyPersonnesDeContact As New System.Collections.Generic.List(Of clsPersonne) My clsPersonne raises some events, and I want to be able to handle...
0
by: lkr | last post by:
hi i got the code for handling some of the events in MSWord.But i need ScrollBar events to be handled .i got the handle to the word application window through an API using ForeGroundWindow().By...
7
by: lkr | last post by:
hi is there is anyway to capture the document events of a MSWord?eg:I have to handle the event awhen WM_KEYUP or anyother events will occur. give me a solution............ thanks in advance lkr
0
by: VB Programmer | last post by:
I was reading an article that talked about using the OnBubbleEvent overrides function to handle events from a web user control (http://www.ftponline.com/vsm/2002_11/online/hottips/delcogliano/). ...
7
by: Amadelle | last post by:
Hi all and thanks in advance, I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if...
1
by: Andreas Klemt | last post by:
Hello, how can I find out in Page_Load which button event is fired? Thanks for any help in advance!! Andreas
3
by: Jerry Wei | last post by:
Dear all: I have a problem about how to handle the events. For example, there are two forms, Form1 and Form2. Form2 is owned by Form1 and there is a button,Button1, in Form2. It is no problem for...
4
by: Paul Fi | last post by:
I want to handle external events from my c# app, especially to events from windows explorer (windows shell) and windows media player how do i go about handling such external events? *** Sent...
4
by: PokerMan | last post by:
Hi I have a few controls on apage that cause a postback. But want to handle a postback differently depending on which one of these controls fired the postback. How do we do this? c#. Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.