shareddata.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file LICENSE, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. /*
  10. Implementation note
  11. ===================
  12. SharedDataPtr/SharedData offer the same basic functionality as std::shared_ptr,
  13. but do not use atomic counters under the hood.
  14. Since kiwi operates within a single thread context, atomic counters are not necessary,
  15. especially given the extra CPU cost.
  16. Therefore the use of SharedDataPtr/SharedData is preferred over std::shared_ptr.
  17. */
  18. namespace kiwi
  19. {
  20. class SharedData
  21. {
  22. public:
  23. SharedData() : m_refcount(0) {}
  24. SharedData(const SharedData &other) = delete;
  25. SharedData(SharedData&& other) = delete;
  26. int m_refcount;
  27. SharedData &operator=(const SharedData &other) = delete;
  28. SharedData &operator=(SharedData&& other) = delete;
  29. };
  30. template <typename T>
  31. class SharedDataPtr
  32. {
  33. public:
  34. using Type = T;
  35. SharedDataPtr() : m_data(nullptr) {}
  36. explicit SharedDataPtr(T *data) : m_data(data)
  37. {
  38. incref(m_data);
  39. }
  40. ~SharedDataPtr()
  41. {
  42. decref(m_data);
  43. }
  44. T *data()
  45. {
  46. return m_data;
  47. }
  48. const T *data() const
  49. {
  50. return m_data;
  51. }
  52. operator T *()
  53. {
  54. return m_data;
  55. }
  56. operator const T *() const
  57. {
  58. return m_data;
  59. }
  60. T *operator->()
  61. {
  62. return m_data;
  63. }
  64. const T *operator->() const
  65. {
  66. return m_data;
  67. }
  68. T &operator*()
  69. {
  70. return *m_data;
  71. }
  72. const T &operator*() const
  73. {
  74. return *m_data;
  75. }
  76. bool operator!() const
  77. {
  78. return !m_data;
  79. }
  80. bool operator<(const SharedDataPtr<T> &other) const
  81. {
  82. return m_data < other.m_data;
  83. }
  84. bool operator==(const SharedDataPtr<T> &other) const
  85. {
  86. return m_data == other.m_data;
  87. }
  88. bool operator!=(const SharedDataPtr<T> &other) const
  89. {
  90. return m_data != other.m_data;
  91. }
  92. SharedDataPtr(const SharedDataPtr<T> &other) : m_data(other.m_data)
  93. {
  94. incref(m_data);
  95. }
  96. SharedDataPtr(SharedDataPtr&& other) noexcept : m_data(other.m_data)
  97. {
  98. other.m_data = nullptr;
  99. }
  100. SharedDataPtr<T> &operator=(const SharedDataPtr<T> &other)
  101. {
  102. if (m_data != other.m_data)
  103. {
  104. T *temp = m_data;
  105. m_data = other.m_data;
  106. incref(m_data);
  107. decref(temp);
  108. }
  109. return *this;
  110. }
  111. SharedDataPtr<T>& operator=(SharedDataPtr<T>&& other) noexcept
  112. {
  113. if (m_data != other.m_data)
  114. {
  115. decref(m_data);
  116. m_data = other.m_data;
  117. other.m_data = nullptr;
  118. }
  119. return *this;
  120. }
  121. SharedDataPtr<T> &operator=(T *other)
  122. {
  123. if (m_data != other)
  124. {
  125. T *temp = m_data;
  126. m_data = other;
  127. incref(m_data);
  128. decref(temp);
  129. }
  130. return *this;
  131. }
  132. private:
  133. static void incref(T *data)
  134. {
  135. if (data)
  136. ++data->m_refcount;
  137. }
  138. static void decref(T *data)
  139. {
  140. if (data && --data->m_refcount == 0)
  141. delete data;
  142. }
  143. T *m_data;
  144. };
  145. } // namespace kiwi