libcruft-util/signal.ipp

211 lines
5.8 KiB
Plaintext
Raw Normal View History

2013-08-06 17:43:52 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2013-08-06 17:43:52 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2013-08-06 17:43:52 +10:00
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2013-08-06 17:43:52 +10:00
*
* 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 "nocopy.hpp"
#include <algorithm>
2013-08-06 17:43:52 +10:00
namespace util {
///////////////////////////////////////////////////////////////////////////
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
struct signal<F,C>::cookie : public nocopy {
cookie (typename group::iterator, signal<F,C> &parent);
cookie (cookie &&rhs);
~cookie ();
void reset (callback &&cb);
typename group::iterator m_position;
2015-03-10 22:52:38 +11:00
signal<F,C> &m_parent;
};
///////////////////////////////////////////////////////////////////////////
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
signal<F,C>::cookie::cookie (typename group::iterator _position,
signal<F,C> &_parent):
m_position (_position),
2013-08-06 17:43:52 +10:00
m_parent (_parent)
{ ; }
//-------------------------------------------------------------------------
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
signal<F,C>::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
}
//-------------------------------------------------------------------------
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
signal<F,C>::cookie::~cookie ()
{
if (m_parent.m_children.end () != m_position)
m_parent.disconnect (*this);
2013-08-06 17:43:52 +10:00
}
///////////////////////////////////////////////////////////////////////////
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
2013-08-06 17:43:52 +10:00
void
2015-03-10 22:52:38 +11:00
signal<F,C>::cookie::reset (callback &&cb)
{
*m_position = std::move (cb);
}
2013-08-06 17:43:52 +10:00
///////////////////////////////////////////////////////////////////////////
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
signal<F,C>::signal ()
2013-08-06 17:43:52 +10:00
{ ; }
//-------------------------------------------------------------------------
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
signal<F,C>::~signal ()
{
CHECK (empty ());
}
2013-08-06 17:43:52 +10:00
///////////////////////////////////////////////////////////////////////////
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
typename signal<F,C>::cookie
signal<F,C>::connect (const callback &_cb)
{
2015-03-10 22:52:38 +11:00
return cookie (
m_children.insert (
m_children.end (),
std::move (_cb)
),
*this
);
}
2013-08-06 17:43:52 +10:00
//-------------------------------------------------------------------------
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
typename signal<F,C>::cookie
signal<F,C>::connect (callback &&_cb)
{
return cookie (
m_children.insert (
m_children.end (),
std::move (_cb)
),
*this
);
}
//-------------------------------------------------------------------------
template <typename F, template <typename> class C>
2013-08-06 17:43:52 +10:00
void
2015-03-10 22:52:38 +11:00
signal<F,C>::disconnect (cookie &c)
{
m_children.erase (c.m_position);
2015-03-10 22:52:38 +11:00
c.m_position = m_children.end ();
}
2013-08-06 17:43:52 +10:00
//-------------------------------------------------------------------------
2013-08-06 17:43:52 +10:00
/// Disconnect all callbacks
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
2013-08-06 17:43:52 +10:00
void
2015-04-13 18:06:08 +10:00
signal<F,C>::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.
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
size_t
2015-03-10 22:52:38 +11:00
signal<F,C>::size (void) const
{
return m_children.size ();
}
2013-08-06 17:43:52 +10:00
//-------------------------------------------------------------------------
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
2013-08-06 17:43:52 +10:00
bool
2015-03-10 22:52:38 +11:00
signal<F,C>::empty (void) const
{
return m_children.empty ();
}
2013-08-06 17:43:52 +10:00
///////////////////////////////////////////////////////////////////////////
2015-03-10 22:52:38 +11:00
template <typename F, template <typename> class C>
template <typename ...Args>
2015-03-10 22:52:38 +11:00
typename signal<F,C>::R
signal<F,C>::operator () (Args&&... tail) {
2013-08-06 17:43:52 +10:00
if (m_children.empty ())
2015-03-10 22:52:38 +11:00
return R();
//auto i = m_children.cbegin ();
//bool looping;
2013-08-06 17:43:52 +10:00
2015-03-10 22:52:38 +11:00
C<F> combiner;
return combiner (m_children.begin (), m_children.end (), std::forward<Args> (tail)...);
2013-08-06 17:43:52 +10:00
2015-03-10 22:52:38 +11:00
//do {
// // Increment before we execute so that the caller is able to
// // deregister themselves during execution.
// auto current = i++;
// looping = m_children.cend () != i;
2013-08-06 17:43:52 +10:00
2015-03-10 22:52:38 +11:00
// (*current)(std::forward<Args> (tail)...);
//} while (looping);
2013-08-06 17:43:52 +10:00
}
2015-02-19 13:27:47 +11:00
///////////////////////////////////////////////////////////////////////////
template <typename T>
value_signal<T>::value_signal (T t):
m_value (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
}