From 15679364986866ec396698c5822b96a09c329a8b Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Wed, 16 Nov 2022 00:23:06 +0100 Subject: [PATCH] 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. --- src/libs/tgfclient/guistack.cpp | 46 +++++++++++++++++++++++++++++++++ src/libs/tgfclient/guistack.h | 20 ++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/libs/tgfclient/guistack.cpp create mode 100644 src/libs/tgfclient/guistack.h diff --git a/src/libs/tgfclient/guistack.cpp b/src/libs/tgfclient/guistack.cpp new file mode 100644 index 000000000..def2622c8 --- /dev/null +++ b/src/libs/tgfclient/guistack.cpp @@ -0,0 +1,46 @@ +#include "guistack.h" +#include "tgfclient.h" +#include +#include + +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; +} diff --git a/src/libs/tgfclient/guistack.h b/src/libs/tgfclient/guistack.h new file mode 100644 index 000000000..556024cf6 --- /dev/null +++ b/src/libs/tgfclient/guistack.h @@ -0,0 +1,20 @@ +#ifndef GUI_STACK_H +#define GUI_STACK_H + +#include + +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 screens; +}; + +#endif /* GUI_STACK_H */