Turn CChan::m_msNicks into a map<CString, CNick>

Saving a pointer in a map seems like a bad idea and means we have to delete all
the contained stuff by hand when the channel is destroyed. This requires us to
loop through the channel which is slow. A map is meant as a container, so use it
as one and directly save the stuff we want it to save in there.


git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@2175 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2010-11-06 19:41:40 +00:00
parent 612b61dada
commit 5e070e7881
7 changed files with 38 additions and 37 deletions
+19 -19
View File
@@ -128,18 +128,18 @@ void CChan::JoinUser(bool bForce, const CString& sKey, CClient* pClient) {
else else
pThisClient = pClient; pThisClient = pClient;
for (map<CString,CNick*>::iterator a = m_msNicks.begin(); a != m_msNicks.end(); ++a) { for (map<CString,CNick>::iterator a = m_msNicks.begin(); a != m_msNicks.end(); ++a) {
if (pThisClient->HasNamesx()) { if (pThisClient->HasNamesx()) {
sPerm = a->second->GetPermStr(); sPerm = a->second.GetPermStr();
} else { } else {
char c = a->second->GetPermChar(); char c = a->second.GetPermChar();
sPerm = ""; sPerm = "";
if (c != '\0') { if (c != '\0') {
sPerm += c; sPerm += c;
} }
} }
if (pThisClient->HasUHNames() && !a->second->GetIdent().empty() && !a->second->GetHost().empty()) { if (pThisClient->HasUHNames() && !a->second.GetIdent().empty() && !a->second.GetHost().empty()) {
sNick = a->first + "!" + a->second->GetIdent() + "@" + a->second->GetHost(); sNick = a->first + "!" + a->second.GetIdent() + "@" + a->second.GetHost();
} else { } else {
sNick = a->first; sNick = a->first;
} }
@@ -369,10 +369,6 @@ CString CChan::GetModeArg(CString& sArgs) const {
} }
void CChan::ClearNicks() { void CChan::ClearNicks() {
for (map<CString,CNick*>::iterator a = m_msNicks.begin(); a != m_msNicks.end(); ++a) {
delete a->second;
}
m_msNicks.clear(); m_msNicks.clear();
} }
@@ -434,7 +430,7 @@ bool CChan::AddNick(const CString& sNick) {
} }
} }
m_msNicks[pNick->GetNick()] = pNick; m_msNicks[pNick->GetNick()] = *pNick;
return true; return true;
} }
@@ -442,9 +438,9 @@ bool CChan::AddNick(const CString& sNick) {
map<char, unsigned int> CChan::GetPermCounts() const { map<char, unsigned int> CChan::GetPermCounts() const {
map<char, unsigned int> mRet; map<char, unsigned int> mRet;
map<CString,CNick*>::const_iterator it; map<CString,CNick>::const_iterator it;
for (it = m_msNicks.begin(); it != m_msNicks.end(); ++it) { for (it = m_msNicks.begin(); it != m_msNicks.end(); ++it) {
CString sPerms = it->second->GetPermStr(); CString sPerms = it->second.GetPermStr();
for (unsigned int p = 0; p < sPerms.size(); p++) { for (unsigned int p = 0; p < sPerms.size(); p++) {
mRet[sPerms[p]]++; mRet[sPerms[p]]++;
@@ -455,7 +451,7 @@ map<char, unsigned int> CChan::GetPermCounts() const {
} }
bool CChan::RemNick(const CString& sNick) { bool CChan::RemNick(const CString& sNick) {
map<CString,CNick*>::iterator it; map<CString,CNick>::iterator it;
set<unsigned char>::iterator it2; set<unsigned char>::iterator it2;
it = m_msNicks.find(sNick); it = m_msNicks.find(sNick);
@@ -463,21 +459,20 @@ bool CChan::RemNick(const CString& sNick) {
return false; return false;
} }
delete it->second;
m_msNicks.erase(it); m_msNicks.erase(it);
return true; return true;
} }
bool CChan::ChangeNick(const CString& sOldNick, const CString& sNewNick) { bool CChan::ChangeNick(const CString& sOldNick, const CString& sNewNick) {
map<CString,CNick*>::iterator it = m_msNicks.find(sOldNick); map<CString,CNick>::iterator it = m_msNicks.find(sOldNick);
if (it == m_msNicks.end()) { if (it == m_msNicks.end()) {
return false; return false;
} }
// Rename this nick // Rename this nick
it->second->SetNick(sNewNick); it->second.SetNick(sNewNick);
// Insert a new element into the map then erase the old one, do this to change the key to the new nick // Insert a new element into the map then erase the old one, do this to change the key to the new nick
m_msNicks[sNewNick] = it->second; m_msNicks[sNewNick] = it->second;
@@ -486,9 +481,14 @@ bool CChan::ChangeNick(const CString& sOldNick, const CString& sNewNick) {
return true; return true;
} }
CNick* CChan::FindNick(const CString& sNick) const { const CNick* CChan::FindNick(const CString& sNick) const {
map<CString,CNick*>::const_iterator it = m_msNicks.find(sNick); map<CString,CNick>::const_iterator it = m_msNicks.find(sNick);
return (it != m_msNicks.end()) ? it->second : NULL; return (it != m_msNicks.end()) ? &it->second : NULL;
}
CNick* CChan::FindNick(const CString& sNick) {
map<CString,CNick>::iterator it = m_msNicks.find(sNick);
return (it != m_msNicks.end()) ? &it->second : NULL;
} }
int CChan::AddBuffer(const CString& sLine) { int CChan::AddBuffer(const CString& sLine) {
+4 -3
View File
@@ -75,7 +75,8 @@ public:
// Nicks // Nicks
void ClearNicks(); void ClearNicks();
CNick* FindNick(const CString& sNick) const; const CNick* FindNick(const CString& sNick) const;
CNick* FindNick(const CString& sNick);
int AddNicks(const CString& sNicks); int AddNicks(const CString& sNicks);
bool AddNick(const CString& sNick); bool AddNick(const CString& sNick);
bool RemNick(const CString& sNick); bool RemNick(const CString& sNick);
@@ -127,7 +128,7 @@ public:
unsigned int GetTopicDate() const { return m_ulTopicDate; } unsigned int GetTopicDate() const { return m_ulTopicDate; }
const CString& GetDefaultModes() const { return m_sDefaultModes; } const CString& GetDefaultModes() const { return m_sDefaultModes; }
const vector<CString>& GetBuffer() const { return m_vsBuffer; } const vector<CString>& GetBuffer() const { return m_vsBuffer; }
const map<CString,CNick*>& GetNicks() const { return m_msNicks; } const map<CString,CNick>& GetNicks() const { return m_msNicks; }
unsigned int GetNickCount() const { return m_msNicks.size(); } unsigned int GetNickCount() const { return m_msNicks.size(); }
unsigned int GetBufferCount() const { return m_uBufferCount; } unsigned int GetBufferCount() const { return m_uBufferCount; }
bool KeepBuffer() const { return m_bKeepBuffer; } bool KeepBuffer() const { return m_bKeepBuffer; }
@@ -154,7 +155,7 @@ protected:
CNick m_Nick; CNick m_Nick;
unsigned int m_uJoinTries; unsigned int m_uJoinTries;
CString m_sDefaultModes; CString m_sDefaultModes;
map<CString,CNick*> m_msNicks; // Todo: make this caseless (irc style) map<CString,CNick> m_msNicks; // Todo: make this caseless (irc style)
unsigned int m_uBufferCount; unsigned int m_uBufferCount;
vector<CString> m_vsBuffer; vector<CString> m_vsBuffer;
+6 -6
View File
@@ -51,7 +51,7 @@ void CClient::UserCommand(CString& sLine) {
return; return;
} }
const map<CString,CNick*>& msNicks = pChan->GetNicks(); const map<CString,CNick>& msNicks = pChan->GetNicks();
CIRCSock* pIRCSock = m_pUser->GetIRCSock(); CIRCSock* pIRCSock = m_pUser->GetIRCSock();
const CString& sPerms = (pIRCSock) ? pIRCSock->GetPerms() : ""; const CString& sPerms = (pIRCSock) ? pIRCSock->GetPerms() : "";
@@ -72,20 +72,20 @@ void CClient::UserCommand(CString& sLine) {
Table.AddColumn("Ident"); Table.AddColumn("Ident");
Table.AddColumn("Host"); Table.AddColumn("Host");
for (map<CString,CNick*>::const_iterator a = msNicks.begin(); a != msNicks.end(); ++a) { for (map<CString,CNick>::const_iterator a = msNicks.begin(); a != msNicks.end(); ++a) {
Table.AddRow(); Table.AddRow();
for (unsigned int b = 0; b < sPerms.size(); b++) { for (unsigned int b = 0; b < sPerms.size(); b++) {
if (a->second->HasPerm(sPerms[b])) { if (a->second.HasPerm(sPerms[b])) {
CString sPerm; CString sPerm;
sPerm += sPerms[b]; sPerm += sPerms[b];
Table.SetCell(sPerm, sPerm); Table.SetCell(sPerm, sPerm);
} }
} }
Table.SetCell("Nick", a->second->GetNick()); Table.SetCell("Nick", a->second.GetNick());
Table.SetCell("Ident", a->second->GetIdent()); Table.SetCell("Ident", a->second.GetIdent());
Table.SetCell("Host", a->second->GetHost()); Table.SetCell("Host", a->second.GetHost());
} }
PutStatus(Table); PutStatus(Table);
+2 -2
View File
@@ -55,9 +55,9 @@ unsigned int CNick::GetCommonChans(vector<CChan*>& vRetChans, CUser* pUser) cons
for (unsigned int a = 0; a < vChans.size(); a++) { for (unsigned int a = 0; a < vChans.size(); a++) {
CChan* pChan = vChans[a]; CChan* pChan = vChans[a];
const map<CString,CNick*>& msNicks = pChan->GetNicks(); const map<CString,CNick>& msNicks = pChan->GetNicks();
for (map<CString,CNick*>::const_iterator it = msNicks.begin(); it != msNicks.end(); ++it) { for (map<CString,CNick>::const_iterator it = msNicks.begin(); it != msNicks.end(); ++it) {
if (it->first.Equals(m_sNick)) { if (it->first.Equals(m_sNick)) {
vRetChans.push_back(pChan); vRetChans.push_back(pChan);
continue; continue;
+2 -2
View File
@@ -124,8 +124,8 @@ protected:
return; return;
// Is that person us and we don't have op? // Is that person us and we don't have op?
const CNick* pNick = Channel.GetNicks().begin()->second; const CNick& pNick = Channel.GetNicks().begin()->second;
if (!pNick->HasPerm(CChan::Op) && pNick->GetNick().Equals(m_pUser->GetCurNick())) if (!pNick.HasPerm(CChan::Op) && pNick.GetNick().Equals(m_pUser->GetCurNick()))
Channel.Cycle(); Channel.Cycle();
} }
+2 -2
View File
@@ -352,7 +352,7 @@ public:
for (size_t a = 0; a < Chans.size(); a++) { for (size_t a = 0; a < Chans.size(); a++) {
const CChan& Chan = *Chans[a]; const CChan& Chan = *Chans[a];
CNick* pNick = Chan.FindNick(Nick.GetNick()); const CNick* pNick = Chan.FindNick(Nick.GetNick());
if (pNick) { if (pNick) {
if (pNick->HasPerm(CChan::Op) && pUser->ChannelMatches(Chan.GetName())) { if (pNick->HasPerm(CChan::Op) && pUser->ChannelMatches(Chan.GetName())) {
@@ -446,7 +446,7 @@ public:
const CChan& Chan = *Chans[a]; const CChan& Chan = *Chans[a];
if (Chan.HasPerm(CChan::Op) && User.ChannelMatches(Chan.GetName())) { if (Chan.HasPerm(CChan::Op) && User.ChannelMatches(Chan.GetName())) {
CNick* pNick = Chan.FindNick(Nick.GetNick()); const CNick* pNick = Chan.FindNick(Nick.GetNick());
if (pNick && !pNick->HasPerm(CChan::Op)) { if (pNick && !pNick->HasPerm(CChan::Op)) {
PutIRC("MODE " + Chan.GetName() + " +o " + Nick.GetNick()); PutIRC("MODE " + Chan.GetName() + " +o " + Nick.GetNick());
+3 -3
View File
@@ -302,9 +302,9 @@ private:
return TCL_ERROR; return TCL_ERROR;
} }
const map<CString,CNick*>& msNicks = pChannel->GetNicks(); const map<CString,CNick>& msNicks = pChannel->GetNicks();
for (map<CString,CNick*>::const_iterator it = msNicks.begin(); it != msNicks.end(); ++it) { for (map<CString,CNick>::const_iterator it = msNicks.begin(); it != msNicks.end(); ++it) {
const CNick& Nick = *it->second; const CNick& Nick = it->second;
l[0] = (Nick.GetNick()).c_str(); l[0] = (Nick.GetNick()).c_str();
l[1] = (Nick.GetIdent()).c_str(); l[1] = (Nick.GetIdent()).c_str();
l[2] = (Nick.GetHost()).c_str(); l[2] = (Nick.GetHost()).c_str();