binary_guard.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/algorithm.h"
  9. #include <atomic>
  10. namespace base {
  11. class binary_guard {
  12. public:
  13. binary_guard() = default;
  14. binary_guard(binary_guard &&other);
  15. binary_guard &operator=(binary_guard &&other);
  16. ~binary_guard();
  17. binary_guard &operator=(std::nullptr_t);
  18. bool alive() const;
  19. binary_guard make_guard();
  20. explicit operator bool() const;
  21. private:
  22. void destroy();
  23. std::atomic<bool> *_bothAlive = nullptr;
  24. };
  25. inline binary_guard::binary_guard(binary_guard &&other)
  26. : _bothAlive(base::take(other._bothAlive)) {
  27. }
  28. inline binary_guard &binary_guard::operator=(binary_guard &&other) {
  29. if (this != &other) {
  30. destroy();
  31. _bothAlive = base::take(other._bothAlive);
  32. }
  33. return *this;
  34. }
  35. inline binary_guard::~binary_guard() {
  36. destroy();
  37. }
  38. inline binary_guard &binary_guard::operator=(std::nullptr_t) {
  39. destroy();
  40. return *this;
  41. }
  42. inline binary_guard::operator bool() const {
  43. return alive();
  44. }
  45. inline bool binary_guard::alive() const {
  46. return _bothAlive && _bothAlive->load();
  47. }
  48. inline void binary_guard::destroy() {
  49. if (const auto both = base::take(_bothAlive)) {
  50. auto old = true;
  51. if (!both->compare_exchange_strong(old, false)) {
  52. delete both;
  53. }
  54. }
  55. }
  56. inline binary_guard binary_guard::make_guard() {
  57. destroy();
  58. auto result = binary_guard();
  59. _bothAlive = result._bothAlive = new std::atomic<bool>(true);
  60. return result;
  61. }
  62. } // namespace base
  63. namespace crl {
  64. template <typename T, typename Enable>
  65. struct guard_traits;
  66. template <>
  67. struct guard_traits<base::binary_guard, void> {
  68. static base::binary_guard create(base::binary_guard value) {
  69. return value;
  70. }
  71. static bool check(const base::binary_guard &guard) {
  72. return guard.alive();
  73. }
  74. };
  75. } // namespace crl