Archived

This topic is now archived and is closed to further replies.

On the use of __event

This topic is 5574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ever since I laid my hands on Visual Studio .NET for the first time, this has been my favorite addition--the __event keyword. I just can''t get enough of these little suckers. It is, in my opinion, a sorely needed addition to the C++ language. Anyways, I was wondering what kind of experience everybody else has with its usage? I can think of just about a million and one places I''d like to use events, but just how efficient are they? Are they fast enough that using them widely throughout the core logic of a game engine would not become prohibitively slow? Any experiences anybody would like to share regarding using events in game programming?

Share this post


Link to post
Share on other sites
haven''t used them yet, but aren''t they just some sort of automagic callback functions? at least, those are used in most engines for very much stuff..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
The Boost library offers Signals & Slots which have the same functionality, but are more flexible and portable . They should be integrated to the main library tree in the next (1.29) release (any day now, though it still being in Boost's CVS repository didn't stop me from using it). There is, therefore no urgent need to integrate them into the language itself (furthermore, that would not necessarily mesh with legacy APIs : look at how many people try to pass member function pointers to the Win32 API. Closures would be a very handy addition on the other hand).

The main use of signals (objects to which you can dynamically connect and disconnect functions to be called when the object is 'called'), it in the implementation of the Observer pattern. (e.g. notify all of my life-linked spells to expire when I die.) Unlike __event, Boost's signals let the callback have a return value (letting you specify a function that will aggregate them, e.g. add them, return the max, think, for example, stat modifiers in a RPG, add a modifier to the list and it automatically gets taken into account), specify priorities (first check if my outer layer of protective spells stop the attack, if not, check my armor (damaging it), if not ...). They can also deal with closures (bound pointers to member functions) !

As to their speed, well, it is implementation-dependent. You will always pay for a number of function pointer calls, which are more expensive than straight function call, since it is a jump to an address which has to be loaded from data, instead of it being part off the instruction flow Plus bookkeeping, walking through the list (probably a vector) of function pointers.

My advice would thus be : use them freely, coding up your system, until profiling reveals they are a critical bottleneck and you can implement them to be faster, while still maintening convenient syntax... Which I don't think you or I would be up to : they implement a rather low-level concept for which it is the interface and general usability that matter, there are no real 'clever' tricks that can be applied to boost performance without sacrifying it ... which would make them useless.

To sum up : go have a look at the Boost library


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on September 9, 2002 5:09:04 AM]

Share this post


Link to post
Share on other sites
Fruny - besides all the Boost crap you said, __event has other pmplications your genius mind is not aware of. Such as integration with COM, which has an event mechanism that is "interesting to use" without such automations.

Now, for __event - this is a pretty nifty mechanism especially when writing .NET interop classes (managed C++). Full integration with the .NET event handling mechanism. It is also nice when using COM classes. When doing so, I urgent you to also use the other attribute based concepts of C++ 7.0 - can get your amount of code DOWN big time for a lot of operations related to COM - the most visible things peopably being that you dont have to maintain an idl file anymore, as one is autogenerated from your annotated cpp files.



Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
- condescending trollbait snipped -

I do agree that the interoperability properties are a much welcome relief, however, the original poster had concerns about the efficiency of the __event mechanism.

Considering that you have expertise in both performance-critical systems and with Microsoft's .NET platform, while I have only dabbled -- cross platform code being part of the constraints I am subject to -- could you make us all benefit from your experience with a performance analysis ?

Thanks anyway for your obviously unbiased (*cough* MS MVP *cough*) opinion, so in line with all those you've honored these fora with in the past. Consistency is a valuable quality in a programmer.

Yours truly,

Y.L.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on September 9, 2002 6:56:50 AM]

Share this post


Link to post
Share on other sites
Performance is normally not seen as mission critical with event mechanisms.

Anyhow, from my knowledge with .NET managed code - note it is entirely different with __event in a COM environment - the created code is pretty efficient.

It is basically an array is function pointers to all subscribed callback methods. Callbacks are stored as function pointers. The list is traversed by the event throwing function and the medhots called in a specific order (notable: IMHO the order of subscription).

What I dont like here about the __event methods, compared to callback interfaces, is that you create a lot of delegates - if you throw 5 events and one class subscribes them all, you ahve 5 pointers running wild instead of having one callback interface. On the other hand, you are more efficient with "sparce events" - when a particular class does not subscribe one event, northing is thrown.

For COM events (__event __interface) the mechanism generated is similar to the "textbook" implementation of COM interfaces. As such this is "performance by default".

I mean, frankly, unless you allow only ONE callback interface pointer, the difference in performance between two implementations of the callback list is nil, unless one is done UTTERLY stupid. Thats really easy code anyhow.

ALso note, that the main reason I dislike Boost are people like you. What you dont realize is that platform independance is not an issue for a lot of people in the professional world. Surely, if someone says he wants platform independence, these tools are nice. But I am writing windows software for more than 10 years, and I have NO inclination to wr5ite software for anything else for now. OTOH I write high performance high availability software at times, and I do so professionally (means: I charge for my services, and I do so in the mor expensive ragne). As such, I value my time - and anything the compiler can take from me is greatly asked for.

In particular - sadly, the original poster did not say in what context he talks of __event, and there are three possible uses (managed C++, COM events, native C++) - wirting COM objects with mechanisms like ATL was one of the most riduculos things MS ever did - not that ATL was particularly bad, but the necessity for an IDL file for the typelib and other "non tinegration" thigns made it pretty bad. Now, with "attributed programming", all I do is adding some attributes and keywords, and I get my COM objects native from the cpp file - without the need to actually manages yet another source file (namely the IDL file). Talk about saving 50+% of the coding time for some tasks (like integrating OleDb from C++), and you know how valuable this technology is.

BTW - I earned my MVP by not being nice during the MS Beta test. I am proud of it. MS is cooking some thing really nice here - they make the things right that SUN sadly is doing wrong.


Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
quote:
Original post by thona
ALso note, that the main reason I dislike Boost are people like you. What you dont realize is that platform independance is not an issue for a lot of people in the professional world.

High quality code is also not an issue for a lot of people in the professional world. They don''t care as long as it sells. But should we encourage that too?

I should also point out that this is a game development forum. Most game development companies are not limited to the Windows platform.
quote:
Surely, if someone says he wants platform independence, these tools are nice. But I am writing windows software for more than 10 years, and I have NO inclination to wr5ite software for anything else for now.

No-one said you had to start writing Linux software. But a portable solution - especially an open source portable solution - is likely to be better because it is reviewed by numerous people using numerous environments, including language experts. You also know it''s not going away, unlike compiler-specific options. And if you have problems, you have a wider array of people who are going to be familiar with the code you''re using and who are therefore able to help.

Portable and standard code is a good thing, even if you never intend coding for more than 1 platform.
quote:
OTOH I write high performance high availability software at times, and I do so professionally (means: I charge for my services, and I do so in the mor expensive ragne). As such, I value my time - and anything the compiler can take from me is greatly asked for.

That is also what libraries are for - making things easier for the programmer. Does it really make much difference whether it is a #include or a __proprietary_keyword?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
quote:

That is also what libraries are for - making things easier for the programmer. Does it really make much difference whether it is a #include or a __proprietary_keyword?



SADLY it does __event does way more than just some include could do - it does even more than possibly by a template in an easy to understand syntax

This is getting even "worse" when you start seeing the code generation coupled with COM integration. Here you save big time - and there is no mechanism to provide this functionality otherwise. What #include can you use to generate an external file during the compilation process that rips out certain information into an IDL file? Suggestions, please.

For open source - well, I LOVE open source. IF I can sue the company who did it. Open Source != free software != GPL.

(a) I dont like GPL and will never touch it - I want to earn money with my software.
(b) I dont like free software. You get something free, you dont have any legal claim against the vendor.
(c) I LOVE open source. Like the DirectShow base classes (provided by MS as source). I can even get professional upport for them.

What about your free software - any professional support I can get there?

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post


Link to post
Share on other sites
quote:
Original post by thona
What about your free software - any professional support I can get there?

I have always found unofficial support from the community to be far more effective than official support.

As for being able to ''sue the company that did it'', that argument is entirely vacuous since 99% of commercial software packages and libraries are licensed under the understanding that it''s not guaranteed to work as you want it to. If you could, Microsoft would be bankrupt due to all the blue-screen claims when trying to run software under Win9x.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by thona
What about your free software - any professional support I can get there?


take a look at [url]http://www.redhat.com/services/[/url]
and [url]http://stlport.org/[/url].

"information at your fingertips".

Share this post


Link to post
Share on other sites