summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAttila Molnar <attilamolnar@hush.com>2014-11-01 18:17:04 +0100
committerAttila Molnar <attilamolnar@hush.com>2014-11-01 18:17:04 +0100
commitfbc73e20784b055485f676096e758d6aeed62e0c (patch)
tree8251c8473af414527becb989558a69c4e7dc49b4
parent5fb509060fa7552a25efccad11595898d420d476 (diff)
Add stdalgo::erase() and use it to simplify code
-rw-r--r--include/stdalgo.h18
-rw-r--r--src/logger.cpp10
-rw-r--r--src/modules.cpp15
-rw-r--r--src/modules/m_callerid.cpp11
-rw-r--r--src/modules/m_dccallow.cpp6
-rw-r--r--src/modules/m_spanningtree/treeserver.cpp8
-rw-r--r--src/xline.cpp8
7 files changed, 29 insertions, 47 deletions
diff --git a/include/stdalgo.h b/include/stdalgo.h
index afbd763fb..cb01a250a 100644
--- a/include/stdalgo.h
+++ b/include/stdalgo.h
@@ -94,4 +94,22 @@ namespace stdalgo
{
std::for_each(cont.begin(), cont.end(), defaultdeleter<T>());
}
+
+ /**
+ * Remove an element from a container
+ * @param cont Container to remove the element from
+ * @param val Value of the element to look for and remove
+ * @return True if the element was found and removed, false otherwise
+ */
+ template <template<typename, typename> class Cont, typename T, typename Alloc>
+ inline bool erase(Cont<T, Alloc>& cont, const T& val)
+ {
+ const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
+ if (it != cont.end())
+ {
+ cont.erase(it);
+ return true;
+ }
+ return false;
+ }
}
diff --git a/src/logger.cpp b/src/logger.cpp
index bf4ef582a..61f1eb179 100644
--- a/src/logger.cpp
+++ b/src/logger.cpp
@@ -207,10 +207,9 @@ void LogManager::DelLogStream(LogStream* l)
{
for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
{
- std::vector<LogStream*>::iterator it;
- while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
+ while (stdalgo::erase(i->second, l))
{
- i->second.erase(it);
+ // Keep erasing while it exists
}
}
@@ -236,11 +235,8 @@ bool LogManager::DelLogType(const std::string &type, LogStream *l)
if (i != LogStreams.end())
{
- std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
-
- if (it != i->second.end())
+ if (stdalgo::erase(i->second, l))
{
- i->second.erase(it);
if (i->second.size() == 0)
{
LogStreams.erase(i);
diff --git a/src/modules.cpp b/src/modules.cpp
index 0a5a8a702..4c0af3bac 100644
--- a/src/modules.cpp
+++ b/src/modules.cpp
@@ -165,12 +165,7 @@ ServiceProvider::ServiceProvider(Module* Creator, const std::string& Name, Servi
void ServiceProvider::DisableAutoRegister()
{
if ((ServerInstance) && (ServerInstance->Modules->NewServices))
- {
- ModuleManager::ServiceList& list = *ServerInstance->Modules->NewServices;
- ModuleManager::ServiceList::iterator it = std::find(list.begin(), list.end(), this);
- if (it != list.end())
- list.erase(it);
- }
+ stdalgo::erase(*ServerInstance->Modules->NewServices, this);
}
ModuleManager::ModuleManager()
@@ -192,13 +187,7 @@ bool ModuleManager::Attach(Implementation i, Module* mod)
bool ModuleManager::Detach(Implementation i, Module* mod)
{
- EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
-
- if (x == EventHandlers[i].end())
- return false;
-
- EventHandlers[i].erase(x);
- return true;
+ return stdalgo::erase(EventHandlers[i], mod);
}
void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
diff --git a/src/modules/m_callerid.cpp b/src/modules/m_callerid.cpp
index f15760dca..7f615494b 100644
--- a/src/modules/m_callerid.cpp
+++ b/src/modules/m_callerid.cpp
@@ -136,10 +136,7 @@ struct CallerIDExtInfo : public ExtensionItem
continue; // shouldn't happen, but oh well.
}
- std::list<callerid_data*>::iterator it2 = std::find(targ->wholistsme.begin(), targ->wholistsme.end(), dat);
- if (it2 != targ->wholistsme.end())
- targ->wholistsme.erase(it2);
- else
+ if (!stdalgo::erase(targ->wholistsme, dat))
ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
}
delete dat;
@@ -323,11 +320,7 @@ public:
return false;
}
- std::list<callerid_data*>::iterator it = std::find(dat2->wholistsme.begin(), dat2->wholistsme.end(), dat);
- if (it != dat2->wholistsme.end())
- // Found me!
- dat2->wholistsme.erase(it);
- else
+ if (!stdalgo::erase(dat2->wholistsme, dat))
ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
diff --git a/src/modules/m_dccallow.cpp b/src/modules/m_dccallow.cpp
index cc7fbd323..1b838be5c 100644
--- a/src/modules/m_dccallow.cpp
+++ b/src/modules/m_dccallow.cpp
@@ -268,11 +268,7 @@ class ModuleDCCAllow : public Module
// remove their DCCALLOW list if they have one
if (udl)
- {
- userlist::iterator it = std::find(ul.begin(), ul.end(), user);
- if (it != ul.end())
- ul.erase(it);
- }
+ stdalgo::erase(ul, user);
// remove them from any DCCALLOW lists
// they are currently on
diff --git a/src/modules/m_spanningtree/treeserver.cpp b/src/modules/m_spanningtree/treeserver.cpp
index 009478635..98d7c8754 100644
--- a/src/modules/m_spanningtree/treeserver.cpp
+++ b/src/modules/m_spanningtree/treeserver.cpp
@@ -322,13 +322,7 @@ void TreeServer::AddChild(TreeServer* Child)
bool TreeServer::DelChild(TreeServer* Child)
{
- std::vector<TreeServer*>::iterator it = std::find(Children.begin(), Children.end(), Child);
- if (it != Children.end())
- {
- Children.erase(it);
- return true;
- }
- return false;
+ return stdalgo::erase(Children, Child);
}
CullResult TreeServer::cull()
diff --git a/src/xline.cpp b/src/xline.cpp
index 3eb556234..13124a392 100644
--- a/src/xline.cpp
+++ b/src/xline.cpp
@@ -312,9 +312,7 @@ bool XLineManager::DelLine(const char* hostmask, const std::string &type, User*
y->second->Unset();
- std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
- if (pptr != pending_lines.end())
- pending_lines.erase(pptr);
+ stdalgo::erase(pending_lines, y->second);
delete y->second;
x->second.erase(y);
@@ -419,9 +417,7 @@ void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
* is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
* -- Brain
*/
- std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
- if (pptr != pending_lines.end())
- pending_lines.erase(pptr);
+ stdalgo::erase(pending_lines, item->second);
delete item->second;
container->second.erase(item);