2011-06-27 15:31:41 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2012-04-23 13:06:41 +10:00
|
|
|
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
2011-06-27 15:31:41 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SIGNAL_HPP
|
|
|
|
#define __SIGNAL_HPP
|
|
|
|
|
2011-08-10 21:31:24 +10:00
|
|
|
#include "debug.hpp"
|
|
|
|
#include "nocopy.hpp"
|
|
|
|
|
2011-06-27 15:31:41 +10:00
|
|
|
#include <algorithm>
|
2011-08-10 21:31:24 +10:00
|
|
|
#include <list>
|
2011-06-27 15:31:41 +10:00
|
|
|
#include <functional>
|
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
namespace util {
|
|
|
|
template <typename Ret, typename ...Args>
|
|
|
|
class signal {
|
|
|
|
public:
|
2012-05-26 18:01:04 +10:00
|
|
|
//typedef Ret (*callback_function)(Args...);
|
2011-07-16 14:47:56 +10:00
|
|
|
typedef std::function<Ret(Args...)> callback_object;
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
protected:
|
2011-08-10 21:31:24 +10:00
|
|
|
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):
|
|
|
|
m_cookie (_cookie),
|
|
|
|
m_parent (_parent)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
scoped_cookie (scoped_cookie &&rhs):
|
|
|
|
m_cookie (rhs.m_cookie),
|
|
|
|
m_parent (rhs.m_parent)
|
|
|
|
{
|
|
|
|
rhs.m_cookie = rhs.m_parent.m_children.end ();
|
|
|
|
}
|
|
|
|
|
|
|
|
~scoped_cookie () {
|
|
|
|
if (m_parent.m_children.end () != m_cookie)
|
|
|
|
m_parent.disconnect (m_cookie);
|
|
|
|
}
|
2011-08-12 00:26:28 +10:00
|
|
|
|
|
|
|
void renew (callback_object &&cb)
|
|
|
|
{ *m_cookie = std::move (cb); }
|
2011-09-16 22:56:52 +10:00
|
|
|
|
|
|
|
void release (void)
|
|
|
|
{ m_cookie = m_parent.m_children.end (); }
|
2011-08-10 21:31:24 +10:00
|
|
|
};
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
public:
|
|
|
|
signal ()
|
2011-08-10 21:31:24 +10:00
|
|
|
{ /*m_children.reserve (16);*/ }
|
|
|
|
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Add a callback to list.
|
2011-08-10 21:31:24 +10:00
|
|
|
const cookie
|
|
|
|
connect (const callback_object &_cb)
|
|
|
|
{ return m_children.insert (m_children.end (), _cb); }
|
|
|
|
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Add a callback to the list.
|
2012-05-26 18:01:04 +10:00
|
|
|
//const cookie
|
|
|
|
//connect (const callback_function &_cb)
|
|
|
|
// { return m_children.insert (m_children.end (), _cb); }
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2011-07-03 16:54:51 +10:00
|
|
|
|
2011-08-10 21:31:24 +10:00
|
|
|
void disconnect (const cookie _cb)
|
|
|
|
{ m_children.erase (_cb); }
|
2011-06-27 15:31:41 +10:00
|
|
|
|
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Disconnect all callbacks
|
2011-08-10 21:31:24 +10:00
|
|
|
void clear (void)
|
2011-07-16 14:47:56 +10:00
|
|
|
{ m_children.clear (); }
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Returns the number of callbacks connected.
|
|
|
|
unsigned int size (void) const
|
|
|
|
{ return m_children.size (); }
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-08-10 21:30:03 +10:00
|
|
|
|
|
|
|
bool empty (void) const
|
|
|
|
{ return m_children.empty (); }
|
|
|
|
|
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Execute all callbacks, ignoring the return parameters. Does not combine results.
|
|
|
|
void operator () (Args... tail) {
|
2012-05-11 12:20:32 +10:00
|
|
|
if (m_children.empty ())
|
|
|
|
return;
|
|
|
|
|
2011-09-16 22:57:25 +10:00
|
|
|
auto i = m_children.cbegin ();
|
2012-05-11 12:20:32 +10:00
|
|
|
bool looping;
|
2011-09-16 22:57:25 +10:00
|
|
|
|
2012-05-11 12:20:32 +10:00
|
|
|
do {
|
2011-08-12 00:26:56 +10:00
|
|
|
// Increment before we execute so that the caller is able to deregister during execution.
|
|
|
|
auto current = i++;
|
2011-09-16 22:57:25 +10:00
|
|
|
looping = m_children.cend () != i;
|
|
|
|
|
2011-08-12 00:26:56 +10:00
|
|
|
(*current)(tail...);
|
2012-05-11 12:20:32 +10:00
|
|
|
} while (looping);
|
2011-07-16 14:47:56 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-06-27 15:31:41 +10:00
|
|
|
|
|
|
|
#endif // __SIGNAL_HPP
|