10 ms·
A lightweight C++ signals and slots implementation
- static_noise 11y agoThey use templates like Signal0<>, Signal1<T1>, Signal2<T1,T2>, etc. with fixed argument length instead of a more uniform solution like variadic templates. The code in the library is pretty old and I suppose that an update with regards to C++11/14/17 would make it much better in every regard.
- kentonv 11y agoAlso under C++11, the availability of lambdas probably completely changes how you'd design this sort of thing.
- john_fushi 11y agoGiven that the comments talk about compatibility issues with DOS and some DSPs I believe it is not the direction pbhogan wants to take.
- static_noise 11y agoAs long as you can use a recent compiler there shouldn't be problems. The resulting assembler code should be the same.
- userbinator 11y agoA C++11 compiler for DOS?
- okasaki 11y agohttp://www.delorie.com/pub/djgpp/mini-faq.txt http://www.delorie.com/pub/djgpp/mini-faq.txt gcc 5.3 (& co) for DOS!
- walrus 11y agoFor DSPs, there's typically only a single, proprietary compiler available, and supporting the latest C++ standards isn't high priority for the companies making them.
- bnastic 11y agoWhen the original article was written (and I remember reading it many, many moons ago) variadic templates were not yet available. And this project seems to have been last updated 4 years ago, so a lot of boilerplate templates is understandable.
- modulus1 11y agoMaybe someone can chime in on why the standard library's std::function can't be implemented as efficiently as this library purportedly is.
- fleitz 11y agoAs with most things it boils down to scope... 'Class template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any Callable target -- functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.' This clearly does more than the OPs code and therefore has more complexity.
- deleted 11y ago[deleted]
- vinkelhake 11y agoAs fleitz says, std::function can do more. It can accept any callable. The delegate is restricted to just calling functions. In particular, it means that the client can't bind any extra parameters which can be really useful. That said, popular implementations of std::function employ small object optimizations so the usescases that delegate supports doesn't incur heap allocations. I would guess that delegate has a slight upper hand when it comes to call performance. I really doubt the difference would matter when used in GUI programming.
- ksherlock 11y agoThe original code and explanation: http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible http://www.codeproject.com/Articles/7150/Member-Function-Poi...
- eco 11y agoThe author of this has been a long time contributor to D and brought these ideas (and many other things) to the language. He's probably the biggest reason D's CTFE (compile time function evaluation) is as powerful as it is, having done most of the implementation.
- tcbawo 11y agoIf you wanted to maintain C++03 compatibility, a dispatching macro might be useful to select the appropriate Signal template arity: http://stackoverflow.com/questions/16683146/can-macros-be-overloaded-by-number-of-arguments http://stackoverflow.com/questions/16683146/can-macros-be-ov...
- zura 11y agoYes (a shameful plug :)), I've used macros to generate code for different number of arguments for std::function like lib for pre-C++11 compilers (back in 2009): https://github.com/zura-kh/yaf.Function https://github.com/zura-kh/yaf.Function
- gpderetta 11y agoNo need for a dispatching macro, the usual solution in c++03 was to have a template with a fixed maximum arity like this: template<class T1 = default_, class T2 = default ..., class T10 = default_> struct Signal; where default is a placeholder type, and then have it dispatch internally to the correct Signal/N/ via template specialization. The template specialization themselves were generated either via preprocessor metaprogramming or by an external tool.
- netheril96 11y agoCan the signal cross threads as in Qt's implementation?
- roel_v 11y agoNo.
- dazzawazza 11y agothis equally old and pre modern c++ implementation can: http://sigslot.sourceforge.net/ http://sigslot.sourceforge.net/
- pjmlp 11y agoWhat does it do better than libsigc++?
- eps 11y agoThis has got to stop. Someone posts a link to A and immediately there is a comment - "How is it better than B? How is it different from C?" You know what, bud, if you know about B and C, excellent. Why don't you take a look at A and do everyone a favor and answer your own question. Otherwise it sound like a lazy-ass highbrow dismissal - "Pfft, A? Why bother? There is B, for chrissake, that even the last imbecile here is familiar with." Please try and make a sliver of effort when commenting on other people's work.
- deleted 11y ago[deleted]
- pjmlp 11y agoBecause my time is limited and I would like to know why I should spend time evaluating it, instead of using a battle tested library that exists since late 90's and that follows modern C++. If the OP only wants to learn how to do it, or to build its portfolio for a possible job interview, it is fair enough and I don't need to bother with it. If the author wants to do a kick-ass replacement of libsigc++, then it is probably worth my expensive time to look into it.
- shin_lao 11y agoEveryone's time is limited, whether it's useful to you (or not), only you can say. You are basically implying that the time of other people is less valuable than yours when you say "please tell me why I must look at it".
- baq 11y agoi'll respectfully disagree. it's very useful if somebody who has used both (or one but with good understanding of the other) could comment exactly what are the pros and cons of both solutions; to put it differently, why you should use one instead of the other.
- EGreg 11y agoWhat are signals/slots the best paradigm for?
- lordnacho 11y agoObserver pattern. Say you have some event that occurs, and some objects that want to know about it. You make a signal and connect some observers to it. When you fire the signal the observers receive some parameters and do their calculations.
- ajuc 11y agoWhy it's interesting alternative: regular observer pattern make observed code depend (at compile time) on the observing code. Signals/slots remove this dependency, and allow for external configuration of these. It's a dependency injection mechanism when you think about it.
- truncate 11y agoQt Framework uses them extensively. You might find pretty good examples there. Its been a long while, so I can't specifically give examples.
- hellofunk 11y agoThey are a useful mechanism for linking different parts of a program without having to couple objects together. OOP can lead to a lot of inflexibility, but historically has been the most obvious mechanism in C++ for one object to learn about what another object is doing: pass a pointer to itself, etc. But this often requires inheritance, polymorphism, virtual interfaces, a number of idioms that can complicate this basic requirement. Signals and slots allow the objects to not know anything about each other, not store pointers to each other, be more or less isolated, except for this one little connection between them that is otherwise anonymous to both.
- mattiemass 11y agoI had a ~ 6 month stint using Qt, after coming from iOS/OSX development. I found it to be a really cumbersome mechanism, particularly when concurrency was involved. I feel like there is a lot of momentum around functional reactive. Anyone know of a comparison between this and reactive approaches?