Files
comaps/platform/safe_callback.hpp
Konstantin Pastbin e3e4a1985a Organic Maps sources as of 02.04.2025 (fad26bbf22ac3da75e01e62aa01e5c8e11861005)
To expand with full Organic Maps and Maps.ME commits history run:
  git remote add om-historic [om-historic.git repo url]
  git fetch --tags om-historic
  git replace squashed-history historic-commits
2025-05-08 21:10:51 +07:00

44 lines
801 B
C++

#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