diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2012-02-15 19:52:49 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2012-02-15 16:28:01 +0200 |
| commit | ee34168070ffd9cfcfd6c35123e9579929dddb6e (patch) | |
| tree | 315d53ff276c57284e264e3eea321af5885f61bc | |
| parent | 731b6a13e0b5d60f0cca814123554410cbc99f62 (diff) | |
| download | sigslot-master.tar.gz sigslot-master.zip | |
| -rw-r--r-- | example.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/example.cpp b/example.cpp new file mode 100644 index 0000000..dee5e1b --- /dev/null +++ b/example.cpp @@ -0,0 +1,103 @@ +#include <cstdio> +#include <string> + +// force single threaded sigslot +#define SIGSLOT_PURE_ISO 1 +#include "sigslot.h" + +using sigslot::signal; + +#define __signals +#define __slots + +#define DBG(FMT,...) printf("%s: " FMT "\n", __PRETTY_FUNCTION__, ##__VA_ARGS__) + +class Foo: public sigslot::has_slots<>, public sigslot::has_signals<Foo> +{ +public: + + Foo() + { + connect(signalIntEmitted, &Foo::handleIntEmission); + connect(signalSomethingHappened, &Foo::handleSomething); + + // above is just some template-sugar for these calls + //connect(signalIntEmitted, this, &Foo::handleIntEmission); + //connect(signalSomethingHappened, this, &Foo::handleSomething); + } + + void doStuff() + { + DBG(""); + signalIntEmitted(42); + } + +public __slots: + + void handleIntEmission(int i) + { + DBG("i = %d", i); + signalSomethingHappened(); + } + + void handleSomething() + { + DBG(""); + } + +public __signals: + + signal<int> signalIntEmitted; + signal<> signalSomethingHappened; + signal<std::string &> signalStr; +}; + +class Bar: public sigslot::has_slots<> +{ +public: + + Bar(Foo *foo) : + signalSig(foo->signalIntEmitted) + { + foo->signalIntEmitted.connect(this, &Bar::handleIntEmission); + foo->signalStr.connect(this, &Bar::handleStr); + } + +public __slots: + + void handleIntEmission(int i) + { + DBG("i = %d", i); + } + + void handleStr(std::string &str) + { + DBG("str = %s", str.c_str()); + str += ", World!"; + } + +public __signals: + + signal<int> signalSig; + +}; + +int main() +{ + Foo foo; + Bar bar(&foo); + + foo.doStuff(); + + std::string str = "Hello"; + foo.signalStr(str); + + DBG("str = %s", str.c_str()); + + printf("\n"); + + bar.signalSig.SIGSLOT_EMIT(1); + + return 0; +} + |
