From 182115409ab3d4c76ea125c538c46ed38c332162 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 18 Mar 2020 12:20:53 +0100 Subject: [PATCH] Internals: added ImSpan helper structure + 2020/10/01 stricter bound checking --- imgui_internal.h | 29 +++++++++++++++++++++++++++++ misc/natvis/imgui.natvis | 10 ++++++++++ 2 files changed, 39 insertions(+) diff --git a/imgui_internal.h b/imgui_internal.h index 3c6009d0..12e67fda 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -249,6 +249,7 @@ namespace ImStb // - Helper: ImRect // - Helper: ImBitArray // - Helper: ImBitVector +// - Helper: ImSpan<> // - Helper: ImPool<> // - Helper: ImChunkStream<> //----------------------------------------------------------------------------- @@ -502,6 +503,34 @@ struct IMGUI_API ImBitVector void ClearBit(int n) { IM_ASSERT(n < (Storage.Size << 5)); ImBitArrayClearBit(Storage.Data, n); } }; +// Helper: ImSpan<> +// Pointing to a span of data we don't own. +template +struct ImSpan +{ + T* Data; + T* DataEnd; + + // Constructors, destructor + inline ImSpan() { Data = DataEnd = NULL; } + inline ImSpan(T* data, int size) { Data = data; DataEnd = data + size; } + inline ImSpan(T* data, T* data_end) { Data = data; DataEnd = data_end; } + + inline void set(T* data, int size) { Data = data; DataEnd = data + size; } + inline void set(T* data, T* data_end) { Data = data; DataEnd = data_end; } + inline int size() const { return (int)(ptrdiff_t)(DataEnd - Data); } + inline T& operator[](int i) { T* p = Data + i; IM_ASSERT(p >= Data && p < DataEnd); return *p; } + inline const T& operator[](int i) const { const T* p = Data + i; IM_ASSERT(p >= Data && p < DataEnd); return *p; } + + inline T* begin() { return Data; } + inline const T* begin() const { return Data; } + inline T* end() { return DataEnd; } + inline const T* end() const { return DataEnd; } + + // Utilities + inline int index_from_ptr(const T* it) const { IM_ASSERT(it >= Data && it < DataEnd); const ptrdiff_t off = it - Data; return (int)off; } +}; + // Helper: ImPool<> // Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID over a dense/hot buffer, // Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object. diff --git a/misc/natvis/imgui.natvis b/misc/natvis/imgui.natvis index cc768bfb..25d72fb6 100644 --- a/misc/natvis/imgui.natvis +++ b/misc/natvis/imgui.natvis @@ -13,6 +13,16 @@ + + + {{Size={DataEnd-Data} }} + + + DataEnd-Data + Data + + + {{x={x,g} y={y,g}}}