netgen/libsrc/core/signal.hpp

49 lines
1.0 KiB
C++
Raw Normal View History

2019-08-16 15:52:37 +05:00
#ifndef NGCORE_SIGNALS_HPP
#define NGCORE_SIGNALS_HPP
#include <list>
#include <functional>
namespace ngcore
{
template<typename ... ParameterTypes>
class Signal
{
private:
std::list<std::function<bool(ParameterTypes...)>> funcs;
bool is_emitting;
public:
Signal() : is_emitting(true) {}
template<typename Cls, typename FUNC>
2019-08-21 14:31:25 +05:00
void Connect(Cls* self, FUNC f)
2019-08-16 15:52:37 +05:00
{
auto ptr = self->weak_from_this();
auto func = [ptr, f](ParameterTypes... args)
{
if (ptr.expired())
return false;
f(args...);
return true;
};
funcs.push_back(func);
}
2019-08-21 14:31:25 +05:00
inline void Emit(ParameterTypes ...args)
2019-08-16 15:52:37 +05:00
{
if(is_emitting)
funcs.remove_if([&](auto& f){ return !f(args...); });
}
2019-08-21 14:31:25 +05:00
inline bool SetEmitting(bool emitting)
{
bool was_emitting = is_emitting;
is_emitting = emitting;
return was_emitting;
}
inline bool GetEmitting() const { return is_emitting; }
2019-08-16 15:52:37 +05:00
};
} // namespace ngcore
#endif // NGCORE_SIGNALS_HPP