Skip to content

Instantly share code, notes, and snippets.

@0xFEEDC0DE64
Created April 2, 2020 11:23
Show Gist options
  • Save 0xFEEDC0DE64/afbe7bacded7357858c987f3dd5f6003 to your computer and use it in GitHub Desktop.
Save 0xFEEDC0DE64/afbe7bacded7357858c987f3dd5f6003 to your computer and use it in GitHub Desktop.
// gcc -std=c++17 -lstdc++ -otest main.cpp && ./test
#include <array>
#include <cstdio>
#include <string>
// interface for one individual menu entry
class MenuItem {
public:
virtual void render() = 0;
};
// interface for a menu
class Menu {
public:
// points to a sequence of pointers to the menuitems
// this allows for complete stack use, see example menu implementations
virtual MenuItem **begin() = 0;
virtual MenuItem **end() = 0;
void render()
{
for (auto iter = begin(); iter != end(); iter++)
{
(*iter)->render();
}
}
};
// example menu item implementations with different sizes, array, bool ints all have different sizes
class MenuItemImpl1 : public MenuItem {
public:
void render() override { std::puts("MenuItemImpl1::render"); };
private:
std::array<int, 64> m_arrayMember{};
};
class MenuItemImpl2 : public MenuItem {
public:
void render() override { std::puts("MenuItemImpl2::render"); };
private:
bool m_boolMember{};
};
class MenuItemImpl3 : public MenuItem {
public:
void render() override { std::puts("MenuItemImpl3::render"); };
private:
int m_intMember{};
};
// example menu implementation
class Menu1 : public Menu {
public:
MenuItem **begin() override { return std::begin(m_items); };
MenuItem **end() override { return std::end(m_items); };
private:
MenuItemImpl1 m_item1{};
MenuItemImpl2 m_item2{};
MenuItemImpl3 m_item3{};
std::array<MenuItem*, 3> m_items { &m_item1, &m_item2, &m_item3 };
};
int main(int argc, char *argv[])
{
Menu1 menu1;
Menu& menu = menu1;
menu.render();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment