From 367e29dae1e4e500e95743064bcd1db66ab62ca9 Mon Sep 17 00:00:00 2001 From: TimePath Date: Tue, 11 Aug 2015 13:04:45 +1000 Subject: [PATCH] Extract registry functionality --- qcsrc/common/items/all.qh | 7 ++++++- qcsrc/common/items/item.qh | 16 +--------------- qcsrc/common/registry.qh | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 qcsrc/common/registry.qh diff --git a/qcsrc/common/items/all.qh b/qcsrc/common/items/all.qh index 1786b4261..647958628 100644 --- a/qcsrc/common/items/all.qh +++ b/qcsrc/common/items/all.qh @@ -1,8 +1,14 @@ +#include "../registry.qh" + #ifndef ALL_H #define ALL_H +void RegisterItems(); const int MAX_ITEMS = 24; entity ITEMS[MAX_ITEMS]; +int ITEM_COUNT; +/** If you register a new item, make sure to add it to all.inc */ +#define REGISTER_ITEM(id, class) REGISTER(RegisterItems, ITEM, ITEMS, ITEM_COUNT, id, class) #define ITEMS_FOREACH(pred, body) do { \ for (int i = 0; i < ITEM_COUNT; i++) { \ @@ -11,7 +17,6 @@ entity ITEMS[MAX_ITEMS]; } \ } while(0) -void RegisterItems(); void Dump_Items(); #endif diff --git a/qcsrc/common/items/item.qh b/qcsrc/common/items/item.qh index 507ca93e0..6ecd6d7f7 100644 --- a/qcsrc/common/items/item.qh +++ b/qcsrc/common/items/item.qh @@ -2,6 +2,7 @@ #define GAMEITEM_H #include "../oo.qh" #define ITEM_HANDLE(signal, ...) __Item_Send_##signal(__VA_ARGS__) +/** If you register a new item, make sure to add it to all.inc */ CLASS(GameItem, Object) ATTRIB(GameItem, m_id, int, 0) METHOD(GameItem, show, void(entity this)) @@ -9,19 +10,4 @@ CLASS(GameItem, Object) void ITEM_HANDLE(Show, entity this) { this.show(this); } ENDCLASS(GameItem) - -int ITEM_COUNT; -/** If you register a new item, make sure to add it to all.inc */ -#define REGISTER_ITEM(id, class) \ - entity ITEM_##id; \ - void RegisterItems_init_##id(entity this) {} \ - void RegisterItems_##id() { \ - entity this = NEW(class); \ - ITEM_##id = this; \ - this.m_id = ITEM_COUNT; \ - ITEMS[ITEM_COUNT++] = this; \ - RegisterItems_init_##id(this); \ - } \ - ACCUMULATE_FUNCTION(RegisterItems, RegisterItems_##id) \ - [[accumulate]] void RegisterItems_init_##id(entity this) #endif diff --git a/qcsrc/common/registry.qh b/qcsrc/common/registry.qh new file mode 100644 index 000000000..d6f884c31 --- /dev/null +++ b/qcsrc/common/registry.qh @@ -0,0 +1,17 @@ +#ifndef REGISTRY_H +#define REGISTRY_H + +#define REGISTER(initfunc, ns, array, counter, id, class) \ + entity ns##_##id; \ + void Register_##ns##_##id##_init(entity this) { } \ + void Register_##ns##_##id() { \ + entity this = NEW(class); \ + ns##_##id = this; \ + this.m_id = ns##_COUNT; \ + array[counter++] = this; \ + Register_##ns##_##id##_init(this); \ + } \ + ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \ + [[accumulate]] void Register_##ns##_##id##_init(entity this) + +#endif -- 2.39.2