From b6e3cc840e255b78ee53e55b420aeee130e51ce1 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 25 Sep 2024 10:53:18 -0500 Subject: Work on wrapping types. --- src/imgui_context.hpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'src/imgui_context.hpp') diff --git a/src/imgui_context.hpp b/src/imgui_context.hpp index 94bac3d..435db9a 100644 --- a/src/imgui_context.hpp +++ b/src/imgui_context.hpp @@ -1,9 +1,10 @@ #ifndef IMGUI_CONTEXT_HPP #define IMGUI_CONTEXT_HPP +#include "gl_context.hpp" #include "sdl_context.hpp" #include "sdl_main_window.hpp" -#include "gl_context.hpp" + #include namespace bookmouse { @@ -24,6 +25,61 @@ class ImguiContext { SdlMainWindow& m_mainWindow; }; +template +class ConditionalRaii { + public: + template + ConditionalRaii(Args&&... args) : m_expr{false} + { + Expr expr{}; + m_expr = expr(std::forward(args)...); + } + ~ConditionalRaii() + { + Cleanup cleanup{}; + if (m_expr) { + cleanup(); + } + } + ConditionalRaii(const ConditionalRaii& rhs) = delete; + ConditionalRaii(ConditionalRaii&& rhs) = delete; + ConditionalRaii& operator=(const ConditionalRaii& rhs) = delete; + ConditionalRaii& operator=(ConditionalRaii&& rhs) = delete; + operator bool() const + { + return m_expr; + } + + private: + bool m_expr; +}; + +#define STRUCT_FUNCTOR(FUNCTOR_NAME, FUNCTOR_FUNCTION) \ + struct FUNCTOR_NAME { \ + auto operator()() -> decltype(FUNCTOR_FUNCTION()) \ + { \ + return FUNCTOR_FUNCTION(); \ + } \ + }; + +#define STRUCT_FUNCTOR_ARGS(FUNCTOR_NAME, FUNCTOR_FUNCTION) \ + struct FUNCTOR_NAME { \ + template \ + auto operator()(Args&&... args) -> /* FORCE BREAK */ \ + decltype(FUNCTOR_FUNCTION(std::forward(args)...)) \ + { \ + return FUNCTOR_FUNCTION(std::forward(args)...); \ + } \ + }; + +STRUCT_FUNCTOR_ARGS(ImGuiBeginMenu, ImGui::BeginMenu) +STRUCT_FUNCTOR(ImGuiEndMenu, ImGui::EndMenu) + +using ImguiMenu = ConditionalRaii; + +#undef STRUCT_FUNCTOR +#undef STRUCT_FUNCTOR_ARGS + } // namespace bookmouse #endif -- cgit v1.2.3