From ee34168070ffd9cfcfd6c35123e9579929dddb6e Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Wed, 15 Feb 2012 19:52:49 +0200 Subject: Add a simple example --- example.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 example.cpp 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 +#include + +// 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 +{ +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 signalIntEmitted; + signal<> signalSomethingHappened; + signal 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 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; +} + -- cgit v1.2.3