[DEV] separate template implementation

This commit is contained in:
Edouard DUPIN 2016-02-24 21:18:02 +01:00
parent c1fdecd316
commit 1f346fd313
17 changed files with 433 additions and 200 deletions

View File

@ -20,7 +20,7 @@
namespace esignal {
class Base {
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)
public:
//! @brief Basic constructor:

48
esignal/Connection.cpp Normal file
View 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();
}

View File

@ -23,54 +23,29 @@ namespace esignal {
class Connection {
public:
//! @brief Constructor (no link)
Connection():
m_signalRefUnique(), m_uid(0) {
}
Connection();
//! @brief Constructor (link)
Connection(const LockSharedPtrRef<Base>& _ref, std::size_t _id):
m_signalRefUnique(_ref),
m_uid(_id) {
}
Connection(const esignal::LockSharedPtrRef<esignal::Base>& _ref, std::size_t _id);
//! @brief Move Constructor
Connection(Connection&& _obj):
m_signalRefUnique(_obj.m_signalRefUnique),
m_uid(_obj.m_uid) {
_obj.m_uid = 0;
}
Connection(Connection&& _obj);
//! @brief Move operator.
Connection& operator=(Connection&& _obj) {
disconnect();
m_signalRefUnique = _obj.m_signalRefUnique;
m_uid = _obj.m_uid;
_obj.m_uid = 0;
return *this;
}
Connection& operator=(Connection&& _obj);
//! @brief Copy constructor (REMOVED)
Connection(const Connection&) = delete;
//! @brief Copy operator (REMOVED)
Connection& operator=(const Connection&) = delete;
//! @brief Destructor.
~Connection() {
m_signalRefUnique.disconnect(m_uid);
m_uid = 0;
}
~Connection();
//! @brief Disconnect the signal.
void disconnect() {
m_signalRefUnique.disconnect(m_uid);
m_uid = 0;
}
void disconnect();
/**
* @brief Check if the connection is alive or signal removed
* @return true The signal is connected.
* @return false The signal is NOT connected.
*/
bool isConnected() {
return m_signalRefUnique.isAlive();
}
bool isConnected();
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.
};
}

View File

@ -33,25 +33,13 @@ namespace esignal {
*/
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);
}
const std::string& _description = "");
/**
* @brief Destructor.
*/
virtual ~ISignal() {
m_signalInterfaceLink.signalRemove(this);
}
virtual const std::string& getName() const {
return m_name;
}
virtual const std::string& getDescription() const {
return m_description;
}
virtual ~ISignal();
virtual const std::string& getName() const;
virtual const std::string& getDescription() const;
};
}

View File

@ -7,18 +7,9 @@
*/
#pragma once
#include <functional>
#include <memory>
#include <esignal/debug.h>
#include <esignal/Base.h>
#include <esignal/RefCount.h>
#include <functional>
#include <type_traits>
#include <utility>
#include <mutex>
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).
* @todo Change this with atomic_shared_ptr<> when availlable.
@ -27,58 +18,29 @@ namespace esignal {
template<class TYPE>
class LockSharedPtrRef {
public:
RefCount<TYPE>* m_counter; //!< Access on the reference counter
esignal::RefCount<TYPE>* m_counter; //!< Access on the reference counter
public:
/**
* @brief Basic contructor (with the object to ref count)
* @param[in] _pointer Pointer on the data (default nullptr)
*/
LockSharedPtrRef(TYPE* _pointer=nullptr) :
m_counter(nullptr) {
if (_pointer != nullptr) {
m_counter = new RefCount<TYPE>(_pointer);
m_counter->inc();
}
}
LockSharedPtrRef(TYPE* _pointer=nullptr);
/**
* @brief Copy contructor
* @param[in] _obj object to copy
*/
LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj) :
m_counter(_obj.m_counter) {
if (m_counter == nullptr) {
return;
}
m_counter->inc();
}
LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj);
/**
* @brief Copy operator (It copy the counter and increment the it).
* @param[in] _obj objetc to copy.
* @return Reference of this
*/
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;
}
LockSharedPtrRef& operator=(const LockSharedPtrRef<TYPE>& _obj);
/**
* @brief Contructor (move)
* @param[in] _obj move object
*/
LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj) :
m_counter(std::move(_obj.m_counter)) {
}
LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj);
/**
* @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...)
*/
~LockSharedPtrRef() {
if (m_counter == nullptr) {
return;
}
int64_t count = m_counter->dec();
if (count > 0) {
return;
}
delete m_counter;
m_counter = nullptr;
}
~LockSharedPtrRef();
/**
* @brief Remove the data on the conter reference (it does not exist anymore)
*/
void removeData() {
if (m_counter != nullptr) {
m_counter->remove();
}
}
void removeData();
/**
* @brief Call disconnect on the parameter class with lock prevention
* @param[in] _uid ID to dicsonnect on the sub element
*/
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();
}
void disconnect(std::size_t _uid);
/**
* @brief Check if the value is availlable
* @return true The data is availlable
* @return false The data has been removed
*/
bool isAlive() {
return m_counter != nullptr;
}
bool isAlive();
};
}

View File

@ -7,10 +7,6 @@
*/
#pragma once
#include <functional>
#include <memory>
#include <esignal/debug.h>
#include <esignal/Base.h>
#include <utility>
#include <mutex>
@ -26,11 +22,7 @@ namespace esignal {
TYPE* m_data; //!< Pointer on the refconting data
public:
//!< generic constructor
RefCount(TYPE* _data) :
m_count(0),
m_data(_data) {
// nothing to do.
}
RefCount(TYPE* _data);
//! @brief Copy constructor (REMOVED)
RefCount(const RefCount&) = delete;
//! @brief Copy operator (REMOVED)
@ -42,46 +34,21 @@ namespace esignal {
//! @brief Move operator (REMOVED)
RefCount& operator=(RefCount&& _obj) = delete;
//! @brief Destructor
~RefCount() {
m_data = nullptr;
}
~RefCount();
public:
//!< @brief Lock the interface
void lock() {
m_lock.lock();
}
void lock();
//!< @brief Unlock the interface
void unlock() {
m_lock.unlock();
}
void unlock();
//!< @brief Increment the ref-counting
void inc() {
lock();
m_count++;
unlock();
}
void inc();
//!< @brief Decrement the ref-counting
int64_t dec() {
int64_t val;
lock();
m_count--;
val = m_count;
unlock();
return val;
}
int64_t dec();
//!< @brief Get number of connected
int64_t getCount() const {
return m_count;
}
int64_t getCount() const;
//!< @brief Remove the data
void remove() {
lock();
m_data = nullptr;
unlock();
}
void remove();
//!< @brief Get the recoreded data
TYPE* get() {
return m_data;
}
TYPE* get();
};
}

View File

@ -66,6 +66,12 @@ namespace esignal {
*/
virtual void emit(const T_ARGS&... _values);
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);
};
protected:
@ -99,48 +105,28 @@ namespace esignal {
* @brief Connect an observer on the signal.
* @param[in] _observer Observer to call.
*/
template< class ObserverType >
Connection connect(ObserverType&& _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);
}
template< class OBSERVER_TYPE >
esignal::Connection connect(OBSERVER_TYPE&& _observer);
/**
* @brief Connect an function member on the signal.
* @param[in] _class Object on whe we need to call.
* @param[in] _func Function to call.
* @param[in] _arg Argument optinnal the user want to add.
*/
template<class classType, class Func, class... Arg>
Connection connect(classType* _class, Func _func, Arg... _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 CLASS_TYPE, class FUNC_TYPE, class... FUNC_ARGS_TYPE>
esignal::Connection connect(CLASS_TYPE* _class,
FUNC_TYPE _func,
FUNC_ARGS_TYPE... _arg);
/**
* @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] _func Function to call.
* @param[in] _arg Argument optinnal the user want to add.
*/
template<class classType, class TYPE, typename... Arg>
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);
if (obj2 == nullptr) {
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));
}
template<class PARENT_CLASS_TYPE, class CLASS_TYPE, typename... FUNC_ARGS_TYPE>
void connect(const std::shared_ptr<PARENT_CLASS_TYPE>& _class,
void (CLASS_TYPE::*_func)(const T_ARGS&..., FUNC_ARGS_TYPE...),
FUNC_ARGS_TYPE... _args);
public:
/**
* @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));
}

View 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>>;

View 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;
}

View 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>;

View 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;
}

View 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>;

View 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;
}

View File

@ -11,8 +11,6 @@
#include <etk/math/Vector3D.h>
#include <etk/Color.h>
size_t esignal::s_uid = 0;
#undef __class__
#define __class__ "Signal<void>"
// void generic signal

View File

@ -8,13 +8,10 @@
#pragma once
#include <esignal/Signal.h>
#include <functional>
#include <memory>
#undef __class__
#define __class__ "Signal<T_ARGS>"
template<typename... T_ARGS>
esignal::Signal<T_ARGS...>::Signal():
m_callInProgress(0) {

View File

@ -32,9 +32,13 @@ def create(target, module_name):
my_module.add_extra_compile_flags()
my_module.add_src_file([
'esignal/debug.cpp',
'esignal/Connection.cpp',
'esignal/Interface.cpp',
'esignal/Base.cpp',
'esignal/details/LockSharedPtrRef.cpp',
'esignal/details/RefCount.cpp',
'esignal/details/Signal.cpp',
'esignal/details/ISignal.cpp',
])
my_module.add_header_file([
'esignal/debug.h',
@ -46,6 +50,9 @@ def create(target, module_name):
'esignal/RefCount.h',
'esignal/Connection.h',
'esignal/details/Signal.hxx',
'esignal/details/ISignal.hxx',
'esignal/details/LockSharedPtrRef.hxx',
'esignal/details/RefCount.hxx',
])
my_module.compile_version("c++", 2011)
my_module.add_module_depend(['etk'])

13
test/declareSignals.cpp Normal file
View 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>;