From 7bcb703bca4af019e94b87bc6a6b4ecb52424485 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Tue, 18 Feb 2014 13:47:10 +0100 Subject: [PATCH] Replace ModeMasks with a two-dimensional array --- include/mode.h | 17 +++++---------- src/mode.cpp | 56 +++++++++++++++++--------------------------------- 2 files changed, 24 insertions(+), 49 deletions(-) diff --git a/include/mode.h b/include/mode.h index f0609fec2..2e40badca 100644 --- a/include/mode.h +++ b/include/mode.h @@ -45,17 +45,6 @@ enum ModeAction MODEACTION_ALLOW = 1 /* Allow the mode */ }; -/** - * Used to mask off the mode types in the mode handler - * array. Used in a simple two instruction hashing function - * "(modeletter - 65) OR mask" - */ -enum ModeMasks -{ - MASK_USER = 128, /* A user mode */ - MASK_CHANNEL = 0 /* A channel mode */ -}; - /** * These fixed values can be used to proportionally compare module-defined prefixes to known values. * For example, if your module queries a Channel, and is told that user 'joebloggs' has the prefix @@ -486,12 +475,16 @@ typedef std::multimap::iterator ModeWatchIter; class CoreExport ModeParser { private: + /** Last item in the ModeType enum + */ + static const unsigned int MODETYPE_LAST = 2; + /** Mode handlers for each mode, to access a handler subtract * 65 from the ascii value of the mode letter. * The upper bit of the value indicates if its a usermode * or a channel mode, so we have 256 of them not 64. */ - ModeHandler* modehandlers[256]; + ModeHandler* modehandlers[MODETYPE_LAST][128]; /** Lists of mode handlers by type */ diff --git a/src/mode.cpp b/src/mode.cpp index 90b8efeb8..a2a0790b8 100644 --- a/src/mode.cpp +++ b/src/mode.cpp @@ -610,9 +610,6 @@ void ModeParser::CleanMask(std::string &mask) bool ModeParser::AddMode(ModeHandler* mh) { - unsigned char mask = 0; - unsigned char pos = 0; - /* Yes, i know, this might let people declare modes like '_' or '^'. * If they do that, thats their problem, and if i ever EVER see an * official InspIRCd developer do that, i'll beat them with a paddle! @@ -634,14 +631,12 @@ bool ModeParser::AddMode(ModeHandler* mh) return false; } - mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL; - pos = (mh->GetModeChar()-65) | mask; - - if (modehandlers[pos]) + ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65]; + if (slot) return false; // Everything is fine, add the mode - modehandlers[pos] = mh; + slot = mh; if (pm) mhlist.prefix.push_back(pm); else if (mh->IsListModeBase()) @@ -653,16 +648,11 @@ bool ModeParser::AddMode(ModeHandler* mh) bool ModeParser::DelMode(ModeHandler* mh) { - unsigned char mask = 0; - unsigned char pos = 0; - if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z')) return false; - mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL; - pos = (mh->GetModeChar()-65) | mask; - - if (modehandlers[pos] != mh) + ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65]; + if (slot != mh) return false; /* Note: We can't stack here, as we have modes potentially being removed across many different channels. @@ -699,7 +689,7 @@ bool ModeParser::DelMode(ModeHandler* mh) break; } - modehandlers[pos] = NULL; + slot = NULL; if (mh->IsPrefixMode()) mhlist.prefix.erase(std::find(mhlist.prefix.begin(), mhlist.prefix.end(), mh->IsPrefixMode())); else if (mh->IsListModeBase()) @@ -711,16 +701,10 @@ bool ModeParser::DelMode(ModeHandler* mh) ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt) { - unsigned char mask = 0; - unsigned char pos = 0; - if ((modeletter < 'A') || (modeletter > 'z')) return NULL; - mt == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL; - pos = (modeletter-65) | mask; - - return modehandlers[pos]; + return modehandlers[mt][modeletter-65]; } PrefixMode* ModeParser::FindPrefixMode(unsigned char modeletter) @@ -734,13 +718,11 @@ PrefixMode* ModeParser::FindPrefixMode(unsigned char modeletter) std::string ModeParser::CreateModeList(ModeType mt, bool needparam) { std::string modestr; - unsigned char mask = ((mt == MODETYPE_CHANNEL) ? MASK_CHANNEL : MASK_USER); for (unsigned char mode = 'A'; mode <= 'z'; mode++) { - unsigned char pos = (mode-65) | mask; - - if ((modehandlers[pos]) && ((!needparam) || (modehandlers[pos]->GetNumParams(true)))) + ModeHandler* mh = modehandlers[mt][mode-65]; + if ((mh) && ((!needparam) || (mh->GetNumParams(true)))) modestr.push_back(mode); } @@ -773,38 +755,38 @@ std::string ModeParser::GiveModeList(ModeType mt) for (unsigned char mode = 'A'; mode <= 'z'; mode++) { - unsigned char pos = (mode-65) | ((mt == MODETYPE_CHANNEL) ? MASK_CHANNEL : MASK_USER); + ModeHandler* mh = modehandlers[mt][mode-65]; /* One parameter when adding */ - if (modehandlers[pos]) + if (mh) { - if (modehandlers[pos]->GetNumParams(true)) + if (mh->GetNumParams(true)) { - PrefixMode* pm = modehandlers[pos]->IsPrefixMode(); - if ((modehandlers[pos]->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0))) + PrefixMode* pm = mh->IsPrefixMode(); + if ((mh->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0))) { - type1 += modehandlers[pos]->GetModeChar(); + type1 += mh->GetModeChar(); } else { /* ... and one parameter when removing */ - if (modehandlers[pos]->GetNumParams(false)) + if (mh->GetNumParams(false)) { /* But not a list mode */ if (!pm) { - type2 += modehandlers[pos]->GetModeChar(); + type2 += mh->GetModeChar(); } } else { /* No parameters when removing */ - type3 += modehandlers[pos]->GetModeChar(); + type3 += mh->GetModeChar(); } } } else { - type4 += modehandlers[pos]->GetModeChar(); + type4 += mh->GetModeChar(); } } } -- 2.39.5