+namespace wxPrivate
+{
+
+// These templates encapsulate memory operations for use by wxVector; there are
+// two implementations, both in generic way for any C++ types and as an
+// optimized version for "movable" types that uses realloc() and memmove().
+
+// version for movable types:
+template<typename T>
+struct wxVectorMemOpsMovable
+{
+ static void Free(T* array)
+ { free(array); }
+
+ static T* Realloc(T* old, size_t newCapacity, size_t WXUNUSED(occupiedSize))
+ { return (T*)realloc(old, newCapacity * sizeof(T)); }
+
+ static void MemmoveBackward(T* dest, T* source, size_t count)
+ { memmove(dest, source, count * sizeof(T)); }
+
+ static void MemmoveForward(T* dest, T* source, size_t count)
+ { memmove(dest, source, count * sizeof(T)); }
+};
+
+// generic version for non-movable types:
+template<typename T>
+struct wxVectorMemOpsGeneric
+{
+ static void Free(T* array)
+ { ::operator delete(array); }
+
+ static T* Realloc(T* old, size_t newCapacity, size_t occupiedSize)
+ {
+ T *mem = (T*)::operator new(newCapacity * sizeof(T));
+ for ( size_t i = 0; i < occupiedSize; i++ )
+ {
+ new(mem + i) T(old[i]);
+ old[i].~T();
+ }
+ ::operator delete(old);
+ return mem;
+ }
+
+ static void MemmoveBackward(T* dest, T* source, size_t count)
+ {
+ wxASSERT( dest < source );
+ T* destptr = dest;
+ T* sourceptr = source;
+ for ( size_t i = count; i > 0; --i, ++destptr, ++sourceptr )
+ {
+ new(destptr) T(*sourceptr);
+ sourceptr->~T();
+ }
+ }
+
+ static void MemmoveForward(T* dest, T* source, size_t count)
+ {
+ wxASSERT( dest > source );
+ T* destptr = dest + count - 1;
+ T* sourceptr = source + count - 1;
+ for ( size_t i = count; i > 0; --i, --destptr, --sourceptr )
+ {
+ new(destptr) T(*sourceptr);
+ sourceptr->~T();
+ }
+ }
+};
+
+
+} // namespace wxPrivate
+