// This file is part of Desktop App Toolkit, // a set of libraries for developing nice desktop applications. // // For license and copyright information please follow this link: // https://github.com/desktop-app/legal/blob/master/LEGAL // #pragma once #include #if defined CRL_USE_DISPATCH && !defined CRL_FORCE_COMMON_QUEUE #include #include #include namespace crl { class queue { public: queue(); template < typename Callable, typename Return = decltype(std::declval()())> void async(Callable &&callable) { using Function = std::decay_t; if constexpr (details::is_plain_function_v) { using Plain = Return(*)(); const auto copy = static_cast(callable); async_plain([](void *passed) { const auto callable = reinterpret_cast(passed); (*callable)(); }, reinterpret_cast(copy)); } else { const auto copy = new Function(std::forward(callable)); async_plain([](void *passed) { const auto callable = static_cast(passed); const auto guard = details::finally([=] { delete callable; }); (*callable)(); }, static_cast(copy)); } } template < typename Callable, typename Return = decltype(std::declval()())> void sync(Callable &&callable) { using Function = std::decay_t; if constexpr (details::is_plain_function_v) { using Plain = Return(*)(); const auto copy = static_cast(callable); sync_plain([](void *passed) { const auto callable = reinterpret_cast(passed); (*callable)(); }, reinterpret_cast(copy)); } else { const auto copy = new Function(std::forward(callable)); sync_plain([](void *passed) { const auto callable = static_cast(passed); const auto guard = details::finally([=] { delete callable; }); (*callable)(); }, static_cast(copy)); } } private: // Hide dispatch_queue_t struct implementation { using pointer = void*; static pointer create(); void operator()(pointer value); }; void async_plain(void (*callable)(void*), void *argument); void sync_plain(void (*callable)(void*), void *argument); std::unique_ptr _handle; }; } // namespace crl #endif // CRL_USE_DISPATCH && !CRL_FORCE_COMMON_QUEUE