[DEV] specify implementation of the Signal (fast build)
This commit is contained in:
parent
b618883aa3
commit
95743826f6
293
esignal/Signal.h
293
esignal/Signal.h
@ -8,6 +8,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <esignal/debug.h>
|
#include <esignal/debug.h>
|
||||||
#include <esignal/Base.h>
|
#include <esignal/Base.h>
|
||||||
|
|
||||||
@ -33,14 +34,11 @@ namespace esignal {
|
|||||||
Signal(esignal::Interface& _signalInterfaceLink,
|
Signal(esignal::Interface& _signalInterfaceLink,
|
||||||
const std::string& _name,
|
const std::string& _name,
|
||||||
const std::string& _description = "",
|
const std::string& _description = "",
|
||||||
bool _periodic = false) :
|
bool _periodic = false);
|
||||||
esignal::Base(_signalInterfaceLink, _name, _description, _periodic) {
|
|
||||||
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Destructor.
|
* @brief Destructor.
|
||||||
*/
|
*/
|
||||||
virtual ~Signal() { };
|
virtual ~Signal();
|
||||||
/**
|
/**
|
||||||
* @brief Bind a callback function to the current signal (generic methis (simplest))
|
* @brief Bind a callback function to the current signal (generic methis (simplest))
|
||||||
* @param[in] _obj Shared pointer on the caller object
|
* @param[in] _obj Shared pointer on the caller object
|
||||||
@ -66,161 +64,27 @@ namespace esignal {
|
|||||||
* @param[in] _func functor to call (do it yourself)
|
* @param[in] _func functor to call (do it yourself)
|
||||||
* @example signalXXXX.connect(shared_from_this(), std::bind(&ClassName::onCallbackXXX, this, std::placeholders::_1));
|
* @example signalXXXX.connect(shared_from_this(), std::bind(&ClassName::onCallbackXXX, this, std::placeholders::_1));
|
||||||
*/
|
*/
|
||||||
void connect(std::shared_ptr<void> _obj, std::function<void(const T&)> _function ) {
|
void connect(std::shared_ptr<void> _obj, std::function<void(const T&)> _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
|
//! @previous
|
||||||
void connect(std::function<void(const T&)> _function ) {
|
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
|
||||||
* @return true The object is connected at this signal.
|
* @return true The object is connected at this signal.
|
||||||
* @return false The object is NOT connected on this signal.
|
* @return false The object is NOT connected on this signal.
|
||||||
*/
|
*/
|
||||||
bool isRegistered(std::shared_ptr<void> _obj) {
|
bool isRegistered(std::shared_ptr<void> _obj);
|
||||||
if (_obj == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (auto &it : m_callerList) {
|
|
||||||
std::shared_ptr<void> obj = it.first.lock();
|
|
||||||
if (obj == _obj) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (auto &it : m_callerListInCallback) {
|
|
||||||
std::shared_ptr<void> obj = it.first.lock();
|
|
||||||
if (obj == _obj) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @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
|
||||||
*/
|
*/
|
||||||
void release(std::shared_ptr<void> _obj) {
|
void release(std::shared_ptr<void> _obj);
|
||||||
if (m_callInProgress == 0) {
|
|
||||||
// Remove from the list :
|
|
||||||
auto it(m_callerList.begin());
|
|
||||||
while(it != m_callerList.end()) {
|
|
||||||
if (it->first.lock() == _obj) {
|
|
||||||
it = m_callerList.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// just remove weak poointer
|
|
||||||
auto it(m_callerList.begin());
|
|
||||||
while(it != m_callerList.end()) {
|
|
||||||
if (it->first.lock() == _obj) {
|
|
||||||
it->first.reset();
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_someOneRemoveInCall = true;
|
|
||||||
}
|
|
||||||
// remove from add list in callback progress
|
|
||||||
auto it = m_callerListInCallback.begin();
|
|
||||||
while(it != m_callerListInCallback.end()) {
|
|
||||||
if (it->first.lock() == _obj) {
|
|
||||||
it = m_callerListInCallback.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Generate a signal on all interface listening.
|
* @brief Generate a signal on all interface listening.
|
||||||
* @param[in] _data data to emit
|
* @param[in] _data data to emit
|
||||||
*/
|
*/
|
||||||
void emit(const T& _data) {
|
void emit(const T& _data);
|
||||||
#ifdef DEBUG
|
size_t getNumberConnected();
|
||||||
m_signalCallLevel++;
|
|
||||||
int32_t tmpID = m_uidSignal++;
|
|
||||||
#endif
|
|
||||||
m_callInProgress++;
|
|
||||||
if (m_periodic == true) {
|
|
||||||
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' data='" << etk::to_string(_data) << "' to: " << m_callerList.size() << " element(s)");
|
|
||||||
} else {
|
|
||||||
ESIGNAL_DEBUG(esignal::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);
|
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " nullptr dest");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (m_periodic == true) {
|
|
||||||
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
} else {
|
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
}
|
|
||||||
it->second(_data);
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
auto it(m_callerListDirect.begin());
|
|
||||||
while (it != m_callerListDirect.end()) {
|
|
||||||
if (m_periodic == true) {
|
|
||||||
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
} else {
|
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
}
|
|
||||||
(*it)(_data);
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_callInProgress--;
|
|
||||||
#ifdef DEBUG
|
|
||||||
m_signalCallLevel--;
|
|
||||||
#endif
|
|
||||||
// Remove element in call phase:
|
|
||||||
if (m_someOneRemoveInCall == true) {
|
|
||||||
m_someOneRemoveInCall = false;
|
|
||||||
// Remove from the list :
|
|
||||||
auto it(m_callerList.begin());
|
|
||||||
while(it != m_callerList.end()) {
|
|
||||||
if (it->first.expired() == true) {
|
|
||||||
it = m_callerList.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// add element in call phase:
|
|
||||||
if (m_callerListInCallback.size() > 0) {
|
|
||||||
for (auto &it : m_callerListInCallback) {
|
|
||||||
m_callerList.push_back(it);
|
|
||||||
}
|
|
||||||
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() + m_callerListDirect.size();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ "Signal<void>"
|
#define __class__ "Signal<void>"
|
||||||
@ -241,15 +105,11 @@ namespace esignal {
|
|||||||
Signal(esignal::Interface& _signalInterfaceLink,
|
Signal(esignal::Interface& _signalInterfaceLink,
|
||||||
const std::string& _name,
|
const std::string& _name,
|
||||||
const std::string& _description = "",
|
const std::string& _description = "",
|
||||||
bool _periodic = false) :
|
bool _periodic = false);
|
||||||
esignal::Base(_signalInterfaceLink, _name, _description, _periodic) {
|
|
||||||
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* @brief Destructor.
|
* @brief Destructor.
|
||||||
*/
|
*/
|
||||||
virtual ~Signal() { };
|
virtual ~Signal();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Bind a callback function to the current signal (generic methis (simplest))
|
* @brief Bind a callback function to the current signal (generic methis (simplest))
|
||||||
* @param[in] _obj Shared pointer on the caller object
|
* @param[in] _obj Shared pointer on the caller object
|
||||||
@ -275,137 +135,16 @@ namespace esignal {
|
|||||||
* @param[in] _func functor to call (do it yourself)
|
* @param[in] _func functor to call (do it yourself)
|
||||||
* @example signalXXXX.connect(shared_from_this(), std::bind(&ClassName::onCallbackXXX, this, std::placeholders::_1));
|
* @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 ) {
|
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
|
//! @previous
|
||||||
void connect(std::function<void()> _function ) {
|
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
|
||||||
*/
|
*/
|
||||||
void release(std::shared_ptr<void> _obj) {
|
void release(std::shared_ptr<void> _obj);
|
||||||
auto it(m_callerList.begin());
|
void emit();
|
||||||
if (m_callInProgress == 0) {
|
size_t getNumberConnected();
|
||||||
// Remove from the list :
|
|
||||||
while(it != m_callerList.end()) {
|
|
||||||
if (it->first.lock() == _obj) {
|
|
||||||
//ESIGNAL_DEBUG(" unbind : " << _obj->getObjectType() << " signal='" << m_name << "'");
|
|
||||||
it = m_callerList.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// just remove weak poointer
|
|
||||||
while(it != m_callerList.end()) {
|
|
||||||
if (it->first.lock() == _obj) {
|
|
||||||
//ESIGNAL_DEBUG(" unbind : " << _obj->getObjectType() << " signal='" << m_name << "' (delayed)");
|
|
||||||
it->first.reset();
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_someOneRemoveInCall = true;
|
|
||||||
}
|
|
||||||
// remove from add list in callback progress
|
|
||||||
it = m_callerListInCallback.begin();
|
|
||||||
while(it != m_callerListInCallback.end()) {
|
|
||||||
if (it->first.lock() == _obj) {
|
|
||||||
//ESIGNAL_DEBUG(" unbind : " << _obj->getObjectType() << " signal='" << m_name << "' (notActive)");
|
|
||||||
it = m_callerListInCallback.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void emit() {
|
|
||||||
#ifdef DEBUG
|
|
||||||
m_signalCallLevel++;
|
|
||||||
int32_t tmpID = m_uidSignal++;
|
|
||||||
#endif
|
|
||||||
m_callInProgress++;
|
|
||||||
if (m_periodic == true) {
|
|
||||||
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' to: " << m_callerList.size() << " element(s)");
|
|
||||||
} else {
|
|
||||||
ESIGNAL_DEBUG(esignal::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);
|
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " nullptr dest");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (m_periodic == true) {
|
|
||||||
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
} else {
|
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
}
|
|
||||||
it->second();
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
auto it(m_callerListDirect.begin());
|
|
||||||
while (it != m_callerListDirect.end()) {
|
|
||||||
if (m_periodic == true) {
|
|
||||||
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
} else {
|
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
|
||||||
}
|
|
||||||
(*it)();
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_callInProgress--;
|
|
||||||
#ifdef DEBUG
|
|
||||||
m_signalCallLevel--;
|
|
||||||
#endif
|
|
||||||
// Remove element in call phase:
|
|
||||||
if (m_someOneRemoveInCall == true) {
|
|
||||||
m_someOneRemoveInCall = false;
|
|
||||||
// Remove from the list :
|
|
||||||
auto it(m_callerList.begin());
|
|
||||||
while(it != m_callerList.end()) {
|
|
||||||
if (it->first.expired() == true) {
|
|
||||||
it = m_callerList.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// add element in call phase:
|
|
||||||
if (m_callerListInCallback.size() > 0) {
|
|
||||||
for (auto &it : m_callerListInCallback) {
|
|
||||||
if (it.first.expired() == false) {
|
|
||||||
m_callerList.push_back(it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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() + m_callerListDirect.size();;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ nullptr
|
#define __class__ nullptr
|
||||||
|
196
esignal/details/Signal.cpp
Normal file
196
esignal/details/Signal.cpp
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#include <esignal/details/Signal.hxx>
|
||||||
|
#include <etk/types.h>
|
||||||
|
#include <etk/math/Vector2D.h>
|
||||||
|
#include <etk/math/Vector3D.h>
|
||||||
|
#include <etk/Color.h>
|
||||||
|
|
||||||
|
#undef __class__
|
||||||
|
#define __class__ "Signal<void>"
|
||||||
|
|
||||||
|
esignal::Signal<void>::Signal(esignal::Interface& _signalInterfaceLink,
|
||||||
|
const std::string& _name,
|
||||||
|
const std::string& _description,
|
||||||
|
bool _periodic) :
|
||||||
|
esignal::Base(_signalInterfaceLink, _name, _description, _periodic) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
esignal::Signal<void>::~Signal() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void esignal::Signal<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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void esignal::Signal<void>::connect(std::function<void()> _function ) {
|
||||||
|
if (m_callInProgress == 0) {
|
||||||
|
m_callerListDirect.push_back(_function);
|
||||||
|
} else {
|
||||||
|
m_callerListDirectInCallback.push_back(_function);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void esignal::Signal<void>::release(std::shared_ptr<void> _obj) {
|
||||||
|
auto it(m_callerList.begin());
|
||||||
|
if (m_callInProgress == 0) {
|
||||||
|
// Remove from the list :
|
||||||
|
while(it != m_callerList.end()) {
|
||||||
|
if (it->first.lock() == _obj) {
|
||||||
|
//ESIGNAL_DEBUG(" unbind : " << _obj->getObjectType() << " signal='" << m_name << "'");
|
||||||
|
it = m_callerList.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// just remove weak poointer
|
||||||
|
while(it != m_callerList.end()) {
|
||||||
|
if (it->first.lock() == _obj) {
|
||||||
|
//ESIGNAL_DEBUG(" unbind : " << _obj->getObjectType() << " signal='" << m_name << "' (delayed)");
|
||||||
|
it->first.reset();
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_someOneRemoveInCall = true;
|
||||||
|
}
|
||||||
|
// remove from add list in callback progress
|
||||||
|
it = m_callerListInCallback.begin();
|
||||||
|
while(it != m_callerListInCallback.end()) {
|
||||||
|
if (it->first.lock() == _obj) {
|
||||||
|
//ESIGNAL_DEBUG(" unbind : " << _obj->getObjectType() << " signal='" << m_name << "' (notActive)");
|
||||||
|
it = m_callerListInCallback.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void esignal::Signal<void>::emit() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
m_signalCallLevel++;
|
||||||
|
int32_t tmpID = m_uidSignal++;
|
||||||
|
#endif
|
||||||
|
m_callInProgress++;
|
||||||
|
if (m_periodic == true) {
|
||||||
|
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' to: " << m_callerList.size() << " element(s)");
|
||||||
|
} else {
|
||||||
|
ESIGNAL_DEBUG(esignal::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);
|
||||||
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " nullptr dest");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (m_periodic == true) {
|
||||||
|
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
} else {
|
||||||
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
}
|
||||||
|
it->second();
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto it(m_callerListDirect.begin());
|
||||||
|
while (it != m_callerListDirect.end()) {
|
||||||
|
if (m_periodic == true) {
|
||||||
|
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
} else {
|
||||||
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
}
|
||||||
|
(*it)();
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_callInProgress--;
|
||||||
|
#ifdef DEBUG
|
||||||
|
m_signalCallLevel--;
|
||||||
|
#endif
|
||||||
|
// Remove element in call phase:
|
||||||
|
if (m_someOneRemoveInCall == true) {
|
||||||
|
m_someOneRemoveInCall = false;
|
||||||
|
// Remove from the list :
|
||||||
|
auto it(m_callerList.begin());
|
||||||
|
while(it != m_callerList.end()) {
|
||||||
|
if (it->first.expired() == true) {
|
||||||
|
it = m_callerList.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// add element in call phase:
|
||||||
|
if (m_callerListInCallback.size() > 0) {
|
||||||
|
for (auto &it : m_callerListInCallback) {
|
||||||
|
if (it.first.expired() == false) {
|
||||||
|
m_callerList.push_back(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_callerListInCallback.clear();
|
||||||
|
}
|
||||||
|
if (m_callerListDirectInCallback.size() > 0) {
|
||||||
|
for (auto &it : m_callerListDirectInCallback) {
|
||||||
|
m_callerListDirect.push_back(it);
|
||||||
|
}
|
||||||
|
m_callerListDirectInCallback.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t esignal::Signal<void>::getNumberConnected() {
|
||||||
|
return m_callerList.size() + m_callerListDirect.size();;
|
||||||
|
}
|
||||||
|
|
||||||
|
// void generic signal
|
||||||
|
template class esignal::Signal<void>;
|
||||||
|
// std generic signal
|
||||||
|
template class esignal::Signal<bool>;
|
||||||
|
template class esignal::Signal<std::string>;
|
||||||
|
#if __CPP_VERSION__ >= 2011
|
||||||
|
template class esignal::Signal<std::u32string>;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template class esignal::Signal<int64_t>;
|
||||||
|
template class esignal::Signal<int32_t>;
|
||||||
|
template class esignal::Signal<int16_t>;
|
||||||
|
template class esignal::Signal<int8_t>;
|
||||||
|
|
||||||
|
template class esignal::Signal<uint64_t>;
|
||||||
|
template class esignal::Signal<uint32_t>;
|
||||||
|
template class esignal::Signal<uint16_t>;
|
||||||
|
template class esignal::Signal<uint8_t>;
|
||||||
|
|
||||||
|
template class esignal::Signal<float>;
|
||||||
|
template class esignal::Signal<double>;
|
||||||
|
// etk generic vetor 2D
|
||||||
|
template class esignal::Signal<vec2>;
|
||||||
|
template class esignal::Signal<bvec2>;
|
||||||
|
template class esignal::Signal<ivec2>;
|
||||||
|
template class esignal::Signal<uivec2>;
|
||||||
|
// etk generic vetor 3D
|
||||||
|
template class esignal::Signal<vec3>;
|
||||||
|
template class esignal::Signal<bvec3>;
|
||||||
|
template class esignal::Signal<ivec3>;
|
||||||
|
template class esignal::Signal<uivec3>;
|
||||||
|
// etk generic color
|
||||||
|
template class esignal::Signal<etk::Color<unsigned char,4>>;
|
||||||
|
template class esignal::Signal<etk::Color<unsigned char,3>>;
|
||||||
|
template class esignal::Signal<etk::Color<float,4>>;
|
||||||
|
template class esignal::Signal<etk::Color<float,3>>;
|
||||||
|
|
176
esignal/details/Signal.hxx
Normal file
176
esignal/details/Signal.hxx
Normal file
@ -0,0 +1,176 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <esignal/Signal.h>
|
||||||
|
|
||||||
|
#undef __class__
|
||||||
|
#define __class__ "Signal<T>"
|
||||||
|
|
||||||
|
template<typename T> esignal::Signal<T>::Signal(esignal::Interface& _signalInterfaceLink,
|
||||||
|
const std::string& _name,
|
||||||
|
const std::string& _description,
|
||||||
|
bool _periodic) :
|
||||||
|
esignal::Base(_signalInterfaceLink, _name, _description, _periodic) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> esignal::Signal<T>::~Signal() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> void esignal::Signal<T>::connect(std::shared_ptr<void> _obj, std::function<void(const T&)> _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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> void esignal::Signal<T>::connect(std::function<void(const T&)> _function ) {
|
||||||
|
if (m_callInProgress == 0) {
|
||||||
|
m_callerListDirect.push_back(_function);
|
||||||
|
} else {
|
||||||
|
m_callerListDirectInCallback.push_back(_function);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> bool esignal::Signal<T>::isRegistered(std::shared_ptr<void> _obj) {
|
||||||
|
if (_obj == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (auto &it : m_callerList) {
|
||||||
|
std::shared_ptr<void> obj = it.first.lock();
|
||||||
|
if (obj == _obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto &it : m_callerListInCallback) {
|
||||||
|
std::shared_ptr<void> obj = it.first.lock();
|
||||||
|
if (obj == _obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> void esignal::Signal<T>::release(std::shared_ptr<void> _obj) {
|
||||||
|
if (m_callInProgress == 0) {
|
||||||
|
// Remove from the list :
|
||||||
|
auto it(m_callerList.begin());
|
||||||
|
while(it != m_callerList.end()) {
|
||||||
|
if (it->first.lock() == _obj) {
|
||||||
|
it = m_callerList.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// just remove weak poointer
|
||||||
|
auto it(m_callerList.begin());
|
||||||
|
while(it != m_callerList.end()) {
|
||||||
|
if (it->first.lock() == _obj) {
|
||||||
|
it->first.reset();
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_someOneRemoveInCall = true;
|
||||||
|
}
|
||||||
|
// remove from add list in callback progress
|
||||||
|
auto it = m_callerListInCallback.begin();
|
||||||
|
while(it != m_callerListInCallback.end()) {
|
||||||
|
if (it->first.lock() == _obj) {
|
||||||
|
it = m_callerListInCallback.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> void esignal::Signal<T>::emit(const T& _data) {
|
||||||
|
#ifdef DEBUG
|
||||||
|
m_signalCallLevel++;
|
||||||
|
int32_t tmpID = m_uidSignal++;
|
||||||
|
#endif
|
||||||
|
m_callInProgress++;
|
||||||
|
if (m_periodic == true) {
|
||||||
|
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' data='" << etk::to_string(_data) << "' to: " << m_callerList.size() << " element(s)");
|
||||||
|
} else {
|
||||||
|
ESIGNAL_DEBUG(esignal::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);
|
||||||
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " nullptr dest");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (m_periodic == true) {
|
||||||
|
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
} else {
|
||||||
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
}
|
||||||
|
it->second(_data);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto it(m_callerListDirect.begin());
|
||||||
|
while (it != m_callerListDirect.end()) {
|
||||||
|
if (m_periodic == true) {
|
||||||
|
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
} else {
|
||||||
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
|
}
|
||||||
|
(*it)(_data);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_callInProgress--;
|
||||||
|
#ifdef DEBUG
|
||||||
|
m_signalCallLevel--;
|
||||||
|
#endif
|
||||||
|
// Remove element in call phase:
|
||||||
|
if (m_someOneRemoveInCall == true) {
|
||||||
|
m_someOneRemoveInCall = false;
|
||||||
|
// Remove from the list :
|
||||||
|
auto it(m_callerList.begin());
|
||||||
|
while(it != m_callerList.end()) {
|
||||||
|
if (it->first.expired() == true) {
|
||||||
|
it = m_callerList.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// add element in call phase:
|
||||||
|
if (m_callerListInCallback.size() > 0) {
|
||||||
|
for (auto &it : m_callerListInCallback) {
|
||||||
|
m_callerList.push_back(it);
|
||||||
|
}
|
||||||
|
m_callerListInCallback.clear();
|
||||||
|
}
|
||||||
|
if (m_callerListDirectInCallback.size() > 0) {
|
||||||
|
for (auto &it : m_callerListDirectInCallback) {
|
||||||
|
m_callerListDirect.push_back(it);
|
||||||
|
}
|
||||||
|
m_callerListDirectInCallback.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> size_t esignal::Signal<T>::getNumberConnected() {
|
||||||
|
return m_callerList.size() + m_callerListDirect.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -33,13 +33,15 @@ def create(target, module_name):
|
|||||||
my_module.add_src_file([
|
my_module.add_src_file([
|
||||||
'esignal/debug.cpp',
|
'esignal/debug.cpp',
|
||||||
'esignal/Interface.cpp',
|
'esignal/Interface.cpp',
|
||||||
'esignal/Base.cpp'
|
'esignal/Base.cpp',
|
||||||
|
'esignal/details/Signal.cpp',
|
||||||
])
|
])
|
||||||
my_module.add_header_file([
|
my_module.add_header_file([
|
||||||
'esignal/debug.h',
|
'esignal/debug.h',
|
||||||
'esignal/Interface.h',
|
'esignal/Interface.h',
|
||||||
'esignal/Base.h',
|
'esignal/Base.h',
|
||||||
'esignal/Signal.h'
|
'esignal/Signal.h',
|
||||||
|
'esignal/details/Signal.hxx',
|
||||||
])
|
])
|
||||||
my_module.add_module_depend(['etk'])
|
my_module.add_module_depend(['etk'])
|
||||||
my_module.add_path(tools.get_current_path(__file__))
|
my_module.add_path(tools.get_current_path(__file__))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user