[DEV] separate template implementation
This commit is contained in:
parent
c1fdecd316
commit
1f346fd313
@ -20,7 +20,7 @@
|
|||||||
namespace esignal {
|
namespace esignal {
|
||||||
class Base {
|
class Base {
|
||||||
protected:
|
protected:
|
||||||
LockSharedPtrRef<Base> m_shared; //!< Reference counter on itself.
|
esignal::LockSharedPtrRef<esignal::Base> m_shared; //!< Reference counter on itself.
|
||||||
static size_t s_uid; //!< blobal id of the signal (STATIC)
|
static size_t s_uid; //!< blobal id of the signal (STATIC)
|
||||||
public:
|
public:
|
||||||
//! @brief Basic constructor:
|
//! @brief Basic constructor:
|
||||||
|
48
esignal/Connection.cpp
Normal file
48
esignal/Connection.cpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#include <esignal/debug.h>
|
||||||
|
#include <esignal/Connection.h>
|
||||||
|
|
||||||
|
esignal::Connection::Connection():
|
||||||
|
m_signalRefUnique(), m_uid(0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
esignal::Connection::Connection(const esignal::LockSharedPtrRef<esignal::Base>& _ref, std::size_t _id):
|
||||||
|
m_signalRefUnique(_ref),
|
||||||
|
m_uid(_id) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
esignal::Connection::Connection(esignal::Connection&& _obj):
|
||||||
|
m_signalRefUnique(_obj.m_signalRefUnique),
|
||||||
|
m_uid(_obj.m_uid) {
|
||||||
|
_obj.m_uid = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
esignal::Connection& esignal::Connection::operator=(esignal::Connection&& _obj) {
|
||||||
|
disconnect();
|
||||||
|
m_signalRefUnique = _obj.m_signalRefUnique;
|
||||||
|
m_uid = _obj.m_uid;
|
||||||
|
_obj.m_uid = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
esignal::Connection::~Connection() {
|
||||||
|
m_signalRefUnique.disconnect(m_uid);
|
||||||
|
m_uid = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void esignal::Connection::disconnect() {
|
||||||
|
m_signalRefUnique.disconnect(m_uid);
|
||||||
|
m_uid = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool esignal::Connection::isConnected() {
|
||||||
|
return m_signalRefUnique.isAlive();
|
||||||
|
}
|
||||||
|
|
@ -23,54 +23,29 @@ namespace esignal {
|
|||||||
class Connection {
|
class Connection {
|
||||||
public:
|
public:
|
||||||
//! @brief Constructor (no link)
|
//! @brief Constructor (no link)
|
||||||
Connection():
|
Connection();
|
||||||
m_signalRefUnique(), m_uid(0) {
|
|
||||||
|
|
||||||
}
|
|
||||||
//! @brief Constructor (link)
|
//! @brief Constructor (link)
|
||||||
Connection(const LockSharedPtrRef<Base>& _ref, std::size_t _id):
|
Connection(const esignal::LockSharedPtrRef<esignal::Base>& _ref, std::size_t _id);
|
||||||
m_signalRefUnique(_ref),
|
|
||||||
m_uid(_id) {
|
|
||||||
|
|
||||||
}
|
|
||||||
//! @brief Move Constructor
|
//! @brief Move Constructor
|
||||||
Connection(Connection&& _obj):
|
Connection(Connection&& _obj);
|
||||||
m_signalRefUnique(_obj.m_signalRefUnique),
|
|
||||||
m_uid(_obj.m_uid) {
|
|
||||||
_obj.m_uid = 0;
|
|
||||||
}
|
|
||||||
//! @brief Move operator.
|
//! @brief Move operator.
|
||||||
Connection& operator=(Connection&& _obj) {
|
Connection& operator=(Connection&& _obj);
|
||||||
disconnect();
|
|
||||||
m_signalRefUnique = _obj.m_signalRefUnique;
|
|
||||||
m_uid = _obj.m_uid;
|
|
||||||
_obj.m_uid = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
//! @brief Copy constructor (REMOVED)
|
//! @brief Copy constructor (REMOVED)
|
||||||
Connection(const Connection&) = delete;
|
Connection(const Connection&) = delete;
|
||||||
//! @brief Copy operator (REMOVED)
|
//! @brief Copy operator (REMOVED)
|
||||||
Connection& operator=(const Connection&) = delete;
|
Connection& operator=(const Connection&) = delete;
|
||||||
//! @brief Destructor.
|
//! @brief Destructor.
|
||||||
~Connection() {
|
~Connection();
|
||||||
m_signalRefUnique.disconnect(m_uid);
|
|
||||||
m_uid = 0;
|
|
||||||
}
|
|
||||||
//! @brief Disconnect the signal.
|
//! @brief Disconnect the signal.
|
||||||
void disconnect() {
|
void disconnect();
|
||||||
m_signalRefUnique.disconnect(m_uid);
|
|
||||||
m_uid = 0;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if the connection is alive or signal removed
|
* @brief Check if the connection is alive or signal removed
|
||||||
* @return true The signal is connected.
|
* @return true The signal is connected.
|
||||||
* @return false The signal is NOT connected.
|
* @return false The signal is NOT connected.
|
||||||
*/
|
*/
|
||||||
bool isConnected() {
|
bool isConnected();
|
||||||
return m_signalRefUnique.isAlive();
|
|
||||||
}
|
|
||||||
private:
|
private:
|
||||||
LockSharedPtrRef<Base> m_signalRefUnique; //!< reference on the Signal.
|
esignal::LockSharedPtrRef<esignal::Base> m_signalRefUnique; //!< reference on the Signal.
|
||||||
std::size_t m_uid; //!< UID of the current connection.
|
std::size_t m_uid; //!< UID of the current connection.
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -33,25 +33,13 @@ namespace esignal {
|
|||||||
*/
|
*/
|
||||||
ISignal(esignal::Interface& _signalInterfaceLink,
|
ISignal(esignal::Interface& _signalInterfaceLink,
|
||||||
const std::string& _name,
|
const std::string& _name,
|
||||||
const std::string& _description = ""):
|
const std::string& _description = "");
|
||||||
m_signalInterfaceLink(_signalInterfaceLink),
|
|
||||||
m_name(_name),
|
|
||||||
m_description(_description) {
|
|
||||||
// add a reference on the current signal ...
|
|
||||||
m_signalInterfaceLink.signalAdd(this);
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Destructor.
|
* @brief Destructor.
|
||||||
*/
|
*/
|
||||||
virtual ~ISignal() {
|
virtual ~ISignal();
|
||||||
m_signalInterfaceLink.signalRemove(this);
|
virtual const std::string& getName() const;
|
||||||
}
|
virtual const std::string& getDescription() const;
|
||||||
virtual const std::string& getName() const {
|
|
||||||
return m_name;
|
|
||||||
}
|
|
||||||
virtual const std::string& getDescription() const {
|
|
||||||
return m_description;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,18 +7,9 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
|
||||||
#include <esignal/debug.h>
|
|
||||||
#include <esignal/Base.h>
|
|
||||||
#include <esignal/RefCount.h>
|
#include <esignal/RefCount.h>
|
||||||
#include <functional>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <utility>
|
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
namespace esignal {
|
namespace esignal {
|
||||||
extern size_t s_uid;
|
|
||||||
/**
|
/**
|
||||||
* @brief shared ptr that permeit to lock access of the internal data (it does not manage the allication and remove of the data).
|
* @brief shared ptr that permeit to lock access of the internal data (it does not manage the allication and remove of the data).
|
||||||
* @todo Change this with atomic_shared_ptr<> when availlable.
|
* @todo Change this with atomic_shared_ptr<> when availlable.
|
||||||
@ -27,58 +18,29 @@ namespace esignal {
|
|||||||
template<class TYPE>
|
template<class TYPE>
|
||||||
class LockSharedPtrRef {
|
class LockSharedPtrRef {
|
||||||
public:
|
public:
|
||||||
RefCount<TYPE>* m_counter; //!< Access on the reference counter
|
esignal::RefCount<TYPE>* m_counter; //!< Access on the reference counter
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Basic contructor (with the object to ref count)
|
* @brief Basic contructor (with the object to ref count)
|
||||||
* @param[in] _pointer Pointer on the data (default nullptr)
|
* @param[in] _pointer Pointer on the data (default nullptr)
|
||||||
*/
|
*/
|
||||||
LockSharedPtrRef(TYPE* _pointer=nullptr) :
|
LockSharedPtrRef(TYPE* _pointer=nullptr);
|
||||||
m_counter(nullptr) {
|
|
||||||
if (_pointer != nullptr) {
|
|
||||||
m_counter = new RefCount<TYPE>(_pointer);
|
|
||||||
m_counter->inc();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Copy contructor
|
* @brief Copy contructor
|
||||||
* @param[in] _obj object to copy
|
* @param[in] _obj object to copy
|
||||||
*/
|
*/
|
||||||
LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj) :
|
LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj);
|
||||||
m_counter(_obj.m_counter) {
|
|
||||||
if (m_counter == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_counter->inc();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Copy operator (It copy the counter and increment the it).
|
* @brief Copy operator (It copy the counter and increment the it).
|
||||||
* @param[in] _obj objetc to copy.
|
* @param[in] _obj objetc to copy.
|
||||||
* @return Reference of this
|
* @return Reference of this
|
||||||
*/
|
*/
|
||||||
LockSharedPtrRef& operator=(const LockSharedPtrRef<TYPE>& _obj) {
|
LockSharedPtrRef& operator=(const LockSharedPtrRef<TYPE>& _obj);
|
||||||
if (&_obj == this) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
if (m_counter != nullptr) {
|
|
||||||
m_counter->dec();
|
|
||||||
m_counter = nullptr;
|
|
||||||
}
|
|
||||||
m_counter = _obj.m_counter;
|
|
||||||
if (m_counter == nullptr) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
m_counter->inc();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Contructor (move)
|
* @brief Contructor (move)
|
||||||
* @param[in] _obj move object
|
* @param[in] _obj move object
|
||||||
*/
|
*/
|
||||||
LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj) :
|
LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj);
|
||||||
m_counter(std::move(_obj.m_counter)) {
|
|
||||||
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Copy operator (force move) ==> removed
|
* @brief Copy operator (force move) ==> removed
|
||||||
*/
|
*/
|
||||||
@ -86,48 +48,22 @@ namespace esignal {
|
|||||||
/**
|
/**
|
||||||
* @brief Destructor of the class (decrement the counter and remove it if it is the last one...)
|
* @brief Destructor of the class (decrement the counter and remove it if it is the last one...)
|
||||||
*/
|
*/
|
||||||
~LockSharedPtrRef() {
|
~LockSharedPtrRef();
|
||||||
if (m_counter == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int64_t count = m_counter->dec();
|
|
||||||
if (count > 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
delete m_counter;
|
|
||||||
m_counter = nullptr;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Remove the data on the conter reference (it does not exist anymore)
|
* @brief Remove the data on the conter reference (it does not exist anymore)
|
||||||
*/
|
*/
|
||||||
void removeData() {
|
void removeData();
|
||||||
if (m_counter != nullptr) {
|
|
||||||
m_counter->remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Call disconnect on the parameter class with lock prevention
|
* @brief Call disconnect on the parameter class with lock prevention
|
||||||
* @param[in] _uid ID to dicsonnect on the sub element
|
* @param[in] _uid ID to dicsonnect on the sub element
|
||||||
*/
|
*/
|
||||||
void disconnect(std::size_t _uid) {
|
void disconnect(std::size_t _uid);
|
||||||
if (m_counter == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_counter->lock();
|
|
||||||
TYPE* val = m_counter->get();
|
|
||||||
if (val != nullptr) {
|
|
||||||
val->disconnect(_uid);
|
|
||||||
}
|
|
||||||
m_counter->unlock();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if the value is availlable
|
* @brief Check if the value is availlable
|
||||||
* @return true The data is availlable
|
* @return true The data is availlable
|
||||||
* @return false The data has been removed
|
* @return false The data has been removed
|
||||||
*/
|
*/
|
||||||
bool isAlive() {
|
bool isAlive();
|
||||||
return m_counter != nullptr;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,10 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
|
||||||
#include <esignal/debug.h>
|
|
||||||
#include <esignal/Base.h>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
@ -26,11 +22,7 @@ namespace esignal {
|
|||||||
TYPE* m_data; //!< Pointer on the refconting data
|
TYPE* m_data; //!< Pointer on the refconting data
|
||||||
public:
|
public:
|
||||||
//!< generic constructor
|
//!< generic constructor
|
||||||
RefCount(TYPE* _data) :
|
RefCount(TYPE* _data);
|
||||||
m_count(0),
|
|
||||||
m_data(_data) {
|
|
||||||
// nothing to do.
|
|
||||||
}
|
|
||||||
//! @brief Copy constructor (REMOVED)
|
//! @brief Copy constructor (REMOVED)
|
||||||
RefCount(const RefCount&) = delete;
|
RefCount(const RefCount&) = delete;
|
||||||
//! @brief Copy operator (REMOVED)
|
//! @brief Copy operator (REMOVED)
|
||||||
@ -42,46 +34,21 @@ namespace esignal {
|
|||||||
//! @brief Move operator (REMOVED)
|
//! @brief Move operator (REMOVED)
|
||||||
RefCount& operator=(RefCount&& _obj) = delete;
|
RefCount& operator=(RefCount&& _obj) = delete;
|
||||||
//! @brief Destructor
|
//! @brief Destructor
|
||||||
~RefCount() {
|
~RefCount();
|
||||||
m_data = nullptr;
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
//!< @brief Lock the interface
|
//!< @brief Lock the interface
|
||||||
void lock() {
|
void lock();
|
||||||
m_lock.lock();
|
|
||||||
}
|
|
||||||
//!< @brief Unlock the interface
|
//!< @brief Unlock the interface
|
||||||
void unlock() {
|
void unlock();
|
||||||
m_lock.unlock();
|
|
||||||
}
|
|
||||||
//!< @brief Increment the ref-counting
|
//!< @brief Increment the ref-counting
|
||||||
void inc() {
|
void inc();
|
||||||
lock();
|
|
||||||
m_count++;
|
|
||||||
unlock();
|
|
||||||
}
|
|
||||||
//!< @brief Decrement the ref-counting
|
//!< @brief Decrement the ref-counting
|
||||||
int64_t dec() {
|
int64_t dec();
|
||||||
int64_t val;
|
|
||||||
lock();
|
|
||||||
m_count--;
|
|
||||||
val = m_count;
|
|
||||||
unlock();
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
//!< @brief Get number of connected
|
//!< @brief Get number of connected
|
||||||
int64_t getCount() const {
|
int64_t getCount() const;
|
||||||
return m_count;
|
|
||||||
}
|
|
||||||
//!< @brief Remove the data
|
//!< @brief Remove the data
|
||||||
void remove() {
|
void remove();
|
||||||
lock();
|
|
||||||
m_data = nullptr;
|
|
||||||
unlock();
|
|
||||||
}
|
|
||||||
//!< @brief Get the recoreded data
|
//!< @brief Get the recoreded data
|
||||||
TYPE* get() {
|
TYPE* get();
|
||||||
return m_data;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,12 @@ namespace esignal {
|
|||||||
*/
|
*/
|
||||||
virtual void emit(const T_ARGS&... _values);
|
virtual void emit(const T_ARGS&... _values);
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief check if the Executor is a managed by this shared_ptr
|
||||||
|
* @param[in] _obj Object to check
|
||||||
|
* @return true The Executor depend on this shared_ptr
|
||||||
|
* @return false The Executor does not depend on this shared_ptr
|
||||||
|
*/
|
||||||
virtual bool isSharedPtr(const std::shared_ptr<void>& _obj);
|
virtual bool isSharedPtr(const std::shared_ptr<void>& _obj);
|
||||||
};
|
};
|
||||||
protected:
|
protected:
|
||||||
@ -99,48 +105,28 @@ namespace esignal {
|
|||||||
* @brief Connect an observer on the signal.
|
* @brief Connect an observer on the signal.
|
||||||
* @param[in] _observer Observer to call.
|
* @param[in] _observer Observer to call.
|
||||||
*/
|
*/
|
||||||
template< class ObserverType >
|
template< class OBSERVER_TYPE >
|
||||||
Connection connect(ObserverType&& _observer ) {
|
esignal::Connection connect(OBSERVER_TYPE&& _observer);
|
||||||
std::unique_ptr<Executor> executer(new Executor(std::forward<ObserverType>(_observer)));
|
|
||||||
std::size_t uid = executer->m_uid;
|
|
||||||
m_executors.push_back(std::move(executer));
|
|
||||||
return Connection(Base::m_shared, uid);
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Connect an function member on the signal.
|
* @brief Connect an function member on the signal.
|
||||||
* @param[in] _class Object on whe we need to call.
|
* @param[in] _class Object on whe we need to call.
|
||||||
* @param[in] _func Function to call.
|
* @param[in] _func Function to call.
|
||||||
* @param[in] _arg Argument optinnal the user want to add.
|
* @param[in] _arg Argument optinnal the user want to add.
|
||||||
*/
|
*/
|
||||||
template<class classType, class Func, class... Arg>
|
template<class CLASS_TYPE, class FUNC_TYPE, class... FUNC_ARGS_TYPE>
|
||||||
Connection connect(classType* _class, Func _func, Arg... _arg) {
|
esignal::Connection connect(CLASS_TYPE* _class,
|
||||||
std::unique_ptr<Executor> executer(new Executor([=](const T_ARGS& ... _argBase){
|
FUNC_TYPE _func,
|
||||||
(*_class.*_func)(_argBase..., _arg... );
|
FUNC_ARGS_TYPE... _arg);
|
||||||
}));
|
|
||||||
std::size_t uid = executer->m_uid;
|
|
||||||
m_executors.push_back(std::move(executer));
|
|
||||||
return Connection(Base::m_shared, uid);
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* @brief Connect an function member on the signal with the shared_ptr object.
|
* @brief Connect an function member on the signal with the shared_ptr object.
|
||||||
* @param[in] _class shared_ptr Object on whe we need to call ==> the object is get in keeped in weak_ptr.
|
* @param[in] _class shared_ptr Object on whe we need to call ==> the object is get in keeped in weak_ptr.
|
||||||
* @param[in] _func Function to call.
|
* @param[in] _func Function to call.
|
||||||
* @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 PARENT_CLASS_TYPE, class CLASS_TYPE, typename... FUNC_ARGS_TYPE>
|
||||||
void connect(const std::shared_ptr<classType>& _class, void (TYPE::*_func)(const T_ARGS&..., Arg...), Arg... _args) {
|
void connect(const std::shared_ptr<PARENT_CLASS_TYPE>& _class,
|
||||||
std::shared_ptr<TYPE> obj2 = std::dynamic_pointer_cast<TYPE>(_class);
|
void (CLASS_TYPE::*_func)(const T_ARGS&..., FUNC_ARGS_TYPE...),
|
||||||
if (obj2 == nullptr) {
|
FUNC_ARGS_TYPE... _args);
|
||||||
ESIGNAL_ERROR("Can not bind signal ...");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
TYPE* directPointer = obj2.get();
|
|
||||||
std::unique_ptr<ExecutorShared> executer(new ExecutorShared(_class, [=]( const T_ARGS& ... _argBase){
|
|
||||||
// TODO : Check if compilator does not use the shared ptr ...
|
|
||||||
(*directPointer.*_func)(_argBase..., _args... );
|
|
||||||
}));
|
|
||||||
m_executors.push_back(std::move(executer));
|
|
||||||
}
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Emit data on the signal.
|
* @brief Emit data on the signal.
|
||||||
@ -175,3 +161,42 @@ namespace esignal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class... T_ARGS>
|
||||||
|
template< class OBSERVER_TYPE >
|
||||||
|
esignal::Connection esignal::Signal<T_ARGS...>::connect(OBSERVER_TYPE&& _observer ) {
|
||||||
|
std::unique_ptr<Executor> executer(new Executor(std::forward<OBSERVER_TYPE>(_observer)));
|
||||||
|
std::size_t uid = executer->m_uid;
|
||||||
|
m_executors.push_back(std::move(executer));
|
||||||
|
return Connection(Base::m_shared, uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class... T_ARGS>
|
||||||
|
template<class CLASS_TYPE, class FUNC_TYPE, class... FUNC_ARGS_TYPE>
|
||||||
|
esignal::Connection esignal::Signal<T_ARGS...>::connect(CLASS_TYPE* _class,
|
||||||
|
FUNC_TYPE _func,
|
||||||
|
FUNC_ARGS_TYPE... _arg) {
|
||||||
|
std::unique_ptr<Executor> executer(new Executor([=](const T_ARGS& ... _argBase){
|
||||||
|
(*_class.*_func)(_argBase..., _arg... );
|
||||||
|
}));
|
||||||
|
std::size_t uid = executer->m_uid;
|
||||||
|
m_executors.push_back(std::move(executer));
|
||||||
|
return Connection(Base::m_shared, uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class... T_ARGS>
|
||||||
|
template<class PARENT_CLASS_TYPE, class CLASS_TYPE, typename... FUNC_ARGS_TYPE>
|
||||||
|
void esignal::Signal<T_ARGS...>::connect(const std::shared_ptr<PARENT_CLASS_TYPE>& _class,
|
||||||
|
void (CLASS_TYPE::*_func)(const T_ARGS&..., FUNC_ARGS_TYPE...),
|
||||||
|
FUNC_ARGS_TYPE... _args) {
|
||||||
|
std::shared_ptr<CLASS_TYPE> obj2 = std::dynamic_pointer_cast<CLASS_TYPE>(_class);
|
||||||
|
if (obj2 == nullptr) {
|
||||||
|
ESIGNAL_ERROR("Can not bind signal ...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CLASS_TYPE* directPointer = obj2.get();
|
||||||
|
std::unique_ptr<ExecutorShared> executer(new ExecutorShared(_class, [=]( const T_ARGS& ... _argBase){
|
||||||
|
// TODO : Check if compilator does not use the shared ptr ...
|
||||||
|
(*directPointer.*_func)(_argBase..., _args... );
|
||||||
|
}));
|
||||||
|
m_executors.push_back(std::move(executer));
|
||||||
|
}
|
51
esignal/details/ISignal.cpp
Normal file
51
esignal/details/ISignal.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#include <esignal/details/ISignal.hxx>
|
||||||
|
#include <etk/types.h>
|
||||||
|
#include <etk/math/Vector2D.h>
|
||||||
|
#include <etk/math/Vector3D.h>
|
||||||
|
#include <etk/Color.h>
|
||||||
|
|
||||||
|
#undef __class__
|
||||||
|
#define __class__ "ISignal<void>"
|
||||||
|
// void generic signal
|
||||||
|
template class esignal::ISignal<>;
|
||||||
|
// std generic signal
|
||||||
|
template class esignal::ISignal<bool>;
|
||||||
|
template class esignal::ISignal<std::string>;
|
||||||
|
#if __CPP_VERSION__ >= 2011
|
||||||
|
template class esignal::ISignal<std::u32string>;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template class esignal::ISignal<int64_t>;
|
||||||
|
template class esignal::ISignal<int32_t>;
|
||||||
|
template class esignal::ISignal<int16_t>;
|
||||||
|
template class esignal::ISignal<int8_t>;
|
||||||
|
|
||||||
|
template class esignal::ISignal<uint64_t>;
|
||||||
|
template class esignal::ISignal<uint32_t>;
|
||||||
|
template class esignal::ISignal<uint16_t>;
|
||||||
|
template class esignal::ISignal<uint8_t>;
|
||||||
|
|
||||||
|
template class esignal::ISignal<float>;
|
||||||
|
template class esignal::ISignal<double>;
|
||||||
|
// etk generic vetor 2D
|
||||||
|
template class esignal::ISignal<vec2>;
|
||||||
|
template class esignal::ISignal<bvec2>;
|
||||||
|
template class esignal::ISignal<ivec2>;
|
||||||
|
template class esignal::ISignal<uivec2>;
|
||||||
|
// etk generic vetor 3D
|
||||||
|
template class esignal::ISignal<vec3>;
|
||||||
|
template class esignal::ISignal<bvec3>;
|
||||||
|
template class esignal::ISignal<ivec3>;
|
||||||
|
template class esignal::ISignal<uivec3>;
|
||||||
|
// etk generic color
|
||||||
|
template class esignal::ISignal<etk::Color<unsigned char,4>>;
|
||||||
|
template class esignal::ISignal<etk::Color<unsigned char,3>>;
|
||||||
|
template class esignal::ISignal<etk::Color<float,4>>;
|
||||||
|
template class esignal::ISignal<etk::Color<float,3>>;
|
39
esignal/details/ISignal.hxx
Normal file
39
esignal/details/ISignal.hxx
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <esignal/ISignal.h>
|
||||||
|
#include <esignal/details/Signal.hxx>
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
esignal::ISignal<Args...>::ISignal(esignal::Interface& _signalInterfaceLink,
|
||||||
|
const std::string& _name,
|
||||||
|
const std::string& _description):
|
||||||
|
m_signalInterfaceLink(_signalInterfaceLink),
|
||||||
|
m_name(_name),
|
||||||
|
m_description(_description) {
|
||||||
|
// add a reference on the current signal ...
|
||||||
|
m_signalInterfaceLink.signalAdd(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
esignal::ISignal<Args...>::~ISignal() {
|
||||||
|
m_signalInterfaceLink.signalRemove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
const std::string& esignal::ISignal<Args...>::getName() const {
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class... Args>
|
||||||
|
const std::string& esignal::ISignal<Args...>::getDescription() const {
|
||||||
|
return m_description;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
16
esignal/details/LockSharedPtrRef.cpp
Normal file
16
esignal/details/LockSharedPtrRef.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <esignal/debug.h>
|
||||||
|
#include <esignal/details/LockSharedPtrRef.hxx>
|
||||||
|
#include <esignal/Base.h>
|
||||||
|
|
||||||
|
// only one really needed ...
|
||||||
|
template class esignal::LockSharedPtrRef<esignal::Base>;
|
||||||
|
|
||||||
|
|
91
esignal/details/LockSharedPtrRef.hxx
Normal file
91
esignal/details/LockSharedPtrRef.hxx
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <esignal/LockSharedPtrRef.h>
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
esignal::LockSharedPtrRef<TYPE>::LockSharedPtrRef(TYPE* _pointer) :
|
||||||
|
m_counter(nullptr) {
|
||||||
|
if (_pointer != nullptr) {
|
||||||
|
m_counter = new RefCount<TYPE>(_pointer);
|
||||||
|
m_counter->inc();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
esignal::LockSharedPtrRef<TYPE>::LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj) :
|
||||||
|
m_counter(_obj.m_counter) {
|
||||||
|
if (m_counter == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_counter->inc();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
esignal::LockSharedPtrRef<TYPE>& esignal::LockSharedPtrRef<TYPE>::operator=(const esignal::LockSharedPtrRef<TYPE>& _obj) {
|
||||||
|
if (&_obj == this) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
if (m_counter != nullptr) {
|
||||||
|
m_counter->dec();
|
||||||
|
m_counter = nullptr;
|
||||||
|
}
|
||||||
|
m_counter = _obj.m_counter;
|
||||||
|
if (m_counter == nullptr) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
m_counter->inc();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
esignal::LockSharedPtrRef<TYPE>::LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj) :
|
||||||
|
m_counter(std::move(_obj.m_counter)) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
esignal::LockSharedPtrRef<TYPE>::~LockSharedPtrRef() {
|
||||||
|
if (m_counter == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int64_t count = m_counter->dec();
|
||||||
|
if (count > 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
delete m_counter;
|
||||||
|
m_counter = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
void esignal::LockSharedPtrRef<TYPE>::removeData() {
|
||||||
|
if (m_counter != nullptr) {
|
||||||
|
m_counter->remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
void esignal::LockSharedPtrRef<TYPE>::disconnect(std::size_t _uid) {
|
||||||
|
if (m_counter == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_counter->lock();
|
||||||
|
TYPE* val = m_counter->get();
|
||||||
|
if (val != nullptr) {
|
||||||
|
val->disconnect(_uid);
|
||||||
|
}
|
||||||
|
m_counter->unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
bool esignal::LockSharedPtrRef<TYPE>::isAlive() {
|
||||||
|
return m_counter != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
16
esignal/details/RefCount.cpp
Normal file
16
esignal/details/RefCount.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <esignal/debug.h>
|
||||||
|
#include <esignal/details/RefCount.hxx>
|
||||||
|
#include <esignal/Base.h>
|
||||||
|
|
||||||
|
// only one really needed ...
|
||||||
|
template class esignal::RefCount<esignal::Base>;
|
||||||
|
|
||||||
|
|
66
esignal/details/RefCount.hxx
Normal file
66
esignal/details/RefCount.hxx
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <esignal/RefCount.h>
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
esignal::RefCount<TYPE>::RefCount(TYPE* _data) :
|
||||||
|
m_count(0),
|
||||||
|
m_data(_data) {
|
||||||
|
// nothing to do.
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
esignal::RefCount<TYPE>::~RefCount() {
|
||||||
|
m_data = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
void esignal::RefCount<TYPE>::lock() {
|
||||||
|
m_lock.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
void esignal::RefCount<TYPE>::unlock() {
|
||||||
|
m_lock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
void esignal::RefCount<TYPE>::inc() {
|
||||||
|
lock();
|
||||||
|
m_count++;
|
||||||
|
unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
int64_t esignal::RefCount<TYPE>::dec() {
|
||||||
|
int64_t val;
|
||||||
|
lock();
|
||||||
|
m_count--;
|
||||||
|
val = m_count;
|
||||||
|
unlock();
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
int64_t esignal::RefCount<TYPE>::getCount() const {
|
||||||
|
return m_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
void esignal::RefCount<TYPE>::remove() {
|
||||||
|
lock();
|
||||||
|
m_data = nullptr;
|
||||||
|
unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class TYPE>
|
||||||
|
TYPE* esignal::RefCount<TYPE>::get() {
|
||||||
|
return m_data;
|
||||||
|
}
|
@ -11,8 +11,6 @@
|
|||||||
#include <etk/math/Vector3D.h>
|
#include <etk/math/Vector3D.h>
|
||||||
#include <etk/Color.h>
|
#include <etk/Color.h>
|
||||||
|
|
||||||
size_t esignal::s_uid = 0;
|
|
||||||
|
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ "Signal<void>"
|
#define __class__ "Signal<void>"
|
||||||
// void generic signal
|
// void generic signal
|
||||||
|
@ -8,13 +8,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <esignal/Signal.h>
|
#include <esignal/Signal.h>
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#undef __class__
|
#undef __class__
|
||||||
#define __class__ "Signal<T_ARGS>"
|
#define __class__ "Signal<T_ARGS>"
|
||||||
|
|
||||||
|
|
||||||
template<typename... T_ARGS>
|
template<typename... T_ARGS>
|
||||||
esignal::Signal<T_ARGS...>::Signal():
|
esignal::Signal<T_ARGS...>::Signal():
|
||||||
m_callInProgress(0) {
|
m_callInProgress(0) {
|
||||||
|
@ -32,9 +32,13 @@ def create(target, module_name):
|
|||||||
my_module.add_extra_compile_flags()
|
my_module.add_extra_compile_flags()
|
||||||
my_module.add_src_file([
|
my_module.add_src_file([
|
||||||
'esignal/debug.cpp',
|
'esignal/debug.cpp',
|
||||||
|
'esignal/Connection.cpp',
|
||||||
'esignal/Interface.cpp',
|
'esignal/Interface.cpp',
|
||||||
'esignal/Base.cpp',
|
'esignal/Base.cpp',
|
||||||
|
'esignal/details/LockSharedPtrRef.cpp',
|
||||||
|
'esignal/details/RefCount.cpp',
|
||||||
'esignal/details/Signal.cpp',
|
'esignal/details/Signal.cpp',
|
||||||
|
'esignal/details/ISignal.cpp',
|
||||||
])
|
])
|
||||||
my_module.add_header_file([
|
my_module.add_header_file([
|
||||||
'esignal/debug.h',
|
'esignal/debug.h',
|
||||||
@ -46,6 +50,9 @@ def create(target, module_name):
|
|||||||
'esignal/RefCount.h',
|
'esignal/RefCount.h',
|
||||||
'esignal/Connection.h',
|
'esignal/Connection.h',
|
||||||
'esignal/details/Signal.hxx',
|
'esignal/details/Signal.hxx',
|
||||||
|
'esignal/details/ISignal.hxx',
|
||||||
|
'esignal/details/LockSharedPtrRef.hxx',
|
||||||
|
'esignal/details/RefCount.hxx',
|
||||||
])
|
])
|
||||||
my_module.compile_version("c++", 2011)
|
my_module.compile_version("c++", 2011)
|
||||||
my_module.add_module_depend(['etk'])
|
my_module.add_module_depend(['etk'])
|
||||||
|
13
test/declareSignals.cpp
Normal file
13
test/declareSignals.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* @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>
|
||||||
|
|
||||||
|
template class esignal::Signal<int32_t, std::string>;
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user