forked from github/server
allow local caching of bt_find results
This commit is contained in:
parent
07bbc022f4
commit
640ba05876
3 changed files with 22 additions and 3 deletions
|
@ -83,15 +83,28 @@ const building_type *bt_find(const char *name)
|
||||||
return bt_find_i(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)
|
void bt_register(building_type * type)
|
||||||
{
|
{
|
||||||
if (type->init) {
|
if (type->init) {
|
||||||
type->init(type);
|
type->init(type);
|
||||||
}
|
}
|
||||||
ql_push(&buildingtypes, (void *)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;
|
building_type *btype = (building_type *)ptr;
|
||||||
free_construction(btype->construction);
|
free_construction(btype->construction);
|
||||||
free(btype->maintenance);
|
free(btype->maintenance);
|
||||||
|
@ -103,6 +116,7 @@ void free_buildingtypes(void) {
|
||||||
ql_foreach(buildingtypes, free_buildingtype);
|
ql_foreach(buildingtypes, free_buildingtype);
|
||||||
ql_free(buildingtypes);
|
ql_free(buildingtypes);
|
||||||
buildingtypes = 0;
|
buildingtypes = 0;
|
||||||
|
++bt_changes;
|
||||||
}
|
}
|
||||||
|
|
||||||
building_type *bt_get_or_create(const char *name)
|
building_type *bt_get_or_create(const char *name)
|
||||||
|
|
|
@ -84,6 +84,7 @@ extern "C" {
|
||||||
extern struct attrib_type at_building_action;
|
extern struct attrib_type at_building_action;
|
||||||
|
|
||||||
building_type *bt_get_or_create(const char *name);
|
building_type *bt_get_or_create(const char *name);
|
||||||
|
bool bt_changed(int *cache);
|
||||||
const building_type *bt_find(const char *name);
|
const building_type *bt_find(const char *name);
|
||||||
void free_buildingtypes(void);
|
void free_buildingtypes(void);
|
||||||
void register_buildings(void);
|
void register_buildings(void);
|
||||||
|
|
|
@ -17,16 +17,20 @@
|
||||||
static void test_register_building(CuTest * tc)
|
static void test_register_building(CuTest * tc)
|
||||||
{
|
{
|
||||||
building_type *btype;
|
building_type *btype;
|
||||||
|
int cache = 0;
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
|
|
||||||
btype = (building_type *)calloc(sizeof(building_type), 1);
|
btype = (building_type *)calloc(sizeof(building_type), 1);
|
||||||
btype->_name = _strdup("herp");
|
btype->_name = _strdup("herp");
|
||||||
|
CuAssertIntEquals(tc, true, bt_changed(&cache));
|
||||||
|
CuAssertIntEquals(tc, false, bt_changed(&cache));
|
||||||
bt_register(btype);
|
bt_register(btype);
|
||||||
|
CuAssertIntEquals(tc, true, bt_changed(&cache));
|
||||||
|
|
||||||
CuAssertPtrNotNull(tc, bt_find("herp"));
|
CuAssertPtrNotNull(tc, bt_find("herp"));
|
||||||
// free(btype->_name);
|
free_buildingtypes();
|
||||||
// free(btype);
|
CuAssertIntEquals(tc, true, bt_changed(&cache));
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue