utils.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <utility>
  26. namespace rtc {
  27. // overloaded helper
  28. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  29. template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
  30. // weak_ptr bind helper
  31. template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&..._args) {
  32. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&...args) {
  33. if (auto shared_this = weak_this.lock())
  34. return bound(args...);
  35. else
  36. return static_cast<decltype(bound(args...))>(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. protected:
  85. virtual void set(std::function<void(Args...)> func) { callback = std::move(func); }
  86. virtual bool call(Args... args) const {
  87. if (!callback)
  88. return false;
  89. callback(std::move(args)...);
  90. return true;
  91. }
  92. std::function<void(Args...)> callback;
  93. mutable std::recursive_mutex mutex;
  94. };
  95. // callback with built-in synchronization and replay of the last missed call
  96. template <typename... Args>
  97. class synchronized_stored_callback final : public synchronized_callback<Args...> {
  98. public:
  99. template <typename... CArgs>
  100. synchronized_stored_callback(CArgs &&...cargs)
  101. : synchronized_callback<Args...>(std::forward<CArgs>(cargs)...) {}
  102. ~synchronized_stored_callback() {}
  103. private:
  104. void set(std::function<void(Args...)> func) {
  105. synchronized_callback<Args...>::set(func);
  106. if (func && stored) {
  107. std::apply(func, std::move(*stored));
  108. stored.reset();
  109. }
  110. }
  111. bool call(Args... args) const {
  112. if (!synchronized_callback<Args...>::call(args...))
  113. stored.emplace(std::move(args)...);
  114. return true;
  115. }
  116. mutable std::optional<std::tuple<Args...>> stored;
  117. };
  118. // pimpl base class
  119. template <typename T> using impl_ptr = std::shared_ptr<T>;
  120. template <typename T> class CheshireCat {
  121. public:
  122. CheshireCat(impl_ptr<T> impl) : mImpl(std::move(impl)) {}
  123. template <typename... Args>
  124. CheshireCat(Args... args) : mImpl(std::make_shared<T>(std::move(args)...)) {}
  125. CheshireCat(CheshireCat<T> &&cc) { *this = std::move(cc); }
  126. CheshireCat(const CheshireCat<T> &) = delete;
  127. virtual ~CheshireCat() = default;
  128. CheshireCat &operator=(CheshireCat<T> &&cc) {
  129. mImpl = std::move(cc.mImpl);
  130. return *this;
  131. };
  132. CheshireCat &operator=(const CheshireCat<T> &) = delete;
  133. protected:
  134. impl_ptr<T> impl() { return mImpl; }
  135. impl_ptr<const T> impl() const { return mImpl; }
  136. private:
  137. impl_ptr<T> mImpl;
  138. };
  139. } // namespace rtc
  140. #endif