[DEV] add signal capability of direct function callback

This commit is contained in:
Edouard DUPIN 2016-01-28 22:47:03 +01:00
parent ab22c78237
commit e91da861a6

View File

@ -21,6 +21,8 @@ namespace ewol {
std::function<void(const T&)>>> m_callerList; // current list of binded element
std::vector<std::pair<std::weak_ptr<void>,
std::function<void(const T&)>>> m_callerListInCallback; // temporaty list (when add one in call process)
std::vector<std::function<void(const T&)>> m_callerListDirect; // current list of binded element
std::vector<std::function<void(const T&)>> m_callerListDirectInCallback; // temporaty list (when add one in call process)
public:
/**
* @brief Create a signal with a specific type.
@ -47,16 +49,16 @@ namespace ewol {
* @example signalXXXX.bind(shared_from_this(), &ClassName::onCallbackXXX);
*/
template<class TYPE_CLASS, class TYPE, typename... TArgs>
void bind(std::shared_ptr<TYPE_CLASS> _obj, void (TYPE::*_func)(const T&, TArgs...), TArgs... args2) {
void bind(std::shared_ptr<TYPE_CLASS> _obj, void (TYPE::*_func)(const T&, TArgs...), TArgs... _args2) {
std::shared_ptr<TYPE> obj2 = std::dynamic_pointer_cast<TYPE>(_obj);
if (obj2 == nullptr) {
EWOL_ERROR("Can not bind signal ...");
return;
}
if (m_callInProgress == 0) {
m_callerList.push_back(std::make_pair(std::weak_ptr<void>(_obj), std::bind(_func, obj2.get(), std::placeholders::_1, std::forward<TArgs>(args2)...)));
m_callerList.push_back(std::make_pair(std::weak_ptr<void>(_obj), std::bind(_func, obj2.get(), std::placeholders::_1, std::forward<TArgs>(_args2)...)));
} else {
m_callerListInCallback.push_back(std::make_pair(std::weak_ptr<void>(_obj), std::bind(_func, obj2.get(), std::placeholders::_1, std::forward<TArgs>(args2)...)));
m_callerListInCallback.push_back(std::make_pair(std::weak_ptr<void>(_obj), std::bind(_func, obj2.get(), std::placeholders::_1, std::forward<TArgs>(_args2)...)));
}
}
/**
@ -72,6 +74,14 @@ namespace ewol {
m_callerListInCallback.push_back(std::make_pair(std::weak_ptr<void>(_obj), _function));
}
}
//! @previous
void connect(std::function<void(const T&)> _function ) {
if (m_callInProgress == 0) {
m_callerListDirect.push_back(_function);
} else {
m_callerListDirectInCallback.push_back(_function);
}
}
/**
* @brief Check if an object is registered in the Signal
* @param[in] _obj shared pointer on the object
@ -148,21 +158,35 @@ namespace ewol {
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' data='" << etk::to_string(_data) << "' to: " << m_callerList.size() << " element(s)");
}
auto it(m_callerList.begin());
while (it != m_callerList.end()) {
std::shared_ptr<void> destObject = it->first.lock();
if (destObject == nullptr) {
it = m_callerList.erase(it);
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " nullptr dest");
continue;
{
auto it(m_callerList.begin());
while (it != m_callerList.end()) {
std::shared_ptr<void> destObject = it->first.lock();
if (destObject == nullptr) {
it = m_callerList.erase(it);
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " nullptr dest");
continue;
}
if (m_periodic == true) {
EWOL_VERBOSE(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
}
it->second(_data);
++it;
}
if (m_periodic == true) {
EWOL_VERBOSE(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
}
{
auto it(m_callerListDirect.begin());
while (it != m_callerListDirect.end()) {
if (m_periodic == true) {
EWOL_VERBOSE(ewol::signal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
}
(*it)(_data);
++it;
}
it->second(_data);
++it;
}
m_callInProgress--;
#ifdef DEBUG
@ -188,9 +212,15 @@ namespace ewol {
}
m_callerListInCallback.clear();
}
if (m_callerListDirectInCallback.size() > 0) {
for (auto &it : m_callerListDirectInCallback) {
m_callerListDirect.push_back(it);
}
m_callerListDirectInCallback.clear();
}
}
size_t getNumberConnected() {
return m_callerList.size();
return m_callerList.size() + m_callerListDirect.size();
}
};
#undef __class__
@ -199,6 +229,8 @@ namespace ewol {
private:
std::vector<std::pair<std::weak_ptr<void>, std::function<void()>>> m_callerList;
std::vector<std::pair<std::weak_ptr<void>, std::function<void()>>> m_callerListInCallback;
std::vector<std::function<void()>> m_callerListDirect;
std::vector<std::function<void()>> m_callerListDirectInCallback;
public:
/**
* @brief Create a signal with a specific 'void' type.
@ -238,6 +270,27 @@ namespace ewol {
m_callerListInCallback.push_back(std::make_pair(std::weak_ptr<void>(_obj), std::bind(_func, obj2.get(), std::forward<TArgs>(args2)...)));
}
}
/**
* @brief Advanced binding a callback function to the current signal.
* @param[in] _obj Shared pointer on the caller object
* @param[in] _func functor to call (do it yourself)
* @example signalXXXX.connect(shared_from_this(), std::bind(&ClassName::onCallbackXXX, this, std::placeholders::_1));
*/
void connect(std::shared_ptr<void> _obj, std::function<void()> _function ) {
if (m_callInProgress == 0) {
m_callerList.push_back(std::make_pair(std::weak_ptr<void>(_obj), _function));
} else {
m_callerListInCallback.push_back(std::make_pair(std::weak_ptr<void>(_obj), _function));
}
}
//! @previous
void connect(std::function<void()> _function ) {
if (m_callInProgress == 0) {
m_callerListDirect.push_back(_function);
} else {
m_callerListDirectInCallback.push_back(_function);
}
}
/**
* @brief remove link on the signal.
* @param[in] _obj shared pointer on the removing object
@ -288,21 +341,35 @@ namespace ewol {
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' to: " << m_callerList.size() << " element(s)");
}
auto it(m_callerList.begin());
while (it != m_callerList.end()) {
std::shared_ptr<void> destObject = it->first.lock();
if (destObject == nullptr) {
it = m_callerList.erase(it);
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " nullptr dest");
continue;
{
auto it(m_callerList.begin());
while (it != m_callerList.end()) {
std::shared_ptr<void> destObject = it->first.lock();
if (destObject == nullptr) {
it = m_callerList.erase(it);
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " nullptr dest");
continue;
}
if (m_periodic == true) {
EWOL_VERBOSE(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
}
it->second();
++it;
}
if (m_periodic == true) {
EWOL_VERBOSE(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
}
{
auto it(m_callerListDirect.begin());
while (it != m_callerListDirect.end()) {
if (m_periodic == true) {
EWOL_VERBOSE(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
} else {
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
}
(*it)();
++it;
}
it->second();
++it;
}
m_callInProgress--;
#ifdef DEBUG
@ -330,9 +397,15 @@ namespace ewol {
}
m_callerListInCallback.clear();
}
if (m_callerListDirectInCallback.size() > 0) {
for (auto &it : m_callerListDirectInCallback) {
m_callerListDirect.push_back(it);
}
m_callerListDirectInCallback.clear();
}
}
size_t getNumberConnected() {
return m_callerList.size();
return m_callerList.size() + m_callerListDirect.size();;
}
};
#undef __class__