Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
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
| |
26 * | |
27 */ | |
28 template<class T, T nullValue> | |
29 class SyncSet | |
30 { | |
31 typedef std::lock_guard<std::mutex> SentryType; | |
32 | |
33 /** | |
34 * Underlying set container | |
35 */ | |
36 std::set<T> set; | |
37 | |
38 /** | |
39 * Synchronization primitive | |
40 */ | |
41 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(
| |
42 | |
43 public: | |
44 /** | |
45 * \return | |
46 * true if (as expected) the argument was not already a member of the set | |
47 * false otherwise | |
48 * | |
49 * \par Postcondition | |
50 * - argument is a member of the set | |
51 */ | |
52 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.
| |
53 { | |
54 SentryType sentry(mutex); | |
55 auto it = set.find(p); | |
56 if (it != set.end()) | |
57 { | |
58 return false; | |
59 } | |
60 set.insert(p); | |
61 return true; | |
62 } | |
63 | |
64 /** | |
65 * \return | |
66 * true if (as expected) the argument was already a member of the set | |
67 * false otherwise | |
68 * | |
69 * \par Postcondition | |
70 * - argument is not a member of the set | |
71 */ | |
72 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.
| |
73 { | |
74 SentryType sentry(mutex); | |
75 auto it = set.find(p); | |
76 if (it == set.end()) | |
77 { | |
78 return false; | |
79 } | |
80 set.erase(it); | |
81 return true; | |
82 } | |
83 | |
84 /** | |
85 * \param f | |
86 * Search criterion is a simple predicate function | |
87 * | |
88 * \return | |
89 * - If no member of the set meets the search criterion, the null value | |
90 * - Otherwise some member of the set that meets the search criterion | |
91 */ | |
92 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
| |
93 { | |
94 SentryType sentry(mutex); | |
95 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.
| |
96 { | |
97 bool b = f(it); | |
98 if (b) | |
99 { | |
100 return it; | |
101 } | |
102 } | |
103 return nullValue; | |
104 } | |
105 | |
106 bool Empty() const | |
107 { | |
108 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
| |
109 } | |
110 }; | |
111 | |
112 #endif | |
OLD | NEW |