[DEV] externalyse the signal implementation
This commit is contained in:
@@ -15,9 +15,8 @@ size_t esignal::s_uid = 0;
|
||||
|
||||
#undef __class__
|
||||
#define __class__ "Signal<void>"
|
||||
#if 0
|
||||
// void generic signal
|
||||
//template class esignal::Signal<void>;
|
||||
template class esignal::Signal<>;
|
||||
// std generic signal
|
||||
template class esignal::Signal<bool>;
|
||||
template class esignal::Signal<std::string>;
|
||||
@@ -52,5 +51,3 @@ 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>>;
|
||||
|
||||
#endif
|
||||
|
@@ -12,7 +12,159 @@
|
||||
#include <memory>
|
||||
|
||||
#undef __class__
|
||||
#define __class__ "Signal<T>"
|
||||
#define __class__ "Signal<T_ARGS>"
|
||||
|
||||
|
||||
template<typename... T_ARGS>
|
||||
esignal::Signal<T_ARGS...>::Signal():
|
||||
m_callInProgress(0) {
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
void esignal::Signal<T_ARGS...>::emit(const T_ARGS&... _args) {
|
||||
// TODO : Add protection ... but how ...
|
||||
m_callInProgress++;
|
||||
for (size_t iii=0; iii < m_executors.size(); ++iii) {
|
||||
m_executors[iii]->emit(_args...);
|
||||
}
|
||||
if (m_callInProgress == 1) {
|
||||
auto it = m_executors.begin();
|
||||
while (it != m_executors.end()) {
|
||||
if ( *it == nullptr
|
||||
|| (*it)->m_removed == true) {
|
||||
it = m_executors.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
m_callInProgress--;
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
void esignal::Signal<T_ARGS...>::removeIfPossible() {
|
||||
auto it = m_executors.begin();
|
||||
while (it != m_executors.end()) {
|
||||
if ( *it == nullptr
|
||||
|| (*it)->m_removed == true) {
|
||||
it = m_executors.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
void esignal::Signal<T_ARGS...>::disconnect(std::size_t _uid) {
|
||||
for (size_t iii=0; iii < m_executors.size(); ++iii) {
|
||||
if (m_executors[iii]->m_uid == _uid) {
|
||||
m_executors[iii]->m_removed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
removeIfPossible();
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
void esignal::Signal<T_ARGS...>::disconnectShared(const std::shared_ptr<void>& _obj) {
|
||||
for (size_t iii=0; iii < m_executors.size(); ++iii) {
|
||||
if (m_executors[iii]->isSharedPtr(_obj) == true) {
|
||||
m_executors[iii]->m_removed = true;
|
||||
}
|
||||
}
|
||||
removeIfPossible();
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
size_t esignal::Signal<T_ARGS...>::size() const {
|
||||
return m_executors.size();
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
bool esignal::Signal<T_ARGS...>::empty() const {
|
||||
return m_executors.empty();
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
void esignal::Signal<T_ARGS...>::clear() {
|
||||
m_executors.clear();
|
||||
}
|
||||
|
||||
|
||||
template<typename... T_ARGS>
|
||||
esignal::Signal<T_ARGS...>::Executor::Executor(Observer&& _observer):
|
||||
m_removed(false),
|
||||
m_uid(0) {
|
||||
m_uid = s_uid++;
|
||||
m_observer = std::move(_observer);
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
void esignal::Signal<T_ARGS...>::Executor::emit(const T_ARGS&... _values) {
|
||||
if (m_removed == true) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
m_observer(_values...);
|
||||
} catch(...) {
|
||||
m_removed = true;
|
||||
std::cout << "LOL"<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
bool esignal::Signal<T_ARGS...>::Executor::isSharedPtr(const std::shared_ptr<void>& _obj) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename... T_ARGS>
|
||||
esignal::Signal<T_ARGS...>::ExecutorShared::ExecutorShared(std::weak_ptr<void> _object, Observer&& _observer) :
|
||||
Executor(std::move(_observer)),
|
||||
m_object(_object) {
|
||||
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
void esignal::Signal<T_ARGS...>::ExecutorShared::emit(const T_ARGS&... _values) {
|
||||
// TODO: maybe an error if the object is not manage by the same thread.
|
||||
std::shared_ptr<void> destObject = m_object.lock();
|
||||
if (destObject == nullptr) {
|
||||
Executor::m_removed = true;
|
||||
return;
|
||||
}
|
||||
if (Executor::m_removed == true) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Executor::m_observer(_values...);
|
||||
} catch(...) {
|
||||
Executor::m_removed = true;
|
||||
std::cout << "LOL"<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... T_ARGS>
|
||||
bool esignal::Signal<T_ARGS...>::ExecutorShared::isSharedPtr(const std::shared_ptr<void>& _obj) {
|
||||
std::shared_ptr<void> destObject = m_object.lock();
|
||||
if (destObject == nullptr) {
|
||||
return true;
|
||||
}
|
||||
if (destObject == _obj) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
template<typename... T> esignal::Signal<T...>::Signal() :
|
||||
m_callInProgress(0),
|
||||
@@ -24,7 +176,7 @@ 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 ) {
|
||||
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 {
|
||||
|
Reference in New Issue
Block a user