crl_dispatch_queue.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <crl/common/crl_common_config.h>
  9. #if defined CRL_USE_DISPATCH && !defined CRL_FORCE_COMMON_QUEUE
  10. #include <crl/common/crl_common_utils.h>
  11. #include <memory>
  12. #include <atomic>
  13. namespace crl {
  14. class queue {
  15. public:
  16. queue();
  17. template <
  18. typename Callable,
  19. typename Return = decltype(std::declval<Callable>()())>
  20. void async(Callable &&callable) {
  21. using Function = std::decay_t<Callable>;
  22. if constexpr (details::is_plain_function_v<Function, Return>) {
  23. using Plain = Return(*)();
  24. const auto copy = static_cast<Plain>(callable);
  25. async_plain([](void *passed) {
  26. const auto callable = reinterpret_cast<Plain>(passed);
  27. (*callable)();
  28. }, reinterpret_cast<void*>(copy));
  29. } else {
  30. const auto copy = new Function(std::forward<Callable>(callable));
  31. async_plain([](void *passed) {
  32. const auto callable = static_cast<Function*>(passed);
  33. const auto guard = details::finally([=] { delete callable; });
  34. (*callable)();
  35. }, static_cast<void*>(copy));
  36. }
  37. }
  38. template <
  39. typename Callable,
  40. typename Return = decltype(std::declval<Callable>()())>
  41. void sync(Callable &&callable) {
  42. using Function = std::decay_t<Callable>;
  43. if constexpr (details::is_plain_function_v<Function, Return>) {
  44. using Plain = Return(*)();
  45. const auto copy = static_cast<Plain>(callable);
  46. sync_plain([](void *passed) {
  47. const auto callable = reinterpret_cast<Plain>(passed);
  48. (*callable)();
  49. }, reinterpret_cast<void*>(copy));
  50. } else {
  51. const auto copy = new Function(std::forward<Callable>(callable));
  52. sync_plain([](void *passed) {
  53. const auto callable = static_cast<Function*>(passed);
  54. const auto guard = details::finally([=] { delete callable; });
  55. (*callable)();
  56. }, static_cast<void*>(copy));
  57. }
  58. }
  59. private:
  60. // Hide dispatch_queue_t
  61. struct implementation {
  62. using pointer = void*;
  63. static pointer create();
  64. void operator()(pointer value);
  65. };
  66. void async_plain(void (*callable)(void*), void *argument);
  67. void sync_plain(void (*callable)(void*), void *argument);
  68. std::unique_ptr<implementation::pointer, implementation> _handle;
  69. };
  70. } // namespace crl
  71. #endif // CRL_USE_DISPATCH && !CRL_FORCE_COMMON_QUEUE