Skip to content

Commit da53caf

Browse files
committed
Moved ImVector<> after the main block (#219)
1 parent b6f3c97 commit da53caf

File tree

1 file changed

+64
-66
lines changed

1 file changed

+64
-66
lines changed

imgui.h

+64-66
Original file line numberDiff line numberDiff line change
@@ -73,73 +73,8 @@ struct ImVec4
7373
#endif
7474
};
7575

76-
namespace ImGui
77-
{
78-
// Proxy functions to access the MemAllocFn/MemFreeFn pointers in ImGui::GetIO(). The only reason they exist here is to allow ImVector<> to compile inline.
79-
IMGUI_API void* MemAlloc(size_t sz);
80-
IMGUI_API void MemFree(void* ptr);
81-
}
82-
83-
// Lightweight std::vector<> like class to avoid dragging dependencies (also: windows implementation of STL with debug enabled is absurdly slow, so let's bypass it so our code runs fast in debug).
84-
// Use '#define ImVector std::vector' if you want to use the STL type or your own type.
85-
// Our implementation does NOT call c++ constructors because we don't use them in ImGui. Don't use this class as a straight std::vector replacement in your code!
86-
#ifndef ImVector
87-
template<typename T>
88-
class ImVector
89-
{
90-
protected:
91-
size_t Size;
92-
size_t Capacity;
93-
T* Data;
94-
95-
public:
96-
typedef T value_type;
97-
typedef value_type* iterator;
98-
typedef const value_type* const_iterator;
99-
100-
ImVector() { Size = Capacity = 0; Data = NULL; }
101-
~ImVector() { if (Data) ImGui::MemFree(Data); }
102-
103-
inline bool empty() const { return Size == 0; }
104-
inline size_t size() const { return Size; }
105-
inline size_t capacity() const { return Capacity; }
106-
107-
inline value_type& at(size_t i) { IM_ASSERT(i < Size); return Data[i]; }
108-
inline const value_type& at(size_t i) const { IM_ASSERT(i < Size); return Data[i]; }
109-
inline value_type& operator[](size_t i) { IM_ASSERT(i < Size); return Data[i]; }
110-
inline const value_type& operator[](size_t i) const { IM_ASSERT(i < Size); return Data[i]; }
111-
112-
inline void clear() { if (Data) { Size = Capacity = 0; ImGui::MemFree(Data); Data = NULL; } }
113-
inline iterator begin() { return Data; }
114-
inline const_iterator begin() const { return Data; }
115-
inline iterator end() { return Data + Size; }
116-
inline const_iterator end() const { return Data + Size; }
117-
inline value_type& front() { IM_ASSERT(Size > 0); return Data[0]; }
118-
inline const value_type& front() const { IM_ASSERT(Size > 0); return Data[0]; }
119-
inline value_type& back() { IM_ASSERT(Size > 0); return Data[Size-1]; }
120-
inline const value_type& back() const { IM_ASSERT(Size > 0); return Data[Size-1]; }
121-
inline void swap(ImVector<T>& rhs) { const size_t rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; const size_t rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; value_type* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; }
122-
123-
inline void resize(size_t new_size) { if (new_size > Capacity) reserve(new_size); Size = new_size; }
124-
inline void reserve(size_t new_capacity)
125-
{
126-
if (new_capacity <= Capacity) return;
127-
T* new_data = (value_type*)ImGui::MemAlloc(new_capacity * sizeof(value_type));
128-
memcpy(new_data, Data, Size * sizeof(value_type));
129-
ImGui::MemFree(Data);
130-
Data = new_data;
131-
Capacity = new_capacity;
132-
}
133-
134-
inline void push_back(const value_type& v) { if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); Data[Size++] = v; }
135-
inline void pop_back() { IM_ASSERT(Size > 0); Size--; }
136-
137-
inline iterator erase(const_iterator it) { IM_ASSERT(it >= begin() && it < end()); const ptrdiff_t off = it - begin(); memmove(Data + off, Data + off + 1, (Size - (size_t)off - 1) * sizeof(value_type)); Size--; return Data + off; }
138-
inline iterator insert(const_iterator it, const value_type& v) { IM_ASSERT(it >= begin() && it <= end()); const ptrdiff_t off = it - begin(); if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); if (off < (int)Size) memmove(Data + off + 1, Data + off, (Size - (size_t)off) * sizeof(value_type)); Data[off] = v; Size++; return Data + off; }
139-
};
140-
#endif // #ifndef ImVector
141-
14276
// Helpers at bottom of the file:
77+
// - class ImVector<> // Lightweight std::vector like class. Use '#define ImVector std::vector' if you want to use the STL type or your own type.
14378
// - IMGUI_ONCE_UPON_A_FRAME // Execute a block of code once per frame only (convenient for creating UI within deep-nested code that runs multiple times)
14479
// - struct ImGuiTextFilter // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
14580
// - struct ImGuiTextBuffer // Text buffer for logging/accumulating text
@@ -424,6 +359,10 @@ namespace ImGui
424359
IMGUI_API void ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v);
425360
IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b);
426361

362+
// Proxy functions to access the MemAllocFn/MemFreeFn pointers in ImGui::GetIO()
363+
IMGUI_API void* MemAlloc(size_t sz);
364+
IMGUI_API void MemFree(void* ptr);
365+
427366
// Internal state access - if you want to share ImGui state between modules (e.g. DLL) or allocate it yourself
428367
IMGUI_API const char* GetVersion();
429368
IMGUI_API void* GetInternalState();
@@ -727,6 +666,65 @@ struct ImGuiIO
727666
// Helpers
728667
//-----------------------------------------------------------------------------
729668

669+
// Lightweight std::vector<> like class to avoid dragging dependencies (also: windows implementation of STL with debug enabled is absurdly slow, so let's bypass it so our code runs fast in debug).
670+
// Use '#define ImVector std::vector' if you want to use the STL type or your own type.
671+
// Our implementation does NOT call c++ constructors because we don't use them in ImGui. Don't use this class as a straight std::vector replacement in your code!
672+
#ifndef ImVector
673+
template<typename T>
674+
class ImVector
675+
{
676+
protected:
677+
size_t Size;
678+
size_t Capacity;
679+
T* Data;
680+
681+
public:
682+
typedef T value_type;
683+
typedef value_type* iterator;
684+
typedef const value_type* const_iterator;
685+
686+
ImVector() { Size = Capacity = 0; Data = NULL; }
687+
~ImVector() { if (Data) ImGui::MemFree(Data); }
688+
689+
inline bool empty() const { return Size == 0; }
690+
inline size_t size() const { return Size; }
691+
inline size_t capacity() const { return Capacity; }
692+
693+
inline value_type& at(size_t i) { IM_ASSERT(i < Size); return Data[i]; }
694+
inline const value_type& at(size_t i) const { IM_ASSERT(i < Size); return Data[i]; }
695+
inline value_type& operator[](size_t i) { IM_ASSERT(i < Size); return Data[i]; }
696+
inline const value_type& operator[](size_t i) const { IM_ASSERT(i < Size); return Data[i]; }
697+
698+
inline void clear() { if (Data) { Size = Capacity = 0; ImGui::MemFree(Data); Data = NULL; } }
699+
inline iterator begin() { return Data; }
700+
inline const_iterator begin() const { return Data; }
701+
inline iterator end() { return Data + Size; }
702+
inline const_iterator end() const { return Data + Size; }
703+
inline value_type& front() { IM_ASSERT(Size > 0); return Data[0]; }
704+
inline const value_type& front() const { IM_ASSERT(Size > 0); return Data[0]; }
705+
inline value_type& back() { IM_ASSERT(Size > 0); return Data[Size-1]; }
706+
inline const value_type& back() const { IM_ASSERT(Size > 0); return Data[Size-1]; }
707+
inline void swap(ImVector<T>& rhs) { const size_t rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; const size_t rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; value_type* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; }
708+
709+
inline void resize(size_t new_size) { if (new_size > Capacity) reserve(new_size); Size = new_size; }
710+
inline void reserve(size_t new_capacity)
711+
{
712+
if (new_capacity <= Capacity) return;
713+
T* new_data = (value_type*)ImGui::MemAlloc(new_capacity * sizeof(value_type));
714+
memcpy(new_data, Data, Size * sizeof(value_type));
715+
ImGui::MemFree(Data);
716+
Data = new_data;
717+
Capacity = new_capacity;
718+
}
719+
720+
inline void push_back(const value_type& v) { if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); Data[Size++] = v; }
721+
inline void pop_back() { IM_ASSERT(Size > 0); Size--; }
722+
723+
inline iterator erase(const_iterator it) { IM_ASSERT(it >= begin() && it < end()); const ptrdiff_t off = it - begin(); memmove(Data + off, Data + off + 1, (Size - (size_t)off - 1) * sizeof(value_type)); Size--; return Data + off; }
724+
inline iterator insert(const_iterator it, const value_type& v) { IM_ASSERT(it >= begin() && it <= end()); const ptrdiff_t off = it - begin(); if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); if (off < (int)Size) memmove(Data + off + 1, Data + off, (Size - (size_t)off) * sizeof(value_type)); Data[off] = v; Size++; return Data + off; }
725+
};
726+
#endif // #ifndef ImVector
727+
730728
// Helper: execute a block of code once a frame only
731729
// Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame.
732730
// Usage:

0 commit comments

Comments
 (0)