2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * Erase a single element from a vector by overwriting it with a copy of the last element,
28 * which is then removed. This, in contrast to vector::erase(), does not result in all
29 * elements after the erased element being moved.
30 * @param vect Vector to remove the element from
31 * @param it Iterator to the element to remove
32 * @return Nothing, but all iterators, references and pointers to the erased element and the
33 * last element are invalidated
36 inline void swaperase(typename std::vector<T>& vect, const typename std::vector<T>::iterator& it)
43 * Find and if exists, erase a single element from a vector by overwriting it with a
44 * copy of the last element, which is then removed. This, in contrast to vector::erase(),
45 * does not result in all elements after the erased element being moved.
46 * If the given value occurs multiple times, the one with the lowest index is removed.
47 * Individual elements are compared to the given value using operator==().
48 * @param vect Vector to remove the element from
49 * @param val Value of the element to look for and remove
50 * @return True if the element was found and removed, false if it wasn't found.
51 * If true, all iterators, references and pointers pointing to either the first element that
52 * is equal to val or to the last element are invalidated.
55 inline bool swaperase(typename std::vector<T>& vect, const T& val)
57 const typename std::vector<T>::iterator it = std::find(vect.begin(), vect.end(), val);
69 /** Get underlying C string of the string passed as parameter. Useful in template functions.
71 * @return Same as input
73 inline const char* tocstr(const char* str)
78 /** Get underlying C string of the string passed as parameter. Useful in template functions.
79 * @param str std::string object
82 inline const char* tocstr(const std::string& str)
87 /** Check if two strings are equal case insensitively.
88 * @param str1 First string to compare.
89 * @param str2 Second string to compare.
90 * @return True if the strings are equal case-insensitively, false otherwise.
92 template <typename S1, typename S2>
93 inline bool equalsci(const S1& str1, const S2& str2)
95 return (!strcasecmp(tocstr(str1), tocstr(str2)));
98 /** Replace first occurrence of a substring ('target') in a string ('str') with another string ('replacement').
99 * @param str String to perform replacement in
100 * @param target String to replace
101 * @param replacement String to put in place of 'target'
102 * @return True if 'target' was replaced with 'replacement', false if it was not found in 'str'.
104 template<typename CharT, typename Traits, typename Alloc>
105 inline bool replace(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
107 const typename std::basic_string<CharT, Traits, Alloc>::size_type p = str.find(target);
108 if (p == std::basic_string<CharT, Traits, Alloc>::npos)
110 str.replace(p, target.size(), replacement);
114 /** Replace all occurrences of a string ('target') in a string ('str') with another string ('replacement').
115 * @param str String to perform replacement in
116 * @param target String to replace
117 * @param replacement String to put in place of 'target'
119 template<typename CharT, typename Traits, typename Alloc>
120 inline void replace_all(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
125 typename std::basic_string<CharT, Traits, Alloc>::size_type p = 0;
126 while ((p = str.find(target, p)) != std::basic_string<CharT, Traits, Alloc>::npos)
128 str.replace(p, target.size(), replacement);
129 p += replacement.size();
135 * Deleter that uses operator delete to delete the item
137 template <typename T>
138 struct defaultdeleter
140 void operator()(T* o)
147 * Deleter that adds the item to the cull list, that is, queues it for
148 * deletion at the end of the current mainloop iteration
152 void operator()(classbase* item);
156 * Deletes all elements in a container using operator delete
157 * @param cont The container containing the elements to delete
159 template <template<typename, typename> class Cont, typename T, typename Alloc>
160 inline void delete_all(const Cont<T*, Alloc>& cont)
162 std::for_each(cont.begin(), cont.end(), defaultdeleter<T>());
166 * Remove an element from a container
167 * @param cont Container to remove the element from
168 * @param val Value of the element to look for and remove
169 * @return True if the element was found and removed, false otherwise
171 template <template<typename, typename> class Cont, typename T, typename Alloc>
172 inline bool erase(Cont<T, Alloc>& cont, const T& val)
174 const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
175 if (it != cont.end())
184 * Check if an element with the given value is in a container. Equivalent to (std::find(cont.begin(), cont.end(), val) != cont.end()).
185 * @param cont Container to find the element in
186 * @param val Value of the element to look for
187 * @return True if the element was found in the container, false otherwise
189 template <template<typename, typename> class Cont, typename T, typename Alloc>
190 inline bool isin(const Cont<T, Alloc>& cont, const T& val)
192 return (std::find(cont.begin(), cont.end(), val) != cont.end());