Skip to content

Instantly share code, notes, and snippets.

@r2p2
Created June 2, 2012 11:44
Show Gist options
  • Select an option

  • Save r2p2/2857963 to your computer and use it in GitHub Desktop.

Select an option

Save r2p2/2857963 to your computer and use it in GitHub Desktop.
signals for functors
#pragma once
#include <list>
#include <stdint.h>
#include <tr1/functional>
class NoParam {};
template <
class P1 = NoParam,
class P2 = NoParam,
class P3 = NoParam,
class P4 = NoParam>
class Signal
{
};
template <class P1, class P2, class P3>
class Signal<P1, P2, P3, NoParam>
{
public:
typedef std::tr1::function<void (P1, P2, P3)> functor;
Signal() : _handler() {}
void emit(const P1& arg1, const P2& arg2, const P3& arg3)
{
typename std::list<functor>::iterator it;
for(it = _handler.begin(); it != _handler.end(); it++)
{
(*it)(arg1, arg2, arg3);
}
}
void connect(functor fun)
{
_handler.push_back(fun);
}
private:
std::list<functor> _handler;
};
template <class P1, class P2>
class Signal<P1, P2, NoParam, NoParam>
{
public:
typedef std::tr1::function<void (P1, P2)> functor;
Signal() : _handler() {}
void emit(const P1& arg1, const P2& arg2)
{
typename std::list<functor>::iterator it;
for(it = _handler.begin(); it != _handler.end(); it++)
{
(*it)(arg1, arg2);
}
}
void connect(functor fun)
{
_handler.push_back(fun);
}
private:
std::list<functor> _handler;
};
template <class P1>
class Signal<P1, NoParam, NoParam, NoParam>
{
public:
typedef std::tr1::function<void (P1)> functor;
Signal() : _handler() {}
void emit(const P1& arg1)
{
typename std::list<functor>::iterator it;
for(it = _handler.begin(); it != _handler.end(); it++)
{
(*it)(arg1);
}
}
void connect(functor fun)
{
_handler.push_back(fun);
}
private:
std::list<functor> _handler;
};
template <>
class Signal<NoParam, NoParam, NoParam, NoParam>
{
public:
typedef std::tr1::function<void ()> functor;
Signal() : _handler() {}
void emit()
{
std::list<functor>::iterator it;
for(it = _handler.begin(); it != _handler.end(); it++)
{
(*it)();
}
}
void connect(functor fun)
{
_handler.push_back(fun);
}
private:
std::list<functor> _handler;
};
#pragma once
#include "signal.h"
#include <string>
#include <iostream>
struct BeginDrawing2
{
void operator()(int button) const
{
std::cout << "mouse button down: " << button << std::endl;
}
};
struct StdLogSignals2
{
StdLogSignals2(const std::string& signal)
:_signal(signal)
{
}
template <class T>
void operator()(T t) const
{
std::cout << "emit of signal: " << _signal << " detected" << std::endl;
}
private:
std::string _signal;
};
void one_argument()
{
Signal<int> on_mouse_down;
on_mouse_down.connect(StdLogSignals2("on_mouse_down"));
on_mouse_down.connect(BeginDrawing2());
on_mouse_down.emit(4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment