summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2010-03-24 11:11:38 +0000
committeryexo <yexo@openttd.org>2010-03-24 11:11:38 +0000
commitf4a107dae518ddad9d41240123d62cda3b88b671 (patch)
tree3c68bbc10d02a57503eef6a3f8255d052239f0f9
parent4d67c5ddaf0d06e482ec3a547565279dd5cf4ad2 (diff)
downloadopenttd-f4a107dae518ddad9d41240123d62cda3b88b671.tar.xz
(svn r19511) -Codechange: use a template for IConsoleAddSorted
-rw-r--r--src/console.cpp70
1 files changed, 30 insertions, 40 deletions
diff --git a/src/console.cpp b/src/console.cpp
index d486e780c..222bed024 100644
--- a/src/console.cpp
+++ b/src/console.cpp
@@ -192,45 +192,35 @@ bool GetArgumentInteger(uint32 *value, const char *arg)
}
/**
- * 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 @@ void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *
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 @@ void IConsoleAliasRegister(const char *name, const char *cmd)
item_new->cmdline = cmd_aliased;
item_new->name = new_alias;
- IConsoleAddSorted(_iconsole_aliases, item_new, IConsoleAlias, "an alias");
+ IConsoleAddSorted(&_iconsole_aliases, item_new);
}
/**