(svn r19511) -Codechange: use a template for IConsoleAddSorted

This commit is contained in:
yexo 2010-03-24 11:11:38 +00:00
parent 4d67c5ddaf
commit f4a107dae5
1 changed files with 30 additions and 40 deletions

View File

@ -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 * Add an item to an alphabetically sorted list.
* twice, just with different variables. Yes, templates would be handy. It was * @param base first item of the list
* either this define or an even more ugly void* magic function * @param item_new the item to add
*/ */
#define IConsoleAddSorted(_base, item_new, IConsoleType, type) \ template<class T>
{ \ void IConsoleAddSorted(T **base, T *item_new)
IConsoleType *item, *item_before; \ {
/* first command */ \ if (*base == NULL) {
if (_base == NULL) { \ *base = item_new;
_base = item_new; \ return;
return; \ }
} \
\ T *item_before = NULL;
item_before = NULL; \ T *item = *base;
item = _base; \ /* The list is alphabetically sorted, insert the new item at the correct location */
\ while (item != NULL) {
/* BEGIN - Alphabetically insert the commands into the linked list */ \ if (strcmp(item->name, item_new->name) > 0) break; // insert here
while (item != NULL) { \
int i = strcmp(item->name, item_new->name); \ item_before = item;
if (i == 0) { \ item = item->next;
IConsoleError(type " with this name already exists; insertion aborted"); \ }
free(item_new); \
return; \ if (item_before == NULL) {
} \ *base = item_new;
\ } else {
if (i > 0) break; /* insert at this position */ \ item_before->next = item_new;
\ }
item_before = item; \
item = item->next; \ item_new->next = item;
} \
\
if (item_before == NULL) { \
_base = item_new; \
} else { \
item_before->next = item_new; \
} \
\
item_new->next = item; \
/* END - Alphabetical insert */ \
} }
/** /**
@ -246,7 +236,7 @@ void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *
item_new->proc = proc; item_new->proc = proc;
item_new->hook = hook; 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->cmdline = cmd_aliased;
item_new->name = new_alias; item_new->name = new_alias;
IConsoleAddSorted(_iconsole_aliases, item_new, IConsoleAlias, "an alias"); IConsoleAddSorted(&_iconsole_aliases, item_new);
} }
/** /**