[DEV] add signal capability of direct function callback
This commit is contained in:
parent
ab22c78237
commit
e91da861a6
@ -21,6 +21,8 @@ namespace ewol {
|
|||||||
std::function<void(const T&)>>> m_callerList; // current list of binded element
|
std::function<void(const T&)>>> m_callerList; // current list of binded element
|
||||||
std::vector<std::pair<std::weak_ptr<void>,
|
std::vector<std::pair<std::weak_ptr<void>,
|
||||||
std::function<void(const T&)>>> m_callerListInCallback; // temporaty list (when add one in call process)
|
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:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Create a signal with a specific type.
|
* @brief Create a signal with a specific type.
|
||||||
@ -47,16 +49,16 @@ namespace ewol {
|
|||||||
* @example signalXXXX.bind(shared_from_this(), &ClassName::onCallbackXXX);
|
* @example signalXXXX.bind(shared_from_this(), &ClassName::onCallbackXXX);
|
||||||
*/
|
*/
|
||||||
template<class TYPE_CLASS, class TYPE, typename... TArgs>
|
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);
|
std::shared_ptr<TYPE> obj2 = std::dynamic_pointer_cast<TYPE>(_obj);
|
||||||
if (obj2 == nullptr) {
|
if (obj2 == nullptr) {
|
||||||
EWOL_ERROR("Can not bind signal ...");
|
EWOL_ERROR("Can not bind signal ...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (m_callInProgress == 0) {
|
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 {
|
} 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));
|
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
|
* @brief Check if an object is registered in the Signal
|
||||||
* @param[in] _obj shared pointer on the object
|
* @param[in] _obj shared pointer on the object
|
||||||
@ -148,6 +158,7 @@ namespace ewol {
|
|||||||
} else {
|
} 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)");
|
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());
|
auto it(m_callerList.begin());
|
||||||
while (it != m_callerList.end()) {
|
while (it != m_callerList.end()) {
|
||||||
std::shared_ptr<void> destObject = it->first.lock();
|
std::shared_ptr<void> destObject = it->first.lock();
|
||||||
@ -164,6 +175,19 @@ namespace ewol {
|
|||||||
it->second(_data);
|
it->second(_data);
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
m_callInProgress--;
|
m_callInProgress--;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
m_signalCallLevel--;
|
m_signalCallLevel--;
|
||||||
@ -188,9 +212,15 @@ namespace ewol {
|
|||||||
}
|
}
|
||||||
m_callerListInCallback.clear();
|
m_callerListInCallback.clear();
|
||||||
}
|
}
|
||||||
|
if (m_callerListDirectInCallback.size() > 0) {
|
||||||
|
for (auto &it : m_callerListDirectInCallback) {
|
||||||
|
m_callerListDirect.push_back(it);
|
||||||
|
}
|
||||||
|
m_callerListDirectInCallback.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
size_t getNumberConnected() {
|
size_t getNumberConnected() {
|
||||||
return m_callerList.size();
|
return m_callerList.size() + m_callerListDirect.size();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#undef __class__
|
#undef __class__
|
||||||
@ -199,6 +229,8 @@ namespace ewol {
|
|||||||
private:
|
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_callerList;
|
||||||
std::vector<std::pair<std::weak_ptr<void>, std::function<void()>>> m_callerListInCallback;
|
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:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Create a signal with a specific 'void' type.
|
* @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)...)));
|
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.
|
* @brief remove link on the signal.
|
||||||
* @param[in] _obj shared pointer on the removing object
|
* @param[in] _obj shared pointer on the removing object
|
||||||
@ -288,6 +341,7 @@ namespace ewol {
|
|||||||
} else {
|
} else {
|
||||||
EWOL_DEBUG(ewol::signal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' to: " << m_callerList.size() << " element(s)");
|
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());
|
auto it(m_callerList.begin());
|
||||||
while (it != m_callerList.end()) {
|
while (it != m_callerList.end()) {
|
||||||
std::shared_ptr<void> destObject = it->first.lock();
|
std::shared_ptr<void> destObject = it->first.lock();
|
||||||
@ -304,6 +358,19 @@ namespace ewol {
|
|||||||
it->second();
|
it->second();
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
m_callInProgress--;
|
m_callInProgress--;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
m_signalCallLevel--;
|
m_signalCallLevel--;
|
||||||
@ -330,9 +397,15 @@ namespace ewol {
|
|||||||
}
|
}
|
||||||
m_callerListInCallback.clear();
|
m_callerListInCallback.clear();
|
||||||
}
|
}
|
||||||
|
if (m_callerListDirectInCallback.size() > 0) {
|
||||||
|
for (auto &it : m_callerListDirectInCallback) {
|
||||||
|
m_callerListDirect.push_back(it);
|
||||||
|
}
|
||||||
|
m_callerListDirectInCallback.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
size_t getNumberConnected() {
|
size_t getNumberConnected() {
|
||||||
return m_callerList.size();
|
return m_callerList.size() + m_callerListDirect.size();;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#undef __class__
|
#undef __class__
|
||||||
|
Loading…
x
Reference in New Issue
Block a user