Skip to content

Instantly share code, notes, and snippets.

@frsyuki
Created September 30, 2010 15:56
Show Gist options
  • Save frsyuki/604810 to your computer and use it in GitHub Desktop.
Save frsyuki/604810 to your computer and use it in GitHub Desktop.
#include <tr1/functional>
#include <vector>
#include <string>
#include <iostream>
#include <exception>
namespace ebus {
using std::tr1::bind;
using std::tr1::function;
template <typename Signalture>
class call_slot;
template <typename R>
class call_slot<R ()> {
public:
typedef std::tr1::function<R ()> function_type;
void connect(const function_type& func) {
m_func = func;
}
R call() {
return m_func();
}
private:
function_type m_func;
};
%varlen_each do |gen|
template <typename R, [%gen.template%]>
class call_slot<R ([%gen.types%])> {
public:
typedef std::tr1::function<R ([%gen.types%])> function_type;
void connect(const function_type& func) {
m_func = func;
}
R call([%gen.args%]) {
return m_func([%gen.params%]);
}
private:
function_type m_func;
};
%end
template <typename Signalture>
class signal_slot;
template <>
class signal_slot<void ()> {
public:
typedef std::tr1::function<void ()> function_type;
void connect(const function_type& func) {
m_funcs.push_back(func);
}
void signal() {
for(std::vector<function_type>::iterator it(m_funcs.begin()),
it_end(m_funcs.end()); it != it_end; ++it) {
try {
(*it)();
} catch (const std::exception& e) {
#ifdef EBUS_DEBUG
std::cerr << std::string("ebus signal error: ").append(e.what()) << std::endl;
#endif
}
}
}
private:
std::vector<function_type> m_funcs;
};
%varlen_each do |gen|
template <[%gen.template%]>
class signal_slot<void ([%gen.types%])> {
public:
typedef std::tr1::function<void ([%gen.types%])> function_type;
void connect(const function_type& func) {
m_funcs.push_back(func);
}
void signal([%gen.args%]) {
for(typename std::vector<function_type>::iterator it(m_funcs.begin()),
it_end(m_funcs.end()); it != it_end; ++it) {
try {
(*it)([%gen.params%]);
} catch (const std::exception& e) {
#ifdef EBUS_DEBUG
std::cerr << std::string("ebus signal error: ").append(e.what()) << std::endl;
#endif
}
}
}
private:
std::vector<function_type> m_funcs;
};
%end
} // namespace ebus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment