summaryrefslogtreecommitdiff
path: root/core/cowdata.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/cowdata.h')
-rw-r--r--core/cowdata.h63
1 files changed, 26 insertions, 37 deletions
diff --git a/core/cowdata.h b/core/cowdata.h
index 975a572906..3f72362e29 100644
--- a/core/cowdata.h
+++ b/core/cowdata.h
@@ -54,30 +54,30 @@ class CowData {
friend class VMap;
private:
- mutable T *_ptr;
+ mutable T *_ptr = nullptr;
// internal helpers
_FORCE_INLINE_ uint32_t *_get_refcount() const {
-
- if (!_ptr)
+ if (!_ptr) {
return nullptr;
+ }
return reinterpret_cast<uint32_t *>(_ptr) - 2;
}
_FORCE_INLINE_ uint32_t *_get_size() const {
-
- if (!_ptr)
+ if (!_ptr) {
return nullptr;
+ }
return reinterpret_cast<uint32_t *>(_ptr) - 1;
}
_FORCE_INLINE_ T *_get_data() const {
-
- if (!_ptr)
+ if (!_ptr) {
return nullptr;
+ }
return reinterpret_cast<T *>(_ptr);
}
@@ -125,31 +125,29 @@ public:
_FORCE_INLINE_ int size() const {
uint32_t *size = (uint32_t *)_get_size();
- if (size)
+ if (size) {
return *size;
- else
+ } else {
return 0;
+ }
}
_FORCE_INLINE_ void clear() { resize(0); }
- _FORCE_INLINE_ bool empty() const { return _ptr == 0; }
+ _FORCE_INLINE_ bool empty() const { return _ptr == nullptr; }
_FORCE_INLINE_ void set(int p_index, const T &p_elem) {
-
CRASH_BAD_INDEX(p_index, size());
_copy_on_write();
_get_data()[p_index] = p_elem;
}
_FORCE_INLINE_ T &get_m(int p_index) {
-
CRASH_BAD_INDEX(p_index, size());
_copy_on_write();
return _get_data()[p_index];
}
_FORCE_INLINE_ const T &get(int p_index) const {
-
CRASH_BAD_INDEX(p_index, size());
return _get_data()[p_index];
@@ -158,12 +156,10 @@ public:
Error resize(int p_size);
_FORCE_INLINE_ void remove(int p_index) {
-
ERR_FAIL_INDEX(p_index, size());
T *p = ptrw();
int len = size();
for (int i = p_index; i < len - 1; i++) {
-
p[i] = p[i + 1];
};
@@ -171,11 +167,11 @@ public:
};
Error insert(int p_pos, const T &p_val) {
-
ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER);
resize(size() + 1);
- for (int i = (size() - 1); i > p_pos; i--)
+ for (int i = (size() - 1); i > p_pos; i--) {
set(i, get(i - 1));
+ }
set(p_pos, p_val);
return OK;
@@ -183,21 +179,22 @@ public:
int find(const T &p_val, int p_from = 0) const;
- _FORCE_INLINE_ CowData();
+ _FORCE_INLINE_ CowData() {}
_FORCE_INLINE_ ~CowData();
_FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); };
};
template <class T>
void CowData<T>::_unref(void *p_data) {
-
- if (!p_data)
+ if (!p_data) {
return;
+ }
uint32_t *refc = _get_refcount();
- if (atomic_decrement(refc) > 0)
+ if (atomic_decrement(refc) > 0) {
return; // still in use
+ }
// clean up
if (!__has_trivial_destructor(T)) {
@@ -216,9 +213,9 @@ void CowData<T>::_unref(void *p_data) {
template <class T>
void CowData<T>::_copy_on_write() {
-
- if (!_ptr)
+ if (!_ptr) {
return;
+ }
uint32_t *refc = _get_refcount();
@@ -250,13 +247,13 @@ void CowData<T>::_copy_on_write() {
template <class T>
Error CowData<T>::resize(int p_size) {
-
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
int current_size = size();
- if (p_size == current_size)
+ if (p_size == current_size) {
return OK;
+ }
if (p_size == 0) {
// wants to clean up
@@ -273,7 +270,6 @@ Error CowData<T>::resize(int p_size) {
ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY);
if (p_size > current_size) {
-
if (alloc_size != current_alloc_size) {
if (current_size == 0) {
// alloc from scratch
@@ -304,7 +300,6 @@ Error CowData<T>::resize(int p_size) {
*_get_size() = p_size;
} else if (p_size < current_size) {
-
if (!__has_trivial_destructor(T)) {
// deinitialize no longer needed elements
for (uint32_t i = p_size; i < *_get_size(); i++) {
@@ -351,15 +346,16 @@ void CowData<T>::_ref(const CowData *p_from) {
template <class T>
void CowData<T>::_ref(const CowData &p_from) {
-
- if (_ptr == p_from._ptr)
+ if (_ptr == p_from._ptr) {
return; // self assign, do nothing.
+ }
_unref(_ptr);
_ptr = nullptr;
- if (!p_from._ptr)
+ if (!p_from._ptr) {
return; //nothing to do
+ }
if (atomic_conditional_increment(p_from._get_refcount()) > 0) { // could reference
_ptr = p_from._ptr;
@@ -367,14 +363,7 @@ void CowData<T>::_ref(const CowData &p_from) {
}
template <class T>
-CowData<T>::CowData() {
-
- _ptr = nullptr;
-}
-
-template <class T>
CowData<T>::~CowData() {
-
_unref(_ptr);
}