how do you implement an EventDispatcher the rust way? - c++

So I have been trying to create a rendering engine using rust, and I've been basing it on some C++ code that does the same thing. And I'm stuck on how you write an event dispatcher the rusty way.
here's the C++ code
class EventDispatcher {
template<typename T>
using EventFn = std::function<bool(T&)>;
public:
EventDispatcher(Event& event) : m_Event(event) {}
template<typename T>
bool Dispatch(EventFn<T> func) {
if (m_Event.GetEventType() == T::GetStaticType()) {
m_Event.m_Handeled = func(*(T*)&m_Event);
return true;
}
return false;
}
private:
Event& m_Event;
};
and it will be used like this
void SomeOtherClass::OnEvent(Event& e) {
EventDispatcher dispatcher(e);
dispatcher.Dispatch<RandomEvent>(std::bind(&Application::EventHandler, this, std::placeholders::_1));
}
I've tried to implement a similar pattern in rust, this is what I've got so far
pub struct EventDispatcher<T: EventTrait> {
event: Box<T>,
}
impl<T: EventTrait> EventDispatcher<T> {
pub fn dispatch<V: EventTrait>(&mut self, func: impl Fn(&mut V) -> bool) -> bool {
if self.event.type_id() == TypeId::of::<V>() {
let res = func(unsafe { &mut *(self.event as *mut T as *mut V) });
self.event.set_is_handeled(res);
return res;
} else {
false
}
}
}
I'm pretty sure this is horribly wrong, I'm still trying to learn rust. Any help would be greatly appreciated! :D

well I figured that my current approach wasn't the best, I tried to implement the observer pattern for this problem, and I feel that it's a better fit!
pub trait IObserver {
fn update(&self, event: &impl EventTrait);
}
pub trait ISubject<'a, T: IObserver> {
fn attach(&mut self, observer: &'a T);
fn detach(&mut self, observer: &'a T);
fn notify(&self, event: &impl EventTrait);
}

Related

Trying to create pipeline pattern with c++

I am trying to build the following in my head in c++, but seems like there is no good translation of java to c++ for everything, here is what I am trying to accomplish:
create a pipeline that each steps has an input and output (outputs are fed to the next step)
process pipeline end to end:
public interface Step<I, O> {
public static class StepException extends RuntimeException {
public StepException(Throwable t) {
super(t);
}
}
public O process(I input) throws StepException;
}
public class Pipeline<I, O> {
private final Step<I, O> current;
private Pipeline(Step<I, O> current) {
this.current = current;
}
private <NewO> Pipeline<I, NewO> pipe(Step<O, NewO> next) {
return new Pipeline<>(input -> next.process(current.process(input)));
}
public O execute(I input) throws Step.StepException {
return current.process(input);
}
}
public class ExamplePipeline {
public static class AdditionInput {
public final int int1;
public final int int2;
public AdditionInput(int int1, int int2) {
this.int1 = int1;
this.int2 = int2;
}
}
public static class AddIntegersStep implements Step<AdditionInput, Integer> {
public Integer process(AdditionInput input) {
return input.int1 + input.int2;
}
}
public static class IntToStringStep implements Step<Integer, String> {
public String process(Integer input) {
return input.toString();
}
}
public static void main(String[] args) {
Pipeline<AdditionInput, String> pipeline = new Pipeline<>(new AddIntegersStep())
.pipe(new IntToStringStep());
System.out.println(pipeline.execute(new AdditionInput(1, 3))); // outputs 4
}
}
How can I model the above in c++?
I cant get the pipeline modeled, step is pretty simple:
template <typename I>
template <typename O>
class Step {
virtual O process(I input) = 0;
public:
typedef I inputType;
typedef O outputType;
}
I think there are multiple ways this problem can be solved. Here is a way using lambdas:
auto pipe = [](auto a, auto b){ return [&](auto c){ return b(a(c)); }; };
And an example program using it:
#include <iostream>
#include <string>
auto pipe = [](auto a, auto b){ return [&](auto c){ return b(a(c)); }; };
int main() {
auto Addition = [](std::pair<int, int> p) {return p.first + p.second;};
auto IntToString = [](int a) {return std::to_string(a);};
auto AddNewline = [](std::string a) {return a + "\n";};
auto pipeline = pipe(pipe(Addition, IntToString), AddNewline);
std::cout << pipeline(std::pair<int, int>{1, 3});
}
Another interesting approach, that will allow you to create a pipeline using the |-operator can be found in this answer.
There are several ways to do this depending on how flexible, fast or simple the solution needs to be.
If you don't need to build/store the individual steps in the pipeline at runtime to call them later on, then you can do something pretty similar to the Java example, like modelling each step as a callable type and then have your pipe member function be a template which applies the step passed as a parameter.

Can I create a new map on the fly and pass as a function parameter?

In C#, I could so something like the following:
public static Dictionary<string,string> ExampleFunction(Dictionary<string, string> dict)
{
return dict;
}
public static void CallingFunction()
{
ExampleFunction(new Dictionary<string, string>() { { "test", "test" } });
}
Is there an equivalent way to do this in C++? Forgive me if this is answered elsewhere--this seems like a basic question, but I must not be using the right search terms. This comes closest to what I was looking for but no cigar: Creating a new object and passing in method parameter.
You may be looking for something like this:
std::map<std::string, std::string> ExampleFunction(
const std::map<std::string, std::string>& dict) {
return dict;
}
void CallingFunction() {
ExampleFunction({ { "test", "test" } });
}
Demo
you can do that like this:
#include <iostream>
template<typename A, typename B> struct Dictionary
{
A Var1;
B Var2;
};
template<typename A, typename B> Dictionary<A, B> func(Dictionary<A, B> dict)
{
return dict;
}
int main()
{
auto res = func(Dictionary<int, int>{123, 445});
printf("%d - %d\n", res.Var1, res.Var2);
getchar();
return 0;
}

how to expect pointer argument which is created later

I have such group -> element to add/remove, with code snippt:
// production code:
Group::add(Element* e) {...};
Group::remove(Element* e) {...};
ElementDerived::ElementDerived(Group* group){ group->add(this);}
ElementDerived::~ElementDerived(Group* group){ group->remove(this);}
whenever i want to test the class ElementDerived, i have to do the following in the setup/teardown. However, how can I set the argument expectation explicitly without _?
// test code:
struct Test_ElementDerived : public ::testing::Test {
void SetUp() override {
ElementDerived* p = nullptr;
EXPECT_CALL(group, add(_)) // how to set expection on the pointer argument?
.WillOnce(Invoke([&p](auto base_ptr) {
p = static_cast<ElementDerived*>(base_ptr);
}));
sut = std::make_unique<ElementDerived>(&group);
}
void TearDown() override {
EXPECT_CALL(group, remove(sut.get()));
}
MockGroup group{};
std::unique_ptr<ElementDerived> sut;
};
You can do this:
Element* p = nullptr;
EXPECT_CALL(group, add(_)).WillOnce(SaveArg<0>(&p));
sut = std::make_unique<ElementDerived>(&group);
ASSERT_EQ(p, static_cast<Element*>(sut.get()));
This does not differ much from what you just did. To replace _ with some matcher - you would have to know the pointer - the only way to achieve that is to use placement new:
struct DestructCaller {
template <typename T> void operator()(T* ptr)
{
if (ptr) ptr->~T();
}
};
std::aligned_storage_t<sizeof(ElementDerived), alignof(ElementDerived)> sut_storage;
std::unique_ptr<ElementDerived, DestructCaller> sut;
void SetUp() override
{
EXPECT_CALL(group, add((ElementDerived*)&sut_storage));
sut.reset(new (&sut_storage) ElementDerived(&group));
}
Or similar concept with unrestricted union:
union
{
ElementDerived sut;
};
void SetUp() override
{
EXPECT_CALL(group, add(&sut));
new (&sut) ElementDerived(&group);
}
void TearDown() override
{
EXPECT_CALL(group, remove(&sut));
sut.~ElementDerived();
}
In my personal opinion - a way with SaveArg and ASSERT_EQ is more readable.

Using Lambda/Template/SFINAE to automate try/catch-safeguarding of trampoline functions

I have 100 or so trampoline functions. I would like to know whether it is possible to automate wrapping each one inside a try/catch block.
Please be warned in advance, this is not an easy question. I will start by describing the problem with (simplified) code, and will then attempt to answer it as best I can below, so the reader may see where I am at.
Foo has a function pointer table:
EDIT: This is a C function pointer table. So it could accept static W::w.
Signatures are here: http://svn.python.org/projects/python/trunk/Include/object.h
EDIT: I've attempted a test case here:
class Foo {
Table table;
Foo() {
// Each slot has a default lambda.
:
table->fp_53 = [](S s, A a, B b) -> int {cout<<"load me!";};
table->fp_54 = [](S s, C c, D d, E e) -> float {cout<<"load me!";};
// ^ Note: slots MAY have different signatures
// only the first parameter 'S s' is guaranteed
}
// Foo also has a method for loading a particular slot:
:
void load53() { table->fp_53 = func53; }
void load54() { table->fp_54 = func54; }
:
}
If a particular slot is 'loaded', this is what gets loaded into it:
int func53(S s, A a, B b) {
try{
return get_base(s)->f53(a,b);
}
catch(...) { return 42;}
}
float func54(S s, C c, D d, E e) {
try{
return get_base(s)->f54(c,d,e);
}
catch(...) { return 3.14;}
}
I am trying to accomplish this using lambdas, so as to bypass having to define all of these func53 separately. Something like this:
class Foo {
:
void load53() {
table->fp_53 =
[](S s, A a, B b)->int { return get_base(s)->f53(a,b); }
}
void load54() {
table->fp_54 =
[](S s, C c, D d, E e)->float { return get_base(s)->f54(c,d,e); }
}
However, this is failing to trap errors. I need to be putting a try/catch around the return statement:
try{ return get_base(s)->f53(a,b); } catch{ return 42; }
However, this creates a lot of clutter. It would be nice if I could do:
return trap( get_base(s)->f53(a,b); )
My question is: is there any way to write this trap function (without using #define)?
This is what I've come up with so far:
I think this would pass all the necessary information:
trap<int, &Base::f53>(s,a,b)
trap's definition could then look like this:
template<typename RET, Base::Func>
static RET
trap(S s, ...) {
try {
return get_base(s)->Func(...);
}
catch {
return std::is_integral<RET>::value ? (RET)(42) : (RET)(3.14);
}
}
This may allow for a very clean syntax:
class Foo {
:
void load53() { table->fp_53 = &trap<int, &Base::f53>; }
void load54() { table->fp_54 = &trap<float, &Base::f54>; }
}
At this point I'm not even sure whether some laws have been violated. table->fp_53 must be a valid C function pointer.
Passing in the address of a nonstatic member function (&Base::f53>) won't violate this, as it is a template parameter, and is not affecting the signature for trap
Similarly, ... should be okay as C allows varargs.
So if this is indeed valid, can it be cleaned up?
My thoughts are:
1) maybe the ... should be moved back to the template parameter as a pack.
2) maybe it is possible to deduce the return type for trap, and save one template parameter
3) that Base::Func template parameter is illegal syntax. And I suspect it isn't even close to something legal. Which might scupper the whole approach.
#include <utility>
template <typename T, T t>
struct trap;
template <typename R, typename... Args, R(Base::*t)(Args...)>
struct trap<R(Base::*)(Args...), t>
{
static R call(int s, Args... args)
{
try
{
return (get_base(s)->*t)(std::forward<Args>(args)...);
}
catch (...)
{
return std::is_integral<R>::value ? static_cast<R>(42)
: static_cast<R>(3.14);
}
}
};
Usage:
table->fp_53 = &trap<decltype(&Base::f53), &Base::f53>::call;
table->fp_54 = &trap<decltype(&Base::f54), &Base::f54>::call;
DEMO
Note: std::forward can still be used although Args is not a forwarding reference itself.
template<typename RET, typename... Args>
struct trap_base {
template<RET (Base::* mfptr)(Args...)>
static RET
trap(S s, Args... args) {
try {
return (get_base(s).*mfptr)(args...);
}
catch (...) {
return std::is_integral<RET>::value ? (RET)(42) : (RET)(3.14);
}
}
};
Usage:
void load53() { table.fp_53 = &trap_base<int, int>::trap<&Base::f53>; }
void load54() { table.fp_54 = &trap_base<float, int, float>::trap<&Base::f54>; }
Demo.
You can probably also use a partial specialization to extract RET and Args from decltype(&base::f53) etc.
trap_gen is a function that returns a function pointer to a function generated on the fly, the equivalent of your trap function.
Here is how you use it
table->fp_53 = trap_gen<>(Base::f53);
table->fp_54 = trap_gen<>(Base::f54);
...
Where Base::f53 and Base::f54 are static member functions (or function pointers, or global functions in a namespace).
Proof of concept :
#include <iostream>
template<typename R, class...A>
R (*trap_gen(R(*f)(A...)))(A...)
{
static auto g = f;
return [](A... a)
{
try {
return g(a...);
} catch (...) {
return std::is_integral<R>::value ? static_cast<R>(42)
: static_cast<R>(3.14);
}
};
}
int add(int a, int b)
{
return a+b;
}
int main() {
int(*f)(int, int) = trap_gen<>(add);
std::cout << f(2, 3) << std::endl;
return 0;
}

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

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 dependence on anything external to the core language or standard library (like boost::signal)?
EDIT
If someone could post some code showing how dependence on boost::signal could be reduced using new language features, that would still be very useful.
I think that bind makes it easier to create slots (cfr. the 'preferred' syntax vs. the 'portable' syntax - that's all going away). The observer management, however, is not becoming less complex.
But as #R. Martinho Fernandes mentions: an std::vector<std::function< r(a1) > > is now easily created without the hassle for an (artificial) 'pure virtual' interface class.
Upon request: an idea on connection management - probably full of bugs, but you'll get the idea:
// note that the Func parameter is something
// like std::function< void(int,int) > or whatever, greatly simplified
// by the C++11 standard
template<typename Func>
struct signal {
typedef int Key; //
Key nextKey;
std::map<Key,Func> connections;
// note that connection management is the same in C++03 or C++11
// (until a better idea arises)
template<typename FuncLike>
Key connect( FuncLike f ) {
Key k=nextKey++;
connections[k]=f;
return k;
}
void disconnect(Key k){
connections.erase(k);
}
// note: variadic template syntax to be reviewed
// (not the main focus of this post)
template<typename Args...>
typename Func::return_value call(Args... args){
// supposing no subcription changes within call:
for(auto &connection: connections){
(*connection.second)(std::forward(...args));
}
}
};
Usage:
signal<function<void(int,int)>> xychanged;
void dump(int x, int y) { cout << x << ", " << y << endl; }
struct XY { int x, y; } xy;
auto dumpkey=xychanged.connect(dump);
auto lambdakey=xychanged.connect([&xy](int x, int y){ xy.x=x; xy.y=y; });
xychanged.call(1,2);
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<void (std::string, int)> sig2;
sig2() += [] (std::string msg, int d) { /* handler logic */ };
sig2.emit ("string arg", 17);
More examples can be found in this unit test.
I wrote my own light weight Signal/Slot classes which return connection handles. The existing answer's key system is pretty fragile in the face of exceptions. You have to be exceptionally careful about deleting things with an explicit call. I much prefer using RAII for open/close pairs.
One notable lack of support in my library is the ability to get a return value from your calls. I believe boost::signal has methods of calculating the aggregate return values. In practice usually you don't need this and I just find it cluttering, but I may come up with such a return method for fun as an exercise in the future.
One cool thing about my classes is the Slot and SlotRegister classes. SlotRegister provides a public interface which you can safely link to a private Slot. This protects against external objects calling your observer methods. It's simple, but nice encapsulation.
I do not believe my code is thread safe, however.
//"MIT License + do not delete this comment" - M2tM : http://michaelhamilton.com
#ifndef __MV_SIGNAL_H__
#define __MV_SIGNAL_H__
#include <memory>
#include <utility>
#include <functional>
#include <vector>
#include <set>
#include "Utility/scopeGuard.hpp"
namespace MV {
template <typename T>
class Signal {
public:
typedef std::function<T> FunctionType;
typedef std::shared_ptr<Signal<T>> SharedType;
static std::shared_ptr< Signal<T> > make(std::function<T> a_callback){
return std::shared_ptr< Signal<T> >(new Signal<T>(a_callback, ++uniqueId));
}
template <class ...Arg>
void notify(Arg... a_parameters){
if(!isBlocked){
callback(std::forward<Arg>(a_parameters)...);
}
}
template <class ...Arg>
void operator()(Arg... a_parameters){
if(!isBlocked){
callback(std::forward<Arg>(a_parameters)...);
}
}
void block(){
isBlocked = true;
}
void unblock(){
isBlocked = false;
}
bool blocked() const{
return isBlocked;
}
//For sorting and comparison (removal/avoiding duplicates)
bool operator<(const Signal<T>& a_rhs){
return id < a_rhs.id;
}
bool operator>(const Signal<T>& a_rhs){
return id > a_rhs.id;
}
bool operator==(const Signal<T>& a_rhs){
return id == a_rhs.id;
}
bool operator!=(const Signal<T>& a_rhs){
return id != a_rhs.id;
}
private:
Signal(std::function<T> a_callback, long long a_id):
id(a_id),
callback(a_callback),
isBlocked(false){
}
bool isBlocked;
std::function< T > callback;
long long id;
static long long uniqueId;
};
template <typename T>
long long Signal<T>::uniqueId = 0;
template <typename T>
class Slot {
public:
typedef std::function<T> FunctionType;
typedef Signal<T> SignalType;
typedef std::shared_ptr<Signal<T>> SharedSignalType;
//No protection against duplicates.
std::shared_ptr<Signal<T>> connect(std::function<T> a_callback){
if(observerLimit == std::numeric_limits<size_t>::max() || cullDeadObservers() < observerLimit){
auto signal = Signal<T>::make(a_callback);
observers.insert(signal);
return signal;
} else{
return nullptr;
}
}
//Duplicate Signals will not be added. If std::function ever becomes comparable this can all be much safer.
bool connect(std::shared_ptr<Signal<T>> a_value){
if(observerLimit == std::numeric_limits<size_t>::max() || cullDeadObservers() < observerLimit){
observers.insert(a_value);
return true;
}else{
return false;
}
}
void disconnect(std::shared_ptr<Signal<T>> a_value){
if(!inCall){
observers.erase(a_value);
} else{
disconnectQueue.push_back(a_value);
}
}
template <typename ...Arg>
void operator()(Arg... a_parameters){
inCall = true;
SCOPE_EXIT{
inCall = false;
for(auto& i : disconnectQueue){
observers.erase(i);
}
disconnectQueue.clear();
};
for (auto i = observers.begin(); i != observers.end();) {
if (i->expired()) {
observers.erase(i++);
} else {
auto next = i;
++next;
i->lock()->notify(std::forward<Arg>(a_parameters)...);
i = next;
}
}
}
void setObserverLimit(size_t a_newLimit){
observerLimit = a_newLimit;
}
void clearObserverLimit(){
observerLimit = std::numeric_limits<size_t>::max();
}
int getObserverLimit(){
return observerLimit;
}
size_t cullDeadObservers(){
for(auto i = observers.begin(); i != observers.end();) {
if(i->expired()) {
observers.erase(i++);
}
}
return observers.size();
}
private:
std::set< std::weak_ptr< Signal<T> >, std::owner_less<std::weak_ptr<Signal<T>>> > observers;
size_t observerLimit = std::numeric_limits<size_t>::max();
bool inCall = false;
std::vector< std::shared_ptr<Signal<T>> > disconnectQueue;
};
//Can be used as a public SlotRegister member for connecting slots to a private Slot member.
//In this way you won't have to write forwarding connect/disconnect boilerplate for your classes.
template <typename T>
class SlotRegister {
public:
typedef std::function<T> FunctionType;
typedef Signal<T> SignalType;
typedef std::shared_ptr<Signal<T>> SharedSignalType;
SlotRegister(Slot<T> &a_slot) :
slot(a_slot){
}
//no protection against duplicates
std::shared_ptr<Signal<T>> connect(std::function<T> a_callback){
return slot.connect(a_callback);
}
//duplicate shared_ptr's will not be added
bool connect(std::shared_ptr<Signal<T>> a_value){
return slot.connect(a_value);
}
void disconnect(std::shared_ptr<Signal<T>> a_value){
slot.disconnect(a_value);
}
private:
Slot<T> &slot;
};
}
#endif
Supplimental scopeGuard.hpp:
#ifndef _MV_SCOPEGUARD_H_
#define _MV_SCOPEGUARD_H_
//Lifted from Alexandrescu's ScopeGuard11 talk.
namespace MV {
template <typename Fun>
class ScopeGuard {
Fun f_;
bool active_;
public:
ScopeGuard(Fun f)
: f_(std::move(f))
, active_(true) {
}
~ScopeGuard() { if(active_) f_(); }
void dismiss() { active_ = false; }
ScopeGuard() = delete;
ScopeGuard(const ScopeGuard&) = delete;
ScopeGuard& operator=(const ScopeGuard&) = delete;
ScopeGuard(ScopeGuard&& rhs)
: f_(std::move(rhs.f_))
, active_(rhs.active_) {
rhs.dismiss();
}
};
template<typename Fun>
ScopeGuard<Fun> scopeGuard(Fun f){
return ScopeGuard<Fun>(std::move(f));
}
namespace ScopeMacroSupport {
enum class ScopeGuardOnExit {};
template <typename Fun>
MV::ScopeGuard<Fun> operator+(ScopeGuardOnExit, Fun&& fn) {
return MV::ScopeGuard<Fun>(std::forward<Fun>(fn));
}
}
#define SCOPE_EXIT \
auto ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE) \
= MV::ScopeMacroSupport::ScopeGuardOnExit() + [&]()
#define CONCATENATE_IMPL(s1, s2) s1##s2
#define CONCATENATE(s1, s2) CONCATENATE_IMPL(s1, s2)
#ifdef __COUNTER__
#define ANONYMOUS_VARIABLE(str) \
CONCATENATE(str, __COUNTER__)
#else
#define ANONYMOUS_VARIABLE(str) \
CONCATENATE(str, __LINE__)
#endif
}
#endif
An example application making use of my library:
#include <iostream>
#include <string>
#include "signal.hpp"
class Observed {
private:
//Note: This is private to ensure not just anyone can spawn a signal
MV::Slot<void (int)> onChangeSlot;
public:
typedef MV::Slot<void (int)>::SharedSignalType ChangeEventSignal;
//SlotRegister is public, users can hook up signals to onChange with this value.
MV::SlotRegister<void (int)> onChange;
Observed():
onChange(onChangeSlot){ //Here is where the binding occurs
}
void change(int newValue){
onChangeSlot(newValue);
}
};
class Observer{
public:
Observer(std::string a_name, Observed &a_observed){
connection = a_observed.onChange.connect([=](int value){
std::cout << a_name << " caught changed value: " << value << std::endl;
});
}
private:
Observed::ChangeEventSignal connection;
};
int main(){
Observed observed;
Observer observer1("o[1]", observed);
{
Observer observer2("o[2]", observed);
observed.change(1);
}
observed.change(2);
}
Output of the above would be:
o[1] caught changed value: 1
o[2] caught changed value: 1
o[1] caught changed value: 2
As you can see, the slot disconnects dead signals automatically.
Here's what I came up with.
This assumes no need to aggregate results from the listeners of a broadcast signal.
Also, the "slot" or Signal::Listener is the owner of the callback.
This ought to live with the object that your (I'm guessing...) lambda is probably capturing so that when that object goes out of scope, so does the callback, which prevents it from being called anymore.
You could use methods described in other answers as well to store the Listener owner objects in a way you can lookup.
template <typename... FuncArgs>
class Signal
{
using fp = std::function<void(FuncArgs...)>;
std::forward_list<std::weak_ptr<fp> > registeredListeners;
public:
using Listener = std::shared_ptr<fp>;
Listener add(const std::function<void(FuncArgs...)> &cb) {
// passing by address, until copy is made in the Listener as owner.
Listener result(std::make_shared<fp>(cb));
registeredListeners.push_front(result);
return result;
}
void raise(FuncArgs... args) {
registeredListeners.remove_if([&args...](std::weak_ptr<fp> e) -> bool {
if (auto f = e.lock()) {
(*f)(args...);
return false;
}
return true;
});
}
};
usage
Signal<int> bloopChanged;
// ...
Signal<int>::Listener bloopResponse = bloopChanged.add([](int i) { ... });
// or
decltype(bloopChanged)::Listener bloopResponse = ...
// let bloopResponse go out of scope.
// or re-assign it
// or reset the shared_ptr to disconnect it
bloopResponse.reset();
I have made a gist for this too, with a more in-depth example:
https://gist.github.com/johnb003/dbc4a69af8ea8f4771666ce2e383047d
I have had a go at this myself also. My efforts can be found at this gist, which will continue to evolve . . .
https://gist.github.com/4172757
I use a different style, more similar to the change notifications in JUCE than BOOST signals. Connection management is done using some lambda syntax that does some capture by copy. It is working well so far.