Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'platform/safe_callback.hpp')
-rw-r--r--platform/safe_callback.hpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/platform/safe_callback.hpp b/platform/safe_callback.hpp
new file mode 100644
index 0000000000..313e095fc5
--- /dev/null
+++ b/platform/safe_callback.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include "platform/platform.hpp"
+
+#include <future>
+#include <functional>
+#include <utility>
+
+namespace platform
+{
+template <typename T>
+class SafeCallback;
+
+// Calls callback on main thread, all params are copied.
+// *NOTE* The class is not thread-safe.
+template <typename R, typename ...Args>
+class SafeCallback<R(Args...)>
+{
+public:
+ SafeCallback() = default;
+
+ template <typename Fn>
+ SafeCallback(Fn const & fn)
+ : m_fn(fn)
+ {
+ }
+
+ operator bool() const noexcept
+ {
+ return static_cast<bool>(m_fn);
+ }
+
+ void operator()(Args... args) const
+ {
+ GetPlatform().RunOnGuiThread(std::bind(m_fn, std::move(args)...));
+ }
+
+private:
+ std::function<R(Args...)> m_fn;
+};
+} // namespace platform