14

I'm looking for some code allowing easy asigning many event handlers to a single event of object... I needed it very much for my plugin architecture, so I implemented some replacement for that, however I don't like my solution for that, so I'd like to know what is yours idea/solution/tip ... My solution is just a list with items like eventName: string; proc: TMyEventProc; where TMyEventProc takes two arguments, sender and eventData: pointer. depending on name of the event, eventData points to different record / object.

Unfortunately this requires declaration of many, many records for being passed as argument. The method is also very slow, and requires to implement calling the required "callbacks" while the "real" event gets hit.

migajek
  • 8,524
  • 15
  • 77
  • 116

4 Answers4

11

I implemented a solution to this that works in just about any version of Delphi - it was originally implemented in Delphi 7, although I haven't tested it in earlier versions (but if you're using Delphi 7 yourself, then that's all you need to know, right?). :)

iirc this was at least in part the inspiration for Allen Bauer's post. You can see my implementation demonstrated in some videos and download the code from my blog:

The posts you are interested in are tagged "multicast". The download link for the final code is available in this post.

In my approach, you derive a class from TMultiCastEvent. All your derived class has to do is implement some simple type safety protection for adding, removing and invoking an event with a specific signature.

An implementation for TNotifyEvent - procedure(Sender: TObject) - is provided with the implementation both "to get you going" (ime most "useful" multicast events are simple notifications) and also as an example of how to derive multicast event classes for specific event signatures.

Once you have your multicast event class you can use regular "event handlers" interchangeably with the multi-cast version, e.g. given some imaginary button class with a multi-cast On_Click event (I've adopted a convention of interposing an underscore in the event name to identify it as multicast, vs regular "uni-cast" events):

Code that assigns a handler to a unicast click event:

  Button.OnClick := MyClickHandler;

Can directly add that same handler to a multi-cast Notify event:

  MultiCastButton.On_Click.Add(MyClickHandler);

My implementation also includes a number of refinements, such as the ability to disable events and have handlers automatically removed from handlers when the implementing object is destroyed (this involves a small amount of housekeeping which can be ignored if necessary but which can be useful under certain circumstances).

All of which is described and demonstrated in my blog posts.

Enjoy. :)

Deltics
  • 22,162
  • 2
  • 42
  • 70
2

Allen Bauer has a blog post about multicast events that might be helpful. It only works for Delphi 2009 or later, though.

EDIT: If you're still on D7, you might still be able to make this work if you don't need too many different event signatures. Try looking at Allen's code and see if you can adapt it to a non-generic solution.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • unfortunately, I'm using D7 Personal ... I'm a poor student ;) Can't afford D2009 ... – migajek Sep 08 '09 at 23:21
  • I hear you. I'd really like to see a personal edition of D2009 too, but apparently releasing one would cut into the Delphi Professional sales too much, and unlike Microsoft, Embarcadero doesn't have huge non-dev divisions bringing in money to subsidize that. – Mason Wheeler Sep 08 '09 at 23:39
  • 3
    The question is in the long term is "can Embarcadero afford to NOT have a cheap version for 'poor students' and hobbyists" – Gerry Coll Sep 08 '09 at 23:55
  • @Gerry: +1. I wish I could + that more than once! @Argalatyr: Depends on the student. It sure was for me when I was taking 16 credits (plus all the homework that implies) and didn't have time to even *think* about a job... – Mason Wheeler Sep 09 '09 at 00:54
  • Plus, getting college students is nice, but the kids Embarcadero really needs to attract for long-term viability are in grade school, and *they* almost certainly can't afford it. – Mason Wheeler Sep 09 '09 at 01:41
  • @Mason: don't assume you have bragging rights on hard work in school. People just have to set priorities. I agree that Embarcadero would do well to make Delphi as accessible as possible, but I have no doubt $98 is within reach for those who try. – Argalatyr Sep 09 '09 at 04:00
  • Sorry if that came off as "bragging rights". I meant it as more of a neutral explanation. I don't doubt that a lot of people on here worked even harder than I did during college. – Mason Wheeler Sep 09 '09 at 04:34
  • And I came across heavy-handedly. I do think people confuse priorities, and I'd like to emphasize that D2010 Pro is worth more than a pair of new sneakers. – Argalatyr Sep 09 '09 at 04:41
  • Heh. My sneakers actually cost a fair bit more than that academic version. :P I could never justify the purchase except each pair lasts for *years* and they're the most comfortable thing I've ever worn on my feet that wasn't house slippers. – Mason Wheeler Sep 09 '09 at 04:50
  • Huh, 98USD is much better than the full price, but it's still quite a lot ... especially that I'm using Delphi for hobby programming, if I need some commercial usage, I compile the code in D 2005 Personal which allows for commercial usage... anyway I'd be interested, but can't find any info on differences in licensing between "academic" and normal edition ... ? – migajek Sep 09 '09 at 08:29
  • If you are interested in looking at full source code of (basically) Allen Bauer's generic multicast event class, it is available to look at here -> http://stackoverflow.com/questions/1225256/getting-allen-bauers-tmulticasteventt-working – Nat Sep 09 '09 at 13:41
  • Allen's original code didn't work under 64 bit. You can find a updated version here: https://github.com/JensBorrisholt/OnlineOffline – Jens Borrisholt Sep 30 '18 at 18:33
1

If you're implementing a plugin system, I think, you can't just get away with event handlers - be it multicast or not. I suggest having a look at observer pattern. Might sound a little bit too verbose near multicast events but at least more flexible when you need.

utku_karatas
  • 6,163
  • 4
  • 40
  • 52
  • actually I know that patter, even though I didn't knew it's name ;) anyway what I need is some kind of automation while integrating with VCL. I have many controls types which events I need to catch ... – migajek Sep 09 '09 at 16:59
0

You can use the observer design pattern for that. Here is a sample delphi implementation: http://blogs.teamb.com/joannacarter/2004/06/30/690