瀏覽代碼

Moved ImVector<> after the main block (#219)

ocornut 10 年之前
父節點
當前提交
da53caf310
共有 1 個文件被更改,包括 64 次插入66 次删除
  1. 64 66
      imgui.h

+ 64 - 66
imgui.h

@@ -73,73 +73,8 @@ struct ImVec4
 #endif
 };
 
-namespace ImGui
-{
-    // Proxy functions to access the MemAllocFn/MemFreeFn pointers in ImGui::GetIO(). The only reason they exist here is to allow ImVector<> to compile inline.
-    IMGUI_API void*       MemAlloc(size_t sz);
-    IMGUI_API void        MemFree(void* ptr);
-}
-
-// 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). 
-// Use '#define ImVector std::vector' if you want to use the STL type or your own type.
-// 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!
-#ifndef ImVector
-template<typename T>
-class ImVector
-{
-protected:
-    size_t                      Size;
-    size_t                      Capacity;
-    T*                          Data;
-
-public:
-    typedef T                   value_type;
-    typedef value_type*         iterator;
-    typedef const value_type*   const_iterator;
-
-    ImVector()                  { Size = Capacity = 0; Data = NULL; }
-    ~ImVector()                 { if (Data) ImGui::MemFree(Data); }
-
-    inline bool                 empty() const                   { return Size == 0; }
-    inline size_t               size() const                    { return Size; }
-    inline size_t               capacity() const                { return Capacity; }
-
-    inline value_type&          at(size_t i)                    { IM_ASSERT(i < Size); return Data[i]; }
-    inline const value_type&    at(size_t i) const              { IM_ASSERT(i < Size); return Data[i]; }
-    inline value_type&          operator[](size_t i)            { IM_ASSERT(i < Size); return Data[i]; }
-    inline const value_type&    operator[](size_t i) const      { IM_ASSERT(i < Size); return Data[i]; }
-
-    inline void                 clear()                         { if (Data) { Size = Capacity = 0; ImGui::MemFree(Data); Data = NULL; } }
-    inline iterator             begin()                         { return Data; }
-    inline const_iterator       begin() const                   { return Data; }
-    inline iterator             end()                           { return Data + Size; }
-    inline const_iterator       end() const                     { return Data + Size; }
-    inline value_type&          front()                         { IM_ASSERT(Size > 0); return Data[0]; }
-    inline const value_type&    front() const                   { IM_ASSERT(Size > 0); return Data[0]; }
-    inline value_type&          back()                          { IM_ASSERT(Size > 0); return Data[Size-1]; }
-    inline const value_type&    back() const                    { IM_ASSERT(Size > 0); return Data[Size-1]; }
-    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; }
-
-    inline void                 resize(size_t new_size)         { if (new_size > Capacity) reserve(new_size); Size = new_size; }
-    inline void                 reserve(size_t new_capacity)    
-    { 
-        if (new_capacity <= Capacity) return;
-        T* new_data = (value_type*)ImGui::MemAlloc(new_capacity * sizeof(value_type));
-        memcpy(new_data, Data, Size * sizeof(value_type));
-        ImGui::MemFree(Data);
-        Data = new_data;
-        Capacity = new_capacity; 
-    }
-
-    inline void                 push_back(const value_type& v)  { if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); Data[Size++] = v; }
-    inline void                 pop_back()                      { IM_ASSERT(Size > 0); Size--; }
-
-    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; }
-    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; }
-};
-#endif // #ifndef ImVector
-
 // Helpers at bottom of the file:
+// - class ImVector<>                   // Lightweight std::vector like class. Use '#define ImVector std::vector' if you want to use the STL type or your own type.
 // - 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)
 // - struct ImGuiTextFilter             // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
 // - struct ImGuiTextBuffer             // Text buffer for logging/accumulating text
@@ -424,6 +359,10 @@ namespace ImGui
     IMGUI_API void          ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v);
     IMGUI_API void          ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b);
 
+    // Proxy functions to access the MemAllocFn/MemFreeFn pointers in ImGui::GetIO()
+    IMGUI_API void*         MemAlloc(size_t sz);
+    IMGUI_API void          MemFree(void* ptr);
+
     // Internal state access - if you want to share ImGui state between modules (e.g. DLL) or allocate it yourself
     IMGUI_API const char*   GetVersion();
     IMGUI_API void*         GetInternalState();
@@ -727,6 +666,65 @@ struct ImGuiIO
 // Helpers
 //-----------------------------------------------------------------------------
 
+// 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). 
+// Use '#define ImVector std::vector' if you want to use the STL type or your own type.
+// 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!
+#ifndef ImVector
+template<typename T>
+class ImVector
+{
+protected:
+    size_t                      Size;
+    size_t                      Capacity;
+    T*                          Data;
+
+public:
+    typedef T                   value_type;
+    typedef value_type*         iterator;
+    typedef const value_type*   const_iterator;
+
+    ImVector()                  { Size = Capacity = 0; Data = NULL; }
+    ~ImVector()                 { if (Data) ImGui::MemFree(Data); }
+
+    inline bool                 empty() const                   { return Size == 0; }
+    inline size_t               size() const                    { return Size; }
+    inline size_t               capacity() const                { return Capacity; }
+
+    inline value_type&          at(size_t i)                    { IM_ASSERT(i < Size); return Data[i]; }
+    inline const value_type&    at(size_t i) const              { IM_ASSERT(i < Size); return Data[i]; }
+    inline value_type&          operator[](size_t i)            { IM_ASSERT(i < Size); return Data[i]; }
+    inline const value_type&    operator[](size_t i) const      { IM_ASSERT(i < Size); return Data[i]; }
+
+    inline void                 clear()                         { if (Data) { Size = Capacity = 0; ImGui::MemFree(Data); Data = NULL; } }
+    inline iterator             begin()                         { return Data; }
+    inline const_iterator       begin() const                   { return Data; }
+    inline iterator             end()                           { return Data + Size; }
+    inline const_iterator       end() const                     { return Data + Size; }
+    inline value_type&          front()                         { IM_ASSERT(Size > 0); return Data[0]; }
+    inline const value_type&    front() const                   { IM_ASSERT(Size > 0); return Data[0]; }
+    inline value_type&          back()                          { IM_ASSERT(Size > 0); return Data[Size-1]; }
+    inline const value_type&    back() const                    { IM_ASSERT(Size > 0); return Data[Size-1]; }
+    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; }
+
+    inline void                 resize(size_t new_size)         { if (new_size > Capacity) reserve(new_size); Size = new_size; }
+    inline void                 reserve(size_t new_capacity)    
+    { 
+        if (new_capacity <= Capacity) return;
+        T* new_data = (value_type*)ImGui::MemAlloc(new_capacity * sizeof(value_type));
+        memcpy(new_data, Data, Size * sizeof(value_type));
+        ImGui::MemFree(Data);
+        Data = new_data;
+        Capacity = new_capacity; 
+    }
+
+    inline void                 push_back(const value_type& v)  { if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); Data[Size++] = v; }
+    inline void                 pop_back()                      { IM_ASSERT(Size > 0); Size--; }
+
+    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; }
+    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; }
+};
+#endif // #ifndef ImVector
+
 // Helper: execute a block of code once a frame only
 // Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame.
 // Usage: