Index: src/plugin/Instances.h |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/src/plugin/Instances.h |
@@ -0,0 +1,112 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+#ifndef _INSTANCES_H_ |
+#define _INSTANCES_H_ |
+ |
+#include <mutex> |
+#include <set> |
+ |
+/** |
+ * Template class for a synchronized set of BHO instances |
sergei
2016/01/04 15:34:31
Do you mind to change the comment a bit?
If it's
Eric
2016/01/04 17:49:20
I don't see a need, given what you've said so far.
sergei
2016/01/29 15:28:55
I disagree that the description of generic templat
Eric
2016/01/29 16:16:12
Your objection here is that you don't like the tem
Oleksandr
2016/02/01 18:03:13
I think Sergei was merely talking about the commen
Eric
2016/02/03 14:57:27
I don't believe so. He has said more that once tha
|
+ * |
+ */ |
+template<class T, T nullValue> |
+class SyncSet |
+{ |
+ typedef std::lock_guard<std::mutex> SentryType; |
+ |
+ /** |
+ * Underlying set container |
+ */ |
+ std::set<T> set; |
+ |
+ /** |
+ * Synchronization primitive |
+ */ |
+ std::mutex mutex; |
sergei
2016/01/04 15:34:31
I guess it should be `mutable` because it can be u
Eric
2016/01/04 17:49:20
Done.
The only 'const' method, though, is 'Empty(
|
+ |
+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) |
sergei
2016/01/04 15:34:30
it would be better to have instead methods which a
Eric
2016/01/04 17:49:20
If this were generic library code, we'd want decla
sergei
2016/01/29 15:28:55
I disagree with that attitude.
I think it should b
Eric
2016/01/29 16:16:12
You haven't identified a defect here.
You have no
Oleksandr
2016/02/01 18:03:13
I don't have a strong opinion on this, but I agree
Eric
2016/02/03 14:57:27
Answered above.
|
+ { |
+ 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) |
sergei
2016/01/04 15:34:30
The argument should be `const T&`.
Eric
2016/01/04 17:49:20
See above. Pass by value is desirable.
|
+ { |
+ 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) |
sergei
2016/01/04 15:34:30
I would like to have a constant reference to T as
sergei
2016/01/04 15:34:30
Should it be a constant method?
sergei
2016/01/04 15:34:31
it would be also good to pass the predicate as a c
Eric
2016/01/04 17:49:20
It's only ever used with lambda functions, so an l
Eric
2016/01/04 17:49:20
No.
If we wanted a second, 'const'-aware definiti
Eric
2016/01/04 17:49:20
We're passing 'T' by value everywhere.
sergei
2016/01/29 15:28:55
It's just a dirty code, isn't it clear?
sergei
2016/01/29 15:28:55
This method does not change the main sate of the c
Eric
2016/01/29 16:16:12
The whole discussion of 'const' declaration here h
|
+ { |
+ SentryType sentry(mutex); |
+ for (auto it : set) |
sergei
2016/01/04 15:34:30
JIC, `it` does not refer to an iterator here, I wo
Eric
2016/01/04 17:49:20
OK.
|
+ { |
+ bool b = f(it); |
+ if (b) |
+ { |
+ return it; |
+ } |
+ } |
+ return nullValue; |
+ } |
+ |
+ bool Empty() const |
+ { |
+ return set.empty(); |
sergei
2016/01/04 15:34:30
SentryType sentry(mutex);
is missed here
Eric
2016/01/04 17:49:20
This function is only instantiated in the unit tes
|
+ } |
+}; |
+ |
+#endif |