From fd4bbdfbe3b71ba9c02a18cdb997421153c9bed8 Mon Sep 17 00:00:00 2001 From: Cristian-Adrian Frasineanu Date: Fri, 18 Nov 2016 16:03:33 +0200 Subject: [PATCH] Sleep helper function and check if the view can be opened in the render. --- app/Bootstrap.cpp | 19 ++++++++++--------- app/ClassContainer.cpp | 6 +++++- app/Helpers.cpp | 20 +++++++++++++++++--- app/Helpers.h | 4 +++- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/Bootstrap.cpp b/app/Bootstrap.cpp index e2daa01..390d640 100644 --- a/app/Bootstrap.cpp +++ b/app/Bootstrap.cpp @@ -2,6 +2,7 @@ #include #include +#include "Helpers.h" #include "ClassContainer.h" using namespace std; @@ -21,6 +22,7 @@ void main() console.setLastInput(_getch()); cout << console.getLastInput() << endl; + //console.renderNextView(); } catch (const invalid_argument &e) { @@ -28,17 +30,17 @@ void main() << e.what() << endl; - Sleep(console.getDelay()); - - // Clear the keyboard buffer during the sleep cycle (if there are any keys pressed), - // so as long as the keyboard is hit during that period, the input won't be taken into consideration. - while (_kbhit()) - { - _getch(); - } + sleepAndClearBuffer(console.getDelay()); console.reloadView(); } + catch (const system_error &e) + { + cout << e.code() + << " " + << e.what() + << endl; + } } while (console.getLastInput() != 'q'); } catch (const invalid_argument &e) @@ -46,5 +48,4 @@ void main() cout << e.what() << endl; } - } diff --git a/app/ClassContainer.cpp b/app/ClassContainer.cpp index 46659c3..d973312 100644 --- a/app/ClassContainer.cpp +++ b/app/ClassContainer.cpp @@ -39,7 +39,7 @@ unsigned User::getUuid() //---View--- //---------- -map> View::ViewsOptions = {{ "", {{'\0', ""}} }}; +map> View::ViewsOptions = { { "", {{'\0', ""}} } }; View::View(string viewName, map availableOptions, bool hasInterpolation) { @@ -175,6 +175,10 @@ void Console::renderView(View &view) << endl; buffer.clear(); } + else + { + throw system_error(error_code(500, system_category()), "The view stream couldn't be opened."); + } viewFile.close(); } diff --git a/app/Helpers.cpp b/app/Helpers.cpp index cb4bece..cccefe0 100644 --- a/app/Helpers.cpp +++ b/app/Helpers.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "Helpers.h" @@ -9,17 +11,29 @@ bool isInCharStringMap(const map &haystack, char needle) return haystack.find(needle) != haystack.end(); } -bool isInCharVector(vector haystack, char needle) +bool isInCharVector(const vector &haystack, char needle) { return find(haystack.begin(), haystack.end(), needle) != haystack.end(); } -bool isInIntVector(vector haystack, int needle) +bool isInIntVector(const vector &haystack, int needle) { return find(haystack.begin(), haystack.end(), needle) != haystack.end(); } -bool isInStringVector(vector haystack, string needle) +bool isInStringVector(const vector &haystack, string needle) { return find(haystack.begin(), haystack.end(), needle) != haystack.end(); } + +void sleepAndClearBuffer(unsigned delay) +{ + Sleep(delay); + + // Clear the keyboard buffer during the sleep cycle (if there are any keys pressed), + // so as long as the keyboard is hit during that period, the input won't be taken into consideration. + while (_kbhit()) + { + _getch(); + } +} diff --git a/app/Helpers.h b/app/Helpers.h index fdaaa9f..db773bc 100644 --- a/app/Helpers.h +++ b/app/Helpers.h @@ -1,9 +1,11 @@ #include #include +#include using namespace std; bool isInCharStringMap(const map &, char); bool isInCharVector(const vector &, char); bool isInIntVector(const vector &, int); -bool isInStringVector(const vector &, string); \ No newline at end of file +bool isInStringVector(const vector &, string); +void sleepAndClearBuffer(unsigned delay); \ No newline at end of file