utils.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace rtc {
  24. // overloaded helper
  25. template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
  26. template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
  27. // weak_ptr bind helper
  28. template <typename F, typename T, typename... Args> auto weak_bind(F &&f, T *t, Args &&..._args) {
  29. return [bound = std::bind(f, t, _args...), weak_this = t->weak_from_this()](auto &&...args) {
  30. using result_type = typename decltype(bound)::result_type;
  31. if (auto shared_this = weak_this.lock())
  32. return bound(args...);
  33. else
  34. return static_cast<result_type>(false);
  35. };
  36. }
  37. // scope_guard helper
  38. class scope_guard {
  39. public:
  40. scope_guard(std::function<void()> func) : function(std::move(func)) {}
  41. scope_guard(scope_guard &&other) = delete;
  42. scope_guard(const scope_guard &) = delete;
  43. void operator=(const scope_guard &) = delete;
  44. ~scope_guard() {
  45. if (function)
  46. function();
  47. }
  48. private:
  49. std::function<void()> function;
  50. };
  51. // callback with built-in synchronization
  52. template <typename... Args> class synchronized_callback {
  53. public:
  54. synchronized_callback() = default;
  55. synchronized_callback(synchronized_callback &&cb) { *this = std::move(cb); }
  56. synchronized_callback(const synchronized_callback &cb) { *this = cb; }
  57. synchronized_callback(std::function<void(Args...)> func) { *this = std::move(func); }
  58. ~synchronized_callback() { *this = nullptr; }
  59. synchronized_callback &operator=(synchronized_callback &&cb) {
  60. std::scoped_lock lock(mutex, cb.mutex);
  61. callback = std::move(cb.callback);
  62. cb.callback = nullptr;
  63. return *this;
  64. }
  65. synchronized_callback &operator=(const synchronized_callback &cb) {
  66. std::scoped_lock lock(mutex, cb.mutex);
  67. callback = cb.callback;
  68. return *this;
  69. }
  70. synchronized_callback &operator=(std::function<void(Args...)> func) {
  71. std::lock_guard lock(mutex);
  72. callback = std::move(func);
  73. return *this;
  74. }
  75. void operator()(Args... args) const {
  76. std::lock_guard lock(mutex);
  77. if (callback)
  78. callback(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. private:
  88. std::function<void(Args...)> callback;
  89. mutable std::recursive_mutex mutex;
  90. };
  91. // pimpl base class
  92. template <typename T> using impl_ptr = std::shared_ptr<T>;
  93. template <typename T> class CheshireCat {
  94. public:
  95. CheshireCat(impl_ptr<T> impl) : mImpl(std::move(impl)) {}
  96. template <typename... Args>
  97. CheshireCat(Args... args) : mImpl(std::make_shared<T>(std::move(args)...)) {}
  98. virtual ~CheshireCat() = default;
  99. protected:
  100. impl_ptr<T> impl() { return mImpl; }
  101. impl_ptr<const T> impl() const { return mImpl; }
  102. private:
  103. impl_ptr<T> mImpl;
  104. };
  105. } // namespace rtc
  106. #endif