[DEV] test1
This commit is contained in:
parent
95743826f6
commit
7a712a8d8c
352
esignal/Signal.h
352
esignal/Signal.h
@ -11,18 +11,104 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <esignal/debug.h>
|
#include <esignal/debug.h>
|
||||||
#include <esignal/Base.h>
|
#include <esignal/Base.h>
|
||||||
|
#include <functional>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace esignal {
|
namespace esignal {
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ "Signal<T>"
|
#define __class__ "Signal<T>"
|
||||||
template<typename T> class Signal : public esignal::Base {
|
template<class... Args>
|
||||||
|
class Signal {
|
||||||
|
public:
|
||||||
|
using Observer = std::function<void(const Args&...)>;
|
||||||
private:
|
private:
|
||||||
std::vector<std::pair<std::weak_ptr<void>,
|
|
||||||
std::function<void(const T&)>>> m_callerList; // current list of binded element
|
class Executor {
|
||||||
std::vector<std::pair<std::weak_ptr<void>,
|
protected:
|
||||||
std::function<void(const T&)>>> m_callerListInCallback; // temporaty list (when add one in call process)
|
Observer m_observer;
|
||||||
std::vector<std::function<void(const T&)>> m_callerListDirect; // current list of binded element
|
public:
|
||||||
std::vector<std::function<void(const T&)>> m_callerListDirectInCallback; // temporaty list (when add one in call process)
|
Executor(Observer&& _observer) {
|
||||||
|
m_observer = std::move(_observer);
|
||||||
|
}
|
||||||
|
virtual ~Executor() = default;
|
||||||
|
public:
|
||||||
|
virtual bool emit(Args... _values) {
|
||||||
|
try{
|
||||||
|
m_observer( _values... );
|
||||||
|
} catch(...) {
|
||||||
|
std::cout << "LOL"<< std::endl;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ExecutorShared : public Executor {
|
||||||
|
protected:
|
||||||
|
std::week_ptr<void> m_object;
|
||||||
|
public:
|
||||||
|
ExecutorShared(std::weak_ptr<void> _object, Observer&& _observer) :
|
||||||
|
Executor(std::move(_observer)),
|
||||||
|
m_object(_object) {
|
||||||
|
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
virtual bool emit(Args... _values) {
|
||||||
|
try{
|
||||||
|
m_observer( _values... );
|
||||||
|
} catch(...) {
|
||||||
|
std::cout << "LOL"<< std::endl;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
public:
|
||||||
|
|
||||||
|
template< class ObserverType >
|
||||||
|
void connect( ObserverType&& observer ) {
|
||||||
|
m_observers.push_back( std::forward<ObserverType>(observer) );
|
||||||
|
m_executors.push_back(Executor(std::forward<ObserverType>(observer)));
|
||||||
|
}
|
||||||
|
template< class... CallArgs>
|
||||||
|
void emit( CallArgs&&... args ) {
|
||||||
|
for(auto& executor: m_executors) {
|
||||||
|
executor.emit(args... );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<class pointer, class Func, class... Arg>
|
||||||
|
void connect2(pointer* _class, Func _f, Arg... _arg) {
|
||||||
|
m_observers.push_back( [=]( auto&&... cargs ){
|
||||||
|
(*_class.*_f)(cargs..., _arg... );
|
||||||
|
});
|
||||||
|
m_executors.push_back(Executor([=]( auto&&... cargs ){
|
||||||
|
(*_class.*_f)(cargs..., _arg... );
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
template<class pointer, class Func, class... Arg>
|
||||||
|
void connect2(std::shared_ptr<pointer> _class, Func _f, Arg... _arg) {
|
||||||
|
m_executors.push_back(Executor([=]( auto&&... cargs ){
|
||||||
|
(*_class.*_f)(cargs..., _arg... );
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Observer> m_observers;
|
||||||
|
std::vector<Executor> m_executors;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
template<typename... T> class Signal {
|
||||||
|
public:
|
||||||
|
using Observer std::function<void(const T&...)>;
|
||||||
|
private:
|
||||||
|
std::vector<std::pair<std::weak_ptr<void>, Observer>> m_callerList; // current list of binded element
|
||||||
|
std::vector<std::pair<std::weak_ptr<void>, Observer>> m_callerListInCallback; // temporaty list (when add one in call process)
|
||||||
|
std::vector<Observer> m_callerListDirect; // current list of binded element
|
||||||
|
std::vector<Observer> m_callerListDirectInCallback; // temporaty list (when add one in call process)
|
||||||
|
int32_t m_callInProgress;
|
||||||
|
bool m_someOneRemoveInCall;
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Create a signal with a specific type.
|
* @brief Create a signal with a specific type.
|
||||||
@ -31,14 +117,16 @@ namespace esignal {
|
|||||||
* @param[in] _description Description of the signal.
|
* @param[in] _description Description of the signal.
|
||||||
* @param[in] _periodic Customisation of the log display tag at true to down debug lebel at verbose.
|
* @param[in] _periodic Customisation of the log display tag at true to down debug lebel at verbose.
|
||||||
*/
|
*/
|
||||||
Signal(esignal::Interface& _signalInterfaceLink,
|
Signal() :
|
||||||
const std::string& _name,
|
m_callInProgress(0),
|
||||||
const std::string& _description = "",
|
m_someOneRemoveInCall(false) {
|
||||||
bool _periodic = false);
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Destructor.
|
* @brief Destructor.
|
||||||
*/
|
*/
|
||||||
virtual ~Signal();
|
~Signal() = default;
|
||||||
/**
|
/**
|
||||||
* @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
|
||||||
@ -46,16 +134,18 @@ namespace esignal {
|
|||||||
* @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) {
|
||||||
ESIGNAL_ERROR("Can not bind signal ...");
|
ESIGNAL_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)...)));
|
if (COUNT_ELEM == 1) {
|
||||||
|
m_callerList.push_back(std::make_pair(std::weak_ptr<void>(_obj), [](TArgs...){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)...)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -64,89 +154,193 @@ 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 ) {
|
||||||
//! @previous
|
if (m_callInProgress == 0) {
|
||||||
void connect(std::function<void(const T&)> _function );
|
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(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);{
|
||||||
size_t getNumberConnected();
|
#ifdef DEBUG
|
||||||
};
|
m_signalCallLevel++;
|
||||||
#undef __class__
|
int32_t tmpID = m_uidSignal++;
|
||||||
#define __class__ "Signal<void>"
|
#endif
|
||||||
template<> class Signal<void> : public esignal::Base {
|
m_callInProgress++;
|
||||||
private:
|
if (m_periodic == true) {
|
||||||
std::vector<std::pair<std::weak_ptr<void>, std::function<void()>>> m_callerList;
|
ESIGNAL_VERBOSE(esignal::logIndent(m_signalCallLevel-1) << "emit signal{" << tmpID << "} : signal='" << m_name << "' data='" << /*etk::to_string(_data) <<*/ "' to: " << m_callerList.size() << " element(s)");
|
||||||
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.
|
|
||||||
* @param[in] _signalInterfaceLink reference on the signal lister.
|
|
||||||
* @param[in] _name Static name of the signal.
|
|
||||||
* @param[in] _description Description of the signal.
|
|
||||||
* @param[in] _periodic Customisation of the log display tag at true to down debug lebel at verbose.
|
|
||||||
*/
|
|
||||||
Signal(esignal::Interface& _signalInterfaceLink,
|
|
||||||
const std::string& _name,
|
|
||||||
const std::string& _description = "",
|
|
||||||
bool _periodic = false);
|
|
||||||
/**
|
|
||||||
* @brief Destructor.
|
|
||||||
*/
|
|
||||||
virtual ~Signal();
|
|
||||||
/**
|
|
||||||
* @brief Bind a callback function to the current signal (generic methis (simplest))
|
|
||||||
* @param[in] _obj Shared pointer on the caller object
|
|
||||||
* @param[in] _func Link on the fuction that might be called (inside a class)
|
|
||||||
* @example signalXXXX.connect(shared_from_this(), &ClassName::onCallbackXXX);
|
|
||||||
*/
|
|
||||||
template<class TYPE_CLASS, class TYPE, typename... TArgs>
|
|
||||||
void bind(std::shared_ptr<TYPE_CLASS> _obj, void (TYPE::*_func)(TArgs...), TArgs... args2) {
|
|
||||||
std::shared_ptr<TYPE> obj2 = std::dynamic_pointer_cast<TYPE>(_obj);
|
|
||||||
if (obj2 == nullptr) {
|
|
||||||
ESIGNAL_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::forward<TArgs>(args2)...)));
|
|
||||||
} else {
|
} else {
|
||||||
m_callerListInCallback.push_back(std::make_pair(std::weak_ptr<void>(_obj), std::bind(_func, obj2.get(), std::forward<TArgs>(args2)...)));
|
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();{
|
||||||
* @brief Advanced binding a callback function to the current signal.
|
return m_callerList.size() + m_callerListDirect.size();
|
||||||
* @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);
|
|
||||||
//! @previous
|
|
||||||
void connect(std::function<void()> _function);
|
|
||||||
/**
|
|
||||||
* @brief remove link on the signal.
|
|
||||||
* @param[in] _obj shared pointer on the removing object
|
|
||||||
*/
|
|
||||||
void release(std::shared_ptr<void> _obj);
|
|
||||||
void emit();
|
|
||||||
size_t getNumberConnected();
|
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ nullptr
|
#define __class__ nullptr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//#define SDFGSDFGSDFGSDFGSDFGSDFG
|
||||||
|
#ifdef SDFGSDFGSDFGSDFGSDFGSDFG
|
||||||
|
#include <functional>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
template<int I> struct placeholder{};
|
||||||
|
|
||||||
|
namespace std{
|
||||||
|
template<int I>
|
||||||
|
struct is_placeholder< ::placeholder<I>> : std::integral_constant<int, I>{
|
||||||
|
|
||||||
|
};
|
||||||
|
} // std::
|
||||||
|
|
||||||
|
namespace detail{
|
||||||
|
template<std::size_t... Is, class F, class... Args>
|
||||||
|
auto easy_bind(indices<Is...>, F const& f, Args&&... args) -> decltype(std::bind(f, std::forward<Args>(args)..., placeholder<Is + 1>{}...)) {
|
||||||
|
return std::bind(f, std::forward<Args>(args)..., placeholder<Is + 1>{}...);
|
||||||
|
}
|
||||||
|
} // detail::
|
||||||
|
|
||||||
|
template<class R, class... FArgs, class... Args>
|
||||||
|
auto easy_bind(std::function<R(FArgs...)> const& f, Args&&... args) -> decltype(detail::easy_bind(build_indices<sizeof...(FArgs) - sizeof...(Args)>{}, f, std::forward<Args>(args)...)) {
|
||||||
|
return detail::easy_bind(build_indices<sizeof...(FArgs) - sizeof...(Args)>{}, f, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
@ -13,152 +13,9 @@
|
|||||||
|
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ "Signal<void>"
|
#define __class__ "Signal<void>"
|
||||||
|
#if 0
|
||||||
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
|
// void generic signal
|
||||||
template class esignal::Signal<void>;
|
//template class esignal::Signal<void>;
|
||||||
// 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>;
|
||||||
@ -194,3 +51,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,4>>;
|
||||||
template class esignal::Signal<etk::Color<float,3>>;
|
template class esignal::Signal<etk::Color<float,3>>;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@ -8,23 +8,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <esignal/Signal.h>
|
#include <esignal/Signal.h>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ "Signal<T>"
|
#define __class__ "Signal<T>"
|
||||||
|
#if 0
|
||||||
template<typename T> esignal::Signal<T>::Signal(esignal::Interface& _signalInterfaceLink,
|
template<typename... T> esignal::Signal<T...>::Signal() :
|
||||||
const std::string& _name,
|
m_callInProgress(0),
|
||||||
const std::string& _description,
|
m_someOneRemoveInCall(false) {
|
||||||
bool _periodic) :
|
|
||||||
esignal::Base(_signalInterfaceLink, _name, _description, _periodic) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> esignal::Signal<T>::~Signal() {
|
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 ) {
|
template<typename... T> 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 {
|
||||||
@ -32,7 +32,7 @@ template<typename T> void esignal::Signal<T>::connect(std::shared_ptr<void> _obj
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> void esignal::Signal<T>::connect(std::function<void(const T&)> _function ) {
|
template<typename... T> void esignal::Signal<T...>::connect(std::function<void(const T&...)> _function ) {
|
||||||
if (m_callInProgress == 0) {
|
if (m_callInProgress == 0) {
|
||||||
m_callerListDirect.push_back(_function);
|
m_callerListDirect.push_back(_function);
|
||||||
} else {
|
} else {
|
||||||
@ -40,7 +40,7 @@ template<typename T> void esignal::Signal<T>::connect(std::function<void(const T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> bool esignal::Signal<T>::isRegistered(std::shared_ptr<void> _obj) {
|
template<typename... T> bool esignal::Signal<T...>::isRegistered(std::shared_ptr<void> _obj) {
|
||||||
if (_obj == nullptr) {
|
if (_obj == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ template<typename T> bool esignal::Signal<T>::isRegistered(std::shared_ptr<void>
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> void esignal::Signal<T>::release(std::shared_ptr<void> _obj) {
|
template<typename... T> void esignal::Signal<T...>::release(std::shared_ptr<void> _obj) {
|
||||||
if (m_callInProgress == 0) {
|
if (m_callInProgress == 0) {
|
||||||
// Remove from the list :
|
// Remove from the list :
|
||||||
auto it(m_callerList.begin());
|
auto it(m_callerList.begin());
|
||||||
@ -93,16 +93,16 @@ template<typename T> void esignal::Signal<T>::release(std::shared_ptr<void> _obj
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> void esignal::Signal<T>::emit(const T& _data) {
|
template<typename... T> void esignal::Signal<T...>::emit(const T&... _data) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
m_signalCallLevel++;
|
m_signalCallLevel++;
|
||||||
int32_t tmpID = m_uidSignal++;
|
int32_t tmpID = m_uidSignal++;
|
||||||
#endif
|
#endif
|
||||||
m_callInProgress++;
|
m_callInProgress++;
|
||||||
if (m_periodic == true) {
|
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)");
|
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 {
|
} 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)");
|
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());
|
auto it(m_callerList.begin());
|
||||||
@ -118,7 +118,7 @@ template<typename T> void esignal::Signal<T>::emit(const T& _data) {
|
|||||||
} else {
|
} else {
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << " signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
}
|
}
|
||||||
it->second(_data);
|
it->second(_data...);
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,7 +130,7 @@ template<typename T> void esignal::Signal<T>::emit(const T& _data) {
|
|||||||
} else {
|
} else {
|
||||||
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
ESIGNAL_DEBUG(esignal::logIndent(m_signalCallLevel-1) << "X signal{" << tmpID << "} :");// [" << destObject->getId() << "]" << destObject->getObjectType());
|
||||||
}
|
}
|
||||||
(*it)(_data);
|
(*it)(_data...);
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,9 +166,10 @@ template<typename T> void esignal::Signal<T>::emit(const T& _data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> size_t esignal::Signal<T>::getNumberConnected() {
|
template<typename... T> size_t esignal::Signal<T...>::getNumberConnected() {
|
||||||
return m_callerList.size() + m_callerListDirect.size();
|
return m_callerList.size() + m_callerListDirect.size();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ def create(target, module_name):
|
|||||||
'esignal/Signal.h',
|
'esignal/Signal.h',
|
||||||
'esignal/details/Signal.hxx',
|
'esignal/details/Signal.hxx',
|
||||||
])
|
])
|
||||||
|
my_module.compile_version("c++", 2014)
|
||||||
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__))
|
||||||
my_module.compile_flags('c++', [
|
my_module.compile_flags('c++', [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user