Provide GuiStack class

This class is meant as an alternative to the existing manual screen
handling done via GfuiScreenActivate/GfuiScreenDeactivate and
GfuiScreenReplace, which is error-prone and might lead to double-free
errors.

Instead, GuiStack is a singleton class based in std::stack which
provides push/pop functions to allow switching easily between screens.
This commit is contained in:
Xavier Del Campo Romero 2022-11-16 00:23:06 +01:00
parent 7aa114efb1
commit 1567936498
Signed by: xavi
GPG Key ID: 91E79CB9F88C50AF
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,46 @@
#include "guistack.h"
#include "tgfclient.h"
#include <stack>
#include <cstddef>
GuiStack &
GuiStack::get()
{
static GuiStack stack;
return stack;
}
void *
GuiStack::top()
{
GuiStack &g = get();
return g.screens.empty() ? NULL : g.screens.top();
}
void
GuiStack::push(void *screen)
{
get().screens.push(screen);
GfuiScreenActivate(screen);
}
void *
GuiStack::pop()
{
void *ret;
GuiStack &g = get();
if (g.screens.empty())
return NULL;
GfuiScreenRelease(g.screens.top());
g.screens.pop();
if (g.screens.empty())
return NULL;
ret = g.screens.top();
GfuiScreenActivate(ret);
return ret;
}

View File

@ -0,0 +1,20 @@
#ifndef GUI_STACK_H
#define GUI_STACK_H
#include <stack>
class GuiStack
{
public:
static void *top();
static void push(void *screen);
static void *pop();
private:
static GuiStack &get();
GuiStack() {};
void operator=(const GuiStack &);
std::stack<void *> screens;
};
#endif /* GUI_STACK_H */