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

safe_callback.hpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c2f48ae72fb0ea5ade97ff4c9ad39cd4731d850b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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.
// If not initialized nothing will be done.
// *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
  {
    if (m_fn)
      GetPlatform().RunTask(Platform::Thread::Gui, std::bind(m_fn, std::move(args)...));
  }

private:
  std::function<R(Args...)> m_fn;
};
}  // namespace platform