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

View File

@@ -128,18 +128,18 @@ void CChan::JoinUser(bool bForce, const CString& sKey, CClient* pClient) {
else
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()) {
sPerm = a->second->GetPermStr();
sPerm = a->second.GetPermStr();
} else {
char c = a->second->GetPermChar();
char c = a->second.GetPermChar();
sPerm = "";
if (c != '\0') {
sPerm += c;
}
}
if (pThisClient->HasUHNames() && !a->second->GetIdent().empty() && !a->second->GetHost().empty()) {
sNick = a->first + "!" + a->second->GetIdent() + "@" + a->second->GetHost();
if (pThisClient->HasUHNames() && !a->second.GetIdent().empty() && !a->second.GetHost().empty()) {
sNick = a->first + "!" + a->second.GetIdent() + "@" + a->second.GetHost();
} else {
sNick = a->first;
}
@@ -369,10 +369,6 @@ CString CChan::GetModeArg(CString& sArgs) const {
}
void CChan::ClearNicks() {
for (map<CString,CNick*>::iterator a = m_msNicks.begin(); a != m_msNicks.end(); ++a) {
delete a->second;
}
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;
}
@@ -442,9 +438,9 @@ bool CChan::AddNick(const CString& sNick) {
map<char, unsigned int> CChan::GetPermCounts() const {
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) {
CString sPerms = it->second->GetPermStr();
CString sPerms = it->second.GetPermStr();
for (unsigned int p = 0; p < sPerms.size(); p++) {
mRet[sPerms[p]]++;
@@ -455,7 +451,7 @@ map<char, unsigned int> CChan::GetPermCounts() const {
}
bool CChan::RemNick(const CString& sNick) {
map<CString,CNick*>::iterator it;
map<CString,CNick>::iterator it;
set<unsigned char>::iterator it2;
it = m_msNicks.find(sNick);
@@ -463,21 +459,20 @@ bool CChan::RemNick(const CString& sNick) {
return false;
}
delete it->second;
m_msNicks.erase(it);
return true;
}
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()) {
return false;
}
// 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
m_msNicks[sNewNick] = it->second;
@@ -486,9 +481,14 @@ bool CChan::ChangeNick(const CString& sOldNick, const CString& sNewNick) {
return true;
}
CNick* CChan::FindNick(const CString& sNick) const {
map<CString,CNick*>::const_iterator it = m_msNicks.find(sNick);
return (it != m_msNicks.end()) ? it->second : NULL;
const CNick* CChan::FindNick(const CString& sNick) const {
map<CString,CNick>::const_iterator it = m_msNicks.find(sNick);
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) {

7
Chan.h
View File

@@ -75,7 +75,8 @@ public:
// Nicks
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);
bool AddNick(const CString& sNick);
bool RemNick(const CString& sNick);
@@ -127,7 +128,7 @@ public:
unsigned int GetTopicDate() const { return m_ulTopicDate; }
const CString& GetDefaultModes() const { return m_sDefaultModes; }
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 GetBufferCount() const { return m_uBufferCount; }
bool KeepBuffer() const { return m_bKeepBuffer; }
@@ -154,7 +155,7 @@ protected:
CNick m_Nick;
unsigned int m_uJoinTries;
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;
vector<CString> m_vsBuffer;

View File

@@ -51,7 +51,7 @@ void CClient::UserCommand(CString& sLine) {
return;
}
const map<CString,CNick*>& msNicks = pChan->GetNicks();
const map<CString,CNick>& msNicks = pChan->GetNicks();
CIRCSock* pIRCSock = m_pUser->GetIRCSock();
const CString& sPerms = (pIRCSock) ? pIRCSock->GetPerms() : "";
@@ -72,20 +72,20 @@ void CClient::UserCommand(CString& sLine) {
Table.AddColumn("Ident");
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();
for (unsigned int b = 0; b < sPerms.size(); b++) {
if (a->second->HasPerm(sPerms[b])) {
if (a->second.HasPerm(sPerms[b])) {
CString sPerm;
sPerm += sPerms[b];
Table.SetCell(sPerm, sPerm);
}
}
Table.SetCell("Nick", a->second->GetNick());
Table.SetCell("Ident", a->second->GetIdent());
Table.SetCell("Host", a->second->GetHost());
Table.SetCell("Nick", a->second.GetNick());
Table.SetCell("Ident", a->second.GetIdent());
Table.SetCell("Host", a->second.GetHost());
}
PutStatus(Table);

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++) {
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)) {
vRetChans.push_back(pChan);
continue;

View File

@@ -124,8 +124,8 @@ protected:
return;
// Is that person us and we don't have op?
const CNick* pNick = Channel.GetNicks().begin()->second;
if (!pNick->HasPerm(CChan::Op) && pNick->GetNick().Equals(m_pUser->GetCurNick()))
const CNick& pNick = Channel.GetNicks().begin()->second;
if (!pNick.HasPerm(CChan::Op) && pNick.GetNick().Equals(m_pUser->GetCurNick()))
Channel.Cycle();
}

View File

@@ -352,7 +352,7 @@ public:
for (size_t a = 0; a < Chans.size(); a++) {
const CChan& Chan = *Chans[a];
CNick* pNick = Chan.FindNick(Nick.GetNick());
const CNick* pNick = Chan.FindNick(Nick.GetNick());
if (pNick) {
if (pNick->HasPerm(CChan::Op) && pUser->ChannelMatches(Chan.GetName())) {
@@ -446,7 +446,7 @@ public:
const CChan& Chan = *Chans[a];
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)) {
PutIRC("MODE " + Chan.GetName() + " +o " + Nick.GetNick());

View File

@@ -302,9 +302,9 @@ private:
return TCL_ERROR;
}
const map<CString,CNick*>& msNicks = pChannel->GetNicks();
for (map<CString,CNick*>::const_iterator it = msNicks.begin(); it != msNicks.end(); ++it) {
const CNick& Nick = *it->second;
const map<CString,CNick>& msNicks = pChannel->GetNicks();
for (map<CString,CNick>::const_iterator it = msNicks.begin(); it != msNicks.end(); ++it) {
const CNick& Nick = it->second;
l[0] = (Nick.GetNick()).c_str();
l[1] = (Nick.GetIdent()).c_str();
l[2] = (Nick.GetHost()).c_str();