Skip to content

Commit

Permalink
ADAPT_IMGUI_BUNDLE: adapt ImVector bindings (minimum bindings to make…
Browse files Browse the repository at this point in the history
… it iterable in python)
  • Loading branch information
pthom committed Nov 3, 2023
1 parent 44b0637 commit c956f43
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,8 @@ IM_MSVC_RUNTIME_CHECKS_OFF
template<typename T>
struct ImVector
{
#ifdef IMGUI_BUNDLE_PYTHON_UNSUPPORTED_API
// [ADAPT_IMGUI_BUNDLE]: raw members are not published
int Size;
int Capacity;
T* Data;
Expand All @@ -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<T>& src) { Size = Capacity = 0; Data = NULL; operator=(src); }
inline ImVector<T>& operator=(const ImVector<T>& 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; }
Expand All @@ -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; }
Expand All @@ -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

Expand Down

0 comments on commit c956f43

Please sign in to comment.