Skip to content

Commit

Permalink
Removed unnecessary underscores (internal macros)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnesarco committed Jul 27, 2021
1 parent 7bbaf52 commit f0d8a03
Showing 1 changed file with 68 additions and 68 deletions.
136 changes: 68 additions & 68 deletions imgui_sugar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ namespace ImGuiSugar
struct BooleanGuard
{
BooleanGuard(const bool state, const ScopeEndCallback end) noexcept
: _state(state), _end(end) {}
: m_state(state), m_end(end) {}

~BooleanGuard() noexcept
{
if (AlwaysCallEnd || _state) { _end(); }
if (AlwaysCallEnd || m_state) { m_end(); }
}

operator bool() const & noexcept { return _state; }
operator bool() const & noexcept { return m_state; }

private:
const bool _state;
const ScopeEndCallback _end;
const bool m_state;
const ScopeEndCallback m_end;
};

// For special cases, transform void(*)(int) to void(*)()
Expand All @@ -61,12 +61,12 @@ namespace ImGuiSugar
// ----------------------------------------------------------------------------

// Portable Expression Statement, calls void function and returns true
#define _IMGUI_SUGAR_ES(FN, ...) ([&]() -> bool { FN(__VA_ARGS__); return true; }())
#define _IMGUI_SUGAR_ES_0(FN) ([&]() -> bool { FN(); return true; }())
#define IMGUI_SUGAR_ES(FN, ...) ([&]() -> bool { FN(__VA_ARGS__); return true; }())
#define IMGUI_SUGAR_ES_0(FN) ([&]() -> bool { FN(); return true; }())

// Concatenating symbols with __LINE__ requires two levels of indirection
#define _IMGUI_SUGAR_CONCAT0(A, B) A ## B
#define _IMGUI_SUGAR_CONCAT1(A, B) _IMGUI_SUGAR_CONCAT0(A, B)
#define IMGUI_SUGAR_CONCAT0(A, B) A ## B
#define IMGUI_SUGAR_CONCAT1(A, B) IMGUI_SUGAR_CONCAT0(A, B)

// ----------------------------------------------------------------------------
// [SECTION] Generic macros to simplify repetitive declarations
Expand All @@ -79,95 +79,95 @@ namespace ImGuiSugar
// | function name | function name | unconditional? | function arguments |
// +----------------------+-------------------+-----------------+---------------------+

#define _IMGUI_SUGAR_SCOPED_BOOL(BEGIN, END, ALWAYS, ...) \
#define IMGUI_SUGAR_SCOPED_BOOL(BEGIN, END, ALWAYS, ...) \
if (const ImGuiSugar::BooleanGuard<ALWAYS> _ui_scope_guard = {ImGui::BEGIN(__VA_ARGS__), &ImGui::END})

#define _IMGUI_SUGAR_SCOPED_BOOL_0(BEGIN, END, ALWAYS) \
#define IMGUI_SUGAR_SCOPED_BOOL_0(BEGIN, END, ALWAYS) \
if (const ImGuiSugar::BooleanGuard<ALWAYS> _ui_scope_guard = {ImGui::BEGIN(), &ImGui::END})

#define _IMGUI_SUGAR_SCOPED_VOID_N(BEGIN, END, ...) \
if (const ImGuiSugar::BooleanGuard<true> _ui_scope_guard = {_IMGUI_SUGAR_ES(ImGui::BEGIN, __VA_ARGS__), &ImGui::END})
#define IMGUI_SUGAR_SCOPED_VOID_N(BEGIN, END, ...) \
if (const ImGuiSugar::BooleanGuard<true> _ui_scope_guard = {IMGUI_SUGAR_ES(ImGui::BEGIN, __VA_ARGS__), &ImGui::END})

#define _IMGUI_SUGAR_SCOPED_VOID_0(BEGIN, END) \
if (const ImGuiSugar::BooleanGuard<true> _ui_scope_guard = {_IMGUI_SUGAR_ES_0(ImGui::BEGIN), &ImGui::END})
#define IMGUI_SUGAR_SCOPED_VOID_0(BEGIN, END) \
if (const ImGuiSugar::BooleanGuard<true> _ui_scope_guard = {IMGUI_SUGAR_ES_0(ImGui::BEGIN), &ImGui::END})

#define _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(BEGIN, END, ...) \
const ImGuiSugar::BooleanGuard<true> _IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = {_IMGUI_SUGAR_ES(ImGui::BEGIN, __VA_ARGS__), &ImGui::END}
#define IMGUI_SUGAR_PARENT_SCOPED_VOID_N(BEGIN, END, ...) \
const ImGuiSugar::BooleanGuard<true> IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = {IMGUI_SUGAR_ES(ImGui::BEGIN, __VA_ARGS__), &ImGui::END}

// ---------------------------------------------------------------------------
// [SECTION] ImGui DSL
// ----------------------------------------------------------------------------

// Self scoped blocks

#define with_Window(...) _IMGUI_SUGAR_SCOPED_BOOL(Begin, End, true, __VA_ARGS__)
#define with_Child(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginChild, EndChild, true, __VA_ARGS__)
#define with_ChildFrame(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginChildFrame, EndChildFrame, true, __VA_ARGS__)
#define with_Combo(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginCombo, EndCombo, false, __VA_ARGS__)
#define with_ListBox(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginListBox, EndListBox, false, __VA_ARGS__)
#define with_Menu(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginMenu, EndMenu, false, __VA_ARGS__)
#define with_Popup(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginPopup, EndPopup, false, __VA_ARGS__)
#define with_PopupModal(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginPopupModal, EndPopup, false, __VA_ARGS__)
#define with_PopupContextItem(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginPopupContextItem, EndPopup, false, __VA_ARGS__)
#define with_PopupContextWindow(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginPopupContextWindow, EndPopup, false, __VA_ARGS__)
#define with_PopupContextVoid(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginPopupContextVoid, EndPopup, false, __VA_ARGS__)
#define with_Table(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginTable, EndTable, false, __VA_ARGS__)
#define with_TabBar(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginTabBar, EndTabBar, false, __VA_ARGS__)
#define with_TabItem(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginTabItem, EndTabItem, false, __VA_ARGS__)
#define with_DragDropSource(...) _IMGUI_SUGAR_SCOPED_BOOL(BeginDragDropSource, EndDragDropSource, false, __VA_ARGS__)
#define with_TreeNode(...) _IMGUI_SUGAR_SCOPED_BOOL(TreeNode, TreePop, false, __VA_ARGS__)
#define with_TreeNodeV(...) _IMGUI_SUGAR_SCOPED_BOOL(TreeNodeV, TreePop, false, __VA_ARGS__)
#define with_TreeNodeEx(...) _IMGUI_SUGAR_SCOPED_BOOL(TreeNodeEx, TreePop, false, __VA_ARGS__)
#define with_TreeNodeExV(...) _IMGUI_SUGAR_SCOPED_BOOL(TreeNodeExV, TreePop, false, __VA_ARGS__)

#define with_DragDropTarget _IMGUI_SUGAR_SCOPED_BOOL_0(BeginDragDropTarget, EndDragDropTarget, false)
#define with_MainMenuBar _IMGUI_SUGAR_SCOPED_BOOL_0(BeginMainMenuBar, EndMainMenuBar, false)
#define with_MenuBar _IMGUI_SUGAR_SCOPED_BOOL_0(BeginMenuBar, EndMenuBar, false)

#define with_Group _IMGUI_SUGAR_SCOPED_VOID_0(BeginGroup, EndGroup)
#define with_Tooltip _IMGUI_SUGAR_SCOPED_VOID_0(BeginTooltip, EndTooltip)

#define with_Font(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushFont, PopFont, __VA_ARGS__)
#define with_AllowKeyboardFocus(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushAllowKeyboardFocus, PopAllowKeyboardFocus, __VA_ARGS__)
#define with_ButtonRepeat(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushButtonRepeat, PopButtonRepeat, __VA_ARGS__)
#define with_ItemWidth(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushItemWidth, PopItemWidth, __VA_ARGS__)
#define with_TextWrapPos(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushTextWrapPos, PopTextWrapPos, __VA_ARGS__)
#define with_ID(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushID, PopID, __VA_ARGS__)
#define with_ClipRect(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushClipRect, PopClipRect, __VA_ARGS__)
#define with_TextureID(...) _IMGUI_SUGAR_SCOPED_VOID_N(PushTextureID, PopTextureID, __VA_ARGS__)
#define with_Window(...) IMGUI_SUGAR_SCOPED_BOOL(Begin, End, true, __VA_ARGS__)
#define with_Child(...) IMGUI_SUGAR_SCOPED_BOOL(BeginChild, EndChild, true, __VA_ARGS__)
#define with_ChildFrame(...) IMGUI_SUGAR_SCOPED_BOOL(BeginChildFrame, EndChildFrame, true, __VA_ARGS__)
#define with_Combo(...) IMGUI_SUGAR_SCOPED_BOOL(BeginCombo, EndCombo, false, __VA_ARGS__)
#define with_ListBox(...) IMGUI_SUGAR_SCOPED_BOOL(BeginListBox, EndListBox, false, __VA_ARGS__)
#define with_Menu(...) IMGUI_SUGAR_SCOPED_BOOL(BeginMenu, EndMenu, false, __VA_ARGS__)
#define with_Popup(...) IMGUI_SUGAR_SCOPED_BOOL(BeginPopup, EndPopup, false, __VA_ARGS__)
#define with_PopupModal(...) IMGUI_SUGAR_SCOPED_BOOL(BeginPopupModal, EndPopup, false, __VA_ARGS__)
#define with_PopupContextItem(...) IMGUI_SUGAR_SCOPED_BOOL(BeginPopupContextItem, EndPopup, false, __VA_ARGS__)
#define with_PopupContextWindow(...) IMGUI_SUGAR_SCOPED_BOOL(BeginPopupContextWindow, EndPopup, false, __VA_ARGS__)
#define with_PopupContextVoid(...) IMGUI_SUGAR_SCOPED_BOOL(BeginPopupContextVoid, EndPopup, false, __VA_ARGS__)
#define with_Table(...) IMGUI_SUGAR_SCOPED_BOOL(BeginTable, EndTable, false, __VA_ARGS__)
#define with_TabBar(...) IMGUI_SUGAR_SCOPED_BOOL(BeginTabBar, EndTabBar, false, __VA_ARGS__)
#define with_TabItem(...) IMGUI_SUGAR_SCOPED_BOOL(BeginTabItem, EndTabItem, false, __VA_ARGS__)
#define with_DragDropSource(...) IMGUI_SUGAR_SCOPED_BOOL(BeginDragDropSource, EndDragDropSource, false, __VA_ARGS__)
#define with_TreeNode(...) IMGUI_SUGAR_SCOPED_BOOL(TreeNode, TreePop, false, __VA_ARGS__)
#define with_TreeNodeV(...) IMGUI_SUGAR_SCOPED_BOOL(TreeNodeV, TreePop, false, __VA_ARGS__)
#define with_TreeNodeEx(...) IMGUI_SUGAR_SCOPED_BOOL(TreeNodeEx, TreePop, false, __VA_ARGS__)
#define with_TreeNodeExV(...) IMGUI_SUGAR_SCOPED_BOOL(TreeNodeExV, TreePop, false, __VA_ARGS__)

#define with_DragDropTarget IMGUI_SUGAR_SCOPED_BOOL_0(BeginDragDropTarget, EndDragDropTarget, false)
#define with_MainMenuBar IMGUI_SUGAR_SCOPED_BOOL_0(BeginMainMenuBar, EndMainMenuBar, false)
#define with_MenuBar IMGUI_SUGAR_SCOPED_BOOL_0(BeginMenuBar, EndMenuBar, false)

#define with_Group IMGUI_SUGAR_SCOPED_VOID_0(BeginGroup, EndGroup)
#define with_Tooltip IMGUI_SUGAR_SCOPED_VOID_0(BeginTooltip, EndTooltip)

#define with_Font(...) IMGUI_SUGAR_SCOPED_VOID_N(PushFont, PopFont, __VA_ARGS__)
#define with_AllowKeyboardFocus(...) IMGUI_SUGAR_SCOPED_VOID_N(PushAllowKeyboardFocus, PopAllowKeyboardFocus, __VA_ARGS__)
#define with_ButtonRepeat(...) IMGUI_SUGAR_SCOPED_VOID_N(PushButtonRepeat, PopButtonRepeat, __VA_ARGS__)
#define with_ItemWidth(...) IMGUI_SUGAR_SCOPED_VOID_N(PushItemWidth, PopItemWidth, __VA_ARGS__)
#define with_TextWrapPos(...) IMGUI_SUGAR_SCOPED_VOID_N(PushTextWrapPos, PopTextWrapPos, __VA_ARGS__)
#define with_ID(...) IMGUI_SUGAR_SCOPED_VOID_N(PushID, PopID, __VA_ARGS__)
#define with_ClipRect(...) IMGUI_SUGAR_SCOPED_VOID_N(PushClipRect, PopClipRect, __VA_ARGS__)
#define with_TextureID(...) IMGUI_SUGAR_SCOPED_VOID_N(PushTextureID, PopTextureID, __VA_ARGS__)

// Non self scoped guards (managed by parent scope)

#define set_Font(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushFont, PopFont, __VA_ARGS__)
#define set_AllowKeyboardFocus(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushAllowKeyboardFocus, PopAllowKeyboardFocus, __VA_ARGS__)
#define set_ButtonRepeat(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushButtonRepeat, PopButtonRepeat, __VA_ARGS__)
#define set_ItemWidth(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushItemWidth, PopItemWidth, __VA_ARGS__)
#define set_TextWrapPos(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushTextWrapPos, PopTextWrapPos, __VA_ARGS__)
#define set_ID(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushID, PopID, __VA_ARGS__)
#define set_ClipRect(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushClipRect, PopClipRect, __VA_ARGS__)
#define set_TextureID(...) _IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushTextureID, PopTextureID, __VA_ARGS__)
#define set_Font(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushFont, PopFont, __VA_ARGS__)
#define set_AllowKeyboardFocus(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushAllowKeyboardFocus, PopAllowKeyboardFocus, __VA_ARGS__)
#define set_ButtonRepeat(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushButtonRepeat, PopButtonRepeat, __VA_ARGS__)
#define set_ItemWidth(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushItemWidth, PopItemWidth, __VA_ARGS__)
#define set_TextWrapPos(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushTextWrapPos, PopTextWrapPos, __VA_ARGS__)
#define set_ID(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushID, PopID, __VA_ARGS__)
#define set_ClipRect(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushClipRect, PopClipRect, __VA_ARGS__)
#define set_TextureID(...) IMGUI_SUGAR_PARENT_SCOPED_VOID_N(PushTextureID, PopTextureID, __VA_ARGS__)

// Special case (overloaded functions StyleColor and StyleVar)

#define set_StyleColor(...) \
const ImGuiSugar::BooleanGuard<true> \
_IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{_IMGUI_SUGAR_ES(ImGui::PushStyleColor, __VA_ARGS__), &ImGuiSugar::PopStyleColor}
IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{IMGUI_SUGAR_ES(ImGui::PushStyleColor, __VA_ARGS__), &ImGuiSugar::PopStyleColor}

#define set_StyleVar(...) \
const ImGuiSugar::BooleanGuard<true> \
_IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{_IMGUI_SUGAR_ES(ImGui::PushStyleVar, __VA_ARGS__), &ImGuiSugar::PopStyleVar}
IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{IMGUI_SUGAR_ES(ImGui::PushStyleVar, __VA_ARGS__), &ImGuiSugar::PopStyleVar}

#define with_StyleColor(...) \
if (const ImGuiSugar::BooleanGuard<true> \
_IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{_IMGUI_SUGAR_ES(ImGui::PushStyleColor, __VA_ARGS__), &ImGuiSugar::PopStyleColor})
IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{IMGUI_SUGAR_ES(ImGui::PushStyleColor, __VA_ARGS__), &ImGuiSugar::PopStyleColor})

#define with_StyleVar(...) \
if (const ImGuiSugar::BooleanGuard<true> \
_IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{_IMGUI_SUGAR_ES(ImGui::PushStyleVar, __VA_ARGS__), &ImGuiSugar::PopStyleVar})
IMGUI_SUGAR_CONCAT1(_ui_scope_, __LINE__) = \
{IMGUI_SUGAR_ES(ImGui::PushStyleVar, __VA_ARGS__), &ImGuiSugar::PopStyleVar})

// Non RAII

Expand Down

0 comments on commit f0d8a03

Please sign in to comment.