Changeset - r14904:3e524d0a3be3
[Not reviewed]
master
0 1 0
yexo - 14 years ago 2010-03-24 11:11:38
yexo@openttd.org
(svn r19511) -Codechange: use a template for IConsoleAddSorted
1 file changed with 30 insertions and 40 deletions:
0 comments (0 inline, 0 general)
src/console.cpp
Show inline comments
 
@@ -192,45 +192,35 @@ bool GetArgumentInteger(uint32 *value, c
 
}
 

	
 
/**
 
 * 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 *nam
 
	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 *n
 
	item_new->cmdline = cmd_aliased;
 
	item_new->name = new_alias;
 

	
 
	IConsoleAddSorted(_iconsole_aliases, item_new, IConsoleAlias, "an alias");
 
	IConsoleAddSorted(&_iconsole_aliases, item_new);
 
}
 

	
 
/**
0 comments (0 inline, 0 general)