Move signal into namespace util
signal will clash with the POSIX signal function. This tends to be too annoying to work around with careful inclusion orders or other namespacing.
This commit is contained in:
parent
64448e7e5e
commit
d6b943500c
@ -21,4 +21,4 @@
|
|||||||
#include "signal.hpp"
|
#include "signal.hpp"
|
||||||
|
|
||||||
// Instance something probably useful here so that we generate early compile/link errors.
|
// Instance something probably useful here so that we generate early compile/link errors.
|
||||||
template class signal <void>;
|
template class util::signal <void>;
|
||||||
|
79
signal.hpp
79
signal.hpp
@ -24,53 +24,54 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
class signal {
|
||||||
|
public:
|
||||||
|
typedef Ret (*callback_function)(Args...);
|
||||||
|
typedef std::function<Ret(Args...)> callback_object;
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
protected:
|
||||||
class signal {
|
std::vector<callback_object> m_children;
|
||||||
public:
|
|
||||||
typedef Ret (*callback_function)(Args...);
|
|
||||||
typedef std::function<Ret(Args...)> callback_object;
|
|
||||||
|
|
||||||
protected:
|
public:
|
||||||
std::vector<callback_object> m_children;
|
signal ()
|
||||||
|
{ m_children.reserve (16); }
|
||||||
|
|
||||||
public:
|
/// Add a callback to list.
|
||||||
signal ()
|
void connect (callback_object _cb)
|
||||||
{ m_children.reserve (16); }
|
{ m_children.push_back (_cb); }
|
||||||
|
|
||||||
/// Add a callback to list.
|
/// Add a callback to the list.
|
||||||
void connect (callback_object _cb)
|
void connect (callback_function _cb)
|
||||||
{ m_children.push_back (_cb); }
|
{ m_children.push_back (_cb); }
|
||||||
|
|
||||||
/// Add a callback to the list.
|
/// Remove all instances of callback `cb'
|
||||||
void connect (callback_function _cb)
|
//void disconnect (callback_function _cb)
|
||||||
{ m_children.push_back (_cb); }
|
// { disconnect (callback_object (_cb)); }
|
||||||
|
|
||||||
/// Remove all instances of callback `cb'
|
/// Remove all instances of callback `cb'
|
||||||
//void disconnect (callback_function _cb)
|
/*void disconnect (callback_object _cb) {
|
||||||
// { disconnect (callback_object (_cb)); }
|
m_children.erase (std::remove (m_children.begin (),
|
||||||
|
m_children.end (),
|
||||||
|
_cb),
|
||||||
|
m_children.end ());
|
||||||
|
}*/
|
||||||
|
|
||||||
/// Remove all instances of callback `cb'
|
/// Disconnect all callbacks
|
||||||
/*void disconnect (callback_object _cb) {
|
void clear (void)
|
||||||
m_children.erase (std::remove (m_children.begin (),
|
{ m_children.clear (); }
|
||||||
m_children.end (),
|
|
||||||
_cb),
|
|
||||||
m_children.end ());
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/// Disconnect all callbacks
|
/// Returns the number of callbacks connected.
|
||||||
void clear (void)
|
unsigned int size (void) const
|
||||||
{ m_children.clear (); }
|
{ return m_children.size (); }
|
||||||
|
|
||||||
/// Returns the number of callbacks connected.
|
/// Execute all callbacks, ignoring the return parameters. Does not combine results.
|
||||||
unsigned int size (void) const
|
void operator () (Args... tail) {
|
||||||
{ return m_children.size (); }
|
for (auto i = m_children.begin (), end = m_children.end (); i != end; ++i)
|
||||||
|
(*i)(tail...);
|
||||||
/// Execute all callbacks, ignoring the return parameters. Does not combine results.
|
}
|
||||||
void operator () (Args... tail) {
|
};
|
||||||
for (auto i = m_children.begin (), end = m_children.end (); i != end; ++i)
|
}
|
||||||
(*i)(tail...);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // __SIGNAL_HPP
|
#endif // __SIGNAL_HPP
|
||||||
|
Loading…
Reference in New Issue
Block a user