| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 
Oleksandr
2016/02/01 18:03:14
Nit: This file is also being added in https://code
 
Eric
2016/02/03 14:57:28
No. I made sure they were not interdependent. The
 | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 #ifndef _INSTANCES_H_ | |
| 19 #define _INSTANCES_H_ | |
| 20 | |
| 21 #include <mutex> | |
| 22 #include <set> | |
| 23 | |
| 24 /** | |
| 25 * Template class for a synchronized set of BHO instances | |
| 26 */ | |
| 27 template<class T, T nullValue> | |
| 28 class SyncSet | |
| 29 { | |
| 30 typedef std::lock_guard<std::mutex> SentryType; | |
| 31 | |
| 32 /** | |
| 33 * Underlying set container | |
| 34 */ | |
| 35 std::set<T> set; | |
| 36 | |
| 37 /** | |
| 38 * Synchronization primitive | |
| 39 */ | |
| 40 mutable std::mutex mutex; | |
| 41 | |
| 42 public: | |
| 43 /** | |
| 44 * \return | |
| 45 * true if (as expected) the argument was not already a member of the set | |
| 46 * false otherwise | |
| 47 * | |
| 48 * \par Postcondition | |
| 49 * - argument is a member of the set | |
| 50 */ | |
| 51 bool InsertIfAbsent(T p) | |
| 52 { | |
| 53 SentryType sentry(mutex); | |
| 54 auto it = set.find(p); | |
| 55 if (it != set.end()) | |
| 56 { | |
| 57 return false; | |
| 58 } | |
| 59 set.insert(p); | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * \return | |
| 65 * true if (as expected) the argument was already a member of the set | |
| 66 * false otherwise | |
| 67 * | |
| 68 * \par Postcondition | |
| 69 * - argument is not a member of the set | |
| 70 */ | |
| 71 bool EraseWithCheck(T p) | |
| 72 { | |
| 73 SentryType sentry(mutex); | |
| 74 auto it = set.find(p); | |
| 75 if (it == set.end()) | |
| 76 { | |
| 77 return false; | |
| 78 } | |
| 79 set.erase(it); | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * \param f | |
| 85 * Search criterion is a simple predicate function | |
| 86 * | |
| 87 * \return | |
| 88 * - If no member of the set meets the search criterion, the null value | |
| 89 * - Otherwise some member of the set that meets the search criterion | |
| 90 */ | |
| 91 T LinearSearch(std::function<bool(T)> f) | |
| 92 { | |
| 93 SentryType sentry(mutex); | |
| 94 for (auto member : set) | |
| 95 { | |
| 96 bool b = f(member); | |
| 97 if (b) | |
| 98 { | |
| 99 return member; | |
| 100 } | |
| 101 } | |
| 102 return nullValue; | |
| 103 } | |
| 104 | |
| 105 bool Empty() const | |
| 106 { | |
| 107 SentryType sentry(mutex); | |
| 108 return set.empty(); | |
| 109 } | |
| 110 }; | |
| 111 | |
| 112 #endif | |
| OLD | NEW |