/* * 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 . * * Copyright 2011-2015 Danny Robson */ #ifndef __UTIL_SIGNAL_HPP #define __UTIL_SIGNAL_HPP #include "nocopy.hpp" #include "types/traits.hpp" #include #include #include namespace util { namespace combine { template struct logical_and { using R = typename func_traits::return_type; template R operator() (T first, T last, Args&&... args) { while (first != last) if (!(*first++)(std::forward (args)...)) return false; return true; } }; template struct logical_or { using R = typename func_traits::return_type; template R operator() (T first, T last, Args&&... args) { while (first != last) if ((*first++)(std::forward (args)...)) return true; return false; } }; template struct noop { using R = void; template R operator() (T first, T last, Args&&... args) { while (first != last) { (*first++)(std::forward (args)...); } } }; } template class C = combine::noop> class signal { public: using R = typename C::R; using callback = std::function; struct cookie; public: signal (); ~signal (); /// Add a callback to list. cookie connect (callback&&); cookie connect (const callback&); void disconnect (cookie&); /// Disconnect all callbacks void clear (void); /// Returns the number of callbacks connected. unsigned int size (void) const; bool empty (void) const; /// Execute all callbacks template R operator() (Args&&... tail); private: typedef std::list group; group m_children; }; /////////////////////////////////////////////////////////////////////////// // wrap a value in a signal and trigger on assignment //template class C> template class value_signal : public signal { public: value_signal (T); value_signal () = default; operator const T&() const; value_signal& operator= (const T&); private: T m_value; }; } #include "signal.ipp" #endif // __SIGNAL_HPP