QueueOnObject.h 878 B

12345678910111213141516171819202122232425262728
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <QObject>
  5. #include <utility>
  6. // QWidget and subclasses are not thread-safe! However, Qt's signal-slot connection mechanism will
  7. // invoke slots/functions on the correct thread for any object. We can (ab)use this to queue up
  8. // arbitrary code from non-GUI threads. For more information, see:
  9. // https://stackoverflow.com/questions/21646467/
  10. template <typename T, typename F>
  11. static void QueueOnObject(T* obj, F&& func)
  12. {
  13. QObject src;
  14. QObject::connect(&src, &QObject::destroyed, obj, std::forward<F>(func), Qt::QueuedConnection);
  15. }
  16. template <typename T, typename F>
  17. static void QueueOnObjectBlocking(T* obj, F&& func)
  18. {
  19. QObject src;
  20. QObject::connect(&src, &QObject::destroyed, obj, std::forward<F>(func),
  21. Qt::BlockingQueuedConnection);
  22. }