Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 <mutex> | 21 #include <mutex> |
22 #include <map> | 22 #include <map> |
23 | 23 |
24 /** | 24 /** |
25 * A base class for a synchronized map from threads to BHO instances. | 25 * A base class for a synchronized map from threads to BHO instances. |
sergei
2016/05/23 13:14:49
As I told already there is no reason to have such
sergei
2016/05/23 13:14:49
This class is not designed to be a base class beca
Eric
2016/07/17 16:04:45
The rule is that you need a virtual destructor in
Eric
2016/07/17 16:04:45
And as *I* said before, it is *not* only for insta
| |
26 * | |
27 * This is a base class for 'CurrentThreadMap', defined in PluginClass.cpp. | |
28 * 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. | |
26 * | 30 * |
27 * The member functions here not simply forwarded versions of the container func tions. | 31 * The member functions here not simply forwarded versions of the container func tions. |
28 * Rather, they are specialized for tracking BHO calls to SetSite(). | 32 * Rather, they are specialized for tracking BHO calls to SetSite(). |
29 * Their semantics allow for verification that the call pattern is as expected. | 33 * Their semantics allow for verification that the call pattern is as expected. |
30 * | 34 * |
31 * The key to the map (in the subclass) is the thread ID, thus operations are se rialized on a per-key basis. | 35 * The key to the map (in the subclass) is the thread ID, thus operations are se rialized on a per-key basis. |
32 * Calls to SetSite() bracket all other calls, so on a per-key basis | 36 * Calls to SetSite() bracket all other calls, so on a per-key basis |
33 * the order of operations is always either (insert / find-success / erase) or (find-failure). | 37 * the order of operations is always either (insert / find-success / erase) or (find-failure). |
34 * The library guarantees for std::map seem to indicate that operations on diffe rent keys | 38 * The library guarantees for std::map seem to indicate that operations on diffe rent keys |
35 * do not interfer with each other, but there's some ambiguity there. | 39 * do not interfer with each other, but there's some ambiguity there. |
36 * This class is synchronized as a matter of defensive programming. | 40 * This class is synchronized as a matter of defensive programming. |
37 */ | 41 */ |
sergei
2016/05/23 13:14:49
We can save a lot of energy if move the implementa
Eric
2016/07/17 16:04:45
Everything in this source file can be easily unit
| |
38 template<class Key, class T, T nullValue> | 42 template<class Key, class T, T nullValue> |
39 class SyncMap | 43 class SyncMap |
40 { | 44 { |
41 typedef std::lock_guard<std::mutex> SentryType; | 45 typedef std::lock_guard<std::mutex> SentryType; |
42 | 46 |
43 /** | 47 /** |
44 * Underlying map container | 48 * Underlying map container |
45 */ | 49 */ |
46 std::map<Key, T> idMap; | 50 std::map<Key, T> idMap; |
47 | 51 |
48 /** | 52 /** |
49 * Synchronization primitive | 53 * Synchronization primitive |
50 */ | 54 */ |
51 mutable std::mutex mutex; | 55 mutable std::mutex mutex; |
52 | 56 |
53 public: | 57 public: |
54 /** | 58 /** |
55 * Returns true if (as expected) no key of value 'id' was present. | 59 * Returns true if (as expected) no key of value 'id' was present. |
56 * Returns false otherwise. | 60 * Returns false otherwise. |
57 */ | 61 */ |
58 bool AddIfAbsent(Key id, T p) | 62 bool AddIfAbsent(Key id, T p) |
59 { | 63 { |
60 SentryType sentry(mutex); | 64 SentryType sentry(mutex); |
61 auto it = idMap.find(id); | 65 auto it = idMap.insert(std::make_pair(id,p)); |
sergei
2016/07/19 07:56:09
missing space after comma
Eric
2016/07/27 20:14:22
Fixed.
| |
62 if (it != idMap.end()) | 66 return it.second; |
63 { | 67 // Assert it.second==true implies the insertion took place, |
64 return false; | 68 // which means there was no key of value 'id' already present. |
sergei
2016/07/19 07:56:10
I think we don't need this comment.
| |
65 } | |
66 idMap[id] = p; | |
67 return true; | |
68 } | 69 } |
69 | 70 |
70 /** | 71 /** |
71 * Returns true if (as expected) a key of value 'id' was already present. | 72 * Returns true if (as expected) a key of value 'id' was already present. |
72 * Returns false otherwise. | 73 * Returns false otherwise. |
73 */ | 74 */ |
74 bool RemoveIfPresent(Key id) | 75 bool RemoveIfPresent(Key id) |
75 { | 76 { |
76 SentryType sentry(mutex); | 77 SentryType sentry(mutex); |
77 auto it = idMap.find(id); | 78 auto it = idMap.find(id); |
(...skipping 11 matching lines...) Expand all Loading... | |
89 */ | 90 */ |
90 T Locate(Key id) const | 91 T Locate(Key id) const |
91 { | 92 { |
92 SentryType sentry(mutex); | 93 SentryType sentry(mutex); |
93 auto it = idMap.find(id); | 94 auto it = idMap.find(id); |
94 return (it != idMap.end()) ? it->second : nullValue; | 95 return (it != idMap.end()) ? it->second : nullValue; |
95 } | 96 } |
96 }; | 97 }; |
97 | 98 |
98 #endif | 99 #endif |
LEFT | RIGHT |