Move signal definitions to ipp
This commit is contained in:
parent
de0d05df97
commit
f40771eb87
@ -118,6 +118,7 @@ UTIL_FILES = \
|
|||||||
si.cpp \
|
si.cpp \
|
||||||
signal.cpp \
|
signal.cpp \
|
||||||
signal.hpp \
|
signal.hpp \
|
||||||
|
signal.ipp \
|
||||||
si.hpp \
|
si.hpp \
|
||||||
stats.cpp \
|
stats.cpp \
|
||||||
stats.hpp \
|
stats.hpp \
|
||||||
|
85
signal.hpp
85
signal.hpp
@ -14,11 +14,11 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2011-2013 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __SIGNAL_HPP
|
#ifndef __UTIL_SIGNAL_HPP
|
||||||
#define __SIGNAL_HPP
|
#define __UTIL_SIGNAL_HPP
|
||||||
|
|
||||||
#include "debug.hpp"
|
#include "debug.hpp"
|
||||||
#include "nocopy.hpp"
|
#include "nocopy.hpp"
|
||||||
@ -44,83 +44,34 @@ namespace util {
|
|||||||
cookie m_cookie;
|
cookie m_cookie;
|
||||||
signal<Ret, Args...>& m_parent;
|
signal<Ret, Args...>& m_parent;
|
||||||
|
|
||||||
scoped_cookie (cookie _cookie,
|
scoped_cookie (cookie _cookie, signal<Ret, Args...> &_parent);
|
||||||
signal<Ret, Args...> &_parent):
|
scoped_cookie (scoped_cookie &&rhs);
|
||||||
m_cookie (_cookie),
|
~scoped_cookie ();
|
||||||
m_parent (_parent)
|
|
||||||
{ ; }
|
|
||||||
|
|
||||||
scoped_cookie (scoped_cookie &&rhs):
|
void renew (callback_object &&cb);
|
||||||
m_cookie (rhs.m_cookie),
|
void release (void);
|
||||||
m_parent (rhs.m_parent)
|
|
||||||
{
|
|
||||||
rhs.m_cookie = rhs.m_parent.m_children.end ();
|
|
||||||
}
|
|
||||||
|
|
||||||
~scoped_cookie () {
|
|
||||||
if (m_parent.m_children.end () != m_cookie)
|
|
||||||
m_parent.disconnect (m_cookie);
|
|
||||||
}
|
|
||||||
|
|
||||||
void renew (callback_object &&cb)
|
|
||||||
{ *m_cookie = std::move (cb); }
|
|
||||||
|
|
||||||
void release (void)
|
|
||||||
{ m_cookie = m_parent.m_children.end (); }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
signal ()
|
signal ();
|
||||||
{ /*m_children.reserve (16);*/ }
|
|
||||||
|
|
||||||
|
|
||||||
/// Add a callback to list.
|
/// Add a callback to list.
|
||||||
const cookie
|
cookie connect (const callback_object &_cb);
|
||||||
connect (const callback_object &_cb)
|
scoped_cookie scoped_connect (const callback_object &_cb);
|
||||||
{ return m_children.insert (m_children.end (), _cb); }
|
|
||||||
|
|
||||||
|
|
||||||
/// Add a callback to the list.
|
|
||||||
//const cookie
|
|
||||||
//connect (const callback_function &_cb)
|
|
||||||
// { return m_children.insert (m_children.end (), _cb); }
|
|
||||||
|
|
||||||
|
|
||||||
void disconnect (const cookie _cb)
|
|
||||||
{ m_children.erase (_cb); }
|
|
||||||
|
|
||||||
|
|
||||||
|
void disconnect (const cookie _cb);
|
||||||
/// Disconnect all callbacks
|
/// Disconnect all callbacks
|
||||||
void clear (void)
|
void clear (void);
|
||||||
{ m_children.clear (); }
|
|
||||||
|
|
||||||
|
|
||||||
/// Returns the number of callbacks connected.
|
/// Returns the number of callbacks connected.
|
||||||
unsigned int size (void) const
|
unsigned int size (void) const;
|
||||||
{ return m_children.size (); }
|
bool empty (void) const;
|
||||||
|
|
||||||
|
|
||||||
bool empty (void) const
|
|
||||||
{ return m_children.empty (); }
|
|
||||||
|
|
||||||
|
|
||||||
/// Execute all callbacks, ignoring the return parameters. Does not combine results.
|
/// Execute all callbacks, ignoring the return parameters. Does not combine results.
|
||||||
void operator () (Args... tail) {
|
void operator () (Args... tail);
|
||||||
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.
|
|
||||||
auto current = i++;
|
|
||||||
looping = m_children.cend () != i;
|
|
||||||
|
|
||||||
(*current)(tail...);
|
|
||||||
} while (looping);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "signal.ipp"
|
||||||
|
|
||||||
#endif // __SIGNAL_HPP
|
#endif // __SIGNAL_HPP
|
||||||
|
128
signal.ipp
Normal file
128
signal.ipp
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
/*
|
||||||
|
* 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-2013 Danny Robson <danny@nerdcruft.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __UTIL_SIGNAL_HPP
|
||||||
|
#error
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
signal<Ret, Args...>::scoped_cookie::scoped_cookie (cookie _cookie,
|
||||||
|
signal<Ret, Args...> &_parent):
|
||||||
|
m_cookie (_cookie),
|
||||||
|
m_parent (_parent)
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
signal<Ret, Args...>::scoped_cookie::scoped_cookie (scoped_cookie &&rhs):
|
||||||
|
m_cookie (rhs.m_cookie),
|
||||||
|
m_parent (rhs.m_parent)
|
||||||
|
{
|
||||||
|
rhs.m_cookie = rhs.m_parent.m_children.end ();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
signal<Ret, Args...>::scoped_cookie::~scoped_cookie () {
|
||||||
|
if (m_parent.m_children.end () != m_cookie)
|
||||||
|
m_parent.disconnect (m_cookie);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
void
|
||||||
|
signal<Ret, Args...>::scoped_cookie::renew (callback_object &&cb)
|
||||||
|
{ *m_cookie = std::move (cb); }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
void
|
||||||
|
signal<Ret, Args...>::scoped_cookie::release (void)
|
||||||
|
{ m_cookie = m_parent.m_children.end (); }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
signal<Ret, Args...>::signal ()
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
typename signal<Ret, Args...>::cookie
|
||||||
|
signal<Ret, Args...>::connect (const callback_object &_cb)
|
||||||
|
{ return m_children.insert (m_children.end (), _cb); }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
typename signal<Ret, Args...>::scoped_cookie
|
||||||
|
signal<Ret, Args...>::scoped_connect (const callback_object &_cb)
|
||||||
|
{ return scoped_cookie (connect (_cb), *this); }
|
||||||
|
|
||||||
|
|
||||||
|
/// Add a callback to the list.
|
||||||
|
//const cookie
|
||||||
|
//connect (const callback_function &_cb)
|
||||||
|
// { return m_children.insert (m_children.end (), _cb); }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
void
|
||||||
|
signal<Ret, Args...>::disconnect (const cookie _cb)
|
||||||
|
{ m_children.erase (_cb); }
|
||||||
|
|
||||||
|
|
||||||
|
/// Disconnect all callbacks
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
void
|
||||||
|
signal<Ret, Args...>::clear (void)
|
||||||
|
{ m_children.clear (); }
|
||||||
|
|
||||||
|
|
||||||
|
/// Returns the number of callbacks connected.
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
unsigned int
|
||||||
|
signal<Ret, Args...>::size (void) const
|
||||||
|
{ return m_children.size (); }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
bool
|
||||||
|
signal<Ret, Args...>::empty (void) const
|
||||||
|
{ return m_children.empty (); }
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Ret, typename ...Args>
|
||||||
|
void
|
||||||
|
signal<Ret, Args...>::operator () (Args... tail) {
|
||||||
|
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.
|
||||||
|
auto current = i++;
|
||||||
|
looping = m_children.cend () != i;
|
||||||
|
|
||||||
|
(*current)(tail...);
|
||||||
|
} while (looping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user