ref.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // ======================================================================== //
  2. // Copyright 2009-2019 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "platform.h"
  18. namespace oidn {
  19. class RefCount
  20. {
  21. private:
  22. std::atomic<size_t> count;
  23. public:
  24. __forceinline RefCount(int count = 0) noexcept : count(count) {}
  25. __forceinline size_t incRef() noexcept
  26. {
  27. return count.fetch_add(1) + 1;
  28. }
  29. __forceinline size_t decRef()
  30. {
  31. const size_t newCount = decRefKeep();
  32. if (newCount == 0)
  33. destroy();
  34. return newCount;
  35. }
  36. __forceinline size_t decRefKeep() noexcept
  37. {
  38. return count.fetch_add(-1) - 1;
  39. }
  40. __forceinline void destroy()
  41. {
  42. delete this;
  43. }
  44. protected:
  45. // Disable copying
  46. RefCount(const RefCount&) = delete;
  47. RefCount& operator =(const RefCount&) = delete;
  48. virtual ~RefCount() noexcept = default;
  49. };
  50. template<typename T>
  51. class Ref
  52. {
  53. private:
  54. T* ptr;
  55. public:
  56. __forceinline Ref() noexcept : ptr(nullptr) {}
  57. __forceinline Ref(std::nullptr_t) noexcept : ptr(nullptr) {}
  58. __forceinline Ref(const Ref& other) noexcept : ptr(other.ptr) { if (ptr) ptr->incRef(); }
  59. __forceinline Ref(Ref&& other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
  60. __forceinline Ref(T* ptr) noexcept : ptr(ptr) { if (ptr) ptr->incRef(); }
  61. template<typename Y>
  62. __forceinline Ref(const Ref<Y>& other) noexcept : ptr(other.get()) { if (ptr) ptr->incRef(); }
  63. template<typename Y>
  64. __forceinline explicit Ref(Y* ptr) noexcept : ptr(ptr) { if (ptr) ptr->incRef(); }
  65. __forceinline ~Ref() { if (ptr) ptr->decRef(); }
  66. __forceinline Ref& operator =(const Ref& other)
  67. {
  68. if (other.ptr)
  69. other.ptr->incRef();
  70. if (ptr)
  71. ptr->decRef();
  72. ptr = other.ptr;
  73. return *this;
  74. }
  75. __forceinline Ref& operator =(Ref&& other)
  76. {
  77. if (ptr)
  78. ptr->decRef();
  79. ptr = other.ptr;
  80. other.ptr = nullptr;
  81. return *this;
  82. }
  83. __forceinline Ref& operator =(T* other)
  84. {
  85. if (other)
  86. other->incRef();
  87. if (ptr)
  88. ptr->decRef();
  89. ptr = other;
  90. return *this;
  91. }
  92. __forceinline Ref& operator =(std::nullptr_t)
  93. {
  94. if (ptr)
  95. ptr->decRef();
  96. ptr = nullptr;
  97. return *this;
  98. }
  99. __forceinline operator bool() const noexcept { return ptr != nullptr; }
  100. __forceinline T& operator *() const noexcept { return *ptr; }
  101. __forceinline T* operator ->() const noexcept { return ptr; }
  102. __forceinline T* get() const noexcept { return ptr; }
  103. __forceinline T* detach() noexcept
  104. {
  105. T* res = ptr;
  106. ptr = nullptr;
  107. return res;
  108. }
  109. };
  110. template<typename T> __forceinline bool operator < (const Ref<T>& a, const Ref<T>& b) noexcept { return a.ptr < b.ptr; }
  111. template<typename T> __forceinline bool operator ==(const Ref<T>& a, std::nullptr_t) noexcept { return a.ptr == nullptr; }
  112. template<typename T> __forceinline bool operator ==(std::nullptr_t, const Ref<T>& b) noexcept { return nullptr == b.ptr; }
  113. template<typename T> __forceinline bool operator ==(const Ref<T>& a, const Ref<T>& b) noexcept { return a.ptr == b.ptr; }
  114. template<typename T> __forceinline bool operator !=(const Ref<T>& a, std::nullptr_t) noexcept { return a.ptr != nullptr; }
  115. template<typename T> __forceinline bool operator !=(std::nullptr_t, const Ref<T>& b) noexcept { return nullptr != b.ptr; }
  116. template<typename T> __forceinline bool operator !=(const Ref<T>& a, const Ref<T>& b) noexcept { return a.ptr != b.ptr; }
  117. template<typename T, typename... Args>
  118. __forceinline Ref<T> makeRef(Args&&... args)
  119. {
  120. return Ref<T>(new T(std::forward<Args>(args)...));
  121. }
  122. template<typename T, typename Y>
  123. __forceinline Ref<Y> staticRefCast(const Ref<T>& a)
  124. {
  125. return Ref<Y>(static_cast<Y*>(a.get()));
  126. }
  127. template<typename T, typename Y>
  128. __forceinline Ref<Y> dynamicRefCast(const Ref<T>& a)
  129. {
  130. return Ref<Y>(dynamic_cast<Y*>(a.get()));
  131. }
  132. } // namespace oidn