libcruft-util/signal.ipp

198 lines
5.1 KiB
Plaintext
Raw Normal View History

2013-08-06 17:43:52 +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/>.
*
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
2013-08-06 17:43:52 +10:00
*/
#ifndef __UTIL_SIGNAL_HPP
#error
#endif
#include "debug.hpp"
#include <algorithm>
2013-08-06 17:43:52 +10:00
namespace util {
///////////////////////////////////////////////////////////////////////////
template <typename F>
struct signal<F>::cookie : public nocopy {
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),
2013-08-06 17:43:52 +10:00
m_parent (_parent)
{ ; }
//-------------------------------------------------------------------------
template <typename F>
signal<F>::cookie::cookie (cookie &&rhs):
m_position (rhs.m_position),
2013-08-06 17:43:52 +10:00
m_parent (rhs.m_parent)
{
rhs.m_position = rhs.m_parent.m_children.end ();
2013-08-06 17:43:52 +10:00
}
//-------------------------------------------------------------------------
template <typename F>
signal<F>::cookie::~cookie ()
{
if (m_parent.m_children.end () != m_position)
m_parent.disconnect (*this);
2013-08-06 17:43:52 +10:00
}
///////////////////////////////////////////////////////////////////////////
template <typename F>
2013-08-06 17:43:52 +10:00
void
signal<F>::cookie::reset (callback &&cb)
{
*m_position = std::move (cb);
}
2013-08-06 17:43:52 +10:00
//-------------------------------------------------------------------------
template <typename F>
2013-08-06 17:43:52 +10:00
void
signal<F>::cookie::reset (void)
{
m_position = m_parent.m_children.end ();
}
2013-08-06 17:43:52 +10:00
///////////////////////////////////////////////////////////////////////////
template <typename F>
signal<F>::signal ()
2013-08-06 17:43:52 +10:00
{ ; }
//-------------------------------------------------------------------------
template <typename F>
signal<F>::~signal ()
{
CHECK (empty ());
}
2013-08-06 17:43:52 +10:00
///////////////////////////////////////////////////////////////////////////
template <typename F>
typename signal<F>::cookie
signal<F>::connect (const callback &_cb)
{
return cookie (m_children.insert (m_children.end (), _cb), *this);
}
2013-08-06 17:43:52 +10:00
//-------------------------------------------------------------------------
template <typename F>
2013-08-06 17:43:52 +10:00
void
signal<F>::disconnect (const cookie &c)
{
m_children.erase (c.m_position);
}
2013-08-06 17:43:52 +10:00
//-------------------------------------------------------------------------
2013-08-06 17:43:52 +10:00
/// Disconnect all callbacks
template <typename F>
2013-08-06 17:43:52 +10:00
void
signal<F>::clear (void)
{
m_children.clear ();
}
2013-08-06 17:43:52 +10:00
///////////////////////////////////////////////////////////////////////////
2013-08-06 17:43:52 +10:00
/// Returns the number of callbacks connected.
template <typename F>
2013-08-06 17:43:52 +10:00
unsigned int
signal<F>::size (void) const
{
return m_children.size ();
}
2013-08-06 17:43:52 +10:00
//-------------------------------------------------------------------------
template <typename F>
2013-08-06 17:43:52 +10:00
bool
signal<F>::empty (void) const
{
return m_children.empty ();
}
2013-08-06 17:43:52 +10:00
///////////////////////////////////////////////////////////////////////////
template <typename F>
template <typename ...Args>
2013-08-06 17:43:52 +10:00
void
signal<F>::operator () (Args&&... tail) {
2013-08-06 17:43:52 +10:00
if (m_children.empty ())
return;
auto i = m_children.cbegin ();
bool looping;
do {
// Increment before we execute so that the caller is able to
// deregister during execution.
2013-08-06 17:43:52 +10:00
auto current = i++;
looping = m_children.cend () != i;
(*current)(std::forward<Args> (tail)...);
2013-08-06 17:43:52 +10:00
} while (looping);
}
2015-02-19 13:27:47 +11:00
///////////////////////////////////////////////////////////////////////////
template <typename T>
value_signal<T>::value_signal (T &&t):
m_value (std::forward<T> (t))
{ ; }
//-------------------------------------------------------------------------
template <typename T>
2015-02-19 13:27:47 +11:00
value_signal<T>::operator const T&() const
{
return m_value;
}
//-------------------------------------------------------------------------
2015-02-19 13:27:47 +11:00
template <typename T>
value_signal<T>&
value_signal<T>::operator= (const T &t)
{
m_value = t;
(*this) (m_value);
return *this;
}
2013-08-06 17:43:52 +10:00
}