From e585642a5847c07f4376020e2f2118e3fa387e89 Mon Sep 17 00:00:00 2001 From: TimePath Date: Sun, 15 Nov 2015 14:01:09 +1100 Subject: [PATCH] Registry: simplify API --- qcsrc/common/command/all.qh | 2 +- qcsrc/common/items/all.qh | 2 +- qcsrc/common/minigames/cl_minigames.qh | 6 ++--- qcsrc/common/minigames/sv_minigames.qh | 6 ++--- qcsrc/common/mutators/mutator/buffs/all.qh | 6 ++--- qcsrc/lib/net.qh | 4 +-- qcsrc/lib/registry.qh | 30 ++++++++++++---------- qcsrc/lib/stats.qh | 2 +- qcsrc/server/cl_player.qh | 2 +- 9 files changed, 32 insertions(+), 28 deletions(-) diff --git a/qcsrc/common/command/all.qh b/qcsrc/common/command/all.qh index d1eb3cf76..9e16238d1 100644 --- a/qcsrc/common/command/all.qh +++ b/qcsrc/common/command/all.qh @@ -5,7 +5,7 @@ REGISTRY(GENERIC_COMMANDS, BITS(7)) #define GENERIC_COMMANDS_from(i) _GENERIC_COMMANDS_from(i, NULL) REGISTER_REGISTRY(GENERIC_COMMANDS) -REGISTRY_SORT(GENERIC_COMMANDS, 0) +REGISTRY_SORT(GENERIC_COMMANDS) #define GENERIC_COMMAND(id, description) \ CLASS(genericcommand_##id, Command) \ diff --git a/qcsrc/common/items/all.qh b/qcsrc/common/items/all.qh index ab86ea673..1bd22f660 100644 --- a/qcsrc/common/items/all.qh +++ b/qcsrc/common/items/all.qh @@ -11,7 +11,7 @@ REGISTER_REGISTRY(Items) /** If you register a new item, make sure to add it to all.inc */ #define REGISTER_ITEM(id, class) REGISTER(Items, ITEM, id, m_id, NEW(class)) -REGISTRY_SORT(Items, 0) +REGISTRY_SORT(Items) REGISTRY_CHECK(Items) STATIC_INIT(Items) { FOREACH(Items, true, LAMBDA(it.m_id = i)); } diff --git a/qcsrc/common/minigames/cl_minigames.qh b/qcsrc/common/minigames/cl_minigames.qh index f21c6e482..fe75e038b 100644 --- a/qcsrc/common/minigames/cl_minigames.qh +++ b/qcsrc/common/minigames/cl_minigames.qh @@ -116,11 +116,11 @@ REGISTRY(Minigames, BITS(3)) REGISTER_REGISTRY(Minigames) REGISTRY_CHECK(Minigames) #define REGISTER_MINIGAME(name,nicename) \ - REGISTER(Minigames, MINIGAME, name, m_id, new(minigame_descriptor)); \ + REGISTER(Minigames, MINIGAME_##name, m_id, new(minigame_descriptor)); \ void name##_hud_board(vector, vector); \ void name##_hud_status(vector, vector); \ int name##_client_event(entity, string, ...); \ - REGISTER_INIT_POST(MINIGAME, name) { \ + REGISTER_INIT_POST(MINIGAME_##name) { \ make_pure(this); \ this.netname = strzone(strtolower(#name)); \ this.message = nicename; \ @@ -128,6 +128,6 @@ REGISTRY_CHECK(Minigames) this.minigame_hud_status = name##_hud_status; \ this.minigame_event = name##_client_event; \ } \ - REGISTER_INIT(MINIGAME, name) + REGISTER_INIT(MINIGAME_##name) #endif diff --git a/qcsrc/common/minigames/sv_minigames.qh b/qcsrc/common/minigames/sv_minigames.qh index eb6825ed6..0fb3d572c 100644 --- a/qcsrc/common/minigames/sv_minigames.qh +++ b/qcsrc/common/minigames/sv_minigames.qh @@ -51,14 +51,14 @@ REGISTRY(Minigames, BITS(3)) REGISTER_REGISTRY(Minigames) REGISTRY_CHECK(Minigames) #define REGISTER_MINIGAME(name,nicename) \ - REGISTER(Minigames, MINIGAME, name, m_id, new(minigame_descriptor)); \ + REGISTER(Minigames, MINIGAME_##name, m_id, new(minigame_descriptor)); \ int name##_server_event(entity, string, ...); \ - REGISTER_INIT_POST(MINIGAME, name) { \ + REGISTER_INIT_POST(MINIGAME_##name) { \ make_pure(this); \ this.netname = strzone(strtolower(#name)); \ this.message = nicename; \ this.minigame_event = name##_server_event; \ } \ - REGISTER_INIT(MINIGAME, name) + REGISTER_INIT(MINIGAME_##name) #endif diff --git a/qcsrc/common/mutators/mutator/buffs/all.qh b/qcsrc/common/mutators/mutator/buffs/all.qh index 94a00b9de..db22d3141 100644 --- a/qcsrc/common/mutators/mutator/buffs/all.qh +++ b/qcsrc/common/mutators/mutator/buffs/all.qh @@ -17,13 +17,13 @@ REGISTER_REGISTRY(Buffs) REGISTRY_CHECK(Buffs) #define REGISTER_BUFF(id) \ - REGISTER(Buffs, BUFF, id, m_id, NEW(Buff)); \ - REGISTER_INIT_POST(BUFF, id) { \ + REGISTER(Buffs, BUFF_##id, m_id, NEW(Buff)); \ + REGISTER_INIT_POST(BUFF_##id) { \ this.netname = this.m_name; \ this.m_itemid = BIT(this.m_id - 1); \ this.m_sprite = strzone(strcat("buff-", this.m_name)); \ } \ - REGISTER_INIT(BUFF, id) + REGISTER_INIT(BUFF_##id) #include "../../../items/item/pickup.qh" CLASS(Buff, Pickup) diff --git a/qcsrc/lib/net.qh b/qcsrc/lib/net.qh index 45b377a00..5a683f54c 100644 --- a/qcsrc/lib/net.qh +++ b/qcsrc/lib/net.qh @@ -111,7 +111,7 @@ REGISTRY(LinkedEntities, BITS(8) - 1) #define LinkedEntities_from(i) _LinkedEntities_from(i, NULL) REGISTER_REGISTRY(LinkedEntities) -REGISTRY_SORT(LinkedEntities, 0) +REGISTRY_SORT(LinkedEntities) REGISTRY_CHECK(LinkedEntities) STATIC_INIT(RegisterLinkedEntities_renumber) { @@ -141,7 +141,7 @@ STATIC_INIT(RegisterLinkedEntities_renumber) REGISTRY(TempEntities, BITS(8) - 80) #define TempEntities_from(i) _TempEntities_from(i, NULL) REGISTER_REGISTRY(TempEntities) -REGISTRY_SORT(TempEntities, 0) +REGISTRY_SORT(TempEntities) REGISTRY_CHECK(TempEntities) STATIC_INIT(RegisterTempEntities_renumber) { diff --git a/qcsrc/lib/registry.qh b/qcsrc/lib/registry.qh index 52ab23377..16d35b0dd 100644 --- a/qcsrc/lib/registry.qh +++ b/qcsrc/lib/registry.qh @@ -39,14 +39,16 @@ REGISTRY(Registries, BITS(8)) * @param fld The field to store the locally unique unique entity id * @param inst An expression to create a new instance, invoked for every registration */ -#define REGISTER(registry, ns, id, fld, inst) \ - entity ns##_##id; \ - REGISTER_INIT(ns, id) {} \ - REGISTER_INIT_POST(ns, id) {} \ - void Register_##ns##_##id() \ +#define REGISTER(...) EVAL(OVERLOAD(REGISTER, __VA_ARGS__)) +#define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst) +#define REGISTER_4(registry, id, fld, inst) \ + entity id; \ + REGISTER_INIT(id) {} \ + REGISTER_INIT_POST(id) {} \ + void Register_##id() \ { \ if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(registry##_MAX)); \ - entity this = ns##_##id = inst; \ + entity this = id = inst; \ this.registered_id = #id; \ this.fld = registry##_COUNT; \ _##registry[registry##_COUNT] = this; \ @@ -54,20 +56,22 @@ REGISTRY(Registries, BITS(8)) if (!registry##_first) registry##_first = this; \ if (registry##_last) registry##_last.REGISTRY_NEXT = this; \ registry##_last = this; \ - Register_##ns##_##id##_init(this); \ - Register_##ns##_##id##_init_post(this); \ + Register_##id##_init(this); \ + Register_##id##_init_post(this); \ } \ - ACCUMULATE_FUNCTION(Register##registry, Register_##ns##_##id) \ - REGISTER_INIT(ns, id) + ACCUMULATE_FUNCTION(Register##registry, Register_##id) \ + REGISTER_INIT(id) -#define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this) -#define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this) +#define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this) +#define REGISTER_INIT_POST(id) [[accumulate]] void Register_##id##_init_post(entity this) /** internal next pointer */ #define REGISTRY_NEXT enemy .entity REGISTRY_NEXT; -#define REGISTRY_SORT(id, skip) \ +#define REGISTRY_SORT(...) EVAL(OVERLOAD(REGISTRY_SORT, __VA_ARGS__)) +#define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0) +#define REGISTRY_SORT_2(id, skip) \ void _REGISTRY_SWAP_##id(int i, int j, entity pass) \ { \ i += skip; j += skip; \ diff --git a/qcsrc/lib/stats.qh b/qcsrc/lib/stats.qh index 7dd2706fd..12dc425cd 100644 --- a/qcsrc/lib/stats.qh +++ b/qcsrc/lib/stats.qh @@ -59,7 +59,7 @@ const int STATS_ENGINE_RESERVE = 32 + (8 * 3); // Not sure how to handle vector REGISTRY(Stats, 220 - STATS_ENGINE_RESERVE) REGISTER_REGISTRY(Stats) -REGISTRY_SORT(Stats, 0) +REGISTRY_SORT(Stats) REGISTRY_CHECK(Stats) STATIC_INIT(RegisterStats_renumber) { diff --git a/qcsrc/server/cl_player.qh b/qcsrc/server/cl_player.qh index 76c49d852..37bc06310 100644 --- a/qcsrc/server/cl_player.qh +++ b/qcsrc/server/cl_player.qh @@ -109,7 +109,7 @@ REGISTRY(GlobalSounds, BITS(8) - 1) this.m_globalsoundstr = str; \ } REGISTER_REGISTRY(GlobalSounds) -REGISTRY_SORT(GlobalSounds, 0) +REGISTRY_SORT(GlobalSounds) REGISTRY_CHECK(GlobalSounds) PRECACHE(GlobalSounds) { -- 2.39.2