[DOC] add basic comment on all class

This commit is contained in:
Edouard DUPIN 2016-02-20 14:48:15 +01:00
parent 0a4f400439
commit cf7a3d1f2f
6 changed files with 192 additions and 100 deletions

View File

@ -18,29 +18,33 @@
#include <esignal/LockSharedPtrRef.h> #include <esignal/LockSharedPtrRef.h>
namespace esignal { namespace esignal {
#undef __class__
#define __class__ "Signal<T>"
class Base { class Base {
protected: protected:
LockSharedPtrRef<Base> m_shared; LockSharedPtrRef<Base> m_shared; //!< Reference counter on itself.
static size_t s_uid; static size_t s_uid; //!< blobal id of the signal (STATIC)
public: public:
//! @brief Basic constructor:
Base(); Base();
// copy constructor: //! @brief Copy constructor:
Base(const Base&) = delete; Base(const Base&) = delete;
// copy operator: //! @brief Move constructor
//Base& operator=(Base) = delete;
//Base& operator=(const Base& _obj) = delete;
// Move constructor
Base(Base&& _obj) = delete; Base(Base&& _obj) = delete;
// Move operator
//Base& operator=(Base&& _obj) = delete;
virtual ~Base(); virtual ~Base();
/**
* @brief get name of the signal
*/
virtual void disconnect(const std::shared_ptr<void>& _obj); virtual void disconnect(const std::shared_ptr<void>& _obj);
virtual void disconnect(std::size_t _uid) = 0; virtual void disconnect(std::size_t _uid) = 0;
/**
* @brief Get name of the signal.
* @return requested name.
*/
virtual const std::string& getName() const; virtual const std::string& getName() const;
/**
* @brief Get decription of the signal.
* @return requested decription.
*/
virtual const std::string& getDescription() const; virtual const std::string& getDescription() const;
}; };
std::ostream& operator <<(std::ostream& _os, const esignal::Base& _obj); std::ostream& operator <<(std::ostream& _os, const esignal::Base& _obj);

View File

@ -17,23 +17,29 @@
#include <mutex> #include <mutex>
namespace esignal { namespace esignal {
/**
* @brief connection on the signal (disconnect it whe removed)
*/
class Connection { class Connection {
public: public:
//! @brief Constructor (no link)
Connection(): Connection():
m_signalRefUnique(), m_uid(0) { m_signalRefUnique(), m_uid(0) {
} }
//! @brief Constructor (link)
Connection(const LockSharedPtrRef<Base>& _ref, std::size_t _id): Connection(const LockSharedPtrRef<Base>& _ref, std::size_t _id):
m_signalRefUnique(_ref), m_signalRefUnique(_ref),
m_uid(_id) { m_uid(_id) {
} }
//! @brief Move Constructor
Connection(Connection&& _obj): Connection(Connection&& _obj):
m_signalRefUnique(_obj.m_signalRefUnique), m_signalRefUnique(_obj.m_signalRefUnique),
m_uid(_obj.m_uid) { m_uid(_obj.m_uid) {
_obj.m_uid = 0; _obj.m_uid = 0;
} }
//! @brief Move operator.
Connection& operator=(Connection&& _obj) { Connection& operator=(Connection&& _obj) {
disconnect(); disconnect();
m_signalRefUnique = _obj.m_signalRefUnique; m_signalRefUnique = _obj.m_signalRefUnique;
@ -41,29 +47,31 @@ namespace esignal {
_obj.m_uid = 0; _obj.m_uid = 0;
return *this; return *this;
} }
Connection(const Connection&) = delete; // not copyable //! @brief Copy constructor (REMOVED)
Connection& operator=(const Connection&) = delete; // no copy operator Connection(const Connection&) = delete;
/* //! @brief Copy operator (REMOVED)
void set(Connection&& _obj) { Connection& operator=(const Connection&) = delete;
disconnect(); //! @brief Destructor.
std::swap(m_signalRefUnique, _obj.m_signalRefUnique);
std::swap(m_uid,_obj.m_uid);
}
*/
~Connection() { ~Connection() {
m_signalRefUnique.disconnect(m_uid); m_signalRefUnique.disconnect(m_uid);
m_uid = 0; m_uid = 0;
} }
//! @brief Disconnect the signal.
void disconnect() { void disconnect() {
m_signalRefUnique.disconnect(m_uid); m_signalRefUnique.disconnect(m_uid);
m_uid = 0; m_uid = 0;
} }
/**
* @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() { bool isConnected() {
return m_signalRefUnique.isAlive(); return m_signalRefUnique.isAlive();
} }
private: private:
LockSharedPtrRef<Base> m_signalRefUnique; LockSharedPtrRef<Base> m_signalRefUnique; //!< reference on the Signal.
std::size_t m_uid; std::size_t m_uid; //!< UID of the current connection.
}; };
} }

View File

@ -15,14 +15,15 @@
#include <esignal/Interface.h> #include <esignal/Interface.h>
namespace esignal { namespace esignal {
#undef __class__ /**
#define __class__ "ISignal<T>" * @brief Sigla same as @ref esignal::Signal withe a name and a description to manage a list of signals.
*/
template<class... Args> template<class... Args>
class ISignal : public Signal<Args...> { class ISignal : public Signal<Args...> {
protected: protected:
esignal::Interface& m_signalInterfaceLink; esignal::Interface& m_signalInterfaceLink; //!< interface of the signal manager.
std::string m_name; std::string m_name; //!< name of the signal.
std::string m_description; std::string m_description; //!< description of the signal.
public: public:
/** /**
* @brief Create a signal with a specific type. * @brief Create a signal with a specific type.
@ -45,14 +46,12 @@ namespace esignal {
virtual ~ISignal() { virtual ~ISignal() {
m_signalInterfaceLink.signalRemove(this); m_signalInterfaceLink.signalRemove(this);
} }
const std::string& getName() const { virtual const std::string& getName() const {
return m_name; return m_name;
} }
const std::string& getDescription() const { virtual const std::string& getDescription() const {
return m_description; return m_description;
} }
}; };
#undef __class__
#define __class__ nullptr
} }

View File

@ -19,12 +19,20 @@
namespace esignal { namespace esignal {
extern size_t s_uid; 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.
* @input[in] TYPE Type of the internal data
*/
template<class TYPE> template<class TYPE>
class LockSharedPtrRef { class LockSharedPtrRef {
public: public:
RefCount<TYPE>* m_counter; RefCount<TYPE>* m_counter; //!< Access on the reference counter
public: public:
/**
* @brief Basic contructor (with the object to ref count)
* @param[in] _pointer Pointer on the data (default nullptr)
*/
LockSharedPtrRef(TYPE* _pointer=nullptr) : LockSharedPtrRef(TYPE* _pointer=nullptr) :
m_counter(nullptr) { m_counter(nullptr) {
if (_pointer != nullptr) { if (_pointer != nullptr) {
@ -32,7 +40,10 @@ namespace esignal {
m_counter->inc(); m_counter->inc();
} }
} }
// copy constructor: /**
* @brief Copy contructor
* @param[in] _obj object to copy
*/
LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj) : LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj) :
m_counter(_obj.m_counter) { m_counter(_obj.m_counter) {
if (m_counter == nullptr) { if (m_counter == nullptr) {
@ -40,8 +51,11 @@ namespace esignal {
} }
m_counter->inc(); m_counter->inc();
} }
// copy operator: /**
//LockSharedPtrRef& operator=(LockSharedPtrRef<TYPE>) = delete; * @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) { LockSharedPtrRef& operator=(const LockSharedPtrRef<TYPE>& _obj) {
if (&_obj == this) { if (&_obj == this) {
return *this; return *this;
@ -57,19 +71,21 @@ namespace esignal {
m_counter->inc(); m_counter->inc();
return *this; return *this;
} }
// Move constructor /**
* @brief Contructor (move)
* @param[in] _obj move object
*/
LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj) : LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj) :
m_counter(std::move(_obj.m_counter)) { m_counter(std::move(_obj.m_counter)) {
} }
// Move operator /**
#if 1 * @brief Copy operator (force move) ==> removed
LockSharedPtrRef& operator=(LockSharedPtrRef<TYPE>&& _obj) = delete; */
#else LockSharedPtrRef& operator=(LockSharedPtrRef<TYPE>&& _obj) = delete;
LockSharedPtrRef& operator=(LockSharedPtrRef<TYPE>&& _obj) { /**
m_counter = std::move(_obj.m_counter); * @brief Destructor of the class (decrement the counter and remove it if it is the last one...)
} */
#endif
~LockSharedPtrRef() { ~LockSharedPtrRef() {
if (m_counter == nullptr) { if (m_counter == nullptr) {
return; return;
@ -81,11 +97,18 @@ namespace esignal {
delete m_counter; delete m_counter;
m_counter = nullptr; m_counter = nullptr;
} }
/**
* @brief Remove the data on the conter reference (it does not exist anymore)
*/
void removeData() { void removeData() {
if (m_counter != nullptr) { if (m_counter != nullptr) {
m_counter->remove(); m_counter->remove();
} }
} }
/**
* @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) { void disconnect(std::size_t _uid) {
if (m_counter == nullptr) { if (m_counter == nullptr) {
return; return;
@ -97,6 +120,11 @@ namespace esignal {
} }
m_counter->unlock(); m_counter->unlock();
} }
/**
* @brief Check if the value is availlable
* @return true The data is availlable
* @return false The data has been removed
*/
bool isAlive() { bool isAlive() {
return m_counter != nullptr; return m_counter != nullptr;
} }

View File

@ -15,44 +15,52 @@
#include <mutex> #include <mutex>
namespace esignal { namespace esignal {
/**
* @brief Ref counting tool.
*/
template<class TYPE> template<class TYPE>
class RefCount { class RefCount {
private:
std::mutex m_lock; //!< mutex on the refcounting element
int64_t m_count; //!< number of element connected
TYPE* m_data; //!< Pointer on the refconting data
public: public:
std::mutex m_lock; //!< generic constructor
int64_t m_count;
TYPE* m_data;
public:
RefCount(TYPE* _data) : RefCount(TYPE* _data) :
m_count(0), m_count(0),
m_data(_data) { m_data(_data) {
// nothing to do. // nothing to do.
} }
// copy constructor: //! @brief Copy constructor (REMOVED)
RefCount(const RefCount&) = delete; RefCount(const RefCount&) = delete;
// copy operator: //! @brief Copy operator (REMOVED)
RefCount& operator=(RefCount) = delete; RefCount& operator=(RefCount) = delete;
//! @previous
RefCount& operator=(const RefCount& _obj) = delete; RefCount& operator=(const RefCount& _obj) = delete;
// Move constructor //! @brief Move constructor (REMOVED)
RefCount(RefCount&& _obj) = delete; RefCount(RefCount&& _obj) = delete;
// Move operator //! @brief Move operator (REMOVED)
RefCount& operator=(RefCount&& _obj) = delete; RefCount& operator=(RefCount&& _obj) = delete;
public: //! @brief Destructor
~RefCount() { ~RefCount() {
m_data = nullptr; m_data = nullptr;
} }
public: public:
//!< @brief Lock the interface
void lock() { void lock() {
m_lock.lock(); m_lock.lock();
} }
//!< @brief Unlock the interface
void unlock() { void unlock() {
m_lock.unlock(); m_lock.unlock();
} }
//!< @brief Increment the ref-counting
void inc() { void inc() {
lock(); lock();
m_count++; m_count++;
unlock(); unlock();
} }
//!< @brief Decrement the ref-counting
int64_t dec() { int64_t dec() {
int64_t val; int64_t val;
lock(); lock();
@ -61,14 +69,17 @@ namespace esignal {
unlock(); unlock();
return val; return val;
} }
//!< @brief Get number of connected
int64_t getCount() const { int64_t getCount() const {
return m_count; return m_count;
} }
//!< @brief Remove the data
void remove() { void remove() {
lock(); lock();
m_data = nullptr; m_data = nullptr;
unlock(); unlock();
} }
//!< @brief Get the recoreded data
TYPE* get() { TYPE* get() {
return m_data; return m_data;
} }

View File

@ -20,44 +20,58 @@
#include <mutex> #include <mutex>
namespace esignal { namespace esignal {
/**
#undef __class__ * @brief Basic signal base
#define __class__ "Signal<T>" * @param[in] Args... Argument of the signal
*/
template<class... Args> template<class... Args>
class Signal : public esignal::Base { class Signal : public esignal::Base {
public: public:
using Observer = std::function<void(const Args&...)>; using Observer = std::function<void(const Args&...)>; //!< Define an Observer: function pointer
int32_t m_callInProgress; protected:
int32_t m_callInProgress; //!< know if we are in a recursive loop
public: public:
Signal() : //! @brief Basic constructor
Signal():
m_callInProgress(0) { m_callInProgress(0) {
} }
// copy constructor: //! @brief Copy constructor (REMOVED)
Signal(const Signal&) = delete; Signal(const Signal&) = delete;
// copy operator: //! @brief Copy operator (REMOVED)
Signal& operator=(Signal) = delete; Signal& operator=(Signal) = delete;
Signal& operator=(const Signal& _obj) = delete; Signal& operator=(const Signal& _obj) = delete;
// Move constructor //! @brief Move constructor (REMOVED)
Signal(Signal&& _obj) = delete; Signal(Signal&& _obj) = delete;
// Move operator //! @brief Move operator
Signal& operator=(Signal&& _obj) = delete; Signal& operator=(Signal&& _obj) = delete;
private: private:
/**
* @brief Executor: Class to manage the UID and basic value of an observer
*/
class Executor { class Executor {
public: public:
Observer m_observer; Observer m_observer; //!< Observer to call when needed (if not removed).
bool m_removed; bool m_removed; //!< the executor has been removed.
size_t m_uid; size_t m_uid; //!< unique ID of the signal (used to remove it).
public: public:
/**
* @brief Basic constructor.
* @param[in] _observer Observer to call.
*/
Executor(Observer&& _observer): Executor(Observer&& _observer):
m_removed(false), m_removed(false),
m_uid(0) { m_uid(0) {
m_uid = s_uid++; m_uid = s_uid++;
m_observer = std::move(_observer); m_observer = std::move(_observer);
} }
//! @brief virtual destructor.
virtual ~Executor() = default; virtual ~Executor() = default;
public: public:
/**
* @brief Emit the data on the observer.
* @param[in] _values... Multiple value needed to send on observers
*/
virtual void emit(Args... _values) { virtual void emit(Args... _values) {
if (m_removed == true) { if (m_removed == true) {
return; return;
@ -71,19 +85,33 @@ namespace esignal {
} }
}; };
protected: protected:
std::vector<std::unique_ptr<Executor>> m_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.
* @param[in] Args... Argument of the signal
*/
class ExecutorShared : public Executor { class ExecutorShared : public Executor {
protected: protected:
std::weak_ptr<void> m_object; std::weak_ptr<void> m_object; //!< a weak reference on the object to verify that it is alive
public: public:
/**
* @brief shared constructor.
* @param[in] _object A weak reference of the object.
* @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)), Executor(std::move(_observer)),
m_object(_object) { m_object(_object) {
} }
public: public:
/**
* @brief Emit the data on the observer.
* @param[in] _values... Multiple value needed to send on observers
*/
virtual void emit(Args... _values) { virtual void emit(Args... _values) {
// TODO: maybe an error if the object is not manage by the same thread.
std::shared_ptr<void> destObject = m_object.lock(); std::shared_ptr<void> destObject = m_object.lock();
if (destObject == nullptr) { if (destObject == nullptr) {
Executor::m_removed = true; Executor::m_removed = true;
@ -101,6 +129,10 @@ namespace esignal {
} }
}; };
public: public:
/**
* @brief Connect an observer on the signal.
* @param[in] _observer Observer to call.
*/
template< class ObserverType > template< class ObserverType >
Connection connect(ObserverType&& _observer ) { Connection connect(ObserverType&& _observer ) {
std::unique_ptr<Executor> executer(new Executor(std::forward<ObserverType>(_observer))); std::unique_ptr<Executor> executer(new Executor(std::forward<ObserverType>(_observer)));
@ -108,25 +140,27 @@ namespace esignal {
m_executors.push_back(std::move(executer)); m_executors.push_back(std::move(executer));
return Connection(Base::m_shared, uid); return Connection(Base::m_shared, uid);
} }
/**
* @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> template<class classType, class Func, class... Arg>
Connection connect(classType* _class, Func _f, Arg... _arg) { Connection connect(classType* _class, Func _func, Arg... _arg) {
std::unique_ptr<Executor> executer(new Executor([=]( auto&&... cargs ){ std::unique_ptr<Executor> executer(new Executor([=]( auto&&... cargs ){
(*_class.*_f)(cargs..., _arg... ); (*_class.*_func)(cargs..., _arg... );
})); }));
std::size_t uid = executer->m_uid; std::size_t uid = executer->m_uid;
m_executors.push_back(std::move(executer)); m_executors.push_back(std::move(executer));
return Connection(Base::m_shared, uid); return Connection(Base::m_shared, uid);
} }
/* /**
template<class classType, class Func, class... Arg> * @brief Connect an function member on the signal with the shared_ptr object.
void connect(const std::shared_ptr<classType>& _class, Func _f, Arg... _arg) { * @param[in] _class shared_ptr Object on whe we need to call ==> the object is get in keeped in weak_ptr.
classType* tmp = _class.get(); * @param[in] _func Function to call.
std::unique_ptr<ExecutorShared> executer(new ExecutorShared(_class, [=]( auto&&... cargs ){ * @param[in] _arg Argument optinnal the user want to add.
(*tmp.*_f)(cargs..., _arg... ); */
}));
m_executors.push_back(std::move(executer));
}
*/
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 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);
@ -141,21 +175,17 @@ namespace esignal {
})); }));
m_executors.push_back(std::move(executer)); m_executors.push_back(std::move(executer));
} }
/*
template<class pointer, class Func, class... Arg>
void bind(const std::shared_ptr<pointer>& _class, Func _f, const Arg&... _arg) {
std::unique_ptr<ExecutorShared> executer(new ExecutorShared(_class, [=]( auto&&... cargs ){
(*_class.*_f)(cargs..., _arg... );
}));
m_executors.push_back(std::move(executer));
}
*/
public: public:
/**
* @brief Emit data on the signal.
* @param[in] _args Argument data to emit.
*/
template< class... CallArgs> template< class... CallArgs>
void emit( CallArgs&&... args) { void emit(CallArgs&&... _args) {
// TODO : Add protection ... but how ...
m_callInProgress++; m_callInProgress++;
for (size_t iii=0; iii < m_executors.size(); ++iii) { for (size_t iii=0; iii < m_executors.size(); ++iii) {
m_executors[iii]->emit(args...); m_executors[iii]->emit(_args...);
} }
if (m_callInProgress == 1) { if (m_callInProgress == 1) {
auto it = m_executors.begin(); auto it = m_executors.begin();
@ -170,6 +200,10 @@ namespace esignal {
} }
m_callInProgress--; m_callInProgress--;
} }
/**
* @brief Disconnect an observer 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) { for (size_t iii=0; iii < m_executors.size(); ++iii) {
if (m_executors[iii]->m_uid == _uid) { if (m_executors[iii]->m_uid == _uid) {
@ -179,20 +213,28 @@ namespace esignal {
} }
} }
public: public:
/**
* @brief Get the number of observers connected on the signal.
* @return The count of observer.
*/
size_t size() const { size_t size() const {
return m_executors.size(); return m_executors.size();
} }
/**
* @brief Check if we have a connected observers.
* @return true More than one observers.
* @return false No observers.
*/
bool empty() const { bool empty() const {
return m_executors.empty(); return m_executors.empty();
} }
/**
* @brief Clear all connectd observers.
*/
void clear() { void clear() {
m_executors.clear(); m_executors.clear();
} }
}; };
#undef __class__
#define __class__ nullptr
} }