[DEV] externalyse the signal implementation
This commit is contained in:
parent
9c69d0c1ea
commit
c1fdecd316
139
esignal/Signal.h
139
esignal/Signal.h
@ -24,18 +24,15 @@ namespace esignal {
|
|||||||
* @brief Basic signal base
|
* @brief Basic signal base
|
||||||
* @param[in] Args... Argument of the signal
|
* @param[in] Args... Argument of the signal
|
||||||
*/
|
*/
|
||||||
template<class... Args>
|
template<class... T_ARGS>
|
||||||
class Signal : public esignal::Base {
|
class Signal : public esignal::Base {
|
||||||
public:
|
public:
|
||||||
using Observer = std::function<void(const Args&...)>; //!< Define an Observer: function pointer
|
using Observer = std::function<void(const T_ARGS&...)>; //!< Define an Observer: function pointer
|
||||||
protected:
|
protected:
|
||||||
int32_t m_callInProgress; //!< know if we are in a recursive loop
|
int32_t m_callInProgress; //!< know if we are in a recursive loop
|
||||||
public:
|
public:
|
||||||
//! @brief Basic constructor
|
//! @brief Basic constructor
|
||||||
Signal():
|
Signal();
|
||||||
m_callInProgress(0) {
|
|
||||||
|
|
||||||
}
|
|
||||||
//! @brief Copy constructor (REMOVED)
|
//! @brief Copy constructor (REMOVED)
|
||||||
Signal(const Signal&) = delete;
|
Signal(const Signal&) = delete;
|
||||||
//! @brief Copy operator (REMOVED)
|
//! @brief Copy operator (REMOVED)
|
||||||
@ -59,12 +56,7 @@ namespace esignal {
|
|||||||
* @brief Basic constructor.
|
* @brief Basic constructor.
|
||||||
* @param[in] _observer Observer to call.
|
* @param[in] _observer Observer to call.
|
||||||
*/
|
*/
|
||||||
Executor(Observer&& _observer):
|
Executor(Observer&& _observer);
|
||||||
m_removed(false),
|
|
||||||
m_uid(0) {
|
|
||||||
m_uid = s_uid++;
|
|
||||||
m_observer = std::move(_observer);
|
|
||||||
}
|
|
||||||
//! @brief virtual destructor.
|
//! @brief virtual destructor.
|
||||||
virtual ~Executor() = default;
|
virtual ~Executor() = default;
|
||||||
public:
|
public:
|
||||||
@ -72,28 +64,16 @@ namespace esignal {
|
|||||||
* @brief Emit the data on the observer.
|
* @brief Emit the data on the observer.
|
||||||
* @param[in] _values... Multiple value needed to send on observers
|
* @param[in] _values... Multiple value needed to send on observers
|
||||||
*/
|
*/
|
||||||
virtual void emit(const Args&... _values) {
|
virtual void emit(const T_ARGS&... _values);
|
||||||
if (m_removed == true) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
m_observer(_values...);
|
|
||||||
} catch(...) {
|
|
||||||
m_removed = true;
|
|
||||||
std::cout << "LOL"<< std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
virtual bool isSharedPtr(const std::shared_ptr<void>& _obj) {
|
virtual bool isSharedPtr(const std::shared_ptr<void>& _obj);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::unique_ptr<Executor>> m_executors; //!< List of all executors.
|
std::vector<std::unique_ptr<Executor>> m_executors; //!< List of all executors.
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* @brief Executor specific to the Shared_ptr caller that does not want to worry about the removing of the signal.
|
* @brief Executor specific to the Shared_ptr caller that does not want to worry about the removing of the signal.
|
||||||
* @param[in] Args... Argument of the signal
|
* @param[in] T_ARGS... Argument of the signal
|
||||||
*/
|
*/
|
||||||
class ExecutorShared : public Executor {
|
class ExecutorShared : public Executor {
|
||||||
protected:
|
protected:
|
||||||
@ -104,44 +84,15 @@ namespace esignal {
|
|||||||
* @param[in] _object A weak reference of the object.
|
* @param[in] _object A weak reference of the object.
|
||||||
* @param[in] _observer Observer to call.
|
* @param[in] _observer Observer to call.
|
||||||
*/
|
*/
|
||||||
ExecutorShared(std::weak_ptr<void> _object, Observer&& _observer) :
|
ExecutorShared(std::weak_ptr<void> _object, Observer&& _observer);
|
||||||
Executor(std::move(_observer)),
|
|
||||||
m_object(_object) {
|
|
||||||
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Emit the data on the observer.
|
* @brief Emit the data on the observer.
|
||||||
* @param[in] _values... Multiple value needed to send on observers
|
* @param[in] _values... Multiple value needed to send on observers
|
||||||
*/
|
*/
|
||||||
virtual void emit(const Args&... _values) {
|
virtual void 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
virtual bool isSharedPtr(const std::shared_ptr<void>& _obj) {
|
virtual bool 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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@ -163,7 +114,7 @@ namespace esignal {
|
|||||||
*/
|
*/
|
||||||
template<class classType, class Func, class... Arg>
|
template<class classType, class Func, class... Arg>
|
||||||
Connection connect(classType* _class, Func _func, Arg... _arg) {
|
Connection connect(classType* _class, Func _func, Arg... _arg) {
|
||||||
std::unique_ptr<Executor> executer(new Executor([=](const Args& ... _argBase){
|
std::unique_ptr<Executor> executer(new Executor([=](const T_ARGS& ... _argBase){
|
||||||
(*_class.*_func)(_argBase..., _arg... );
|
(*_class.*_func)(_argBase..., _arg... );
|
||||||
}));
|
}));
|
||||||
std::size_t uid = executer->m_uid;
|
std::size_t uid = executer->m_uid;
|
||||||
@ -177,14 +128,14 @@ namespace esignal {
|
|||||||
* @param[in] _arg Argument optinnal the user want to add.
|
* @param[in] _arg Argument optinnal the user want to add.
|
||||||
*/
|
*/
|
||||||
template<class classType, class TYPE, typename... Arg>
|
template<class classType, class TYPE, typename... Arg>
|
||||||
void connect(const std::shared_ptr<classType>& _class, void (TYPE::*_func)(const Args&..., Arg...), Arg... _args) {
|
void connect(const std::shared_ptr<classType>& _class, void (TYPE::*_func)(const T_ARGS&..., Arg...), Arg... _args) {
|
||||||
std::shared_ptr<TYPE> obj2 = std::dynamic_pointer_cast<TYPE>(_class);
|
std::shared_ptr<TYPE> obj2 = std::dynamic_pointer_cast<TYPE>(_class);
|
||||||
if (obj2 == nullptr) {
|
if (obj2 == nullptr) {
|
||||||
ESIGNAL_ERROR("Can not bind signal ...");
|
ESIGNAL_ERROR("Can not bind signal ...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
TYPE* directPointer = obj2.get();
|
TYPE* directPointer = obj2.get();
|
||||||
std::unique_ptr<ExecutorShared> executer(new ExecutorShared(_class, [=]( const Args& ... _argBase){
|
std::unique_ptr<ExecutorShared> executer(new ExecutorShared(_class, [=]( const T_ARGS& ... _argBase){
|
||||||
// TODO : Check if compilator does not use the shared ptr ...
|
// TODO : Check if compilator does not use the shared ptr ...
|
||||||
(*directPointer.*_func)(_argBase..., _args... );
|
(*directPointer.*_func)(_argBase..., _args... );
|
||||||
}));
|
}));
|
||||||
@ -195,81 +146,31 @@ namespace esignal {
|
|||||||
* @brief Emit data on the signal.
|
* @brief Emit data on the signal.
|
||||||
* @param[in] _args Argument data to emit.
|
* @param[in] _args Argument data to emit.
|
||||||
*/
|
*/
|
||||||
template< class... CallArgs>
|
void emit(const T_ARGS&... _args);
|
||||||
void emit(CallArgs&&... _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--;
|
|
||||||
}
|
|
||||||
protected:
|
protected:
|
||||||
void removeIfPossible() {
|
void 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Disconnect an observer of the signal.
|
* @brief Disconnect an observer of the signal.
|
||||||
* @param[in] _uid Unique id of the signal.
|
* @param[in] _uid Unique id of the signal.
|
||||||
*/
|
*/
|
||||||
void disconnect(std::size_t _uid) {
|
void disconnect(std::size_t _uid);
|
||||||
for (size_t iii=0; iii < m_executors.size(); ++iii) {
|
void disconnectShared(const std::shared_ptr<void>& _obj);
|
||||||
if (m_executors[iii]->m_uid == _uid) {
|
|
||||||
m_executors[iii]->m_removed = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
removeIfPossible();
|
|
||||||
}
|
|
||||||
void 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();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the number of observers connected on the signal.
|
* @brief Get the number of observers connected on the signal.
|
||||||
* @return The count of observer.
|
* @return The count of observer.
|
||||||
*/
|
*/
|
||||||
size_t size() const {
|
size_t size() const;
|
||||||
return m_executors.size();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if we have a connected observers.
|
* @brief Check if we have a connected observers.
|
||||||
* @return true More than one observers.
|
* @return true More than one observers.
|
||||||
* @return false No observers.
|
* @return false No observers.
|
||||||
*/
|
*/
|
||||||
bool empty() const {
|
bool empty() const;
|
||||||
return m_executors.empty();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Clear all connectd observers.
|
* @brief Clear all connectd observers.
|
||||||
*/
|
*/
|
||||||
void clear() {
|
void clear();
|
||||||
m_executors.clear();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,9 +15,8 @@ size_t esignal::s_uid = 0;
|
|||||||
|
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ "Signal<void>"
|
#define __class__ "Signal<void>"
|
||||||
#if 0
|
|
||||||
// void generic signal
|
// void generic signal
|
||||||
//template class esignal::Signal<void>;
|
template class esignal::Signal<>;
|
||||||
// std generic signal
|
// std generic signal
|
||||||
template class esignal::Signal<bool>;
|
template class esignal::Signal<bool>;
|
||||||
template class esignal::Signal<std::string>;
|
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<unsigned char,3>>;
|
||||||
template class esignal::Signal<etk::Color<float,4>>;
|
template class esignal::Signal<etk::Color<float,4>>;
|
||||||
template class esignal::Signal<etk::Color<float,3>>;
|
template class esignal::Signal<etk::Color<float,3>>;
|
||||||
|
|
||||||
#endif
|
|
||||||
|
@ -12,7 +12,159 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#undef __class__
|
#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
|
#if 0
|
||||||
template<typename... T> esignal::Signal<T...>::Signal() :
|
template<typename... T> esignal::Signal<T...>::Signal() :
|
||||||
m_callInProgress(0),
|
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) {
|
if (m_callInProgress == 0) {
|
||||||
m_callerList.push_back(std::make_pair(std::weak_ptr<void>(_obj), _function));
|
m_callerList.push_back(std::make_pair(std::weak_ptr<void>(_obj), _function));
|
||||||
} else {
|
} else {
|
||||||
|
@ -29,6 +29,7 @@ def create(target, module_name):
|
|||||||
my_module = module.Module(__file__, module_name, get_type())
|
my_module = module.Module(__file__, module_name, get_type())
|
||||||
my_module.add_src_file([
|
my_module.add_src_file([
|
||||||
'test/main.cpp',
|
'test/main.cpp',
|
||||||
|
'test/declareSignals.cpp',
|
||||||
'test/test_signal_class_func.cpp',
|
'test/test_signal_class_func.cpp',
|
||||||
'test/test_signal_recursive.cpp',
|
'test/test_signal_recursive.cpp',
|
||||||
'test/test_signal_shared_ptr_func.cpp',
|
'test/test_signal_shared_ptr_func.cpp',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user