Skip to content

Commit

Permalink
ImVector: added clear_delete(), clear_destruct() helpers.
Browse files Browse the repository at this point in the history
# Conflicts:
#	imgui.cpp
  • Loading branch information
ocornut committed Jun 9, 2021
1 parent 865b2ca commit 4161a67
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
15 changes: 4 additions & 11 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2971,8 +2971,7 @@ ImGuiWindow::~ImGuiWindow()
{
IM_ASSERT(DrawList == &DrawListInst);
IM_DELETE(Name);
for (int i = 0; i != ColumnsStorage.Size; i++)
ColumnsStorage[i].~ImGuiOldColumns();
ColumnsStorage.clear_destruct();
}

ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end)
Expand Down Expand Up @@ -4343,9 +4342,7 @@ void ImGui::Shutdown(ImGuiContext* context)
CallContextHooks(&g, ImGuiContextHookType_Shutdown);

// Clear everything else
for (int i = 0; i < g.Windows.Size; i++)
IM_DELETE(g.Windows[i]);
g.Windows.clear();
g.Windows.clear_delete();
g.WindowsFocusOrder.clear();
g.WindowsTempSortBuffer.clear();
g.CurrentWindow = NULL;
Expand All @@ -4362,18 +4359,14 @@ void ImGui::Shutdown(ImGuiContext* context)
g.BeginPopupStack.clear();

g.CurrentViewport = g.MouseViewport = g.MouseLastHoveredViewport = NULL;
for (int i = 0; i < g.Viewports.Size; i++)
IM_DELETE(g.Viewports[i]);
g.Viewports.clear();
g.Viewports.clear_delete();

g.TabBars.Clear();
g.CurrentTabBarStack.clear();
g.ShrinkWidthBuffer.clear();

g.Tables.Clear();
for (int i = 0; i < g.TablesTempDataStack.Size; i++)
g.TablesTempDataStack[i].~ImGuiTableTempData();
g.TablesTempDataStack.clear();
g.TablesTempDataStack.clear_destruct();
g.DrawChannelsTempMergeBuffer.clear();

g.ClipboardHandlerData.clear();
Expand Down
9 changes: 6 additions & 3 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Index of this file:
// Version
// (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens)
#define IMGUI_VERSION "1.84 WIP"
#define IMGUI_VERSION_NUM 18306
#define IMGUI_VERSION_NUM 18307
#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx))
#define IMGUI_HAS_TABLE
#define IMGUI_HAS_VIEWPORT // Viewport WIP branch
Expand Down Expand Up @@ -1743,7 +1743,11 @@ struct ImVector
inline ImVector() { Size = Capacity = 0; Data = NULL; }
inline ImVector(const ImVector<T>& src) { Size = Capacity = 0; Data = NULL; operator=(src); }
inline ImVector<T>& operator=(const ImVector<T>& src) { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; }
inline ~ImVector() { if (Data) IM_FREE(Data); }
inline ~ImVector() { if (Data) IM_FREE(Data); } // Important: does not destruct anything

inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } // Important: does not destruct anything
inline void clear_delete() { for (int n = 0; n < Size; n++) IM_DELETE(Data[n]); clear(); } // Important: never called automatically! always explicit.
inline void clear_destruct() { for (int n = 0; n < Size; n++) Data[n].~T(); clear(); } // Important: never called automatically! always explicit.

inline bool empty() const { return Size == 0; }
inline int size() const { return Size; }
Expand All @@ -1753,7 +1757,6 @@ struct ImVector
inline T& operator[](int i) { IM_ASSERT(i >= 0 && i < Size); return Data[i]; }
inline const T& operator[](int i) const { IM_ASSERT(i >= 0 && i < Size); return Data[i]; }

inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } }
inline T* begin() { return Data; }
inline const T* begin() const { return Data; }
inline T* end() { return Data + Size; }
Expand Down
9 changes: 3 additions & 6 deletions imgui_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2012,9 +2012,7 @@ void ImFontAtlas::ClearTexData()
void ImFontAtlas::ClearFonts()
{
IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!");
for (int i = 0; i < Fonts.Size; i++)
IM_DELETE(Fonts[i]);
Fonts.clear();
Fonts.clear_delete();
}

void ImFontAtlas::Clear()
Expand Down Expand Up @@ -2574,9 +2572,8 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)
}
}

// Cleanup temporary (ImVector doesn't honor destructor)
for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
src_tmp_array[src_i].~ImFontBuildSrcData();
// Cleanup
src_tmp_array.clear_destruct();

ImFontAtlasBuildFinish(atlas);
return true;
Expand Down
3 changes: 1 addition & 2 deletions misc/freetype/imgui_freetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,7 @@ bool ImFontAtlasBuildWithFreeTypeEx(FT_Library ft_library, ImFontAtlas* atlas, u
// Cleanup
for (int buf_i = 0; buf_i < buf_bitmap_buffers.Size; buf_i++)
IM_FREE(buf_bitmap_buffers[buf_i]);
for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)
src_tmp_array[src_i].~ImFontBuildSrcDataFT();
src_tmp_array.clear_destruct();

ImFontAtlasBuildFinish(atlas);

Expand Down

0 comments on commit 4161a67

Please sign in to comment.