]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add stdalgo::isin() and use it to simplify code
authorAttila Molnar <attilamolnar@hush.com>
Sat, 1 Nov 2014 17:21:30 +0000 (18:21 +0100)
committerAttila Molnar <attilamolnar@hush.com>
Sat, 1 Nov 2014 17:21:30 +0000 (18:21 +0100)
include/stdalgo.h
src/configparser.cpp
src/logger.cpp
src/modules.cpp
src/modules/m_showfile.cpp

index cb01a250aac37e2dffc30cd0d069afeae6a9befa..3e00a4cdcd93030cdb2d3c56e1b48dac24599261 100644 (file)
@@ -112,4 +112,16 @@ namespace stdalgo
                }
                return false;
        }
+
+       /**
+        * Check if an element with the given value is in a container. Equivalent to (std::find(cont.begin(), cont.end(), val) != cont.end()).
+        * @param cont Container to find the element in
+        * @param val Value of the element to look for
+        * @return True if the element was found in the container, false otherwise
+        */
+       template <template<typename, typename> class Cont, typename T, typename Alloc>
+       inline bool isin(const Cont<T, Alloc>& cont, const T& val)
+       {
+               return (std::find(cont.begin(), cont.end(), val) != cont.end());
+       }
 }
index 89ad6493afaa3f627234e62917ce6bf7d225c659..6b0d8fa04fc473a6e6f4f6a99d23ef45ff31c78e 100644 (file)
@@ -367,7 +367,7 @@ void ParseStack::DoReadFile(const std::string& key, const std::string& name, int
 bool ParseStack::ParseFile(const std::string& path, int flags, const std::string& mandatory_tag, bool isexec)
 {
        ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading (isexec=%d) %s", isexec, path.c_str());
-       if (std::find(reading.begin(), reading.end(), path) != reading.end())
+       if (stdalgo::isin(reading, path))
                throw CoreException((isexec ? "Executable " : "File ") + path + " is included recursively (looped inclusion)");
 
        /* It's not already included, add it to the list of files we've loaded */
index 61f1eb17990d11bcd91d7c72135663306390425e..8bd5f7f88ac6697eb1ee09049db777f39b87bd80 100644 (file)
@@ -288,7 +288,7 @@ void LogManager::Log(const std::string &type, LogLevel loglevel, const std::stri
 
        for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
        {
-               if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
+               if (stdalgo::isin(gi->second, type))
                {
                        continue;
                }
index 4c0af3bace5e40baac7f348afb9a164f07d3a5a2..05a1552f48d03e405ef18d4ca6880da4c8962466 100644 (file)
@@ -178,7 +178,7 @@ ModuleManager::~ModuleManager()
 
 bool ModuleManager::Attach(Implementation i, Module* mod)
 {
-       if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
+       if (stdalgo::isin(EventHandlers[i], mod))
                return false;
 
        EventHandlers[i].push_back(mod);
index 132a2226765ece96cd62c7edf3dc44381310c1cb..cb51c4387cbf5e2a264d4120a015aaa63b01bc9f 100644 (file)
@@ -113,7 +113,7 @@ class ModuleShowFile : public Module
 
                        // This is our command, make sure we don't have the same entry twice
                        sfcmd = static_cast<CommandShowFile*>(handler);
-                       if (std::find(newcmds.begin(), newcmds.end(), sfcmd) != newcmds.end())
+                       if (stdalgo::isin(newcmds, sfcmd))
                                throw ModuleException("Command " + cmdname + " is already used in a <showfile> tag");
                }
                else