Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-02

New multicast event implementation

11 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anthony Walter

Hello all. I implemented a new type of multicast events in Delphi based on generics. I thought someone could maybe consider and convert this implementation to Free Pascal. Here is a link to an article describing the implementation, its benefits and usage, and well as the full source code. Let me know what you think.

http://www.codebot.org/delphi/?doc=9568

Here are two examples where multicast events to are benefiting me .

1) I have a SystemEvents singleton class that exposes multicast events to which other classes may connect. Events include OnFontChange, OnColorsChange, OnUserSwitch ... which fire when the system fonts or colors change etc.

2) I've written an application configuration class which can be edited while the program is running. This class exposes an OnChange multicast event which many other objects listen to and take action upon if they are interested in configuration changes, such as updating layout or visible options ( http://imagebot.org/snapshops/edit-dialog.jpg).

Reply by email

Unknown author
to Anthony Walter
Anthony Walter wrote 20 lines
Hello all. I implemented a new type of multicast events in Delphi based on generics. I thought someone could maybe consider and convert this implementation to Free Pascal. Here is a link to an article describing the implementation, its benefits and usage, and well as the full source code. Let me know what you think. http://www.codebot.org/delphi/?doc=9568 Here are two examples where multicast events to are benefiting me . 1) I have a SystemEvents singleton class that exposes multicast events to which other classes may connect. Events include OnFontChange, OnColorsChange, OnUserSwitch ... which fire when the system fonts or colors change etc. 2) I've written an application configuration class which can be edited while the program is running. This class exposes an OnChange multicast event which many other objects listen to and take action upon if they are interested in configuration changes, such as updating layout or visible options ( http://imagebot.org/snapshops/edit-dialog.jpg).

Why did you not use the observer pattern as used in tiOPF ? It does not need generics, and is well tested since a long time.

Michael.

Reply by email

Ben
to Anthony Walter: Hello all. I implemented a new type of multicast events in Delphi based on generics. I…

Op 2011-02-02 03:45, Anthony Walter het geskryf:

Hello all. I implemented a new type of multicast events in Delphi based

I've always liked the idea of supporting multiple event handlers per event. Java, Qt, C# has had it for years.

BTW:
The print function (printer friendly version) at the bottom of your website excludes all the source code examples - making the printout rather useless. Just thought I would mention that.

Ben.

Reply by email

Anthony Walter
to Ben

Yeah, I just added the code highlighter, which build the highlights using javascript. I set to "display: none" while the page loads which isn't effecting the print version. The workaround is that you can print the code blocks by clicking the print icon in each code section. I'll fix it later.

Regarding the observer pattern, I'll refer you to this stack overflow question:

http://stackoverflow.com/questions/550785/c-events-or-an-observer-interface-pros-cons

My take is that event are more natural and that they are probably more well established than the pattern of creating an interface to subscribe to events. Once the plumbing is in place (adding method pointer types to a language), they are much easier to hook up. Events also allow you hook up to the only the notifications in which you are interested. If you've used Visual Basic (the old one), Delphi, or C# hooking up mouse click, key press, or selection change events becomes more natural.

Reply by email

Unknown author
to Anthony Walter: Yeah, I just added the code highlighter, which build the highlights using javascript. I…
Anthony Walter wrote 13 lines
Yeah, I just added the code highlighter, which build the highlights using javascript. I set to "display: none" while the page loads which isn't effecting the print version. The workaround is that you can print the code blocks by clicking the print icon in each code section. I'll fix it later. Regarding the observer pattern, I'll refer you to this stack overflow question: http://stackoverflow.com/questions/550785/c-events-or-an-observer-interface-pros-cons My take is that event are more natural and that they are probably more well established than the pattern of creating an interface to subscribe to events.

By contrast, generics are not established at all in Object Pascal, so I wouldn't exactly agree with you that it is more natural :-)

Michael.

Reply by email

Anthony Walter
to Unknown author

By the way, does Free Pascal generic support generic constraints yet? Also, this format seems more natural to me:

TCollection<T: TCollectionItem> = class
  property Count: Integer;
  property Items[Index: Integer]: T;
end;
TStatusPanels = class(TCollection<TStatusPanel>);

rather than ...

generic TCollection<T> = class // no constrain ability
  property Count: Integer;
  property Items[Index: Integer]: T;
end;
TStatusPanels = specialize TCollection<TStatusPanel>;

Reply by email

Unknown author
to Anthony Walter
Anthony Walter wrote
By the way, does Free Pascal generic support generic constraints yet? Also, this format seems more natural to me:

No, it does not.

Michael.

Show quoted text 17 lines
TCollection<T: TCollectionItem> = class property Count: Integer; property Items[Index: Integer]: T; end; TStatusPanels = class(TCollection<TStatusPanel>); rather than ... generic TCollection<T> = class // no constrain ability property Count: Integer; property Items[Index: Integer]: T; end; TStatusPanels = specialize TCollection<TStatusPanel>;

Reply by email

Ben
to Anthony Walter: Yeah, I just added the code highlighter, which build the highlights using javascript. I…

Op 2011-02-02 15:55, Anthony Walter het geskryf:

isn't effecting the print version. The workaround is that you can print the code blocks by clicking the print icon in each code section. I'll fix it later.

I simply used the Browser's Print menu option, instead of the "printer friendly webpage option", and that worked fine for my needs.

Show quoted text 7 lines
Regarding the observer pattern, I'll refer you to this stack overflow question: http://stackoverflow.com/questions/550785/c-events-or-an-observer-interface-pros-cons My take is that event are more natural and that they are probably more well established than the pattern of creating an interface to subscribe

Regarding that stackoverlflow question. That is highly dependant on how you implemented the observer in your BOM. Years ago, I implemented a observer via Interfaces in our Object Pascal code, where I could observe just the fields of a class I was interested in. No generics required, just a plain observer implementation using a InterfaceList - by creating a base BOM class which all your other BO descend from.

I'm not saying your idea or implementation is bad - though personally I have never had the need to use generics yet (and it seems neither has many others - good or bad language feature, I don't know?). I'm just pointing out that there are multiple ways of doing the same thing. Observer could very easily be used too, and without the pitfalls listed in the Stack Overflow article.

Ben.

Reply by email

Anthony Walter
to Ben

Have you used List<T>, Dictionary<K,V>, or IEnumerable<T> in C# much? How about EventHandler<T>? They are pretty useful.

Even in Delphi, TList<T>, TObjectList<T>, and TCompare<T> are more than handy IMO. No longer do I have to write "TMyObject(FList[I])" all over the place. When using "if FList.IndexOf(Pointer(FInstance)) > 1" not only do you need a typecast again, but you lose the ability to write or build in TCompare<T>, for example to consider objects equal by comparing a specific field.

Anyhow, I didn't mean this discussion to be about the merits of generics in programming.

Reply by email

Sven Barth
to Anthony Walter: By the way, does Free Pascal generic support generic constraints yet? Also, this format…
Anthony Walter wrote
By the way, does Free Pascal generic support generic constraints yet? Also, this format seems more natural to me:

They aren't implemented currently, but I hope they will at some time.

Show quoted text 15 lines
TCollection<T: TCollectionItem> = class property Count: Integer; property Items[Index: Integer]: T; end; TStatusPanels = class(TCollection<TStatusPanel>); rather than ... generic TCollection<T> = class // no constrain ability property Count: Integer; property Items[Index: Integer]: T; end; TStatusPanels = specialize TCollection<TStatusPanel>;

When using FPC 2.5.1 you can use the Delphi compatible syntax in mode Delphi (not supported in mode ObjFPC).

When constraints are implemented they should work in both modes.

Regards,
Sven

Reply by email