changeset 14914:4a82d6b2d48a draft

(svn r19511) -Codechange: use a template for IConsoleAddSorted
author yexo <yexo@openttd.org>
date Wed, 24 Mar 2010 11:11:38 +0000
parents af037448df6e
children 924ff198af05
files src/console.cpp
diffstat 1 files changed, 30 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/console.cpp
+++ b/src/console.cpp
@@ -192,45 +192,35 @@
 }
 
 /**
- * Perhaps ugly macro, but this saves us the trouble of writing the same function
- * twice, just with different variables. Yes, templates would be handy. It was
- * either this define or an even more ugly void* magic function
+ * Add an item to an alphabetically sorted list.
+ * @param base first item of the list
+ * @param item_new the item to add
  */
-#define IConsoleAddSorted(_base, item_new, IConsoleType, type)                 \
-{                                                                              \
-	IConsoleType *item, *item_before;                                            \
-	/* first command */                                                          \
-	if (_base == NULL) {                                                         \
-		_base = item_new;                                                          \
-		return;                                                                    \
-	}                                                                            \
-                                                                               \
-	item_before = NULL;                                                          \
-	item = _base;                                                                \
-                                                                               \
-	/* BEGIN - Alphabetically insert the commands into the linked list */        \
-	while (item != NULL) {                                                       \
-		int i = strcmp(item->name, item_new->name);                                \
-		if (i == 0) {                                                              \
-			IConsoleError(type " with this name already exists; insertion aborted"); \
-			free(item_new);                                                          \
-			return;                                                                  \
-		}                                                                          \
-                                                                               \
-		if (i > 0) break; /* insert at this position */                            \
-                                                                               \
-		item_before = item;                                                        \
-		item = item->next;                                                         \
-	}                                                                            \
-                                                                               \
-	if (item_before == NULL) {                                                   \
-		_base = item_new;                                                          \
-	} else {                                                                     \
-		item_before->next = item_new;                                              \
-	}                                                                            \
-                                                                               \
-	item_new->next = item;                                                       \
-	/* END - Alphabetical insert */                                              \
+template<class T>
+void IConsoleAddSorted(T **base, T *item_new)
+{
+	if (*base == NULL) {
+		*base = item_new;
+		return;
+	}
+
+	T *item_before = NULL;
+	T *item = *base;
+	/* The list is alphabetically sorted, insert the new item at the correct location */
+	while (item != NULL) {
+		if (strcmp(item->name, item_new->name) > 0) break; // insert here
+
+		item_before = item;
+		item = item->next;
+	}
+
+	if (item_before == NULL) {
+		*base = item_new;
+	} else {
+		item_before->next = item_new;
+	}
+
+	item_new->next = item;
 }
 
 /**
@@ -246,7 +236,7 @@
 	item_new->proc = proc;
 	item_new->hook = hook;
 
-	IConsoleAddSorted(_iconsole_cmds, item_new, IConsoleCmd, "a command");
+	IConsoleAddSorted(&_iconsole_cmds, item_new);
 }
 
 /**
@@ -279,7 +269,7 @@
 	item_new->cmdline = cmd_aliased;
 	item_new->name = new_alias;
 
-	IConsoleAddSorted(_iconsole_aliases, item_new, IConsoleAlias, "an alias");
+	IConsoleAddSorted(&_iconsole_aliases, item_new);
 }
 
 /**