From c956f43a55398b1a8d2339c09f550b0cc3b556d9 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Fri, 3 Nov 2023 01:02:00 +0100 Subject: [PATCH] ADAPT_IMGUI_BUNDLE: adapt ImVector bindings (minimum bindings to make it iterable in python) --- imgui.h | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/imgui.h b/imgui.h index 6aa1e678f40a..c5ecc7ff825c 100644 --- a/imgui.h +++ b/imgui.h @@ -1935,6 +1935,8 @@ IM_MSVC_RUNTIME_CHECKS_OFF template struct ImVector { +#ifdef IMGUI_BUNDLE_PYTHON_UNSUPPORTED_API +// [ADAPT_IMGUI_BUNDLE]: raw members are not published int Size; int Capacity; T* Data; @@ -1943,25 +1945,39 @@ struct ImVector typedef T value_type; typedef value_type* iterator; typedef const value_type* const_iterator; - - // Constructors, destructor +// [/ADAPT_IMGUI_BUNDLE] +#endif inline ImVector() { Size = Capacity = 0; Data = NULL; } inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); } inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); if (src.Data) memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } 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. + // Important: does not destruct anything + inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } +#ifdef IMGUI_BUNDLE_PYTHON_UNSUPPORTED_API +// [ADAPT_IMGUI_BUNDLE]: clear_delete is not published + // Important: never called automatically! always explicit. + inline void clear_delete() { for (int n = 0; n < Size; n++) IM_DELETE(Data[n]); clear(); } +// [/ADAPT_IMGUI_BUNDLE] +#endif + // Important: never called automatically! always explicit. + inline void clear_destruct() { for (int n = 0; n < Size; n++) Data[n].~T(); clear(); } inline bool empty() const { return Size == 0; } inline int size() const { return Size; } +#ifdef IMGUI_BUNDLE_PYTHON_UNSUPPORTED_API +// [ADAPT_IMGUI_BUNDLE]: unpublished methods inline int size_in_bytes() const { return Size * (int)sizeof(T); } inline int max_size() const { return 0x7FFFFFFF / (int)sizeof(T); } inline int capacity() const { return Capacity; } - inline T& operator[](int i) { IM_ASSERT(i >= 0 && i < Size); return Data[i]; } +// [/ADAPT_IMGUI_BUNDLE] +#endif + inline const T& operator[](int i) const { IM_ASSERT(i >= 0 && i < Size); return Data[i]; } + inline T& operator[](int i) { IM_ASSERT(i >= 0 && i < Size); return Data[i]; } +#ifdef IMGUI_BUNDLE_PYTHON_UNSUPPORTED_API +// [ADAPT_IMGUI_BUNDLE]: unpublished methods inline T* begin() { return Data; } inline const T* begin() const { return Data; } inline T* end() { return Data + Size; } @@ -1978,11 +1994,16 @@ struct ImVector inline void shrink(int new_size) { IM_ASSERT(new_size <= Size); Size = new_size; } // Resize a vector to a smaller size, guaranteed not to cause a reallocation inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); IM_FREE(Data); } Data = new_data; Capacity = new_capacity; } inline void reserve_discard(int new_capacity) { if (new_capacity <= Capacity) return; if (Data) IM_FREE(Data); Data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); Capacity = new_capacity; } +// [/ADAPT_IMGUI_BUNDLE] +#endif // NB: It is illegal to call push_back/push_front/insert with a reference pointing inside the ImVector data itself! e.g. v.push_back(v[10]) is forbidden. inline void push_back(const T& v) { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; } inline void pop_back() { IM_ASSERT(Size > 0); Size--; } inline void push_front(const T& v) { if (Size == 0) push_back(v); else insert(Data, v); } + +#ifdef IMGUI_BUNDLE_PYTHON_UNSUPPORTED_API +// [ADAPT_IMGUI_BUNDLE]: unpublished methods inline T* erase(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + 1, ((size_t)Size - (size_t)off - 1) * sizeof(T)); Size--; return Data + off; } inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data + Size && it_last >= it && it_last <= Data + Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - (size_t)count) * sizeof(T)); Size -= (int)count; return Data + off; } inline T* erase_unsorted(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; if (it < Data + Size - 1) memcpy(Data + off, Data + Size - 1, sizeof(T)); Size--; return Data + off; } @@ -1994,6 +2015,8 @@ struct ImVector inline bool find_erase(const T& v) { const T* it = find(v); if (it < Data + Size) { erase(it); return true; } return false; } inline bool find_erase_unsorted(const T& v) { const T* it = find(v); if (it < Data + Size) { erase_unsorted(it); return true; } return false; } inline int index_from_ptr(const T* it) const { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; return (int)off; } +// [/ADAPT_IMGUI_BUNDLE] +#endif }; IM_MSVC_RUNTIME_CHECKS_RESTORE