From 564fc3fe6d96cad4ac644c35d462123f4ad8035a Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 27 Aug 2017 02:21:54 +0200 Subject: Factorisation de code --- todo.cpp | 75 +++++++++++++++++++++------------------------------------------- 1 file changed, 24 insertions(+), 51 deletions(-) diff --git a/todo.cpp b/todo.cpp index 0de12f5..6bbcd89 100755 --- a/todo.cpp +++ b/todo.cpp @@ -111,6 +111,22 @@ ostream& operator<<( ostream& os, const todo& todoTask) return os; } +template +void modifyTODO( vector& listTodo, int argc, char* argv[], Functor modif ) { + for (auto it = begin(listTodo); it != end(listTodo); ++it) { + bool active = true; + + for( int i = 0 ; i < argc && active ; i++ ) + if( it->str.find(argv[i]) == string::npos) + active = false; + if( active ) { + cout << " # " << it->str << endl; + modif(*it); + } + } +} + + int main(int argc, char *argv[]) { color = true; vector listTodo; @@ -217,29 +233,13 @@ int main(int argc, char *argv[]) { break; } case 3: // check - for (auto it = begin(listTodo); it != end(listTodo); ++it) { - bool active = true; - for( int i = 2 ; i < argc && active ; i++ ) - if( it->str.find(argv[i]) == string::npos) - active = false; - if( active ) { - cout << " # " << it->str << endl; - it->etat = DONE_STATE; - } - } - + modifyTODO( listTodo, argc-2, argv+2, [](todo& it) { it.etat = DONE_STATE; }); break; - case 4: // set - for (auto it = begin(listTodo); it != end(listTodo); ++it) { - bool active = true; - for( int i = 3 ; i < argc && active ; i++ ) - if( it->str.find(argv[i]) == string::npos) - active = false; - if( active ) { - cout << " # " << it->str << endl; - it->priorite = atoi(argv[2]); + case 4: // set + { + int priority = atoi(argv[2]); + modifyTODO( listTodo, argc-3, argv+3, [priority](todo& it) { it.priorite = priority; }); } - } break; case 5: // s for (auto it = begin(listTodo); it != end(listTodo); ++it) @@ -268,40 +268,13 @@ int main(int argc, char *argv[]) { cout << *it; return 0; case 9: // uncheck - for (auto it = begin(listTodo); it != end(listTodo); ++it) { - bool active = true; - for( int i = 2 ; i < argc && active ; i++ ) - if( it->str.find(argv[i]) == string::npos) - active = false; - if( active ) { - cout << " # " << it->str << endl; - it->etat = TODO_STATE; - } - } + modifyTODO( listTodo, argc-2, argv+2, [](todo& it) { it.etat = TODO_STATE; }); break; case 10: // standby - for (auto it = begin(listTodo); it != end(listTodo); ++it) { - bool active = true; - for( int i = 2 ; i < argc && active ; i++ ) - if( it->str.find(argv[i]) == string::npos) - active = false; - if( active ) { - cout << " # " << it->str << endl; - it->etat = STANDBY_STATE; - } - } + modifyTODO( listTodo, argc-2, argv+2, [](todo& it) { it.etat = STANDBY_STATE; }); break; case 11: // miss - for (auto it = begin(listTodo); it != end(listTodo); ++it) { - bool active = true; - for( int i = 2 ; i < argc && active ; i++ ) - if( it->str.find(argv[i]) == string::npos) - active = false; - if( active ) { - cout << " # " << it->str << endl; - it->etat = MISSED_STATE; - } - } + modifyTODO( listTodo, argc-2, argv+2, [](todo& it) { it.etat = MISSED_STATE; }); break; case 12: // Edit for (auto it = begin(listTodo); it != end(listTodo); ++it) { -- cgit v1.2.3