utils.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2019-2021 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_UTILS_H
  19. #define RTC_UTILS_H
  20. #include <functional>
  21. #include <memory>
  22. #include <mutex>
  23. #include <optional>
  24. #include <tuple>
  25. namespace rtc {
  26. // overloaded helper
  27. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  28. template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
  29. // weak_ptr bind helper
  30. template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&..._args) {
  31. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&...args) {
  32. using result_type = typename decltype(bound)::result_type;
  33. if (auto shared_this = weak_this.lock())
  34. return bound(args...);
  35. else
  36. return static_cast<result_type>(false);
  37. };
  38. }
  39. // scope_guard helper
  40. class scope_guard final {
  41. public:
  42. scope_guard(std::function<void()> func) : function(std::move(func)) {}
  43. scope_guard(scope_guard &&other) = delete;
  44. scope_guard(const scope_guard &) = delete;
  45. void operator=(const scope_guard &) = delete;
  46. ~scope_guard() {
  47. if (function)
  48. function();
  49. }
  50. private:
  51. std::function<void()> function;
  52. };
  53. // callback with built-in synchronization
  54. template <typename... Args> class synchronized_callback {
  55. public:
  56. synchronized_callback() = default;
  57. synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
  58. synchronized_callback(const synchronized_callback &cb) { *this = cb; }
  59. synchronized_callback(std::function<void(Args...)> func) { *this = std::move(func); }
  60. virtual ~synchronized_callback() { *this = nullptr; }
  61. synchronized_callback &operator=(synchronized_callback &&cb) {
  62. std::scoped_lock lock(mutex, cb.mutex);
  63. set(std::exchange(cb.callback, nullptr));
  64. return *this;
  65. }
  66. synchronized_callback &operator=(const synchronized_callback &cb) {
  67. std::scoped_lock lock(mutex, cb.mutex);
  68. set(cb.callback);
  69. return *this;
  70. }
  71. synchronized_callback &operator=(std::function<void(Args...)> func) {
  72. std::lock_guard lock(mutex);
  73. set(std::move(func));
  74. return *this;
  75. }
  76. bool operator()(Args... args) const {
  77. std::lock_guard lock(mutex);
  78. return call(std::move(args)...);
  79. }
  80. operator bool() const {
  81. std::lock_guard lock(mutex);
  82. return callback ? true : false;
  83. }
  84. std::function<void(Args...)> wrap() const {
  85. return [this](Args... args) { (*this)(std::move(args)...); };
  86. }
  87. protected:
  88. virtual void set(std::function<void(Args...)> func) { callback = std::move(func); }
  89. virtual bool call(Args... args) const {
  90. if (!callback)
  91. return false;
  92. callback(std::move(args)...);
  93. return true;
  94. }
  95. std::function<void(Args...)> callback;
  96. mutable std::recursive_mutex mutex;
  97. };
  98. // callback with built-in synchronization and replay of the last missed call
  99. template <typename... Args>
  100. class synchronized_stored_callback final : public synchronized_callback<Args...> {
  101. public:
  102. template <typename... CArgs>
  103. synchronized_stored_callback(CArgs &&...cargs)
  104. : synchronized_callback<Args...>(std::forward<CArgs>(cargs)...) {}
  105. ~synchronized_stored_callback() {}
  106. private:
  107. void set(std::function<void(Args...)> func) {
  108. synchronized_callback<Args...>::set(func);
  109. if (func && stored) {
  110. std::apply(func, std::move(*stored));
  111. stored.reset();
  112. }
  113. }
  114. bool call(Args... args) const {
  115. if (!synchronized_callback<Args...>::call(args...))
  116. stored.emplace(std::move(args)...);
  117. return true;
  118. }
  119. mutable std::optional<std::tuple<Args...>> stored;
  120. };
  121. // pimpl base class
  122. template <typename T> using impl_ptr = std::shared_ptr<T>;
  123. template <typename T> class CheshireCat {
  124. public:
  125. CheshireCat(impl_ptr<T> impl) : mImpl(std::move(impl)) {}
  126. template <typename... Args>
  127. CheshireCat(Args... args) : mImpl(std::make_shared<T>(std::move(args)...)) {}
  128. CheshireCat(CheshireCat<T> &&cc) { *this = std::move(cc); }
  129. CheshireCat(const CheshireCat<T> &) = delete;
  130. virtual ~CheshireCat() = default;
  131. CheshireCat &operator=(CheshireCat<T> &&cc) {
  132. mImpl = std::move(cc.mImpl);
  133. return *this;
  134. };
  135. CheshireCat &operator=(const CheshireCat<T> &) = delete;
  136. protected:
  137. impl_ptr<T> impl() { return mImpl; }
  138. impl_ptr<const T> impl() const { return mImpl; }
  139. private:
  140. impl_ptr<T> mImpl;
  141. };
  142. } // namespace rtc
  143. #endif