OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef _INSTANCES_H_ | 18 #ifndef _INSTANCES_H_ |
19 #define _INSTANCES_H_ | 19 #define _INSTANCES_H_ |
20 | 20 |
| 21 #include <map> |
21 #include <mutex> | 22 #include <mutex> |
22 #include <map> | 23 #include <set> |
23 | 24 |
24 /** | 25 /** |
25 * A base class for a synchronized map from threads to BHO instances. | 26 * A base class for a synchronized map from threads to BHO instances. |
26 * | 27 * |
27 * This is a base class for 'CurrentThreadMap', defined in PluginClass.cpp. | 28 * This is a base class for 'CurrentThreadMap', defined in PluginClass.cpp. |
28 * It's separated out here for testability. | 29 * It's separated out here for testability. |
29 * This class should not be used as polymorphic base class, as it has no virtual
base class. | 30 * This class should not be used as polymorphic base class, as it has no virtual
base class. |
30 * | 31 * |
31 * The member functions here not simply forwarded versions of the container func
tions. | 32 * The member functions here not simply forwarded versions of the container func
tions. |
32 * Rather, they are specialized for tracking BHO calls to SetSite(). | 33 * Rather, they are specialized for tracking BHO calls to SetSite(). |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 * Returns nullValue otherwise. | 90 * Returns nullValue otherwise. |
90 */ | 91 */ |
91 T Locate(Key id) const | 92 T Locate(Key id) const |
92 { | 93 { |
93 SentryType sentry(mutex); | 94 SentryType sentry(mutex); |
94 auto it = idMap.find(id); | 95 auto it = idMap.find(id); |
95 return (it != idMap.end()) ? it->second : nullValue; | 96 return (it != idMap.end()) ? it->second : nullValue; |
96 } | 97 } |
97 }; | 98 }; |
98 | 99 |
| 100 /** |
| 101 * Template class for a synchronized set of BHO instances |
| 102 */ |
| 103 template<class T, T nullValue> |
| 104 class SyncSet |
| 105 { |
| 106 typedef std::lock_guard<std::mutex> SentryType; |
| 107 |
| 108 /** |
| 109 * Underlying set container |
| 110 */ |
| 111 std::set<T> set; |
| 112 |
| 113 /** |
| 114 * Synchronization primitive |
| 115 */ |
| 116 mutable std::mutex mutex; |
| 117 |
| 118 public: |
| 119 /** |
| 120 * \return |
| 121 * true if (as expected) the argument was not already a member of the set |
| 122 * false otherwise |
| 123 * |
| 124 * \par Postcondition |
| 125 * - argument is a member of the set |
| 126 */ |
| 127 bool InsertIfAbsent(T p) |
| 128 { |
| 129 SentryType sentry(mutex); |
| 130 auto it = set.find(p); |
| 131 if (it != set.end()) |
| 132 { |
| 133 return false; |
| 134 } |
| 135 set.insert(p); |
| 136 return true; |
| 137 } |
| 138 |
| 139 /** |
| 140 * \return |
| 141 * true if (as expected) the argument was already a member of the set |
| 142 * false otherwise |
| 143 * |
| 144 * \par Postcondition |
| 145 * - argument is not a member of the set |
| 146 */ |
| 147 bool EraseWithCheck(T p) |
| 148 { |
| 149 SentryType sentry(mutex); |
| 150 auto it = set.find(p); |
| 151 if (it == set.end()) |
| 152 { |
| 153 return false; |
| 154 } |
| 155 set.erase(it); |
| 156 return true; |
| 157 } |
| 158 |
| 159 /** |
| 160 * \param f |
| 161 * Search criterion is a simple predicate function |
| 162 * |
| 163 * \return |
| 164 * - If no member of the set meets the search criterion, the null value |
| 165 * - Otherwise some member of the set that meets the search criterion |
| 166 */ |
| 167 T LinearSearch(std::function<bool(T)> f) |
| 168 { |
| 169 SentryType sentry(mutex); |
| 170 for (auto member : set) |
| 171 { |
| 172 bool b = f(member); |
| 173 if (b) |
| 174 { |
| 175 return member; |
| 176 } |
| 177 } |
| 178 return nullValue; |
| 179 } |
| 180 |
| 181 bool Empty() const |
| 182 { |
| 183 SentryType sentry(mutex); |
| 184 return set.empty(); |
| 185 } |
| 186 }; |
| 187 |
99 #endif | 188 #endif |
OLD | NEW |