Danny Robson
0e3fa05f05
ipp files weren't a great way of keeping things clean, and IDEs have a little trouble dealing with the split configuration. this simplifies debugging a great deal.
217 lines
5.6 KiB
C++
217 lines
5.6 KiB
C++
/*
|
|
* 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
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* 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.
|
|
*
|
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
|
*/
|
|
|
|
#ifndef CRUFT_UTIL_SIGNAL_HPP
|
|
#define CRUFT_UTIL_SIGNAL_HPP
|
|
|
|
#include "types/traits.hpp"
|
|
#include "debug.hpp"
|
|
#include "nocopy.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <functional>
|
|
#include <list>
|
|
|
|
namespace util {
|
|
namespace combine {
|
|
template <typename F>
|
|
struct logical_and {
|
|
using R = typename func_traits<F>::return_type;
|
|
|
|
template <typename T, typename ...Args>
|
|
R operator() (T first, T last, Args&&... args)
|
|
{
|
|
while (first != last)
|
|
if (!(*first++)(args...))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
template <typename F>
|
|
struct logical_or {
|
|
using R = typename func_traits<F>::return_type;
|
|
|
|
template <typename T, typename ...Args>
|
|
R operator() (T first, T last, Args&&... args)
|
|
{
|
|
while (first != last)
|
|
if ((*first++)(args...))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
};
|
|
|
|
template <typename F>
|
|
struct noop {
|
|
using R = void;
|
|
|
|
template <typename T, typename ...Args>
|
|
R operator() (T first, T last, Args&&... args)
|
|
{
|
|
while (first != last) {
|
|
(*first++)(args...);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
template <typename F, template <typename> class C = combine::noop>
|
|
class signal {
|
|
public:
|
|
using R = typename C<F>::R;
|
|
using callback = std::function<F>;
|
|
|
|
struct cookie;
|
|
|
|
public:
|
|
signal () = default;
|
|
~signal ()
|
|
{
|
|
CHECK (empty ());
|
|
}
|
|
|
|
/// Add a callback to list.
|
|
cookie connect [[nodiscard]] (callback &&_cb)
|
|
{
|
|
return cookie (
|
|
m_children.insert (
|
|
m_children.end (),
|
|
std::move (_cb)
|
|
),
|
|
*this
|
|
);
|
|
}
|
|
|
|
cookie connect [[nodiscard]] (const callback &_cb)
|
|
{
|
|
{
|
|
return cookie (
|
|
m_children.insert (
|
|
m_children.end (),
|
|
std::move (_cb)
|
|
),
|
|
*this
|
|
);
|
|
}
|
|
}
|
|
|
|
void disconnect (cookie &c)
|
|
{
|
|
m_children.erase (c.m_position);
|
|
c.m_position = m_children.end ();
|
|
}
|
|
|
|
/// Disconnect all callbacks
|
|
void clear (void)
|
|
{
|
|
m_children.clear ();
|
|
}
|
|
|
|
/// Returns the number of callbacks connected.
|
|
size_t size (void) const
|
|
{
|
|
return m_children.size ();
|
|
}
|
|
|
|
bool empty (void) const
|
|
{
|
|
return m_children.empty ();
|
|
}
|
|
|
|
/// Execute all callbacks
|
|
template <typename ...Args>
|
|
R
|
|
operator() (Args&&... tail)
|
|
{
|
|
if (m_children.empty ())
|
|
return R();
|
|
|
|
C<F> combiner;
|
|
return combiner (
|
|
m_children.begin (),
|
|
m_children.end (),
|
|
std::forward<Args> (tail)...
|
|
);
|
|
}
|
|
|
|
private:
|
|
typedef std::list<callback> group;
|
|
group m_children;
|
|
};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
template <typename F, template <typename> class C>
|
|
struct signal<F,C>::cookie : public nocopy {
|
|
cookie (typename group::iterator _position,
|
|
signal<F,C> &_parent):
|
|
m_position (_position),
|
|
m_parent (_parent)
|
|
{ ; }
|
|
|
|
cookie (cookie &&rhs):
|
|
m_position (rhs.m_position),
|
|
m_parent (rhs.m_parent)
|
|
{
|
|
rhs.m_position = rhs.m_parent.m_children.end ();
|
|
}
|
|
|
|
~cookie ()
|
|
{
|
|
if (m_parent.m_children.end () != m_position)
|
|
m_parent.disconnect (*this);
|
|
}
|
|
|
|
void reset (callback &&cb)
|
|
{
|
|
*m_position = std::move (cb);
|
|
}
|
|
|
|
typename group::iterator m_position;
|
|
signal<F,C> &m_parent;
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// wrap a value in a signal and trigger on assignment
|
|
//template <typename T, template <typename> class C>
|
|
template <typename T>
|
|
class value_signal : public signal<void(T)> {
|
|
public:
|
|
explicit value_signal (T t): m_value (t) { ; }
|
|
value_signal () = default;
|
|
|
|
operator const T&() const { return m_value; }
|
|
|
|
value_signal<T>&
|
|
operator= (const T &t)
|
|
{
|
|
m_value = t;
|
|
(*this) (m_value);
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
T m_value;
|
|
};
|
|
}
|
|
|
|
#endif
|