Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/plugin/Instances.h

Issue 29333107: Issue #1652, #3456 - Replace 's_asyncWebBrowser2' with thread-safe facilities
Patch Set: address comments Created Jan. 4, 2016, 5:51 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/plugin/Instances.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/plugin/Instances.h
@@ -0,0 +1,112 @@
+/*
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
+ * 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
+ */
+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
« no previous file with comments | « adblockplus.gyp ('k') | src/plugin/PluginClass.h » ('j') | src/plugin/PluginClass.h » ('J')

Powered by Google App Engine
This is Rietveld