| Index: src/plugin/Instances.h |
| =================================================================== |
| --- a/src/plugin/Instances.h |
| +++ b/src/plugin/Instances.h |
| @@ -15,11 +15,12 @@ |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| -#ifndef _INSTANCES_H_ |
| +#ifndef _INSTANCES_H_ |
| #define _INSTANCES_H_ |
| +#include <map> |
| #include <mutex> |
| -#include <map> |
| +#include <set> |
| /** |
| * A base class for a synchronized map from threads to BHO instances. |
| @@ -96,4 +97,92 @@ |
| } |
| }; |
| +/** |
| + * Template class for a synchronized set of BHO instances |
| + */ |
| +template<class T, T nullValue> |
| +class SyncSet |
| +{ |
| + typedef std::lock_guard<std::mutex> SentryType; |
| + |
| + /** |
| + * Underlying set container |
| + */ |
| + std::set<T> set; |
| + |
| + /** |
| + * Synchronization primitive |
| + */ |
| + mutable std::mutex mutex; |
| + |
| +public: |
| + /** |
| + * \return |
| + * true if (as expected) the argument was not already a member of the set |
| + * false otherwise |
| + * |
| + * \par Postcondition |
| + * - argument is a member of the set |
| + */ |
| + bool InsertIfAbsent(T p) |
| + { |
| + SentryType sentry(mutex); |
| + auto it = set.find(p); |
| + if (it != set.end()) |
| + { |
| + return false; |
| + } |
| + set.insert(p); |
| + return true; |
| + } |
| + |
| + /** |
| + * \return |
| + * true if (as expected) the argument was already a member of the set |
| + * false otherwise |
| + * |
| + * \par Postcondition |
| + * - argument is not a member of the set |
| + */ |
| + bool EraseWithCheck(T p) |
| + { |
| + SentryType sentry(mutex); |
| + auto it = set.find(p); |
| + if (it == set.end()) |
| + { |
| + return false; |
| + } |
| + set.erase(it); |
| + return true; |
| + } |
| + |
| + /** |
| + * \param f |
| + * Search criterion is a simple predicate function |
| + * |
| + * \return |
| + * - If no member of the set meets the search criterion, the null value |
| + * - Otherwise some member of the set that meets the search criterion |
| + */ |
| + T LinearSearch(std::function<bool(T)> f) |
| + { |
| + SentryType sentry(mutex); |
| + for (auto member : set) |
| + { |
| + bool b = f(member); |
| + if (b) |
| + { |
| + return member; |
| + } |
| + } |
| + return nullValue; |
| + } |
| + |
| + bool Empty() const |
| + { |
| + SentryType sentry(mutex); |
| + return set.empty(); |
| + } |
| +}; |
| + |
| #endif |