From 595684e08c6b304bfb22b38a1679f64b05415046 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 25 Jan 2016 21:26:53 +0000 Subject: [PATCH] Remove dependency on header (#484, #504) (Now expecting something to fail somewhere..) --- imgui.cpp | 10 +++++----- imgui_draw.cpp | 6 +++--- imgui_internal.h | 9 +++++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 324f9868..f9bd86e8 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -529,13 +529,13 @@ #include "imgui.h" #define IMGUI_DEFINE_MATH_OPERATORS +#define IMGUI_DEFINE_PLACEMENT_NEW #include "imgui_internal.h" #include // toupper, isprint #include // sqrtf, fabsf, fmodf, powf, cosf, sinf, floorf, ceilf #include // NULL, malloc, free, qsort, atoi #include // vsnprintf, sscanf, printf -#include // new (ptr) #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier #include // intptr_t #else @@ -1555,7 +1555,7 @@ ImGuiWindow::ImGuiWindow(const char* name) FontWindowScale = 1.0f; DrawList = (ImDrawList*)ImGui::MemAlloc(sizeof(ImDrawList)); - new(DrawList) ImDrawList(); + IM_PLACEMENT_NEW(DrawList) ImDrawList(); DrawList->_OwnerName = Name; RootWindow = NULL; RootNonPopupWindow = NULL; @@ -1829,7 +1829,7 @@ size_t ImGui::GetInternalStateSize() void ImGui::SetInternalState(void* state, bool construct) { if (construct) - new (state) ImGuiState(); + IM_PLACEMENT_NEW(state) ImGuiState(); GImGui = (ImGuiState*)state; } @@ -1874,7 +1874,7 @@ void ImGui::NewFrame() { // Initialize on first frame g.LogClipboard = (ImGuiTextBuffer*)ImGui::MemAlloc(sizeof(ImGuiTextBuffer)); - new(g.LogClipboard) ImGuiTextBuffer(); + IM_PLACEMENT_NEW(g.LogClipboard) ImGuiTextBuffer(); IM_ASSERT(g.Settings.empty()); LoadSettings(); @@ -3461,7 +3461,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFl // Create window the first time ImGuiWindow* window = (ImGuiWindow*)ImGui::MemAlloc(sizeof(ImGuiWindow)); - new(window) ImGuiWindow(name); + IM_PLACEMENT_NEW(window) ImGuiWindow(name); window->Flags = flags; if (flags & ImGuiWindowFlags_NoSavedSettings) diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 32801430..ea521be2 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -14,10 +14,10 @@ #include "imgui.h" #define IMGUI_DEFINE_MATH_OPERATORS +#define IMGUI_DEFINE_PLACEMENT_NEW #include "imgui_internal.h" #include // vsnprintf, sscanf, printf -#include // new (ptr) #if !defined(alloca) && !defined(__FreeBSD__) #ifdef _WIN32 #include // alloca @@ -266,7 +266,7 @@ void ImDrawList::ChannelsSplit(int channels_count) { if (i >= old_channels_count) { - new(&_Channels[i]) ImDrawChannel(); + IM_PLACEMENT_NEW(&_Channels[i]) ImDrawChannel(); } else { @@ -1097,7 +1097,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) if (!font_cfg->MergeMode) { ImFont* font = (ImFont*)ImGui::MemAlloc(sizeof(ImFont)); - new (font) ImFont(); + IM_PLACEMENT_NEW(font) ImFont(); Fonts.push_back(font); } diff --git a/imgui_internal.h b/imgui_internal.h index b100111a..0ffb5c5f 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -137,6 +137,15 @@ static inline float ImLengthSqr(const ImVec4& lhs) static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / sqrtf(d); return fail_value; } static inline ImVec2 ImRound(ImVec2 v) { return ImVec2((float)(int)v.x, (float)(int)v.y); } +// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. +// Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. +#ifdef IMGUI_DEFINE_PLACEMENT_NEW +struct ImPlacementNewDummy {}; +static inline void* operator new(size_t, ImPlacementNewDummy, void* ptr) { return ptr; } +static inline void operator delete(void*, ImPlacementNewDummy, void*) {} +#define IM_PLACEMENT_NEW(_PTR) new(ImPlacementNewDummy() ,_PTR) +#endif + //----------------------------------------------------------------------------- // Types //-----------------------------------------------------------------------------