allow local caching of bt_find results

This commit is contained in:
Enno Rehling 2016-09-19 04:04:02 +02:00
parent 07bbc022f4
commit 640ba05876
3 changed files with 22 additions and 3 deletions

View File

@ -83,15 +83,28 @@ const building_type *bt_find(const char *name)
return bt_find_i(name);
}
static int bt_changes = 1;
bool bt_changed(int *cache)
{
assert(cache);
if (*cache != bt_changes) {
*cache = bt_changes;
return true;
}
return false;
}
void bt_register(building_type * type)
{
if (type->init) {
type->init(type);
}
ql_push(&buildingtypes, (void *)type);
++bt_changes;
}
void free_buildingtype(void *ptr) {
static void free_buildingtype(void *ptr) {
building_type *btype = (building_type *)ptr;
free_construction(btype->construction);
free(btype->maintenance);
@ -103,6 +116,7 @@ void free_buildingtypes(void) {
ql_foreach(buildingtypes, free_buildingtype);
ql_free(buildingtypes);
buildingtypes = 0;
++bt_changes;
}
building_type *bt_get_or_create(const char *name)

View File

@ -84,6 +84,7 @@ extern "C" {
extern struct attrib_type at_building_action;
building_type *bt_get_or_create(const char *name);
bool bt_changed(int *cache);
const building_type *bt_find(const char *name);
void free_buildingtypes(void);
void register_buildings(void);

View File

@ -17,16 +17,20 @@
static void test_register_building(CuTest * tc)
{
building_type *btype;
int cache = 0;
test_cleanup();
btype = (building_type *)calloc(sizeof(building_type), 1);
btype->_name = _strdup("herp");
CuAssertIntEquals(tc, true, bt_changed(&cache));
CuAssertIntEquals(tc, false, bt_changed(&cache));
bt_register(btype);
CuAssertIntEquals(tc, true, bt_changed(&cache));
CuAssertPtrNotNull(tc, bt_find("herp"));
// free(btype->_name);
// free(btype);
free_buildingtypes();
CuAssertIntEquals(tc, true, bt_changed(&cache));
test_cleanup();
}