| 1 |
/*----------------------------------------------------------------------------- |
| 2 |
| Copyright (c) 2013-2026, 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 |
/* |
| 11 |
Implementation note |
| 12 |
=================== |
| 13 |
SharedDataPtr/SharedData offer the same basic functionality as std::shared_ptr, |
| 14 |
but do not use atomic counters under the hood. |
| 15 |
Since kiwi operates within a single thread context, atomic counters are not necessary, |
| 16 |
especially given the extra CPU cost. |
| 17 |
Therefore the use of SharedDataPtr/SharedData is preferred over std::shared_ptr. |
| 18 |
*/ |
| 19 |
|
| 20 |
namespace kiwi |
| 21 |
{ |
| 22 |
|
| 23 |
class SharedData |
| 24 |
{ |
| 25 |
|
| 26 |
public: |
| 27 |
SharedData() : m_refcount(0) {} |
| 28 |
|
| 29 |
SharedData(const SharedData &other) = delete; |
| 30 |
|
| 31 |
SharedData(SharedData&& other) = delete; |
| 32 |
|
| 33 |
int m_refcount; |
| 34 |
|
| 35 |
SharedData &operator=(const SharedData &other) = delete; |
| 36 |
|
| 37 |
SharedData &operator=(SharedData&& other) = delete; |
| 38 |
}; |
| 39 |
|
| 40 |
template <typename T> |
| 41 |
class SharedDataPtr |
| 42 |
{ |
| 43 |
|
| 44 |
public: |
| 45 |
using Type = T; |
| 46 |
|
| 47 |
SharedDataPtr() : m_data(nullptr) {} |
| 48 |
|
| 49 |
explicit SharedDataPtr(T *data) : m_data(data) |
| 50 |
{ |
| 51 |
incref(m_data); |
| 52 |
} |
| 53 |
|
| 54 |
~SharedDataPtr() |
| 55 |
{ |
| 56 |
decref(m_data); |
| 57 |
} |
| 58 |
|
| 59 |
T *data() |
| 60 |
{ |
| 61 |
return m_data; |
| 62 |
} |
| 63 |
|
| 64 |
const T *data() const |
| 65 |
{ |
| 66 |
return m_data; |
| 67 |
} |
| 68 |
|
| 69 |
operator T *() |
| 70 |
{ |
| 71 |
return m_data; |
| 72 |
} |
| 73 |
|
| 74 |
operator const T *() const |
| 75 |
{ |
| 76 |
return m_data; |
| 77 |
} |
| 78 |
|
| 79 |
T *operator->() |
| 80 |
{ |
| 81 |
return m_data; |
| 82 |
} |
| 83 |
|
| 84 |
const T *operator->() const |
| 85 |
{ |
| 86 |
return m_data; |
| 87 |
} |
| 88 |
|
| 89 |
T &operator*() |
| 90 |
{ |
| 91 |
return *m_data; |
| 92 |
} |
| 93 |
|
| 94 |
const T &operator*() const |
| 95 |
{ |
| 96 |
return *m_data; |
| 97 |
} |
| 98 |
|
| 99 |
bool operator!() const |
| 100 |
{ |
| 101 |
return !m_data; |
| 102 |
} |
| 103 |
|
| 104 |
bool operator<(const SharedDataPtr<T> &other) const |
| 105 |
{ |
| 106 |
return m_data < other.m_data; |
| 107 |
} |
| 108 |
|
| 109 |
bool operator==(const SharedDataPtr<T> &other) const |
| 110 |
{ |
| 111 |
return m_data == other.m_data; |
| 112 |
} |
| 113 |
|
| 114 |
bool operator!=(const SharedDataPtr<T> &other) const |
| 115 |
{ |
| 116 |
return m_data != other.m_data; |
| 117 |
} |
| 118 |
|
| 119 |
SharedDataPtr(const SharedDataPtr<T> &other) : m_data(other.m_data) |
| 120 |
{ |
| 121 |
incref(m_data); |
| 122 |
} |
| 123 |
|
| 124 |
SharedDataPtr(SharedDataPtr&& other) noexcept : m_data(other.m_data) |
| 125 |
{ |
| 126 |
other.m_data = nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
SharedDataPtr<T> &operator=(const SharedDataPtr<T> &other) |
| 130 |
{ |
| 131 |
if (m_data != other.m_data) |
| 132 |
{ |
| 133 |
T *temp = m_data; |
| 134 |
m_data = other.m_data; |
| 135 |
incref(m_data); |
| 136 |
decref(temp); |
| 137 |
} |
| 138 |
return *this; |
| 139 |
} |
| 140 |
|
| 141 |
SharedDataPtr<T>& operator=(SharedDataPtr<T>&& other) noexcept |
| 142 |
{ |
| 143 |
if (m_data != other.m_data) |
| 144 |
{ |
| 145 |
T *temp = m_data; |
| 146 |
m_data = other.m_data; |
| 147 |
other.m_data = nullptr; |
| 148 |
decref(temp); |
| 149 |
} |
| 150 |
return *this; |
| 151 |
} |
| 152 |
|
| 153 |
SharedDataPtr<T> &operator=(T *other) |
| 154 |
{ |
| 155 |
if (m_data != other) |
| 156 |
{ |
| 157 |
T *temp = m_data; |
| 158 |
m_data = other; |
| 159 |
incref(m_data); |
| 160 |
decref(temp); |
| 161 |
} |
| 162 |
return *this; |
| 163 |
} |
| 164 |
|
| 165 |
private: |
| 166 |
static void incref(T *data) |
| 167 |
{ |
| 168 |
if (data) |
| 169 |
++data->m_refcount; |
| 170 |
} |
| 171 |
|
| 172 |
static void decref(T *data) |
| 173 |
{ |
| 174 |
if (data && --data->m_refcount == 0) |
| 175 |
delete data; |
| 176 |
} |
| 177 |
|
| 178 |
T *m_data; |
| 179 |
}; |
| 180 |
|
| 181 |
} // namespace kiwi |