Skip to content

Commit

Permalink
Separate headers, view caching and navigate to previous, memory leak …
Browse files Browse the repository at this point in the history
…fix.
  • Loading branch information
cristianfrasineanu committed Nov 19, 2016
1 parent fd4bbdf commit db39415
Show file tree
Hide file tree
Showing 20 changed files with 698 additions and 90 deletions.
37 changes: 18 additions & 19 deletions app/Bootstrap.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#include <iostream>
#include <Windows.h>
#include <conio.h>

#include "Helpers.h"
#include "ClassContainer.h"
#include "Console.h"

using namespace std;

void main()
{
try
{
Console console;
View::loadViewsOptions();
Console console;

do
{
try
{
cout << " >";

console.showPrompt();
console.setLastInput(_getch());
cout << console.getLastInput()
<< endl;
//console.renderNextView();
if (console.takeActionIfAny() == false)
{
console.renderNextView();
}
}
catch (const invalid_argument &e)
{
Expand All @@ -31,21 +29,22 @@ void main()
<< endl;

sleepAndClearBuffer(console.getDelay());

console.reloadView();
}
catch (const system_error &e)
{
cout << e.code()
<< " "
<< e.what()
<< endl;
}
} while (console.getLastInput() != 'q');
} while (!console.shouldExit());
}
catch (const invalid_argument &e)
{
cout << e.what()
<< endl;
}
catch (const system_error &e)
{
clearScreen();

cout << e.code()
<< " "
<< e.what()
<< endl;
}
}
142 changes: 104 additions & 38 deletions app/ClassContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,21 @@
using namespace std;
namespace fs = std::experimental::filesystem::v1;

//--- User ---
//------------
unsigned User::count = 0;

void User::setFromLastUuid()
{
// Get the last uuid from the db file

User::count = 30;
}

User::User() : uuid(User::count++)
{
this->nick = new char[4];
}

User::~User()
{
delete[] this->nick;
}

unsigned User::getUuid()
{
return this->uuid;
}

//---View---
//----------
// A view has attached to it a range of available actions or certain verbs that are used by the console to do some action.
// It's basically a dictionary holding the options mapping
map<string, map<char, string>> View::ViewsOptions = { { "", {{'\0', ""}} } };

View::View(string viewName, map<char, string> availableOptions, bool hasInterpolation)
View::View(string &viewName, map<char, string> &availableOptions, bool hasInterpolation)
{
this->viewName = new char[strlen(viewName.c_str()) + 1];
strcpy(this->viewName, viewName.c_str());
this->availableOptions = availableOptions;
this->hasInterpolation = hasInterpolation;
}

map<char, string> View::getAvailableOptions()
map<char, string> &View::getAvailableOptions()
{
return this->availableOptions;
}
Expand All @@ -61,11 +37,27 @@ char *View::getViewName()

void View::loadViewsOptions()
{
string someView = "aView",
anotherView = "aaaView";
string homeView = "home.view",
loginView = "login.view",
signupView = "signup.view",
browseIndexView = "browse-index.view",
faqView = "faq.view",
helpView = "help.view";

View::ViewsOptions = {
{ homeView, { { '1', "login.view" }, { '2', "signup.view" }, { '3', "browse-index.view" },
{ '4', "faq.view" }, { '5', "help.view" }, { 'q', "quit" } } },
{ loginView, { { '1', "browse-index.view" }, { 'q', "quit" } } },
{ signupView, { { 'q', "quit" } } },
{ browseIndexView, { {'q', "quit"} } },
{ faqView, { {'q', "quit"} } },
{ helpView, { { 'q', "quit" } } }
};
}

View::ViewsOptions = { { someView, {{'1', "theview.view"}}},
{anotherView, {{'2', "someview.view"}}} };
map<string, map<char, string>> &View::getViewsOptions()
{
return View::ViewsOptions;
}

View::~View()
Expand All @@ -83,11 +75,14 @@ Console::Console()
{
this->mode = new char[strlen("live") + 1];
strcpy(this->mode, "live");
map<char, string> availableOptions = { {'1', "login.view"}, {'2', "signup.view"}, {'3', "browse-index.view"}, {'4', "faq.view"}, {'q', "quit"} };
this->exit = false;

map<char, string> availableOptions = View::getViewsOptions().find(Console::initialView)->second;

this->currentView = new View(Console::initialView, availableOptions, false);
this->delay = 2000;

this->loadActions();
this->loadViews(Console::viewsFolder);

if (find(this->loadedViews.begin(), this->loadedViews.end(), this->currentView->getViewName()) != this->loadedViews.end())
Expand All @@ -105,9 +100,15 @@ Console::Console(char *mode)
{
this->mode = new char[strlen("debug") + 1];
strcpy(this->mode, "debug");
this->exit = false;

string viewName = "debug.view";
this->currentView = new View(viewName, { {'q', "quit"} }, true);
map<char, string> availableOptions = View::getViewsOptions().find(Console::initialView)->second;

this->currentView = new View(viewName, availableOptions, true);
this->delay = 2000;

this->loadActions();
this->loadViews(Console::viewsFolder);

if (find(this->loadedViews.begin(), this->loadedViews.end(), this->currentView->getViewName()) != this->loadedViews.end())
Expand All @@ -133,6 +134,17 @@ void Console::setLastInput(char input)
}
}

vector<char> &Console::getActions()
{
return this->actions;
}

void Console::showPrompt()
{
cout << endl
<< ">> ";
}

char Console::getLastInput()
{
return this->lastInput;
Expand Down Expand Up @@ -160,6 +172,13 @@ void Console::loadViews(const fs::path &viewsFolder)
}
}

void Console::loadActions()
{
vector<char> actions = { 'q', 'b', 'n' };

this->actions = actions;
}

void Console::renderView(View &view)
{
string content;
Expand All @@ -177,17 +196,36 @@ void Console::renderView(View &view)
}
else
{
throw system_error(error_code(500, system_category()), "The view stream couldn't be opened.");
throw system_error(error_code(3, system_category()), "The view stream couldn't be opened");
}

viewFile.close();
}

void Console::renderNextView()
{
this->currentView = new View(this->currentView->getAvailableOptions().find(this->getLastInput())->second, { {'5', "lala"} }, false);
string nextView = this->currentView->getAvailableOptions().find(this->getLastInput())->second;
map<char, string> nextOptions = View::getViewsOptions().find(nextView)->second;

//TODO: finish the renderer
// Cache the current view
this->previousViews.push_back(this->currentView);
// The name and then the corresponding options map to the view from ViewsOptions and the hasInterpolation boolean
this->currentView = new View(nextView,
nextOptions,
false);

//TODO: detemine if the view has interpolation enabled in order to replace the template strings or to splice the view in certain places
system("cls");
this->renderView(*this->currentView);
}

void Console::renderPreviousView()
{
if (!this->previousViews.empty())
{
this->renderView(*this->previousViews.back());
this->previousViews.pop_back();
}
}

void Console::reloadView()
Expand All @@ -196,6 +234,34 @@ void Console::reloadView()
this->renderView(*this->currentView);
}

void Console::takeActionIfAny()
{
if (isInCharVector(this->getActions(), this->lastInput))
{
switch (this->lastInput)
{
case 'q':
this->breakTheLoop();
break;
case 'b':
this->renderPreviousView();
break;
case 'n':
// TODO: implement pagination for the questions index view
}
}
}

bool Console::shouldExit()
{
return this->exit;
}

void Console::breakTheLoop()
{
this->exit = true;
}

unsigned Console::getDelay()
{
return this->delay;
Expand All @@ -207,6 +273,6 @@ Console::~Console()
delete[] this->mode;

system("cls");
cout << "Program exited successfully..."
cout << "Program exited the main console."
<< endl;
}
35 changes: 20 additions & 15 deletions app/ClassContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ namespace fs = std::experimental::filesystem::v1;
class View {
private:
static map<string, map<char, string>> ViewsOptions;

char *viewName;
bool hasInterpolation;

map<char, string> availableOptions;

View();
public:
View(string, map<char, string>, bool);
View(string &, map<char, string> &, bool);

map<char, string> getAvailableOptions();
map<char, string> &getAvailableOptions();
char *getViewName();
static void loadViewsOptions();
static map<string, map<char, string>> &getViewsOptions();

~View();
};
Expand All @@ -27,35 +30,37 @@ class Console {
private:
static string initialView;
static char *viewsFolder;

char *mode;
char lastInput;
unsigned delay;
bool exit;
View *currentView;
vector<View *> previousViews;

vector<fs::path> loadedViews;
vector<char> actions;

void loadViews(const fs::path &);
void loadActions();
public:
Console();
Console(char *);

vector<char> &getActions();

void showPrompt();
char getLastInput();
void setLastInput(char);
unsigned getDelay();

void renderView(View &);
void renderNextView();
void renderPreviousView();
void reloadView();
unsigned getDelay();
void takeActionIfAny();
bool shouldExit();
void breakTheLoop();

~Console();
};

class User {
private:
const unsigned uuid;
char *nick;
public:
static unsigned count;
static void setFromLastUuid();
User();
~User();
unsigned getUuid();
};
Loading

0 comments on commit db39415

Please sign in to comment.