Skip to content

Commit

Permalink
Internals: added ImSpan helper structure + 2020/10/01 stricter bound …
Browse files Browse the repository at this point in the history
…checking
  • Loading branch information
ocornut committed Dec 4, 2020
1 parent 054c670 commit 1821154
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions imgui_internal.h
Expand Up @@ -249,6 +249,7 @@ namespace ImStb
// - Helper: ImRect
// - Helper: ImBitArray
// - Helper: ImBitVector
// - Helper: ImSpan<>
// - Helper: ImPool<>
// - Helper: ImChunkStream<>
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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<typename T>
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.
Expand Down
10 changes: 10 additions & 0 deletions misc/natvis/imgui.natvis
Expand Up @@ -13,6 +13,16 @@
</ArrayItems>
</Expand>
</Type>

<Type Name="ImSpan&lt;*&gt;">
<DisplayString>{{Size={DataEnd-Data} }}</DisplayString>
<Expand>
<ArrayItems>
<Size>DataEnd-Data</Size>
<ValuePointer>Data</ValuePointer>
</ArrayItems>
</Expand>
</Type>

<Type Name="ImVec2">
<DisplayString>{{x={x,g} y={y,g}}}</DisplayString>
Expand Down

0 comments on commit 1821154

Please sign in to comment.