C++11 observer pattern (signals, slots, events, change broadcaster/listener, or whatever you want to call it)

前端 未结 5 1354
囚心锁ツ
囚心锁ツ 2021-01-30 04:41

With the changes made in C++11 (such as the inclusion of std::bind), is there a recommended way to implement a simple single-threaded observer pattern without depen

5条回答
  •  爱一瞬间的悲伤
    2021-01-30 04:51

    Since you're asking for code, my blog entry Performance of a C++11 Signal System contains a single-file implementation of a fully functional signal system based on C++11 features without further dependencies (albeit single-threaded, which was a performance requirement).

    Here is a brief usage example:

    Signal sig2;
    sig2() += [] (std::string msg, int d)   { /* handler logic */ };
    sig2.emit ("string arg", 17);
    

    More examples can be found in this unit test.

提交回复
热议问题