signal: simplify unneeded signal elements
* use function syntax for template type * remove non-scoped cookie * simplify many type names
This commit is contained in:
parent
8e5bd17974
commit
38d3fc1961
@ -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 util::signal <void>;
|
template class util::signal <void(void)>;
|
||||||
|
48
signal.hpp
48
signal.hpp
@ -14,52 +14,36 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* Copyright 2011-2013 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __UTIL_SIGNAL_HPP
|
#ifndef __UTIL_SIGNAL_HPP
|
||||||
#define __UTIL_SIGNAL_HPP
|
#define __UTIL_SIGNAL_HPP
|
||||||
|
|
||||||
#include "debug.hpp"
|
|
||||||
#include "nocopy.hpp"
|
#include "nocopy.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <list>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <list>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template <typename Ret, typename ...Args>
|
template <typename F>
|
||||||
class signal {
|
class signal {
|
||||||
public:
|
public:
|
||||||
//typedef Ret (*callback_function)(Args...);
|
using R = std::result_of<F>;
|
||||||
typedef std::function<Ret(Args...)> callback_object;
|
using callback = std::function<F>;
|
||||||
|
|
||||||
protected:
|
struct cookie;
|
||||||
typedef std::list<callback_object> group;
|
|
||||||
group m_children;
|
|
||||||
|
|
||||||
public:
|
|
||||||
typedef typename group::iterator cookie;
|
|
||||||
struct scoped_cookie : public nocopy {
|
|
||||||
cookie m_cookie;
|
|
||||||
signal<Ret, Args...>& m_parent;
|
|
||||||
|
|
||||||
scoped_cookie (cookie _cookie, signal<Ret, Args...> &_parent);
|
|
||||||
scoped_cookie (scoped_cookie &&rhs);
|
|
||||||
~scoped_cookie ();
|
|
||||||
|
|
||||||
void renew (callback_object &&cb);
|
|
||||||
void release (void);
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
signal ();
|
signal ();
|
||||||
|
~signal ();
|
||||||
|
|
||||||
/// Add a callback to list.
|
/// Add a callback to list.
|
||||||
cookie connect (const callback_object &_cb);
|
cookie connect (const callback&);
|
||||||
scoped_cookie scoped_connect (const callback_object &_cb);
|
|
||||||
|
void disconnect (const cookie&);
|
||||||
|
|
||||||
void disconnect (const cookie _cb);
|
|
||||||
/// Disconnect all callbacks
|
/// Disconnect all callbacks
|
||||||
void clear (void);
|
void clear (void);
|
||||||
|
|
||||||
@ -67,8 +51,14 @@ namespace util {
|
|||||||
unsigned int size (void) const;
|
unsigned int size (void) const;
|
||||||
bool empty (void) const;
|
bool empty (void) const;
|
||||||
|
|
||||||
/// Execute all callbacks, ignoring the return parameters. Does not combine results.
|
/// Execute all callbacks, ignoring the return parameters. Does
|
||||||
void operator () (Args... tail);
|
/// not combine results.
|
||||||
|
template <typename ...Args>
|
||||||
|
void operator () (Args&&... tail);
|
||||||
|
|
||||||
|
private:
|
||||||
|
typedef std::list<callback> group;
|
||||||
|
group m_children;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
149
signal.ipp
149
signal.ipp
@ -14,102 +14,144 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* Copyright 2011-2013 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __UTIL_SIGNAL_HPP
|
#ifndef __UTIL_SIGNAL_HPP
|
||||||
#error
|
#error
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "debug.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template <typename Ret, typename ...Args>
|
///////////////////////////////////////////////////////////////////////////
|
||||||
signal<Ret, Args...>::scoped_cookie::scoped_cookie (cookie _cookie,
|
template <typename F>
|
||||||
signal<Ret, Args...> &_parent):
|
struct signal<F>::cookie : public nocopy {
|
||||||
m_cookie (_cookie),
|
cookie (typename group::iterator, signal<F> &parent);
|
||||||
|
cookie (cookie &&rhs);
|
||||||
|
~cookie ();
|
||||||
|
|
||||||
|
void reset (callback &&cb);
|
||||||
|
void reset (void);
|
||||||
|
|
||||||
|
typename group::iterator m_position;
|
||||||
|
signal<F> &m_parent;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename F>
|
||||||
|
signal<F>::cookie::cookie (typename group::iterator _position,
|
||||||
|
signal<F> &_parent):
|
||||||
|
m_position (_position),
|
||||||
m_parent (_parent)
|
m_parent (_parent)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
//-------------------------------------------------------------------------
|
||||||
signal<Ret, Args...>::scoped_cookie::scoped_cookie (scoped_cookie &&rhs):
|
template <typename F>
|
||||||
m_cookie (rhs.m_cookie),
|
signal<F>::cookie::cookie (cookie &&rhs):
|
||||||
|
m_position (rhs.m_position),
|
||||||
m_parent (rhs.m_parent)
|
m_parent (rhs.m_parent)
|
||||||
{
|
{
|
||||||
rhs.m_cookie = rhs.m_parent.m_children.end ();
|
rhs.m_position = rhs.m_parent.m_children.end ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
//-------------------------------------------------------------------------
|
||||||
signal<Ret, Args...>::scoped_cookie::~scoped_cookie () {
|
template <typename F>
|
||||||
if (m_parent.m_children.end () != m_cookie)
|
signal<F>::cookie::~cookie ()
|
||||||
m_parent.disconnect (m_cookie);
|
{
|
||||||
|
if (m_parent.m_children.end () != m_position)
|
||||||
|
m_parent.disconnect (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename F>
|
||||||
void
|
void
|
||||||
signal<Ret, Args...>::scoped_cookie::renew (callback_object &&cb)
|
signal<F>::cookie::reset (callback &&cb)
|
||||||
{ *m_cookie = std::move (cb); }
|
{
|
||||||
|
*m_position = std::move (cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename F>
|
||||||
void
|
void
|
||||||
signal<Ret, Args...>::scoped_cookie::release (void)
|
signal<F>::cookie::reset (void)
|
||||||
{ m_cookie = m_parent.m_children.end (); }
|
{
|
||||||
|
m_position = m_parent.m_children.end ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
///////////////////////////////////////////////////////////////////////////
|
||||||
signal<Ret, Args...>::signal ()
|
template <typename F>
|
||||||
|
signal<F>::signal ()
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
//-------------------------------------------------------------------------
|
||||||
typename signal<Ret, Args...>::cookie
|
template <typename F>
|
||||||
signal<Ret, Args...>::connect (const callback_object &_cb)
|
signal<F>::~signal ()
|
||||||
{ return m_children.insert (m_children.end (), _cb); }
|
{
|
||||||
|
CHECK (empty ());
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename F>
|
||||||
|
typename signal<F>::cookie
|
||||||
|
signal<F>::connect (const callback &_cb)
|
||||||
|
{
|
||||||
|
return cookie (m_children.insert (m_children.end (), _cb), *this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
//-------------------------------------------------------------------------
|
||||||
typename signal<Ret, Args...>::scoped_cookie
|
template <typename F>
|
||||||
signal<Ret, Args...>::scoped_connect (const callback_object &_cb)
|
|
||||||
{ return scoped_cookie (connect (_cb), *this); }
|
|
||||||
|
|
||||||
|
|
||||||
/// Add a callback to the list.
|
|
||||||
//const cookie
|
|
||||||
//connect (const callback_function &_cb)
|
|
||||||
// { return m_children.insert (m_children.end (), _cb); }
|
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
|
||||||
void
|
void
|
||||||
signal<Ret, Args...>::disconnect (const cookie _cb)
|
signal<F>::disconnect (const cookie &c)
|
||||||
{ m_children.erase (_cb); }
|
{
|
||||||
|
m_children.erase (c.m_position);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
/// Disconnect all callbacks
|
/// Disconnect all callbacks
|
||||||
template <typename Ret, typename ...Args>
|
template <typename F>
|
||||||
void
|
void
|
||||||
signal<Ret, Args...>::clear (void)
|
signal<F>::clear (void)
|
||||||
{ m_children.clear (); }
|
{
|
||||||
|
m_children.clear ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
/// Returns the number of callbacks connected.
|
/// Returns the number of callbacks connected.
|
||||||
template <typename Ret, typename ...Args>
|
template <typename F>
|
||||||
unsigned int
|
unsigned int
|
||||||
signal<Ret, Args...>::size (void) const
|
signal<F>::size (void) const
|
||||||
{ return m_children.size (); }
|
{
|
||||||
|
return m_children.size ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename F>
|
||||||
bool
|
bool
|
||||||
signal<Ret, Args...>::empty (void) const
|
signal<F>::empty (void) const
|
||||||
{ return m_children.empty (); }
|
{
|
||||||
|
return m_children.empty ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename F>
|
||||||
|
template <typename ...Args>
|
||||||
void
|
void
|
||||||
signal<Ret, Args...>::operator () (Args... tail) {
|
signal<F>::operator () (Args&&... tail) {
|
||||||
if (m_children.empty ())
|
if (m_children.empty ())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -117,11 +159,12 @@ namespace util {
|
|||||||
bool looping;
|
bool looping;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// Increment before we execute so that the caller is able to deregister during execution.
|
// Increment before we execute so that the caller is able to
|
||||||
|
// deregister during execution.
|
||||||
auto current = i++;
|
auto current = i++;
|
||||||
looping = m_children.cend () != i;
|
looping = m_children.cend () != i;
|
||||||
|
|
||||||
(*current)(tail...);
|
(*current)(std::forward<Args> (tail)...);
|
||||||
} while (looping);
|
} while (looping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,54 +1,69 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
#include "../signal.hpp"
|
#include "signal.hpp"
|
||||||
#include "../debug.hpp"
|
#include "debug.hpp"
|
||||||
|
#include "raii.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
void
|
void
|
||||||
increment_uint (unsigned int &val)
|
test_null (void)
|
||||||
{ ++val; }
|
{
|
||||||
|
util::signal<void(void)> void_signal;
|
||||||
|
|
||||||
void
|
|
||||||
test_null (void) {
|
|
||||||
util::signal<void> void_signal;
|
|
||||||
void_signal ();
|
void_signal ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void
|
void
|
||||||
test_single (void) {
|
increment_uint (unsigned int &val)
|
||||||
unsigned int val = 0;
|
{
|
||||||
util::signal<void, unsigned int&> void_signal;
|
++val;
|
||||||
|
}
|
||||||
|
|
||||||
void_signal.connect (increment_uint);
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
void
|
||||||
|
test_single (void)
|
||||||
|
{
|
||||||
|
unsigned int val = 0;
|
||||||
|
util::signal<void(unsigned int&)> void_signal;
|
||||||
|
|
||||||
|
auto raii = void_signal.connect (increment_uint);
|
||||||
void_signal (val);
|
void_signal (val);
|
||||||
|
|
||||||
CHECK_EQ (val, 1);
|
CHECK_EQ (val, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
void
|
void
|
||||||
test_double (void) {
|
test_double (void)
|
||||||
|
{
|
||||||
unsigned int val = 0;
|
unsigned int val = 0;
|
||||||
util::signal<void, unsigned int&> void_signal;
|
util::signal<void(unsigned int&)> void_signal;
|
||||||
|
|
||||||
void_signal.connect (increment_uint);
|
auto raii = void_signal.connect (increment_uint);
|
||||||
void_signal.connect (increment_uint);
|
auto raii = void_signal.connect (increment_uint);
|
||||||
void_signal (val);
|
void_signal (val);
|
||||||
|
|
||||||
CHECK_EQ (val, 2);
|
CHECK_EQ (val, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void
|
void
|
||||||
test_linking_pointers (void) {
|
test_linking_pointers (void)
|
||||||
util::signal<void, const char*> ptr_signal;
|
{
|
||||||
ptr_signal (NULL);
|
util::signal<void(const char*)> ptr_signal;
|
||||||
|
ptr_signal (nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
int
|
int
|
||||||
main (int, char **) {
|
main (int, char **)
|
||||||
|
{
|
||||||
test_null ();
|
test_null ();
|
||||||
test_single ();
|
test_single ();
|
||||||
test_double ();
|
test_double ();
|
||||||
|
Loading…
Reference in New Issue
Block a user