utils.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright (c) 2019-2021 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_UTILS_H
  9. #define RTC_UTILS_H
  10. #include <functional>
  11. #include <memory>
  12. #include <mutex>
  13. #include <optional>
  14. #include <tuple>
  15. #include <utility>
  16. namespace rtc {
  17. // overloaded helper
  18. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  19. template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
  20. // weak_ptr bind helper
  21. template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&..._args) {
  22. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&...args) {
  23. if (auto shared_this = weak_this.lock())
  24. return bound(args...);
  25. else
  26. return static_cast<decltype(bound(args...))>(false);
  27. };
  28. }
  29. // scope_guard helper
  30. class scope_guard final {
  31. public:
  32. scope_guard(std::function<void()> func) : function(std::move(func)) {}
  33. scope_guard(scope_guard &&other) = delete;
  34. scope_guard(const scope_guard &) = delete;
  35. void operator=(const scope_guard &) = delete;
  36. ~scope_guard() {
  37. if (function)
  38. function();
  39. }
  40. private:
  41. std::function<void()> function;
  42. };
  43. // callback with built-in synchronization
  44. template <typename... Args> class synchronized_callback {
  45. public:
  46. synchronized_callback() = default;
  47. synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
  48. synchronized_callback(const synchronized_callback &cb) { *this = cb; }
  49. synchronized_callback(std::function<void(Args...)> func) { *this = std::move(func); }
  50. virtual ~synchronized_callback() { *this = nullptr; }
  51. synchronized_callback &operator=(synchronized_callback &&cb) {
  52. std::scoped_lock lock(mutex, cb.mutex);
  53. set(std::exchange(cb.callback, nullptr));
  54. return *this;
  55. }
  56. synchronized_callback &operator=(const synchronized_callback &cb) {
  57. std::scoped_lock lock(mutex, cb.mutex);
  58. set(cb.callback);
  59. return *this;
  60. }
  61. synchronized_callback &operator=(std::function<void(Args...)> func) {
  62. std::lock_guard lock(mutex);
  63. set(std::move(func));
  64. return *this;
  65. }
  66. bool operator()(Args... args) const {
  67. std::lock_guard lock(mutex);
  68. return call(std::move(args)...);
  69. }
  70. operator bool() const {
  71. std::lock_guard lock(mutex);
  72. return callback ? true : false;
  73. }
  74. protected:
  75. virtual void set(std::function<void(Args...)> func) { callback = std::move(func); }
  76. virtual bool call(Args... args) const {
  77. if (!callback)
  78. return false;
  79. callback(std::move(args)...);
  80. return true;
  81. }
  82. std::function<void(Args...)> callback;
  83. mutable std::recursive_mutex mutex;
  84. };
  85. // callback with built-in synchronization and replay of the last missed call
  86. template <typename... Args>
  87. class synchronized_stored_callback final : public synchronized_callback<Args...> {
  88. public:
  89. template <typename... CArgs>
  90. synchronized_stored_callback(CArgs &&...cargs)
  91. : synchronized_callback<Args...>(std::forward<CArgs>(cargs)...) {}
  92. ~synchronized_stored_callback() {}
  93. private:
  94. void set(std::function<void(Args...)> func) {
  95. synchronized_callback<Args...>::set(func);
  96. if (func && stored) {
  97. std::apply(func, std::move(*stored));
  98. stored.reset();
  99. }
  100. }
  101. bool call(Args... args) const {
  102. if (!synchronized_callback<Args...>::call(args...))
  103. stored.emplace(std::move(args)...);
  104. return true;
  105. }
  106. mutable std::optional<std::tuple<Args...>> stored;
  107. };
  108. // pimpl base class
  109. template <typename T> using impl_ptr = std::shared_ptr<T>;
  110. template <typename T> class CheshireCat {
  111. public:
  112. CheshireCat(impl_ptr<T> impl) : mImpl(std::move(impl)) {}
  113. template <typename... Args>
  114. CheshireCat(Args... args) : mImpl(std::make_shared<T>(std::forward<Args>(args)...)) {}
  115. CheshireCat(CheshireCat<T> &&cc) { *this = std::move(cc); }
  116. CheshireCat(const CheshireCat<T> &) = delete;
  117. virtual ~CheshireCat() = default;
  118. CheshireCat &operator=(CheshireCat<T> &&cc) {
  119. mImpl = std::move(cc.mImpl);
  120. return *this;
  121. };
  122. CheshireCat &operator=(const CheshireCat<T> &) = delete;
  123. protected:
  124. impl_ptr<T> impl() { return mImpl; }
  125. impl_ptr<const T> impl() const { return mImpl; }
  126. private:
  127. impl_ptr<T> mImpl;
  128. };
  129. } // namespace rtc
  130. #endif