[DEV] recurtion

This commit is contained in:
Edouard DUPIN 2016-02-19 21:39:27 +01:00
parent 3772d04916
commit 9003745811
2 changed files with 33 additions and 25 deletions

View File

@ -31,18 +31,18 @@ namespace esignal {
virtual void disconnect(std::size_t _uid) = 0;
};
class Handle {
class Connection {
public:
Handle(SignalBase* _sig, std::size_t _id):
Connection(SignalBase* _sig, std::size_t _id):
m_signal(_sig), m_uid(_id) {
}
Handle(const Handle&) = delete; // not copyable
Handle* operator=(const Handle&) = delete; // no copy operator
Handle(Handle&&) = default; // movable
Handle& operator=(Handle&&) = default; // movable op
Connection(const Connection&) = delete; // not copyable
Connection* operator=(const Connection&) = delete; // no copy operator
Connection(Connection&&) = default; // movable
Connection& operator=(Connection&&) = default; // movable op
~Handle() {
~Connection() {
if (m_signal == nullptr) {
return;
}
@ -128,20 +128,20 @@ namespace esignal {
};
public:
template< class ObserverType >
Handle connect(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 Handle(this, uid);
return Connection(this, uid);
}
template<class pointer, class Func, class... Arg>
Handle connect(pointer* _class, Func _f, Arg... _arg) {
Connection connect(pointer* _class, Func _f, Arg... _arg) {
std::unique_ptr<Executor> executer(new Executor([=]( auto&&... cargs ){
(*_class.*_f)(cargs..., _arg... );
}));
std::size_t uid = executer->m_uid;
m_executors.push_back(std::move(executer));
return Handle(this, uid);
return Connection(this, uid);
}
template<class pointer, class Func, class... Arg>
void connect(const std::shared_ptr<pointer>& _class, Func _f, Arg... _arg) {
@ -154,8 +154,8 @@ namespace esignal {
template< class... CallArgs>
void emit( CallArgs&&... args) {
m_callInProgress++;
for(auto& it: m_executors) {
it->emit(args...);
for (size_t iii=0; iii < m_executors.size(); ++iii) {
m_executors[iii]->emit(args...);
}
if (m_callInProgress == 1) {
auto it = m_executors.begin();
@ -171,9 +171,9 @@ namespace esignal {
m_callInProgress--;
}
void disconnect(std::size_t _uid) {
for (auto &it : m_executors) {
if (it->m_uid == _uid) {
it->m_removed = true;
for (size_t iii=0; iii < m_executors.size(); ++iii) {
if (m_executors[iii]->m_uid == _uid) {
m_executors[iii]->m_removed = true;
return;
}
}

View File

@ -103,26 +103,26 @@ TEST(test_signal_arg, checkType) {
auto display_values_1 = []( int a, float b, char c){
std::cout << " 1111 " << a << " " << b << " " << c << " " << std::endl;
};
signal.connect(display_values_1);
esignal::Connection h1 = signal.connect(display_values_1);
// ----------------------------------------------------
auto display_values_2 = []( int a, float b, char c, int w ){
std::cout << " 2222 " << a << " " << b << " " << c << " " << w << std::endl;
};
signal.connect( complete_args( display_values_2, 72 ) );
esignal::Connection h2 = signal.connect( complete_args( display_values_2, 72 ) );
// ----------------------------------------------------
TestConnect connectedClass;
signal.connect( [&](int a, float b, char c) {
connectedClass.display_values_4(a,b,c);}
);
esignal::Connection h3 = signal.connect([&](int a, float b, char c) {
connectedClass.display_values_4(a,b,c);
});
/*
signal.connect( [&](auto && ... _cargs) {
connectedClass.display_values_3(_cargs);}
);
*/
signal.connect(&connectedClass, &TestConnect::display_values_4);
signal.connect(&connectedClass, &TestConnect::display_values_5, "coucou");
esignal::Connection h4 = signal.connect(&connectedClass, &TestConnect::display_values_4);
esignal::Connection h5 = signal.connect(&connectedClass, &TestConnect::display_values_5, "coucou");
std::shared_ptr<TestConnectShared> connectedClassShared = std::make_shared<TestConnectShared>();
signal.connect(connectedClassShared, &TestConnectShared::display_values_6);
@ -132,9 +132,17 @@ TEST(test_signal_arg, checkType) {
//signal.connect( complete_class(&connectedClass, &TestConnect::display_values_3) );
//signal.connect( TestConnect::display_values_3(&connectedClass) );
auto display_values_recursive = [&]( int a, float b, char c, int w ){
std::cout << " 99999 " << a << " " << b << " " << c << " " << w << std::endl;
std::cout << " ----------------------------------" << std::endl;
if (a == 0) {
return;
}
signal.emit(a-1, 2.66, 'l');
};
esignal::Connection h6 = signal.connect( complete_args( display_values_recursive, 72 ) );
// ----------------------------------------------------
signal.emit( 42, 2.99, 'k');
signal.emit( 5, 2.99, 'k');
}