[ANN] Fast C++ Delegate
От: SchweinDeBurg Россия https://zarezky.spb.ru/
Дата: 03.03.06 14:45
Оценка:
Fast C++ Delegate

An implementation of the fast C++ delegate which is portable and C++ Standard-compliant.

Don Cluston explained about the member function pointer and its behavior in the very well organized way in his article "Member Function Pointers and the Fastest Possible C++ Delegates ". In short, member function pointer can not be assigned into void * data pointer so a special care need to be taken to deal with member function pointer as a data (functor). Don's FastestDelegate is the really fastest possible delegate as he claimed but he leveraged reinterpret_cast<> to deceive the compiler, which he called it as 'horrible hack' by himself in his article.

Sometimes later, Sergey Ryazanov came up with a fast C++ delegate which is found to be as fast as Don's FastestDelegate but completely C++ Standard compliant. His technique is using a templatized static member function ('stub') to store/restore the function call information (return type, argument type, cv-qualifier and platform specific calling convention) at compile-time. Actually it isn't new as a similar approach had been already introduced by Rich Hickey in his article "CALLBACKS IN C++ USING TEMPLATE FUNCTORS", 1994. Rich called such a static member function as 'thunk' function. However, Sergey's technique is unique as he passed over member function call information through non-type template parameter. Unfortunately, not many commercially available compilers can support this truly C++ Standard feature. Thus his code is not really portable in that sense.

There is one more great article about how to implement C++ delegate here in CP. It is "Yet Another Generalized Functors Implementation in C++" by Aleksei Trunov. He explained and analyzed so well in detail about generalized functor requirements and problems of the existing delegates. His article inspired me to start implementing my own fast delegate which will be suggested here below.

By the way, these techniques are considered as 'fast' since they were able to avoid the heap memory allocation to store the member function pointer unlikely boost::function does.

[ posted via RSDN@Home 1.1.4 stable SR1 r568, accompanied by silence ]
- Искренне ваш, Поросенок Пафнутий
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.