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/>.
|
|
|
|
*
|
2015-02-18 02:34:10 +11:00
|
|
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
2011-06-27 15:31:41 +10:00
|
|
|
*/
|
|
|
|
|
2013-08-06 17:43:52 +10:00
|
|
|
#ifndef __UTIL_SIGNAL_HPP
|
|
|
|
#define __UTIL_SIGNAL_HPP
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-08-10 21:31:24 +10:00
|
|
|
#include "nocopy.hpp"
|
|
|
|
|
2011-06-27 15:31:41 +10:00
|
|
|
#include <functional>
|
2015-02-18 02:34:10 +11:00
|
|
|
#include <list>
|
|
|
|
#include <type_traits>
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
namespace util {
|
2015-02-18 02:34:10 +11:00
|
|
|
template <typename F>
|
2011-07-16 14:47:56 +10:00
|
|
|
class signal {
|
|
|
|
public:
|
2015-02-18 02:34:10 +11:00
|
|
|
using R = std::result_of<F>;
|
|
|
|
using callback = std::function<F>;
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
struct cookie;
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
public:
|
2013-08-06 17:43:52 +10:00
|
|
|
signal ();
|
2015-02-18 02:34:10 +11:00
|
|
|
~signal ();
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Add a callback to list.
|
2015-02-18 02:34:10 +11:00
|
|
|
cookie connect (const callback&);
|
|
|
|
|
|
|
|
void disconnect (const cookie&);
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Disconnect all callbacks
|
2013-08-06 17:43:52 +10:00
|
|
|
void clear (void);
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Returns the number of callbacks connected.
|
2013-08-06 17:43:52 +10:00
|
|
|
unsigned int size (void) const;
|
|
|
|
bool empty (void) const;
|
2011-08-10 21:30:03 +10:00
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
/// Execute all callbacks, ignoring the return parameters. Does
|
|
|
|
/// not combine results.
|
|
|
|
template <typename ...Args>
|
|
|
|
void operator () (Args&&... tail);
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::list<callback> group;
|
|
|
|
group m_children;
|
2011-07-16 14:47:56 +10:00
|
|
|
};
|
2015-02-19 13:27:47 +11:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class value_signal : public signal<void(T)> {
|
|
|
|
public:
|
|
|
|
operator const T&() const;
|
|
|
|
|
|
|
|
value_signal<T>& operator= (const T&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_value;
|
|
|
|
};
|
2011-07-16 14:47:56 +10:00
|
|
|
}
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2013-08-06 17:43:52 +10:00
|
|
|
#include "signal.ipp"
|
|
|
|
|
2011-06-27 15:31:41 +10:00
|
|
|
#endif // __SIGNAL_HPP
|