aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/vector.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2018-10-28 05:43:51 -0700
committerChris Robinson <[email protected]>2018-10-29 10:10:37 -0700
commit8a8ab68f1c9890f98dcd72dc4adacefa250f41df (patch)
tree4b04c9fd313f72592bd7fd3f090c5489474cf3b4 /Alc/vector.h
parentfd1361c1982e28fe4be287cb41ae24c3fc926ae8 (diff)
Add a more C++-friendly VECTOR_RESIZE
Diffstat (limited to 'Alc/vector.h')
-rw-r--r--Alc/vector.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/Alc/vector.h b/Alc/vector.h
index ed9acfb0..a7be99d0 100644
--- a/Alc/vector.h
+++ b/Alc/vector.h
@@ -26,6 +26,7 @@ typedef const _##N* const_##N;
#define VECTOR_INIT_STATIC() NULL
#define VECTOR_DEINIT(_x) do { al_free((_x)); (_x) = NULL; } while(0)
+#ifndef __cplusplus
#define VECTOR_RESIZE(_x, _s, _c) do { \
size_t _size = (_s); \
size_t _cap = (_c); \
@@ -55,6 +56,37 @@ typedef const _##N* const_##N;
(_x)->Size = _size; \
} while(0) \
+#else
+
+template<typename T>
+inline void do_vector_resize(T *&vec, size_t size, size_t cap)
+{
+ if(size > cap)
+ cap = size;
+
+ if(!vec && cap == 0)
+ return;
+
+ if((vec ? vec->Capacity : 0) < cap)
+ {
+ ptrdiff_t data_offset = vec ? (char*)(vec->Data) - (char*)(vec) : sizeof(*vec);
+ size_t old_size = (vec ? vec->Size : 0);
+ T *temp;
+
+ temp = reinterpret_cast<T*>(al_calloc(16, data_offset + sizeof(vec->Data[0])*cap));
+ assert(temp != nullptr);
+ if(vec)
+ memcpy(temp->Data, vec->Data, sizeof(vec->Data[0])*old_size);
+
+ al_free(vec);
+ vec = temp;
+ vec->Capacity = cap;
+ }
+ vec->Size = size;
+}
+#define VECTOR_RESIZE(_x, _s, _c) do_vector_resize(_x, _s, _c)
+#endif // __cplusplus
+
#define VECTOR_CAPACITY(_x) ((_x) ? (_x)->Capacity : 0)
#define VECTOR_SIZE(_x) ((_x) ? (_x)->Size : 0)