From eb807ec614b9636ed4fd1b2d737bcca047be155c Mon Sep 17 00:00:00 2001
From: TimePath <andrew.hardaker1995@gmail.com>
Date: Sat, 5 Dec 2015 21:23:13 +1100
Subject: [PATCH] ArrayList: pass by reference

---
 qcsrc/common/ent_cs.qh |  6 +++---
 qcsrc/lib/arraylist.qh | 30 ++++++++++++++++++++----------
 qcsrc/lib/i18n.qh      |  6 +++---
 qcsrc/lib/registry.qh  |  4 ++--
 4 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/qcsrc/common/ent_cs.qh b/qcsrc/common/ent_cs.qh
index 363103b379..8122ccdc0e 100644
--- a/qcsrc/common/ent_cs.qh
+++ b/qcsrc/common/ent_cs.qh
@@ -41,14 +41,14 @@ REGISTER_NET_TEMP(CLIENT_ENTCS)
 
 #ifdef CSQC
 
-	AL_declare(_entcs);
+	ArrayList _entcs;
 	STATIC_INIT(_entcs)
 	{
-		AL_init(_entcs, 255, NULL, e);  // 255 is the engine limit on maxclients
+		AL_NEW(_entcs, 255, NULL, e);  // 255 is the engine limit on maxclients
 	}
 	SHUTDOWN(_entcs)
 	{
-		AL_delete(_entcs);
+		AL_DELETE(_entcs);
 	}
 	#define entcs_receiver(...) EVAL(OVERLOAD(entcs_receiver, __VA_ARGS__))
 	#define entcs_receiver_1(i) AL_gete(_entcs, i)
diff --git a/qcsrc/lib/arraylist.qh b/qcsrc/lib/arraylist.qh
index c2aae2b7b5..b071805ef4 100644
--- a/qcsrc/lib/arraylist.qh
+++ b/qcsrc/lib/arraylist.qh
@@ -1,26 +1,36 @@
 #ifndef ARRAYLIST_H
 #define ARRAYLIST_H
 
-typedef int ArrayList;
+typedef entity ArrayList;
+.int al_buf;
+.int al_len;
 
-#define AL_declare(this) ArrayList this; int this##_len = (0)
-#define AL_init(this, n, default, T) \
+#define AL_NEW(this, n, default, T) \
 	do \
 	{ \
-		this = buf_create(); \
-		this##_len = n; \
-		for (int i = 0; i < this##_len; ++i) \
+		ArrayList _al = this = new(ArrayList); \
+		make_pure(_al); \
+		_al.al_buf = buf_create(); \
+		for (int i = 0, _n = _al.al_len = n; i < _n; ++i) \
 		{ \
 			const _AL_type__##T() it = default; \
 			AL_set##T(this, i, it); \
 		} \
 	} \
 	while (0)
-#define AL_delete(this) buf_del(this)
+
+#define AL_DELETE(this) \
+	do \
+	{ \
+		buf_del(this.al_buf); \
+		remove(this); \
+		this = NULL; \
+	} \
+	while (0)
 
 #define _AL_type__s() string
-#define AL_gets(this, idx) bufstr_get(this, idx)
-#define AL_sets(this, idx, val) bufstr_set(this, idx, val)
+#define AL_gets(this, idx) bufstr_get(this.al_buf, idx)
+#define AL_sets(this, idx, val) bufstr_set(this.al_buf, idx, val)
 
 #define _AL_type__f() float
 #define AL_getf(this, idx) stof(AL_gets(this, idx))
@@ -34,7 +44,7 @@ typedef int ArrayList;
 	do \
 	{ \
 		const noref ArrayList _al = this; \
-		for (int i = 0, n = this##_len; i < n; ++i) \
+		for (int i = 0, n = _al.al_len; i < n; ++i) \
 		{ \
 			const noref _AL_type__##T() it = AL_get##T(_al, i); \
 			if (cond) { body } \
diff --git a/qcsrc/lib/i18n.qh b/qcsrc/lib/i18n.qh
index 137e78204a..afb38ff956 100644
--- a/qcsrc/lib/i18n.qh
+++ b/qcsrc/lib/i18n.qh
@@ -26,14 +26,14 @@ string language_filename(string s)
 #endif
 
 #if CTX_CACHE
-	AL_declare(CTX_cache);
+	ArrayList CTX_cache;
 	STATIC_INIT(CTX_cache)
 	{
-		AL_init(CTX_cache, 0, string_null, s);
+		AL_NEW(CTX_cache, 0, string_null, s);
 	}
 	SHUTDOWN(CTX_cache)
 	{
-		AL_delete(CTX_cache);
+		AL_DELETE(CTX_cache);
 	}
 #endif
 
diff --git a/qcsrc/lib/registry.qh b/qcsrc/lib/registry.qh
index 3fc5e48983..a2284b1944 100644
--- a/qcsrc/lib/registry.qh
+++ b/qcsrc/lib/registry.qh
@@ -4,10 +4,10 @@
 #include "oo.qh"
 
 #if 1
-	#define _R_MAP(r, max) AL_declare(r); STATIC_INIT(r) { AL_init(r, max, NULL, e); }
+	#define _R_MAP(r, max) ArrayList r; STATIC_INIT(r) { AL_NEW(r, max, NULL, e); }
 	#define _R_GET(r, i) AL_gete(r, i)
 	#define _R_SET(r, i, e) AL_sete(r, i, e)
-	#define _R_DEL(r) AL_delete(r)
+	#define _R_DEL(r) AL_DELETE(r)
 #else
 	#define _R_MAP(r, max) entity r[max]
 	#define _R_GET(r, i) r[i]
-- 
2.39.5