summaryrefslogtreecommitdiff
path: root/src/imgui_context.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/imgui_context.hpp')
-rw-r--r--src/imgui_context.hpp58
1 files changed, 57 insertions, 1 deletions
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 <imgui/imgui.h>
namespace bookmouse {
@@ -24,6 +25,61 @@ class ImguiContext {
SdlMainWindow& m_mainWindow;
};
+template <typename Expr, typename Cleanup>
+class ConditionalRaii {
+ public:
+ template <typename... Args>
+ ConditionalRaii(Args&&... args) : m_expr{false}
+ {
+ Expr expr{};
+ m_expr = expr(std::forward<Args>(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 <typename... Args> \
+ auto operator()(Args&&... args) -> /* FORCE BREAK */ \
+ decltype(FUNCTOR_FUNCTION(std::forward<Args>(args)...)) \
+ { \
+ return FUNCTOR_FUNCTION(std::forward<Args>(args)...); \
+ } \
+ };
+
+STRUCT_FUNCTOR_ARGS(ImGuiBeginMenu, ImGui::BeginMenu)
+STRUCT_FUNCTOR(ImGuiEndMenu, ImGui::EndMenu)
+
+using ImguiMenu = ConditionalRaii<ImGuiBeginMenu, ImGuiEndMenu>;
+
+#undef STRUCT_FUNCTOR
+#undef STRUCT_FUNCTOR_ARGS
+
} // namespace bookmouse
#endif