[DEV] correct the copy of the connection

This commit is contained in:
Edouard DUPIN 2016-02-20 00:18:02 +01:00
parent c4eb2635a3
commit dd8296fd60
4 changed files with 40 additions and 18 deletions

View File

@ -24,12 +24,23 @@ namespace esignal {
m_signalRefUnique(), m_uid(0) {
}
Connection(LockSharedPtrRef<Base> _ref, std::size_t _id):
m_signalRefUnique(_ref), m_uid(_id) {
Connection(const LockSharedPtrRef<Base>& _ref, std::size_t _id):
m_signalRefUnique(_ref),
m_uid(_id) {
}
Connection(Connection&&) = default; // movable
Connection& operator=(Connection&&) = default; // movable op
Connection(Connection&& _obj):
m_signalRefUnique(_obj.m_signalRefUnique),
m_uid(_obj.m_uid) {
_obj.m_uid = 0;
}
Connection& operator=(Connection&& _obj) {
disconnect();
m_signalRefUnique = _obj.m_signalRefUnique;
m_uid = _obj.m_uid;
_obj.m_uid = 0;
return *this;
}
Connection(const Connection&) = delete; // not copyable
Connection& operator=(const Connection&) = delete; // no copy operator
/*

View File

@ -33,7 +33,7 @@ namespace esignal {
}
}
// copy constructor:
LockSharedPtrRef(const LockSharedPtrRef& _obj) :
LockSharedPtrRef(const LockSharedPtrRef<TYPE>& _obj) :
m_counter(_obj.m_counter) {
if (m_counter == nullptr) {
return;
@ -41,8 +41,8 @@ namespace esignal {
m_counter->inc();
}
// copy operator:
LockSharedPtrRef& operator=(LockSharedPtrRef) = delete;
LockSharedPtrRef& operator=(const LockSharedPtrRef& _obj) {
//LockSharedPtrRef& operator=(LockSharedPtrRef<TYPE>) = delete;
LockSharedPtrRef& operator=(const LockSharedPtrRef<TYPE>& _obj) {
if (&_obj == this) {
return *this;
}
@ -52,20 +52,24 @@ namespace esignal {
}
m_counter = _obj.m_counter;
if (m_counter == nullptr) {
return;
return *this;
}
m_counter->inc();
return *this;
}
// Move constructor
LockSharedPtrRef(LockSharedPtrRef&& _obj) :
LockSharedPtrRef(LockSharedPtrRef<TYPE>&& _obj) :
m_counter(std::move(_obj.m_counter)) {
}
// Move operator
LockSharedPtrRef& operator=(LockSharedPtrRef&& _obj) {
m_counter = std::move(_obj.m_counter);
}
#if 1
LockSharedPtrRef& operator=(LockSharedPtrRef<TYPE>&& _obj) = delete;
#else
LockSharedPtrRef& operator=(LockSharedPtrRef<TYPE>&& _obj) {
m_counter = std::move(_obj.m_counter);
}
#endif
~LockSharedPtrRef() {
int64_t count = m_counter->dec();
if (count > 0) {

View File

@ -102,8 +102,8 @@ namespace esignal {
};
public:
template< class ObserverType >
Connection connect(ObserverType&& observer ) {
std::unique_ptr<Executor> executer(new Executor(std::forward<ObserverType>(observer)));
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);

View File

@ -111,18 +111,18 @@ TEST(test_signal_arg, checkType) {
EXPECT_EQ(connectedClass->m_valueInt32, 22);
*/
#if 0
esignal::Signal<std::string> signal;
/*
TestConnect connectedClass;
esignal::Connection h6564 = signal.connect(connectedClass, TestConnect::non_const);
esignal::Connection h6565 = signal.connect(connectedClass, TestConnect::is_const);
esignal::Connection h6564 = signal.connect(&connectedClass, TestConnect::non_const);
esignal::Connection h6565 = signal.connect(&connectedClass, TestConnect::is_const);
*/
std::shared_ptr<TestConnectShared> connectedClassShared = std::make_shared<TestConnectShared>();
signal.connect(connectedClassShared, &TestConnectShared::non_const);
signal.connect(connectedClassShared, &TestConnectShared::is_const);
#endif
#if 0
esignal::Signal<int, float, char> signal;
@ -205,6 +205,13 @@ TEST(test_signal_arg, checkType) {
#endif
esignal::Signal<std::string> signal;
TestConnect connectedClass;
esignal::Connection h6564 = signal.connect(&connectedClass, &TestConnect::non_const);
h6564.disconnect();
h6564 = signal.connect(&connectedClass, &TestConnect::is_const);
signal.emit("kljlkjlkjlkj");
std::cout << "========================================= " << std::endl;
}